3

I'm trying to work on the following scenario:

  • Getting polygon coordinates from google earth
  • Getting boundaries with Shapely:

    >>> polygon = Polygon([(53.349459,-6.260159),(53.349366,-6.260126),(53.349383,-6.260012),(53.349478,-6.260053),(53.349459,-6.260159)])   
    >>> polygon.bounds
    (53.349366, -6.260159, 53.349478, -6.260012)
    

    I am getting 2 coordinates, which are 2 border points on the top of my figure.

  • Getting distance with geopy

    And now I am stuck... trying to figure out:

    1. How to find 2 other border points (in the bottom)
    2. How to detect whether a user is near (e.g. 3 meters) the polygon from any side? (left, right, up, down). in this case, I need to know not only the edges border points, but also all the border points from left, right, up and down? I can calculate the distance between the user location and the polygon, but what point to from polygon to take dynamically? Can I use existing libs for this, like geopy and Shapely?

    Image

Community
  • 1
  • 1
user1790755
  • 31
  • 1
  • 3
  • this is maybe more an question for http://gis.stackexchange.com – RickyA Nov 21 '12 at 12:08
  • Bounds returns the bounding box of your polygon. The smallest rectangle in which the polygon fits. If you look at it it is not the coordinates of the top points. – RickyA Nov 21 '12 at 12:12

1 Answers1

1

So if I understand correctly you have a bunch of Shapely polygons and you want to test if arbitrary points are close to these shapes. For this problem shapely provides distance:

from shapely.geometry import Point, Polygon
polygon = Polygon([(53.349459,-6.260159),
                   (53.349366,-6.260126),
                   (53.349383,-6.260012),
                   (53.349478,-6.260053),
                   (53.349459,-6.260159)])
testpoint = Point(53.349459,-6.260190)
dist = polygon.distance(testpoint)
print(dist)
>>> 3.09999999999e-05

Mind you: distance is in arcs and not in meters so you have to convert them.

RickyA
  • 15,465
  • 5
  • 71
  • 95
  • the distance is in arcs, if I understand correctly, only if you are using coordinates. In this case, I'm not even sure that's accurate. A better approach to measure distance of long/lat is to project the polygon first (pyproj?) and then measure it. We are using a simpler projection method that relies on the average latitude of the polygon – Guy Jun 18 '18 at 15:37