I have a point on line with two polygons on both sides. The scenario is shown in the following:
Now, I would like to compute the perpendicular distance between two polygons (for example, yellow line) using a PostGIS query. I was wondering if someone could suggest me how to do that?
EDIT1:
Above given scenario was an example. There can be complications in scenarios having streets, points and polygons. For example, the side parallel to street may not always be there.
Scenario_2:
Scenario_3:
EDIT2
Scenario_4
I want to calculate the perpendicular distance only where there is a point on line. I understand there can be exceptions in this, as point by @JGH.
Assuming your data is projected and that the distance between the points and the two nearest polygon is the one you are looking for, you can compute the distance from each point to the two polygons and make the sum.
1) compute the distance. Restrict the search to a reasonable distance, maybe twice the expected largest distance. Make sure the geometries are indexed!!
SELECT pt.gid point_gid, plg.gid polygon_gid, ST_Distance(pt.geom, plg.geom) distance
FROM pointlayer pt, polygonlayer plg
WHERE ST_Distance(pt.geom, plg.geom) < 50
ORDER BY pt.gid, ST_Distance(pt.geom, plg.geom);
2) For each point, get the two closest polygons using the partition function
SELECT
point_gid, polygon_gid, distance
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY point_gid ORDER BY distance asc) AS rank,
t.*
FROM
[distanceTable] t) top_n
WHERE
top_n.rank <= 2;
3) agregate the result and keep track of which polygons were used
select point_gid,
sum(distance) streetwidth,
string_agg(polygon_gid || ' - ' || distance,';') polyid_dist_info
from [top_2_dist dst]
group by dst.point_gid;
All together:
SELECT
point_gid,
sum(distance) streetwidth,
string_agg(polygon_gid || ' - ' || distance,';') polyid_dist_info
FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY point_gid ORDER BY distance asc) AS rank,
t.*
FROM
( SELECT pt.gid point_gid,
plg.gid polygon_gid,
ST_Distance(pt.geom, plg.geom) distance
FROM pointlayer pt, polygonlayer plg
WHERE ST_Distance(pt.geom, plg.geom) < 50
) t
) top_n
WHERE
top_n.rank <= 2
GROUP BY point_gid;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.