0

I have a set of coordinates and want to create a polygon from it and after that, I'll take input lat & long and check if the given coordinates are inside or outside of the polygon.

I tried to plot polygon using Shapely and somewhat is is correct. My coordinates:

coords = [(48.9276684941938, 9.148899187374205),
 (48.927881447105676, 9.148709214807226),
 (48.928097614085175, 9.148527808307),
 (48.92831656589233, 9.148354454524537),
 (48.92853801299333, 9.14818847255271),
 (48.92876304120666, 9.148030296534815),
 (48.92899058574909, 9.147880813070733),
 (48.929220004856425, 9.147737925539758),
 (48.92945050690533, 9.147599646704602),
 (48.92970963088016, 9.147448804345393),
 (48.929969642221785, 9.147302124446165),
 (48.930230552830835, 9.147158909183357),
 (48.93049212225262, 9.147018600375391),
 (48.931016778198384, 9.146744181554437),
 (48.93154237676336, 9.146473988125043),
 (48.9276684941938, 9.148899187374205),
 (48.927881447105676, 9.148709214807226),
 (48.928097614085175, 9.148527808307),
 (48.92831656589233, 9.148354454524537),
 (48.92853801299333, 9.14818847255271),
 (48.92876304120666, 9.148030296534815),
 (48.92899058574909, 9.147880813070733),
 (48.929220004856425, 9.147737925539758),
 (48.92945050690533, 9.147599646704602),
 (48.92970963088016, 9.147448804345393),
 (48.929969642221785, 9.147302124446165),
 (48.930230552830835, 9.147158909183357),
 (48.93049212225262, 9.147018600375391),
 (48.931016778198384, 9.146744181554437),
 (48.93154237676336, 9.146473988125043),
 (48.9276684941938, 9.148899187374205),
 (48.927881447105676, 9.148709214807226),
 (48.928097614085175, 9.148527808307),
 (48.92831656589233, 9.148354454524537),
 (48.92853801299333, 9.14818847255271),
 (48.92876304120666, 9.148030296534815),
 (48.92899058574909, 9.147880813070733),
 (48.929220004856425, 9.147737925539758),
 (48.92945050690533, 9.147599646704602),
 (48.92970963088016, 9.147448804345393),
 (48.929969642221785, 9.147302124446165),
 (48.930230552830835, 9.147158909183357),
 (48.93049212225262, 9.147018600375391),
 (48.931016778198384, 9.146744181554437),
 (48.93154237676336, 9.146473988125043),
 (48.9276684941938, 9.148899187374205),
 (48.927881447105676, 9.148709214807226),
 (48.928097614085175, 9.148527808307),
 (48.92831656589233, 9.148354454524537),
 (48.92853801299333, 9.14818847255271),
 (48.92876304120666, 9.148030296534815),
 (48.92899058574909, 9.147880813070733),
 (48.929220004856425, 9.147737925539758),
 (48.92945050690533, 9.147599646704602),
 (48.92970963088016, 9.147448804345393),
 (48.929969642221785, 9.147302124446165),
 (48.930230552830835, 9.147158909183357),
 (48.93049212225262, 9.147018600375391),
 (48.931016778198384, 9.146744181554437),
 (48.93154237676336, 9.146473988125043),
 (48.92770214317435, 9.14898338362773),
 (48.92791356880573, 9.148794765833149),
 (48.92812829266924, 9.148614547378457),
 (48.92834590048043, 9.14844222176487),
 (48.928566113985696, 9.148277117808037),
 (48.92878946335235, 9.148120067839535),
 (48.929015676775194, 9.147971398151217),
 (48.92924412032041, 9.147829052791057),
 (48.92947400781438, 9.147691074306037),
 (48.929732441980185, 9.147540555874041),
 (48.92999185500086, 9.147394137713535),
 (48.93025226040877, 9.147251128456126),
 (48.93051341950752, 9.147110975575266),
 (48.931037543721, 9.146836738790197),
 (48.931563000656055, 9.146566582017199),
 (48.92770214317435, 9.14898338362773),
 (48.92791356880573, 9.148794765833149),
 (48.92812829266924, 9.148614547378457),
 (48.92834590048043, 9.14844222176487),
 (48.928566113985696, 9.148277117808037),
 (48.92878946335235, 9.148120067839535),
 (48.929015676775194, 9.147971398151217),
 (48.92924412032041, 9.147829052791057),
 (48.92947400781438, 9.147691074306037),
 (48.929732441980185, 9.147540555874041),
 (48.92999185500086, 9.147394137713535),
 (48.93025226040877, 9.147251128456126),
 (48.93051341950752, 9.147110975575266),
 (48.931037543721, 9.146836738790197),
 (48.931563000656055, 9.146566582017199),
 (48.92770214317435, 9.14898338362773),
 (48.92791356880573, 9.148794765833149),
 (48.92812829266924, 9.148614547378457),
 (48.92834590048043, 9.14844222176487),
 (48.928566113985696, 9.148277117808037),
 (48.92878946335235, 9.148120067839535),
 (48.929015676775194, 9.147971398151217),
 (48.92924412032041, 9.147829052791057),
 (48.92947400781438, 9.147691074306037),
 (48.929732441980185, 9.147540555874041),
 (48.92999185500086, 9.147394137713535),
 (48.93025226040877, 9.147251128456126),
 (48.93051341950752, 9.147110975575266),
 (48.931037543721, 9.146836738790197),
 (48.931563000656055, 9.146566582017199),
 (48.92770214317435, 9.14898338362773),
 (48.92791356880573, 9.148794765833149),
 (48.92812829266924, 9.148614547378457),
 (48.92834590048043, 9.14844222176487),
 (48.928566113985696, 9.148277117808037),
 (48.92878946335235, 9.148120067839535),
 (48.929015676775194, 9.147971398151217),
 (48.92924412032041, 9.147829052791057),
 (48.92947400781438, 9.147691074306037),
 (48.929732441980185, 9.147540555874041),
 (48.92999185500086, 9.147394137713535),
 (48.93025226040877, 9.147251128456126),
 (48.93051341950752, 9.147110975575266),
 (48.931037543721, 9.146836738790197),
 (48.931563000656055, 9.146566582017199)]

