NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

I'm using PyOsmium. Given a node on OpenStreetMap, for example a village or a city like Chicago, how should I derive information such as the admin_level=4 region it is located in (in this case Illinois), and the country it belongs to (the USA)? In addition to it, I would also like to derive information on the country (in this example, the USA).

This probably involves using refs and relations, but I don't quite know what strategy I should use to get this information (as efficiently as possible). Can you help?

asked 27 Jul '21, 12:40

fsaler's gravatar image

fsaler
16335
accept rate: 0%


You will save yourself a lot of trouble if you can afford to run a local "Nominatim" installation and then simply use Nominatim's reverse geocoding ability. Alternatively, try "Photon" for the same purpose; there are ready-to-go Photon data dumps available for download, obviating the need for a database and an import process.

Otherwise, the likely easiest path is to pre-filter a planet file with the command-line osmium utility (only keep stuff tagged boundary=administrative and with an admin_level of interest to you), then load the entirety of that file into Pyosmium, construct geometries, use the Pyosmium/Shapely interoperability to stuff the geometries into a Shapely R-Tree. Once that is done, you can efficiently query which administrative bounding boxes a point is in; if this yields more than one result (eg half the points in New Mexico are also in the Texas bounding box), you need to do proper (and more costly) point-in-polygon checks for the resulting polygons but that is also easy with Shapely.

A third option is to do essentially what I wrote above but employ a PostGIS database to do it, i.e. load admin boundaries into PostGIS with osm2pgsql, then run SQL queries to find which polygons a point is in.

permanent link

answered 27 Jul '21, 13:09

Frederik%20Ramm's gravatar image

Frederik Ramm ♦
82.5k927201273
accept rate: 23%

edited 27 Jul '21, 17:18

alester's gravatar image

alester
6.6k266100

I'm going to try the Photon route, it sounds smartest. Where can I find the data dumps you mention? I need one for the whole planet, but I don't need data on ways or most kinds of features, only inhabited places (hamlet or bigger).

(26 Aug '21, 09:54) fsaler

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×362
×236
×191
×6

question asked: 27 Jul '21, 12:40

question was seen: 1,450 times

last updated: 26 Aug '21, 09:54

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum