1

When running an ST_INTERSECTION query on two linestrings in PostGIS, I expect it to return a point (e.g. 'POINT(1 2)'). This works 99,99 % of the time. However, sometimes (for whatever reason), my query returns e.g. a MultiPoint, LineString, ... or any other spatial type. In that case, I simply want to skip my queryResult and move to the next row in my table.

So I am looking for something like :

  if queryResult.SpatialType() = POINT:
         'do something'
  elif queryResult.SpatialType() != POINT:
         pass

But I cannot get anything like this to work.

What would be the correct statement to make a proper distinction between a point as the queryResult and any other spatial type ?

8
  • 1
    What software? If linestrings intersects at several points the result is multipoint, if lines are overlapping the common section is linestring, is they are many then multilinestring, and if there are also point/multipoint intersections the result is a geometry collection. I am surprised if you get any other combinations.
    – user30184
    Jun 4, 2020 at 11:24
  • yes, I have tried ST_GeometryType but with no luck so far
    – EricBHK
    Jun 4, 2020 at 11:27
  • Qgis, Postgis and Python
    – EricBHK
    Jun 4, 2020 at 11:28
  • You should show how you have tried to use ST_GeometryType. This gives ST_Point select ST_GeometryType(ST_GeomFromText('POINT (0 0)')).
    – user30184
    Jun 4, 2020 at 11:34
  • SELECT ST_GeometryType(ST_aseWKT( ST_Intersection( ST_SetSRID( (SELECT "GeomCommuneLINE" FROM public."LINE" WHERE "osm_id" = '123456789'),4326), ST_SetSRID( (SELECT "GeomCommuneLINE" FROM public."LINE" WHERE "osm_id" = '987654321'),4326) )))
    – EricBHK
    Jun 4, 2020 at 11:47

1 Answer 1

1

This may be an ugly example but it works. Intersection in the first query is a point and 1 row is returned, while in the second query intersection is a LineString and no rows are returned.

select * from
(select ST_Intersection((ST_GeomFromText('linestring (0 0,1 1)')),
(ST_GeomFromText('linestring (0 1,1 0)'))) geom)
as a
where 
ST_GeometryType(a.geom)='ST_Point';


select * from
(select ST_Intersection((ST_GeomFromText('linestring (0 0,1 1)')),
(ST_GeomFromText('linestring (0 0,1 1)'))) geom)
as a
where ST_GeometryType(a.geom)='ST_Point';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.