3

I'm trying to remove inner polygons with postgis. I've created a routine, that resumes in:

From a polygon table, with a unique id:

  • Create a linestring table from polygon
  • Import to postgis
  • Get the length of each line

Now I have a table with, for example, 5 linestrings for each id, each linestring was a polygon, and I want to get ONLY the longest line from each ID.

enter image description here

For this, I have a select, but I don't know how to group the geometry, to get ONLY the longest line

select  id, geom as length from esp_lines group by id

I have to group it by another field, but I don't know which one.

The other solution I've found is:

select  id, max(geom) as length from esp_lines group by id

but max(geom) doesn't give me the expected result (the longest linestring)

Does anybody know if is there any function to get the longest linestring of a group?

1
  • 3
    there is no guarantee that the longest line would be the outer line
    – vinayan
    Jul 30, 2014 at 8:43

2 Answers 2

6

You are looking for Exterior ring

SELECT gid, ST_ExteriorRing(the_geom) AS ering
FROM sometable;
1
  • ST_ExteriorRing(the_geom) works only on POLYGONs. We need a solution working on MULTIPOLYGONs... Dec 8, 2020 at 17:46
0

The ST_ExteriorRing returns a linestring. Use ST_MakePolygon to return a polygon that represents the original polygon with the donut holes removed.

SELECT gid, ST_MakePolygon(ST_ExteriorRing(the_geom)) AS ering
FROM sometable;

https://postgis.net/docs/ST_MakePolygon.html

Also be careful that sometable does not contain Multipolygons. https://postgis.net/docs/ST_ExteriorRing.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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