0

I am trying to to find the latitude and longitude of every point along the boundary of a specific neighborhood. For example, I've tried using geopy - and when I input a specific neighborhood it returns one pair of coordinates. I would like the receive a list of coordinates that would outline a specified neighborhood.

To specify, I am working on getting the neighborhoods in Manhattan, NYC.

Thanks.

from geopy.geocoders import Nominatim
geolocator = Nominatim()

location = geolocator.geocode('Gramercy NYC')
print(location)

output: Location(Gramercy, Manhattan, Manhattan Community Board 6, New York County, NYC, New York, USA, (40.7355189, -73.9840794, 0.0))

I am getting one set of coordinates, I would like to have multiple.

Leo
  • 11
  • 1
  • 2

2 Answers2

1

Nominatim class of geopy supports returning a full geometry data instead of a single point:

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="specify_your_app_name_here")

location = geolocator.geocode('Gramercy NYC', geometry='wkt')
geometry = location.raw['geotext']

geometry would contain the following:

'POLYGON((-73.9901251 40.7377992,-73.9869574 40.736466,-73.9887732 40.7339641,-73.9825505 40.7313605,-73.9785138 40.7368725,-73.9847526 40.7395063,-73.9856806 40.7382199,-73.9873061 40.7389028,-73.9877245 40.7383154,-73.9897603 40.7391731,-73.9901251 40.7377992))'

The supported formats for geometry are wkt, svg, kml, and geojson. See the Nominatim.geocode docs for more details.

You may also want to consider trying out your queries on the https://nominatim.openstreetmap.org/ page first, which provides a convenient web-interface which can show these geometries.

KostyaEsmukov
  • 848
  • 6
  • 11
0

I don't know that geopy can provide that.

What you really need are shapefiles for the geographies you are interested in. The US Census Bureau provides several of these for various types of geographies: Cartographic Boundary Shapefiles. Since your problem is specifically related to NYC, we can use a shapefile provided by NYU.

Use the NYU GeoJSON file:

import geopandas
df = geopandas.read_file('nyu-2451-34561-geojson.json')

df[df.ntaname == 'Gramercy'].geometry
#54    (POLYGON ((-73.97849845639804 40.7367909565254...
#Name: geometry, dtype: object

These geometries are Shapely MultiPolygons so you can access some of the useful attributes, for instance you could estimate everything as a rectangle instead of working with the Multipolygon:

df[df.ntaname == 'Gramercy'].geometry.bounds
#         minx      miny       maxx       maxy
#54 -73.989967  40.73135 -73.978052  40.743325

# Or get the full boundary:
df[df.ntaname == 'Gramercy'].geometry.boundary
#54    (LINESTRING (-73.97849845639804 40.73679095652...
#dtype: object

And if working in a jupyter notebook, you can get a sense of the shape:

df[df.ntaname == 'Gramercy'].geometry.item()

enter image description here

ALollz
  • 57,915
  • 7
  • 66
  • 89