Giter Club home page Giter Club logo

Comments (28)

gboeing avatar gboeing commented on May 14, 2024 15

OSMnx is behaving correctly. The API returns a point for the query 'Baltimore, Maryland, USA' but graph_from_place and graph_from_polygon require a geometry that is a Polygon or MultiPolygon to proceed -- as the error message explains.

OSMnx passes the query string 'Baltimore, Maryland, USA' to OSM's nominatim API to get a polygon representing the boundaries of the place. It then collects the street network within these boundaries. But if you pass 'Baltimore, Maryland, USA' to nominatim directly yourself, you can see the result.

Nominatim successfullly geocodes it and returns 2 results. The first's geometry is simply a point representing the center of the city. The 2nd result's geometry is the city's administrative boundary. The API usually prioritizes the latter result with a higher importance score, but for whatever reason, in Baltimore's case it doesn't. By default OSMnx uses the first result and tries to create a street network from it. But with a point, it (obviously) can't find any network data within the geometry.

If one of the geocoding results contains polygon geometry:

We can point OSMnx at the nth result (in this example, the 2nd) that correctly includes the boundary data by using the which_result function argument, as demonstrated in the tutorial:

G = ox.graph_from_place('Baltimore, Maryland, USA', network_type='drive', which_result=2)

If none of the geocoding results contain polygon geometry:

That is, if OpenStreetMap does not have a boundary polygon for your place: you can still get the local street network using graph_from_address(place_name, distance), or the graph_from_point function passing in a lat-long point, or the graph_from_polygon function passing in your own polygon (from a shapefile, etc.) of the place's boundaries.

from osmnx.

gboeing avatar gboeing commented on May 14, 2024 8

Another example:

G = ox.graph_from_place('Beijing, China', network_type='drive')
G_proj = ox.project_graph(G)
fig, ax = ox.plot_graph(G_proj)

...throws the error described above because the first geocode result for "Beijing, China" returns a point as the geometry. The second geocode result returns a polygon so this error is fixed by passing which_result=2:

G = ox.graph_from_place('Beijing, China', which_result=2, network_type='drive')
G_proj = ox.project_graph(G)
fig, ax = ox.plot_graph(G_proj)

...produces:

If OSM had no polygon for this place query in any of the geocoding results, we could have used graph_from_address instead, like:

G = ox.graph_from_address('Beijing, China', distance=10000, network_type='drive')

See the documentation for more info.

from osmnx.

gboeing avatar gboeing commented on May 14, 2024 1

The graph_from_place docstring has been updated to reflect this.

from osmnx.

gboeing avatar gboeing commented on May 14, 2024 1

@scottishbee the Nominatim geocoder that resolves place names has changed since then. Try to provide an explicit query, and it should work, as seen in this example:

place_query = {'city':'San Francisco', 'state':'California', 'country':'USA'}
G = ox.graph_from_place(place_query, network_type='drive')

from osmnx.

gboeing avatar gboeing commented on May 14, 2024 1

No worries. It's just easier for everyone to respond usage questions on StackOverflow than here in the issue tracker. Good luck.

from osmnx.

d3netxer avatar d3netxer commented on May 14, 2024

@gboeing I get this error when trying to run

G = ox.graph_from_place('Beijing, China', which_result=2, network_type='drive')
G = ox.graph_from_place('Beijing, China', which_result=2, network_type='drive')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/myusername/anaconda/envs/osmnx/lib/python3.6/site-packages/osmnx/core.py", line 1800, in graph_from_place
    clean_periphery=clean_periphery, infrastructure=infrastructure)
  File "/Users/myusername/anaconda/envs/osmnx/lib/python3.6/site-packages/osmnx/core.py", line 1679, in graph_from_polygon
    G_buffered = create_graph(response_jsons, name=name, retain_all=True, network_type=network_type)
  File "/Users/myusername/anaconda/envs/osmnx/lib/python3.6/site-packages/osmnx/core.py", line 1290, in create_graph
    raise ValueError('There are no data elements in the response JSON objects')
ValueError: There are no data elements in the response JSON objects

any idea what it could be?

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@d3netxer I just re-ran that code line, and it succeeded without error on my machine.

from osmnx.

scottishbee avatar scottishbee commented on May 14, 2024

I am seeing the same error running a line that previously worked (~last worked in August 2017):

G = ox.graph_from_place('San Francisco, California, USA', network_type='all_private')

Adding the which_result=2 parameter did not change the error.

But dropping the state/country (
G = ox.graph_from_place('San Francisco', network_type='all_private', which_result=2)
) did run successfully.

from osmnx.

shreyatpandey avatar shreyatpandey commented on May 14, 2024

Hey,
I have question regarding the one-square mile image of Irvine,Portland.
Which function did you use to obtain the desired image? If you could please share that code or function with us.
Thanx,

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@shreyatpandey https://github.com/gboeing/osmnx-examples

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

For more on geocoding/polygon logic, see also this thread.

from osmnx.

puntofisso avatar puntofisso commented on May 14, 2024

