Reputation: 459
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
I want to pass coordinates as input and return if coordinates are inside the given coordinates or not.
Upvotes: 0
Views: 1071
Reputation: 304
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()
.
Upvotes: 1