Questa è la mia soluzione completa. Implica una sorta di hack per eseguire la divisione: ottengo i punti sulle linee (modi, per usare la terminologia OSM) ST_ClosestPoint
e poi li bufferizzo per una distanza molto piccola per far funzionare effettivamente la divisione. In caso contrario, errori di imprecisione / arrotondamento impedivano la divisione.
Questo ha il problema che genera due divisioni su ogni riga per punto (a causa del buffering). Per il mio uso questo andava bene, poiché in seguito ho instradato tra i punti di divisione più vicini ai punti originali, che erano al di fuori della linea, e potrebbe essere uno dei due punti di divisione dell'intersezione del buffer di linea.
Ho iniziato scaricando i dati OSM e impattandoli in Postgres:
CITY="MY_CITY"
BBOX="-46.6003,-23.7362,-46.4806,-23.5965"
wget --progress=dot:mega -O "$CITY.osm" "http://www.overpass-api.de/api/xapi?*[bbox=${BBOX}][@meta]"
# create database
createdb my_database
# add extensions
psql -d my_database -c "CREATE EXTENSION postgis;"
psql -d my_database -c "CREATE EXTENSION pgrouting;"
# import osm data to postgres database
osm2pgrouting \
-f MY_CITY.osm \
-d my_database \
-U user
# load points into db
shp2pgsql -I -s 4326 points_to_split_ways.shp public.points_to_split_ways | psql -d my_database
Dividere i modi usando un buffer:
WITH pts_ways AS (
-- get nearest way for each point we want to split the ways by
SELECT s.gid AS pt_id, ws.gid AS way_gid, s.geom AS pt_geom, ws.the_geom AS way_geom FROM points_to_split_ways s
CROSS JOIN LATERAL
(
SELECT w.gid, w.the_geom
FROM ways w
ORDER BY s.geom <-> w.the_geom LIMIT 1
) AS ws
), pts_on_ways AS (
-- "move" these points to be on top of the ways
SELECT pt_id, way_gid, ST_ClosestPoint(way_geom, pt_geom) as geom
FROM pts_ways
), ways_without_pts AS (
-- get the ways that don't have any points on them
SELECT the_geom as the_geom, gid as way_gid FROM ways
WHERE gid NOT IN (SELECT way_gid FROM pts_ways)
)
SELECT
way_gid as old_id,
-- we need to build a new unique ID, because split ways will share the old ID
row_number() over(order by way_gid) as gid,
-- this is the split way geometry
the_geom
FROM (
SELECT
way_gid,
-- split the ways and dump into indiviudal segments
(ST_Dump(ST_Split(line_geom, pt_geom))).geom AS the_geom
FROM (
(SELECT the_geom as line_geom, gid FROM ways) AS lines
LEFT JOIN
-- HACK: use a buffer to fix imprecisions / rounding errors
-- this will generate one extra splitting per point (each buffer will intersect each way twice)
-- but it's ok for our purposes
-- also, collect them grouped by the way to handle cases where there are multiple points on the same way
(SELECT ST_Collect(ST_Buffer(geom, 0.000001)) as pt_geom, way_gid FROM pts_on_ways GROUP BY way_gid) AS pts
ON lines.gid = pts.way_gid
) AS tmp1
-- union the ways without points, otherwise you'd get only the ones that were split
UNION ALL
SELECT way_gid, the_geom FROM ways_without_pts
) AS tmp2;
Crea la topologia necessaria per il routing con pgrouting:
SELECT UpdateGeometrySRID('ways_split','the_geom', 4326);
SELECT find_srid('public','ways_split','the_geom');
ALTER TABLE ways_split ADD COLUMN "source" integer;
ALTER TABLE ways_split ADD COLUMN "target" integer;
ALTER TABLE ways_split ADD PRIMARY KEY (gid);
ALTER TABLE ways_split ADD CONSTRAINT ways_source_fkey FOREIGN KEY (source) REFERENCES ways_split_vertices_pgr (id) MATCH FULL;
ALTER TABLE ways_split ADD CONSTRAINT ways_target_fkey FOREIGN KEY (target) REFERENCES ways_split_vertices_pgr (id) MATCH FULL;
SELECT pgr_createTopology('ways_split', 0.00001, 'the_geom', 'gid', clean := TRUE);
SELECT pgr_analyzeGraph('ways_split', 0.000001, the_geom := 'the_geom', id := 'gid');