[GIS] Getting all GPS points on road from OpenStreetMap data

gpsgpxopenstreetmappostgis

I'm trying to extract GPS points on roads in a region. For each road, I need a GPS point on that road every 10 meters.

I'm aware that Google Map API offer such way points, but due to the latency-sensitive nature of this application and amount of data needed, I cannot afford to do network communications to Google server.

I came across OSM but as someone new, it's very unclear how I can extract GPS points on roads from OSM. I know roads are stored as "line string" with keyword "highway".

Some GeoJSON file includes start and end gps locations for each road but how do I extract all GPS points (Lat,Lng) along the road?

Best Answer

In my answer here I use a query to create points along lines with a distinct distance

WITH line AS 
    (SELECT
        your_polylinestring_id,
        (ST_Dump(geom)).geom AS geom
    FROM your_polylinestring_table),
linemeasure AS
    (SELECT
        ST_AddMeasure(line.geom, 0, ST_Length(line.geom)) AS linem,
        generate_series(0, ST_Length(line.geom)::int, 10) AS i
    FROM line),
geometries AS (
    SELECT
        i,
        (ST_Dump(ST_GeometryN(ST_LocateAlong(linem, i), 1))).geom AS geom 
    FROM linemeasure)

    SELECT
        i,
        ST_SetSRID(ST_MakePoint(ST_X(geom), ST_Y(geom)), 31468) AS geom
    FROM geometries

So, obviously you are using a PostGIS/PostgreSQL database, You can import OSM data (with osm2pgsql, osmosis, etc.).

Important is an unique ID. You can use the osm_id coming along when you import the OSM data.

When you have data in geojson format you can use the postgis function ST_GeomFromGeoJSON. Another way: Here is a howTo to convert geojson data to a shapefile. The shapefile you can easily import to postgres/postgis with gdal/ogr, pgadmin or QGIS.

After that you can export your point data as GPX with ogr2ogr. The comments for this GIS SE answer have some useful Information.

ogr2ogr -f GPX points.gpx PG:'host=server user=username dbname=database'
Related Question