0

I have a point on line with two polygons on both sides. The scenario is shown in the following:

Point on line scenario

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_2

Scenario_3:

Scenario_3

EDIT2

Scenario_4

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.

khajlk
  • 791
  • 1
  • 12
  • 32
  • Using your point, you can find the [distance from the point to the polygons](https://gis.stackexchange.com/questions/86079/distance-between-polygon-and-point) – JGH May 18 '17 at 01:07
  • Thank you for your suggestion. I want to calculate the street width actually (the perpendicular distance between two building polygons). Your suggestion may work in this sample scenario, that is, I may use ST_Distance() and may add both distances to get the street width but in large dataset, it will be problematic. Isn't it? – khajlk May 18 '17 at 07:24
  • Do you have a point in front of each polygon? Do all polygons have a side parallel to the street? If the answer is yes to both question then the suggestion will work. Note that as for all jobs related to streets, be ready to handle some exceptions – JGH May 18 '17 at 12:17
  • @JGH: Please see the edit. I understand there can be exceptions. – khajlk May 18 '17 at 14:29

1 Answers1

1

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;
Community
  • 1
  • 1
JGH
  • 15,928
  • 4
  • 31
  • 48
  • Well, thanks a lot. Your code worked! Please have a look at scenario 4. The original street width is 11.3 marked by red line but the code gives me 41.0 meters which also makes sense according to your prior warning (handling exceptions). I would be happy to accept your proposed code as a solution but what would you suggest to address above exception in scenario 4? – khajlk May 18 '17 at 18:39
  • 1
    For exceptions, it is more import to detect them than to fix them by code. For scenario 4, you could compare the distance between the two polygons and flag entries where the two distance differ by more than X% (in your screenshot is seems a 1:6 ratio). Otherwise draw a buffer using the found distance around each point and visually inspect the larger polygons. – JGH May 18 '17 at 18:55
  • +1 for your first suggestion. I could use the CASE statement to add exceptions in such cases. Regarding second suggestion, it would be fine to visually examine the results but for large number of polygons, it could be time consuming thing. – khajlk May 18 '17 at 19:17