0

I have a list of coordinates like so:

  [
     -79.763635,
     42.257327
  ],
  [
     -73.343723,
     45.046138
  ],
  [
     -74.006183,
     40.704002
  ],
  [
     -79.763635,
     42.257327
  ]

I want to see if a point SomeLat, SomeLong is inside these predefined borders.

I trie used shapely.geometry but the json has to be configured a certain way for it to create a shape(I cannot change that json).

What would be the best way to tell if a point I provide is inside those defined borders

dsuma
  • 1,000
  • 2
  • 9
  • 30
  • two points are equal. is it a triangle ? – B. M. Jan 31 '17 at 17:20
  • They are equal to complete the border, so yes It can be a triangle, It can have more points so can be any polygon. – dsuma Jan 31 '17 at 17:29
  • 1
    http://streamhacker.com/2010/03/23/python-point-in-polygon-shapely/ - does this help? – pragman Jan 31 '17 at 17:45
  • 1
    Just change your list of coordinates and it will work. Do this: `list_of_points = [tuple(i) for i in list_of_points]`. Then you can make the `Polygon` and check if your point is within. I just tested it on my local and it works fine given the format you presented. – gold_cy Jan 31 '17 at 17:47
  • 1
    Note that ordinary point-in-polygon algorithms assume that your points are on a flat surface, and the Earth is not a flat surface. You may get weird results around the poles and the international date line unless you account for this. Related reading: [How do I check if a longitude/latitude point is within a range of coordinates?](http://stackoverflow.com/q/11510326/953482) – Kevin Jan 31 '17 at 18:09

2 Answers2

3

You can pass a list of lists/tuples to shapely:

>>> from shapely.geometry import Point, Polygon
>>> bbox = [ (0, 0), (0,2), (2,2), (2,0)]
>>> poly = Polygon(bbox)
>>> point = Point(1, 1)
>>> poly.contains(point)
True
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
0

I am not sure if this the best way, but this is how I would go about it. I would identify all the points making up the borders of the polygon, which is obtainable from the data you have. Then I would pull together every two border extreme latitude coordinates that share the same longitude coordinate. The final set should be a list of longitude coordinates and their corresponding latitude ranges, which you could loop through to decide if a point is in the polygon.