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

I have a big list of locations with their coordinates in a city that is fully divided by a river that's running from north to south. Now I want to determine for each location if it is located east or west of that river. If it was possible to get the coordinates of the river in a granular enough manner, I could achieve that goal by comparing coordinates between location and the river.

I have not found a way to get these coordinates so far. I'm new to OSM, however, and would be thrilled if someone could point me in the right direction here.

asked 18 Feb '21, 14:55

RedAlakazam's gravatar image

accept rate: 0%

edited 18 Feb '21, 14:56

If you use osm2pgsql to load the OSM data into PostGIS database (based on postsql) then you can use use the ST_closestpoint() function to find the closest point on a way (your river) to a point. You can then take the two points (one on the river and the one you are interested in) and use the ST_Azimuth() function to determine the direction of travel between the two points.

If the the river is serpentine you may get a result other than what you wish for (if the point is within a loop or near a curve in the river the azimuth could be quite different than you want). I don’t have enough experience with this stuff to suggest a way around that issue though.

permanent link

answered 19 Feb '21, 02:24

n76's gravatar image

accept rate: 17%

I ended up using @stf's suggestion of using the ST_AZIMUTH() function.

SELECT CASE WHEN DEGREES(ST_AZIMUTH(pt, cl_pt)) > 180 THEN 'right' ELSE 'left' END AS RIVER_ORIENTATION, ROUND(ST_DISTANCE(pt, cl_pt)) AS RIVER_DISTANCE, name FROM ( SELECT name, ST_ClosestPoint(pt, line) AS pt, ST_CLOSESTPOINT(line, pt) AS cl_pt, line as river FROM (SELECT name, ST_TRANSFORM(ST_setSRID(ST_MAKEPOINT(longitude, latitude), 4326), 3857) AS pt FROM location_information) tmp1, (SELECT ST_UNION(way) AS FROM planet_osm_line WHERE name='INSERT RIVER NAME') tmp2 ) tmp3

Making the left/right distinction at 180 degrees worked out perfectly in this case. @Frederik Ramm's suggestion of splitting the map in two using the river would've lead to a more robust solution but the river had some arms with the same name which made it inconvenient in my case.

permanent link

answered 22 Feb '21, 18:21

RedAlakazam's gravatar image

accept rate: 0%

Another computational option would be: 1. Create polygon around whole city. 2. Get river geometry as a line. 3. split polygon in two along that line. 4. for every point, ask "is this point in the left-land polygon", if not it'll be on the right.

If I understand the question correctly, the main issue is extracting the river's coordinates. This could potentially be done in PostGIS if the data is loaded (something along the lines of SELECT ST_UNION(way) FROM planet_osm_line WHERE "natural"='river' AND name="XYZ River"). This query would also take care of combining several "pieces" of river into one, but would yield unwanted results if the river should have "arms" that share the same name! If you don't want to load the data into PostGIS first, the Overpass service can be used to request all things tagged natural=river with a certain name in a certain bounding box, however additional libraries would then have to be used to combine river parts into one, to split a polygon, or to determine wheter a point is inside a polygon. If you're attempting to do it all in Javascript then Turf.js is an option for that.

permanent link

answered 19 Feb '21, 15:11

Frederik%20Ramm's gravatar image

Frederik Ramm ♦
accept rate: 23%

Follow this question

By Email:

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



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "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:


question asked: 18 Feb '21, 14:55

question was seen: 1,105 times

last updated: 22 Feb '21, 18:21

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