How can I select a line having the centroid inside a polygon? there is a specific fuction?
Asked
Active
Viewed 340 times
3
-
1Can be also useful: Getting the line middle point with SQL in QGIS – Taras Jun 24 '21 at 10:53
1 Answers
4
You want a standard ST_Intersects filter, and then you have three options:
- use the
ST_Centroidto get the (weighted) centroid of the lines:SELECT ln.* FROM <lines> AS ln JOIN <polygons> AS pl ON ST_Intersects(pl.geom, ST_Centroid(ln.geom)) ; - use the
ST_PointOnSurfaceto get a (non-weighted) center point of the lines
SELECT ln.*
FROM <lines> AS ln
JOIN <polygons> AS pl
ON ST_Intersects(pl.geom, ST_PointOnSurface(ln.geom))
;
- use the interpolated midpoint (
ST_LineInterpolatePoint) of the linesSELECT ln.* FROM <lines> AS ln JOIN <polygons> AS pl ON ST_Intersects(pl.geom, ST_LineInterpolatePoint(ln.geom, 0.5)) ;
Depending on your lines, all three may differ:
ST_Centroidresults in a point not guaranteed to be on the line (check the example in the docs) and is likely not what you wantST_PointOnSurfaceis extended in PostGIS to work on arbitrary geometries and guarantees[*] a point on the line around the geometric center using a deterministic algorithmST_LineInterpolatePointgenerates a point on the line at the given fraction of line length (0.5for the midpoint) and guarantees[*] a point on the line
[*] within the margin of floating point rounding errors
geozelot
- 30,050
- 4
- 32
- 56