Hi @gboeing I'm adding to this as I think there's something going on, but please do point out if I'm wrong (or tell me to make this its own issue, if appropriate) :)

I'm trying to make this call, with varying which_result values,
gdf_city = ox.gdf_from_place('City of London, UK')
and I cannot just make it the same as the output of the query
https://nominatim.openstreetmap.org/search?format=json&limit=2&dedupe=0&polygon_geojson=1&q=City%20of%20London,%20UK

I understand that gdf_from_place uses exactly these value for format, dedupe, limit, and polygon_geojson.

If I call nominatim directly, I get two results

  1. a POINT for osm_id 27365030
  2. a POLYGON for osm_id 51800

However, gdf_from_place seems to return other values:

  1. ox.gdf_from_place('City of London, UK',which_result=0) returns a POINT (-0.09199830000000001 51.5156177) for City of London, London, Greater London, England, UK
  2. ox.gdf_from_place('City of London, UK',which_result=1) returns a POINT (-0.09199830000000001 51.5156177) for City of London, London, Greater London, England, UK (same as 1)
  3. ox.gdf_from_place('City of London, UK',which_result=2) returns a POLYGON (-0.1138211 51.5182576, etc) for what appears to be the Inner Temple (a different relation to osm_id 51800).

Am I missing something, or is there any issue in the way nominatim queries are parsed?

On a second note, I cannot find documentations on how to pass gdf_from_place queries that contain more complex searches. For example, I'd like to do something like ox.gdf_from_place({ 'osm_id': '51800'}) to verify the above, but it doesn't seem to produce any result in any form I've tried. The documentation suggests:
query (string or dict) – query string or structured query dict to geocode/download

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@puntofisso regarding your first question, you can run:

import requests
url = 'https://nominatim.openstreetmap.org/search?format=json&limit=2&dedupe=0&polygon_geojson=1&q=City of London, UK'
requests.get(url).text

...to see exactly what nominatim is returning when we make that GET request. If you see something unexpected or inconsistent, please open a new issue.

Regarding your second question, gdf_from_place is for geocoding query strings/dicts, not for looking up IDs. Other discussion about that would be best suited to a new issue or a PR if new functionality is proposed.

from osmnx.

puntofisso avatar puntofisso commented on May 14, 2024

Hi @gboeing, thanks for responding so quickly! :) So... let me clarify a little after doing a couple of tests.

What seems to be happening here is that calling that url via requests returns two different results than when I use a browser, which are different from the results returned by gdf_from_place.

Using a browser, I pass 'City of London, UK' as its (urlencoded) counterpart 'City%20of%20London,%20UK', so the url I enter in the browser is:
https://nominatim.openstreetmap.org/search?format=json&limit=2&dedupe=0&polygon_geojson=1&q=City%20of%20London,%20UK

The query returns the two results as stated above:

  1. a POINT for osm_id 27365030
  2. and a POLYGON for osm_id 51800.

If I use requests, regardless of using urlencoding or not, i.e. whether i call

import requests url = 'https://nominatim.openstreetmap.org/search?format=json&limit=2&dedupe=0&polygon_geojson=1&q=City of London, UK' requests.get(url).text

or

url = 'https://nominatim.openstreetmap.org/search?format=json&limit=2&dedupe=0&polygon_geojson=1&q=City%20of%20London,%20UK'

I get two result which are different from the in-browser ones:

  1. a POINT for osm_id 27365030 which is the same as above
  2. a different POLYGON in the second result, for osm_id 4001857 (instead of osm_id 51800 - although this is an acceptable result).

If I use gdf_from_place, I get something else entirely

  1. ox.gdf_from_place('City of London, UK',which_result=0) -> returns POINT as above
  2. ox.gdf_from_place('City of London, UK',which_result=1) -> returns the same POINT
  3. ox.gdf_from_place('City of London, UK',which_result=2) -> returns the POLYGON for osm_id 4001857

So even if we discount the browser behaviour, which is probably caused by the way the urlencoding engages with the http library, if you say that gdf_from_place should work exactly as the result of requests then there is a discrepancy for the first two results. What would be a good place to understand where the discrepancy stems from?

If I'm not understanding how this is supposed to work, please feel free to say :) I'm just trying to make sure I get 100% how the queries are parsed.

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@puntofisso could you open a new issue for this and I'll explore it? I think this is requests vs browser inconsistency is unrelated to the original issue here.

from osmnx.

puntofisso avatar puntofisso commented on May 14, 2024

Sure @gboeing and thanks!

from osmnx.

tiwaris436 avatar tiwaris436 commented on May 14, 2024

I am getting error while running these codes.

G = ox.graph_from_place('Pune, Maharashtra', which_result=2, network_type='drive')
G_proj = ox.project_graph(G)
fig, ax = ox.plot_graph(G_proj)

"TypeError: Geometry must be a shapely Polygon or MultiPolygon. If you requested graph from place name or address, make sure your query resolves to a Polygon or MultiPolygon, and not some other geometry, like a Point. See OSMnx documentation for details."