Code:

from shapely.geometry import Point, Polygon

poly = Polygon(coords)
poly

Output: enter image description here

I want to pass coordinates as input and return if coordinates are inside the given coordinates or not.

Expected ouput: enter image description here

Georgy
  • 12,464
  • 7
  • 65
  • 73
L Lawliet
  • 419
  • 1
  • 7
  • 20
  • One way is to compute the convex hull of all points with and without the point in question. If the point is within the polygon, the convex hull will be the same. – couka Jan 12 '21 at 17:27
  • Does this answer your question? [What's the fastest way of checking if a point is inside a polygon in python](https://stackoverflow.com/questions/36399381/whats-the-fastest-way-of-checking-if-a-point-is-inside-a-polygon-in-python) – Georgy Jan 12 '21 at 19:30

1 Answers1

1

You didn't specify whether your polygon coordinates are ordered or not. It appears, from your example, that they are not. If not, you might wish to create a polygon using object.convex_hull rather than the polygon constructor:

coords = [
    (48.9276684941938, 9.148899187374205),
    ...
]

poly = MultiPoint(coords).convex_hull

Beyond that, it seems like you're only missing the use of object.intersects() to check if a given point is within* your polygon:

x = 48.929234
y = 9.147870

Point(x, y).intersects(poly)

Returns True

*For the common understanding of "within". In technical terms, "within" would exclude points on the boundary of your polygon, whereas "intersects" includes them. If you don't want points on the boundary (such as the points in your original coords) to return True, substitute object.within() for object.intersects().

dericke
  • 290
  • 4
  • 13