Skip to main content
LightBox Developer Portal logo

Main navigation

  • Home
  • APIs
    API Catalog API Solutions
  • Library
    Documentation Portal Blog Sample Code Videos
  • Support
    FAQs Contact Us Developer Portal Release Notes Where to find my API Key

User account menu

  • Log in
  • Sign up
  1. Home
  2. LightBox API Sample Code
  3. Python
  4. LightBox API to Shapefile

LightBox API to Shapefile Python Sample

In this example we demonstrate the steps to connect to the LightBox Geocoding API, obtain the coordinates of a subject property, and using those coordinates, select a set of building footprints around a radius of that location. If successful the python script  will use the Shapely and Fiona libraries to export the result to a shapefile. 

#################################################
# Author: Skip Cody
# Created: 06/30/2023
# Description: Using Lightbox APIs download Structure
# data based on a radius around a subject property.
# Using the python shapely and fiona libraries,
# write the output to a shapefile
#
#################################################
import requests
import shapely
import fiona
subjectProperty = "800 NE 2ND AVE CAMAS, WA 98607"
urlDomain = 'https://api.lightboxre.com'
geocodePath = 'v1/addresses/search'
structurePath = 'v1/structures/us/geometry'
lightBoxKey = '<Your LightBox Key Here>'
schema = {
'geometry': 'Polygon',
'properties': {
'address': 'str',
'city': 'str',
'state': 'str',
'height': 'float:13.3',
'elevation': 'float:13.3',
'area': 'float:13.3',
}
}
# Get the location of your subject property by preforming a LightBox geocode call
try:
# craft the api url string for the geocode call
url = urlDomain + '/' + geocodePath + '?text=' + subjectProperty
# make the geocode request
response = requests.get(url, headers={"x-api-key" : lightBoxKey })
if response.status_code != 200:
raise Exception('Non 200 http status received, status=' + str(response.status_code))
response = response.json()
# the addresses object is an array so take the first address
address = response['addresses'][0]
# assign latitude and longitude
latitude = address['location']['representativePoint']['latitude']
longitude = address['location']['representativePoint']['longitude']
# create the wkt point string that will be used in the next call to structures
wktString = 'POINT(' + str(longitude) + ' ' + str(latitude) + ')'
# craft the api url string for the structure call
url = urlDomain + '/' + structurePath + '?wkt=' + wktString + "&bufferDistance=1000" + "&bufferUnit=ft"
# make a request to the structures API
response = requests.get(url, headers={"x-api-key" : lightBoxKey })
if response.status_code != 200:
raise Exception('Non 200 http status received, status=' + str(response.status_code))
response = response.json()
# using fiona open the output shapefile
with fiona.open('output.shp', 'w', crs=fiona.crs.from_epsg(4362), driver='ESRI Shapefile', schema=schema) as output:
# loop through each record in the response
for structure in response['structures']:
wkt = structure['location']['geometry']['wkt']
address = structure['location']['streetAddress']
city = structure['location']['locality']
state = structure['location']['regionCode']
height = structure['physicalFeatures']['height']['max']
elevation = structure['physicalFeatures']['groundElevation']['min']
area = structure['physicalFeatures']['area']['footprintArea']
try:
# geometry
polygon = shapely.from_wkt(wkt)
except Exception as error:
print('Exception thrown: Message:' + str(error))
#attributes
prop = {
'address': address,
'city': city,
'state': state,
'height': height,
'elevation': elevation,
'area': area
}
#write to shapefile
output.write({'geometry': shapely.geometry.mapping(polygon), 'properties':prop})
exit()
except Exception as error:
print('Exception thrown: Message:' + str(error))
exit()
view raw LightBoxToShape.py hosted with ❤ by GitHub
  • LIGHTBOX
  • Solutions
  • Data
  • Insight
  • News and Press
  • About
  • LIGHTBOX DEVELOPER
  • API Catalog
  • Documentation
  • Videos
  • FAQs
  • Portal Blog
  • CONTACT
  •  +1 (800) 624-0470

  •  TwitterFollow on Twitter
  •  LinkedInFollow on LinkedIn

©LightBox Holdings, LP      Privacy Policy | Terms of Use