I have not used PostGIS much before.
I have TWO PostGIS tables:
- roads
- country_boundary
The roads table is so big (within the application 500MB) that I wish to display it by country_boundary.
I can do this successfully using the following query:
select ST_ASBINARY(roads.geog::geometry) from roads, country_boundary
where country_boundary.adm0_name='Sudan'
and ST_Intersects(roads.geog::geometry, country_boundary.geog::geometry)
However, it takes too long to display the data (several minutes) note: I have tried most optimization techniques, spatial indexing, (ST_Simplify/ST_SimplifyPreserveTopology) these either remove data or takes just as long.
I thought I had found a simple solution that worked - I add a new column 'adm0_name' (aka country name) to the roads table and populated it using the ST_Intersects query above.
update roads
set adm0_name = 'Sudan'
where ST_ASBINARY(roads.geog::geometry) in
(
select ST_ASBINARY(roads.geog::geometry) from roads, country_boundary where country_boundary.adm0_name='Sudan' and ST_Within(roads.geog::geometry, country_boundary.geog::geometry)
);
The resulting display query: works fast:
select ST_ASBINARY(roads.geog::geometry) from roads where roads.adm0_name='Sudan'
However, the ST_Intersects includes a small number of geometries (polyline(s) that are shared with neighbouring countries. This means polylines (roads) crossing the country boundary, cannot be give a unique country name. Note: I originally used ST_Within but this removed data that was shared between neighbouring countries (as expected with features like roads/rivers that do not respect country boundaries).
I decided I needed to use ST_Intersection because this would avoid the boundary issue - it would 'clip' the road dataset to each country boundary thus avoiding the polyline boundary issue.
I have an intersection query that displays the data OK
Select ST_ASBINARY (
ST_Intersection(g1.geom1, g1.geom2)) FROM (Select country_boundary.geog::geometry As geom1, roads.geog::geometry As geom2 from country_boundary, roads
where country_boundary.adm0_name = 'Sudan') AS g1;
However, I cannot find a way to update the roads.adm0_name column with the country name because ST_Intersection seems to returns a geometry, it does not behave like ST_Intersects, where I could update the column.
My attempt to do this is below ---> 26 hours later I have reached a point of head scratching.
Could you help me out?
update roads
set adm0_name = 'Sudan'
where ST_ASBINARY(roads.geog::geometry) in
(
Select ST_ASBINARY (
ST_Intersection(g1.geom1, g1.geom2)) FROM (Select country_boundary.geog::geometry As geom1, roads.geog::geometry As geom2 from country_boundary, roads where country_boundary.adm0_name = 'Sudan') AS g1;
);
However the above statement fails ---> I need to update the roads.adm0_name with country_boundary.adm0_name where (country_boundary "ST_Intersection" roads).