Tuesday, 26 January 2016

postgis - st_startpoint(the_geom) returning empty values


When I upload a shapefile to PostgreSQL using the QGIS tool I can create the routing topology following this tutorial without any problem.


But when I use the PostGis Shapefile Import/Export Manager or the SQL generated by the shp2pgsql.exe I can't get any values for the x1,x2,y1 and y2 fields.


UPDATE edge_table SET x1 = st_x(st_startpoint(the_geom)),

y1 = st_y(st_startpoint(the_geom)),
x2 = st_x(st_endpoint(the_geom)),
y2 = st_y(st_endpoint(the_geom)),
cost_len = st_length_spheroid(the_geom, 'SPHEROID["WGS84",6378137,298.25728]'),
rcost_len = st_length_spheroid(the_geom, 'SPHEROID["WGS84",6378137,298.25728]');

Only cost_len and rcost_len have values after running that script.


And because of that I can't get any route at all. What am I missing?



Answer



ST_StartPoint() only accepts LineString as input. You'll have to strip your table down from MultiLineString to LineString, either by taking only the first element up each geometry



ALTER TABLE foo 
ALTER COLUMN geom
TYPE Geometry(LineString,4326)
USING ST_GeometryN(geom,1)

Or, more correctly, dumping the multis out, in case there are legit multis.


CREATE TABLE foo_single AS
SELECT (ST_Dump(geom)).geom::Geometry(LineString,4326) AS geom
gid
FROM foo;

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...