-1

I am trying to retrieve the coordinates of all nodes/corners/edges of each commercial building in a list. E.g. for the supermarket Aldi in Macclesfield (UK), I can get from the UI 10 nodes (all the corners/edges of the supermarket) but I can only retrieve from osmnx 2 of those 10 nodes. I would need to access to the complete list of nodes but it truncates the results giving only 2 nodes of 10 in this case.Using this code below:

import osmnx as ox

test = ox.geocode_to_gdf('aldi, Macclesfield, Cheshire, GB')
ax = ox.project_gdf(test).plot()
test.geometry

or

gdf = ox.geometries_from_place('Grosvenor, Macclesfield, Cheshire, GB', tags)
gdf.geometry

Both return just two coordinates and truncate other info/results that is available in openStreetMap UI (you can see it in the first column of the image attached geometry>POLYGON>only two coordinates and other results truncated...). I would appreciate some help on this, thanks in advance.

Code

Ana
  • 103
  • 1
  • 8
  • 1
    This does not "just return two coordinates." It returns a GeoPandas GeoDataFrame. You are using a Jupyter notebook to display a tabular view of that GeoDataFrame, in which the first *n* characters of your various attributes are shown for aesthetic display purposes. If you want to access the geometry itself, then you can `.loc` or `.iloc` that row/column of the GeoDataFrame the same way you always would in the pandas ecosystem. None of your results' values have been truncated. – gboeing Oct 15 '21 at 16:56
  • Thank you very much, I am also now getting it with this code: gdf = ox.geocode_to_gdf('W352332709', by_osmid=True) polygon = gdf.iloc[0]['geometry'] polygon.exterior.coords list(polygon.exterior.coords) – Ana Oct 15 '21 at 17:13

2 Answers2

1

It's hard to guess what you're doing here because you didn't provide a reproducible example (e.g., tags is undefined). But I'll try to guess what you're going for.

I am trying to retrieve the coordinates of all nodes/corners/edges of commercial buildings

Here I retrieve all the tagged commercial building footprints in Macclesfield, then extract the first one's polygon coordinates. You could instead filter these by other attribute values as you see fit if you only want certain kinds of buildings. Proper usage of OSMnx's geometries module is described in the documentation.

import osmnx as ox

# get the building footprints in Macclesfield
place = 'Macclesfield, Cheshire, England, UK'
tags = {'building': 'commercial'}
gdf = ox.geometries_from_place(place, tags)

# how many did we get?
print(gdf.shape)  # (57, 10)

# extract the coordinates for the first building's footprint
gdf.iloc[0]['geometry'].exterior.coords

Alternatively, if you want a specific building's footprint, you can look up its OSM ID and tell OSMnx to geocode that value:

gdf = ox.geocode_to_gdf('W251154408', by_osmid=True)
polygon = gdf.iloc[0]['geometry']
polygon.exterior.coords
gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thank you very much for your help. Sorry, still stuck, I will explain it better: I need to extract the lat/longs of the corners of a list of commercial buildings located in different areas such as hypermarkets, warehouses, etc. This info is available in OSM UI but I can only print two corners of each building in my jupyter notebook. E.g. for 'aldi, Macclesfield, Cheshire, GB' with osm_id "352332709" I can print two nodes/coordinates (of two corners) but the UI shows 10 nodes with info of the whole perimeter of this supermarket. Thanks again! – Ana Oct 15 '21 at 09:28
  • 1
    How do the coordinates of the nodes of the building's perimeter, that you seek, differ from the coordinates of the building's perimeter, that I demonstrated? Providing a complete reproducible code snippet with clearer documentation would aid your description and our troubleshooting. – gboeing Oct 15 '21 at 14:51
  • I've updated the description and added an image, I think it clarifies the explanation. I've tried your piece of code that is correct but still it truncates the result giving just 2 results when I expect 10 for this example. Thank you very much for your help. – Ana Oct 15 '21 at 15:15
  • 1
    My code does not truncate the result. You can just run `list(polygon.exterior.coords)` at the end of my 2nd code snippet to see for yourself. It will return a list of 15 tuples. – gboeing Oct 15 '21 at 16:57
  • Yes, I got it, thank you very much! – Ana Oct 15 '21 at 17:11
0
gdf = ox.geocode_to_gdf('W352332709', by_osmid=True)
polygon = gdf.iloc[0]['geometry']
polygon.exterior.coords
list(polygon.exterior.coords)
Ana
  • 103
  • 1
  • 8