Buildings to Shapefile Python Sample

View Raw

This sample demonstrates how to use Python to geocode to a property and export that property data to a KML file that then can be used in Google Earth or any application that supports KML.

This sample demonstrates:

  • How to geocode to a property using the LightBox Geocoder.
  • Retrieve the latitude and longitude data from the geocode response.
  • Craft a API call to the LightBox Assessment geometry endpoint using the coordinates returned from the geocode response along with a buffer distance and buffer unit.
  • Write a subset of data from the response to a KML file using the simplekml python library.
1#################################################
2# Author: Skip Cody
3# Created: 06/30/2023
4# Description: Using Lightbox APIs download Structure 
5#   data based on a radius around a subject property. 
6#   Using the python shapely and fiona libraries,
7#   write the output to a shapefile
8#
9#################################################
10
11
12import requests
13import shapely
14import fiona 
15
16
17subjectProperty = "800 NE 2ND AVE CAMAS, WA 98607"
18
19urlDomain = 'https://api.lightboxre.com'
20geocodePath = 'v1/addresses/search'
21structurePath = 'v1/structures/us/geometry'
22lightBoxKey = '<Your LightBox Key Here>'
23
24schema = {
25    'geometry': 'Polygon',
26    'properties': {
27        'address': 'str',
28        'city': 'str',
29        'state': 'str',
30        'height': 'float:13.3',
31        'elevation': 'float:13.3',
32        'area': 'float:13.3',
33    }
34}
35
36
37# Get the location of your subject property by preforming a LightBox geocode call
38try:
39    
40    # craft the api url string for the geocode call
41    url = urlDomain + '/' + geocodePath + '?text=' + subjectProperty
42
43    # make the geocode request
44    response = requests.get(url, headers={"x-api-key" : lightBoxKey })
45    if response.status_code != 200:
46        raise Exception('Non 200 http status received, status=' + str(response.status_code))
47    response = response.json()
48
49    # the addresses object is an array so take the first address
50    address = response['addresses'][0]
51
52    # assign latitude and longitude
53    latitude = address['location']['representativePoint']['latitude']
54    longitude = address['location']['representativePoint']['longitude']
55    
56    # create the wkt point string that will be used in the next call to structures
57    wktString = 'POINT(' + str(longitude) + ' ' + str(latitude) + ')'
58
59    # craft the api url string for the structure call
60    url = urlDomain + '/' + structurePath + '?wkt=' + wktString + "&bufferDistance=1000" + "&bufferUnit=ft"
61
62    # make a request to the structures API
63    response = requests.get(url, headers={"x-api-key" : lightBoxKey })
64    if response.status_code != 200:
65        raise Exception('Non 200 http status received, status=' + str(response.status_code))
66    response = response.json()
67
68    # using fiona open the output shapefile
69    with fiona.open('output.shp', 'w', crs=fiona.crs.from_epsg(4362), driver='ESRI Shapefile', schema=schema) as output:
70        
71        # loop through each record in the response
72        for structure in response['structures']:
73
74            wkt = structure['location']['geometry']['wkt']
75            address = structure['location']['streetAddress']
76            city = structure['location']['locality']
77            state = structure['location']['regionCode']
78            height = structure['physicalFeatures']['height']['max']
79            elevation = structure['physicalFeatures']['groundElevation']['min']
80            area = structure['physicalFeatures']['area']['footprintArea']
81
82            try: 
83                # geometry
84                polygon = shapely.from_wkt(wkt)
85            
86            except Exception as error:
87                print('Exception thrown: Message:' + str(error))
88
89            #attributes
90            prop = {
91                'address': address,
92                'city': city,
93                'state': state,
94                'height': height,
95                'elevation': elevation,
96                'area': area
97            }
98
99            #write to shapefile
100            output.write({'geometry': shapely.geometry.mapping(polygon), 'properties':prop})
101
102    exit()
103    
104except Exception as error:
105   print('Exception thrown: Message:' + str(error))
106   exit()
107
108