<div dir="ltr">Hi PostGIS users,<div><br></div><div>I'm trying to figure out what is the best method to publish data that's using linear referencing. I have thought making a view that converts features using linear referencing to a geometries. Now my question is how I could use spatial index with my view.</div>
<div><br></div><div>I have a table of 4D linestrings representing roads and then another table representing features along the roads. Those are using linear referencing. So features has road_id, starting and ending point on a road.</div>
<div>Now I have created a view to the access the geometry easily so that I can publish the view from GeoServer. No my question is how to use spatial index in this case. If I have millions of roads I would like to use spatial index of the roads table to restrict the view to only those rows. Is there a way to pass the spatial where clause of the view to the inner sql?</div>
<div><br></div><div>Here is my table structure:</div><div><div>create table roads(</div><div><span class="" style="white-space:pre"> </span>gid int primary key,</div><div><span class="" style="white-space:pre"> </span>geom geometry(linestringzm));</div>
<div>create index roads_geom on roads using gist(geom);</div><div><br></div><div>create table features_lr(</div><div><span class="" style="white-space:pre"> </span>fid int primary key,</div><div><span class="" style="white-space:pre"> </span>startPoint real,</div>
<div><span class="" style="white-space:pre"> </span>endPoint real,</div><div><span class="" style="white-space:pre"> </span>road_id int references roads(gid));</div><div><br></div><div>insert into roads values (1, 'LINESTRING(0 0 0 0, 1 0 0 1, 1 1 0 2)');</div>
<div>insert into roads values (2, 'LINESTRING(1 1 0 0, 2 1 0 1, 3 1 0 2, 5 1 0 4)');</div><div>insert into features_lr values (1, 0.1, 0.5, 1);</div><div>insert into features_lr values (2, 0.5, 3, 2);</div><div><br>
</div><div>create view features_v as </div><div><span class="" style="white-space:pre"> </span>with roads_w as (</div><div><span class="" style="white-space:pre"> </span> select</div><div><span class="" style="white-space:pre"> </span> gid,</div>
<div><span class="" style="white-space:pre"> </span> st_m(st_startpoint(geom)) as startPoint,</div><div><span class="" style="white-space:pre"> </span> st_m(st_endpoint(geom)) as endPoint,</div><div><span class="" style="white-space:pre"> </span> geom</div>
<div><span class="" style="white-space:pre"> </span> from roads)</div><div><span class="" style="white-space:pre"> </span>select</div><div><span class="" style="white-space:pre"> </span> f.fid as fid,</div><div><span class="" style="white-space:pre"> </span> ST_Line_Substring(r.geom, f.startPoint / (r.startPoint + r.endPoint), f.endPoint / (r.startPoint + r.endPoint)) as geom</div>
<div><span class="" style="white-space:pre"> </span>from</div><div><span class="" style="white-space:pre"> </span> roads_w r</div><div><span class="" style="white-space:pre"> </span> inner join features_lr f on r.gid = f.road_id;</div>
<div><br></div><div>select fid, st_astext(geom) from features_v where geom && 'BOX(0 0,1 1)'::box2d;</div></div><div><br></div><div><br></div><div>BR,</div><div><br></div><div>-Lauri</div></div>