/
process2.py
83 lines (69 loc) · 2.58 KB
/
process2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from lxml import etree
from shapely.geometry import Point
from shapely.geometry import Polygon
from shapely.geometry import MultiPoint
import csv
import json
import urllib
KEY = 'AIzaSyCoY05WH-mI8y3_NDUcwEzvjznLR1YAInQ'
URL = 'https://maps.googleapis.com/maps/api/geocode/json?key={}&address='.format(KEY)
NSMAP = {'kml': 'http://www.opengis.net/kml/2.2'}
COORDINATES = 'kml:Polygon/kml:outerBoundaryIs/kml:LinearRing/kml:coordinates'
COORDINATES_2 = 'kml:MultiGeometry/kml:Polygon/kml:outerBoundaryIs/kml:LinearRing/kml:coordinates'
MARKETS_TO_KMLS = {
'Atlanta': 'atlanta.kml',
'Charlotte': 'charlotte.kml',
'Nashville': 'nashville.kml',
'Raleigh-Durham': 'rdu.kml',
'Salt Lake City': 'slc.kml',
}
def get_neighborhood_from_kml(lat_lon, kml_path):
test_lat, test_lon = lat_lon
test_point = Point(float(test_lat), float(test_lon))
kml = etree.parse(kml_path)
placemarks = kml.findall('//kml:Placemark', namespaces=NSMAP)
found_neighborhood = None
for el in kml.findall('.//kml:coordinates', namespaces=NSMAP):
coords = []
for coord in el.text.split(' '):
lat, lon, _ = coord.split(',')
coords.append((float(lon), float(lat)))
poly = MultiPoint(coords).convex_hull
if poly.contains(test_point):
placemark = el.getparent().getparent().getparent().getparent()
if 'MultiGeometry' in str(placemark.tag):
placemark = placemark.getparent()
val = placemark.find('kml:ExtendedData/kml:Data[@name=\'32CitiesNa\']/kml:value', namespaces=NSMAP)
if val is not None:
found_neighborhood = val.text
return found_neighborhood
def get_neighborhood(results):
for component in results['address_components']:
for type in component['types']:
if 'neighborhood' in type:
return '{} (API)'.format(component['long_name'])
for type in component['types']:
if 'locality' in type:
return '{} (API, Locality)'.format(component['long_name'])
def process(path, kml_path=None):
is_csv = False
is_json = False
if path.endswith('.csv'):
is_csv = True
rows = csv.DictReader(open(path))
elif path.endswith('.json'):
is_json = True
rows = json.load(open(path))
for row in rows:
lat_lon = (row['Latitude'], row['Longitude'])
kml_path = MARKETS_TO_KMLS[row['Market']]
if 'rdu' not in kml_path:
# if 'atlanta' not in kml_path and 'rdu' not in kml_path:
continue
neighborhood = get_neighborhood_from_kml(lat_lon, kml_path)
address = row['Property Address']
print '{} -> {}'.format(address, neighborhood)
def main():
process('idol.csv')
if __name__ == '__main__':
main()