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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
# 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() | |