Assessment to KML Python Sample
View RawThis 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.
1import requests
2from simplekml import Kml, Snippet, Types, Color
3
4subjectProperty = "21004 PIONEER BLVD LAKEWOOD, CA 9071"
5
6urlDomain = 'https://api.lightboxre.com'
7geocodePath = 'v1/addresses/search'
8assessmentPath = 'v1/assessments/us/geometry'
9lightBoxKey = '<Your LightBox Key>'
10
11
12# Get the location of the Subject Property by preforming a LightBox geocode call
13try:
14
15 # craft the api url string for the geocode call
16 url = urlDomain + '/' + geocodePath + '?text=' + subjectProperty
17
18 # make the geocode request
19 response = requests.get(url, headers={"x-api-key" : lightBoxKey })
20 if response.status_code != 200:
21 raise Exception('Non 200 http status received, status=' + str(response.status_code))
22 response = response.json()
23
24 # the addresses object is an array so take the first address
25 address = response['addresses'][0]
26
27 # assign latitude and longitude
28 latitude = address['location']['representativePoint']['latitude']
29 longitude = address['location']['representativePoint']['longitude']
30 # create the wkt point string
31 wktString = 'POINT(' + str(longitude) + ' ' + str(latitude) + ')'
32
33 # craft the api url string for the assessment call
34 url = urlDomain + '/' + assessmentPath + '?wkt=' + wktString + "&bufferDistance=1000" + "&bufferUnit=ft"
35
36 # make the assessments request
37 response = requests.get(url, headers={"x-api-key" : lightBoxKey })
38 if response.status_code != 200:
39 raise Exception('Non 200 http status received, status=' + str(response.status_code))
40 response = response.json()
41
42 # create the KML object
43 kml = Kml()
44
45 # loop through each record in the response
46 for assessment in response['assessments']:
47 latitude = assessment['location']['representativePoint']['latitude']
48 longitude = assessment['location']['representativePoint']['longitude']
49
50 # create the KML record
51 point = kml.newpoint()
52 point.name = assessment['location']['streetAddress']
53
54 # create a table popup
55 html = '<table>'
56 html += '<tr><td>Address:</td><td>' + assessment['location']['streetAddress'] + '</td></tr>'
57 html += '<tr><td>City:</td><td>' + assessment['location']['locality'] + '</td></tr>'
58 html += '<tr><td>State:</td><td>' + assessment['location']['regionCode'] + '</td></tr>'
59 html += '<tr><td>Zip:</td><td>' + assessment['location']['postalCode'] + '</td></tr>'
60 html += '<tr><td>Landuse:</td><td>' + assessment['landUse']['normalized']['description'].replace('&', '&') + '</td></tr>'
61 html += '<tr><td>Landuse Category:</td><td>' + assessment['landUse']['normalized']['categoryDescription'] + '</td></tr>'
62 html += '</table>'
63 point.description = html
64
65 # set the icon color based on Landuse Category
66 category = assessment['landUse']['normalized']['categoryDescription']
67 if category == 'AGRICULTURAL/RURAL':
68 point.style.iconstyle.color = Color.fuchsia
69 elif category == "COMMERCIAL (OFFICE)":
70 point.style.iconstyle.color = Color.cyan
71 elif category == "COMMERCIAL (RETAIL)":
72 point.style.iconstyle.color = Color.lightblue
73 elif category == "EXEMPT, GOVERNMENT AND HISTORICAL":
74 point.style.iconstyle.color = Color.green
75 elif category == "INDUSTRIAL (GENERAL)":
76 point.style.iconstyle.color = Color.darkgreen
77 elif category == "MISCELLANEOUS":
78 point.style.iconstyle.color = Color.gray
79 elif category == "RESIDENTIAL":
80 point.style.iconstyle.color = Color.green
81 elif category == "VACANT LAND":
82 point.style.iconstyle.color = Color.red
83 else:
84 point.style.iconstyle.color = Color.yellow
85
86 # set the point corrds
87 point.coords = [(longitude,latitude)]
88
89 # save the kml
90 kml.save('output.kml')
91
92 exit()
93
94except Exception as error:
95 print('Exception thrown: Message:' + str(error))
96 exit()
97
98