V
V
Viktor2018-03-01 19:38:06
OpenStreetMap
Viktor, 2018-03-01 19:38:06

How to find a region by latitude and longitude?

I have the coordinate of the user's location and I need to find out which region of the Russian Federation this point belongs to. What is the easiest way to do this?
Plus, I need to tighten the logic depending on the region. That is, I will have a table in the database with all the regions, and for each additional info, which I will use later.
I looked at Yandex api: in principle, I found AdministrativeAreaName in the response of the geocoder. It looks like, but I’m not sure that it will always work, plus I don’t quite understand how I can compare with the table: I need to get all possible AdministrativeAreaName options to compare with a column in the table ... They advised me
to look in the direction of OpenStreetMaps, but stopped at the fact that pear install http_request2 did not installed on homestead, on which I have a project, and this dependency is neededhttps://github.com/pear/Services_Openstreetmap/
In general, I don’t quite know which way to dig: to be fast, without crap and correctly :)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
shurshur, 2018-03-31
@shurshur

1. Download local.osm.pbf from gis-lab
2. Extract boundaries:
mkdump:

spoiler
osmosis \
  --rb file=local.osm.pbf \
  --tf accept-relations boundary=administrative --uw --un \
  outPipe.0=rels \
  \
  --rb file=local.osm.pbf \
  --tf accept-ways boundary=administrative --un \
  outPipe.0=ways \
  \
  --merge \
  inPipe.0=rels \
  inPipe.1=ways \
  \
  --wb file=local-bounds.osm.pbf

3. Load borders in postgis:
Style file adm.style:
spoiler
node,way   name:ru         text      linear
node,way   name:en         text      linear
node,way   alt_name        text      linear
node,way   alt_name:ru     text      linear
node,way   official_name   text      linear
node,way   admin_level  text         linear
node,way   boundary     text         linear
node,way   name         text         linear
node,way   ref          text         linear
node,way   place        text    linear

Loading data (to a database named local, table prefix adm_):
4. Now you can use a query to find out the ownership of any point:
spoiler
local=> select osm_id,name,boundary,admin_level from (select ST_Transform(ST_SetSRID(ST_MakePoint(37.6,55.6),4326),3857) as point) a,adm_polygon b where a.point && b.way and ST_Within(a.point,b.way) and boundary='administrative' order by admin_level::INT asc;
  osm_id  |             name              |    boundary    | admin_level 
----------+-------------------------------+----------------+-------------
 -1029256 | Центральный федеральный округ | administrative | 3
  -102269 | Москва                        | administrative | 4
 -1282181 | Южный административный округ  | administrative | 5
  -950664 | район Чертаново Южное         | administrative | 8
(4 rows)

If the data is loaded into the same database, then it is possible to extract data belonging to the desired area with one request.

M
Maxim Fedorov, 2018-03-01
@Maksclub

Is Yandex suitable? https://tech.yandex.ru/maps/geocoder/

A
Anton Shamanov, 2018-03-01
@SilenceOfWinter

Request to the Yandex Maps API.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question