Package to help finding latitude and longitude for NYC street intersections utilizing OSMPythonTools wrapper for OpenStreetMaps.
- OSMPythonTools
- shapely
- Note: GEOS required
pip install nyc_intersection_geocoder
from nyc_intersection_geocoder.geocoder import IntersectionGC
encoder = IntersectionGC()
result = encoder.get_intersection(
'Wythe Ave', 'N 4th Street', 'Brooklyn'
)
print(type(result)) # shapely.geometry.point.Point
print(result.coords.xy) # (array('d', [-73.962206]), array('d', [40.717871]))Note: Geographic data for plot is ordered in (longitude, latitude) for to match (x, y) coordinates.
MapBox/OSM could not geocode the intersection 'Wythe Ave and N 4th Street Brooklyn, NY'. The reason seemed to be a lack of county tags across all OpenStreetMaps street data and intersection querying queries county tags for NYC streets.
from OSMPythonTools.overpass import Overpass, overpassQueryBuilder
from OSMPythonTools.nominatim import Nominatim
nominatim = Nominatim()
nyc = nominatim.query('NYC')
query = overpassQueryBuilder(
area=nyc,
elementType='way',
selector=['highway', '"name"="North 4th Street"'],
out='body',
includeGeometry=True
)
overpass = Overpass()
result = overpass.query(query, timeout=100)
for ele in result.elements():
print(f'{ele.tags()}' + '\n'){'highway': 'residential', 'name': 'North 4th Street', 'oneway': 'yes'}
{'highway': 'residential', 'name': 'North 4th Street', 'oneway': 'yes', 'tiger:cfcc': 'A41', 'tiger:county': 'Kings, NY', 'tiger:name_base': '4th', 'tiger:name_direction_prefix': 'N', 'tiger:name_type': 'St', 'tiger:zip_left': '11211', 'tiger:zip_right': '11211'}
One section of North 4th Street is tagged with 'Kings, NY' while the other section does not. In this example, the first section coincides with the intersection point of North 4th Street and Wythe Avenue.
Merging all street sections with the same name in the same geographic county into a shapely MultiLineString object.