Please help.
Thanks

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@tiwaris436 I'm not really sure what else to say that hasn't already been said in this thread and the many other related issue threads. The error message is pretty straightforward the documentation here provides comprehensive info on why you're seeing this and how to resolve it.

from osmnx.

etrnote avatar etrnote commented on May 14, 2024

I've also run into the same problem a few times, and adding which_result=2 solved it.
How about adding it to the code?
Maybe adding a parameter that allows iteration over the results till a polygon is found,
or an exception that tries to execute gdf_from_place with which_result=2 as a parameter in case it fails on the first result.
I'd be happy to try and add it if this sounds like a good idea.

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@etrnote see #320

from osmnx.

vamatya avatar vamatya commented on May 14, 2024

@scottishbee the Nominatim geocoder that resolves place names has changed since then. Try to provide an explicit query, and it should work, as seen in this example:

place_query = {'city':'San Francisco', 'state':'California', 'country':'USA'}
G = ox.graph_from_place(place_query, network_type='drive')

The above works. However, if I changed place_query as follows:
place_query = {'county':'Los Angeles', 'state':'California', 'country':'USA'}
G = ox.graph_from_place(place_query,which_result=2, network_type='drive')
ox.save_graph_shapefile(G, filename='la_county')

, I get the following error:

File "/Users/username/test_runs/scripts/vto_scripts/osmnx/osmnx_place_by_name.py", line 6, in
G = ox.graph_from_place(place_query,which_result=2, network_type='drive')
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/osmnx/graph.py", line 610, in graph_from_place
polygon = gdf_place["geometry"].unary_union
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/geopandas/geodataframe.py", line 576, in getitem
result = super(GeoDataFrame, self).getitem(key)
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/pandas/core/frame.py", line 2800, in getitem
indexer = self.columns.get_loc(key)
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/pandas/core/indexes/base.py", line 2648, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas/_libs/index.pyx", line 111, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/index.pyx", line 138, in pandas._libs.index.IndexEngine.get_loc
File "pandas/_libs/hashtable_class_helper.pxi", line 1619, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas/_libs/hashtable_class_helper.pxi", line 1627, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'geometry'

I also tried:
place = 'Los Angeles County, California, USA'
in place of "place_query", which gives following error:

File "/Users/username/test_runs/scripts/vto_scripts/osmnx/osmnx_place_by_name.py", line 6, in
G = ox.graph_from_place(place,which_result=2, network_type='drive')
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/osmnx/graph.py", line 626, in graph_from_place
custom_settings=custom_settings,
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/osmnx/graph.py", line 482, in graph_from_polygon
G_buffered, polygon, retain_all=retain_all, truncate_by_edge=truncate_by_edge
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/osmnx/truncate.py", line 166, in truncate_graph_polygon
gdf_nodes, polygon, quadrat_width=quadrat_width, min_num=min_num
File "/Users/username/miniconda3/envs/ox3.6/lib/python3.6/site-packages/osmnx/utils_geo.py", line 427, in _intersect_index_quadrats
raise Exception("There are no nodes within the requested geometry")
Exception: There are no nodes within the requested geometry

Any insight what I might be doing incorrectly?

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

Any insight what I might be doing incorrectly?

@vamatya per the contributing guidelines, please ask general "how-to" usage questions on https://stackoverflow.com/ for community troubleshooting. One quick thought: why are you using which_result=2? It'll probably work fine if you don't.

from osmnx.

vamatya avatar vamatya commented on May 14, 2024

@gboeing Thank you for the prompt reply. Another tool that used OSMNX to generate the shapefile suggested the use of which_result=2. You are probably correct, that ignoring this value could work. Also, thanks for reminding me about the contributing guidelines.

from osmnx.

kenomaerz avatar kenomaerz commented on May 14, 2024

Would you accept a pull request that automatically selects the first polygon from the results if which_result is not specified?

Just started playing around with osmnsx. I intend to use it for a printing project. Thank you for your efforts!

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@kenomaerz see discussion in #320. You may want to weigh in there.

from osmnx.

korasami96 avatar korasami96 commented on May 14, 2024

Hello @gboeing . Iam trying to execute :
import osmnx as ox place_query = {'state':'Illinois', 'country':'USA'} G = ox.graph_from_place(place_query, network_type='drive',which_result=2)

But iam getting this error :
OSM returned fewer than which_result=2 results for query "{'state': 'Illinois', 'country': 'USA'}"

Please, can you help me?
Thanks

from osmnx.

gboeing avatar gboeing commented on May 14, 2024

@korasami96 per the contributing guidelines please as "how to" questions on StackOverflow.

from osmnx.

mejl avatar mejl commented on May 14, 2024

I get

ValueError Found no graph nodes within the requested polygon

when adding dist=1.

G = ox.graph_from_point((lat, lng), dist=1, network_type="bike")


dist=100 works.
G = ox.graph_from_point((lat, lng), dist=100, network_type="bike")

But How can I get rid of this error if I have dist=1?

from osmnx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.