Issue
In the below post I make some queries and it works fine. However when I add the lines that do the coordinates conversion and the line that query the geom
I receive the following error when I run the web-service:
Input geometry has unknown (0) SRID
I am new to Postgis. How can I fix this issue?
Code:
query = """ WITH data AS (
SELECT '{featuresArray}'::json AS featuresCollection
)
SELECT gid,geom,type::text,properties::text,
array_to_string(array_agg(x_4326||' '||y_4326 ORDER BY gid),',') AS g4326,
array_to_string(array_agg(x_25832||' '||y_25832 ORDER BY gid),',') AS g25832
FROM (
SELECT
ROW_NUMBER() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feature->>'geometry')) AS geom,
feature->>'type' AS type,
feature->>'properties' AS properties,
ST_X((ST_DumpPoints((ST_GeomFromGeoJSON(feature->>'geometry')))).geom) x_4326,
ST_Y((ST_DumpPoints((ST_GeomFromGeoJSON(feature->>'geometry')))).geom) y_4326,
ST_X((ST_DumpPoints((ST_Transform(ST_GeomFromGeoJSON(feature->>'geometry'),25832)))).geom) x_25832,
ST_X((ST_DumpPoints((ST_Transform(ST_GeomFromGeoJSON(feature->>'geometry'),25832)))).geom) y_25832
FROM (SELECT json_array_elements(featuresCollection->'features') AS feature FROM data) AS f) j
GROUP BY gid,type::text,properties::text,geom
ORDER BY gid;""".format(featuresArray=featuresArray)
Solution
Some PostGIS functions rely on SRS, such as ST_Transform
. You have to specify which SRS you're transforming from, otherwise the conversion script has no reference to compute the new coordinates, e.g. from EPSG:25832
to EPSG:4326
:
SELECT ST_Transform('SRID=25832;POINT(1 1)',4326);
.. otherwise it will raise an exception
SELECT ST_Transform('POINT(1 1)',4326); -- <-- WKT literal without SRS
ERROR: ST_Transform: Input geometry has unknown (0) SRID
With ST_SetSRID
you can set the SRS to geometries in case they haven't any - as your example suggests, e.g. .
SELECT ST_Transform(
ST_SetSRID('POINT(1 1)'::geometry,25832),
4326);
The same principle goes for CREATE TABLE
and INSERT
/ UPDATE
statements. When creating a table we declare the SRS as follows ..
CREATE TABLE t (geom geometry(point,4326));
.. so PostGIS expects that all incoming geometries have the same SRS ..
INSERT INTO t VALUES ('SRID=4326;POINT(1 1)');
.. otherwise it raises an exception too
INSERT INTO t VALUES ('SRID=25832;POINT(1 1)');
ERROR: Geometry SRID (25832) does not match column SRID (4326)
Answered By - Jim Jones
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.