如何创建一个多边形并检查给定的纬度、经度是否在里面
How to create a polygon and check if given lat, lon are inside
我有一组坐标,想从中创建一个多边形,然后,我将输入经纬度并检查给定坐标是在多边形内部还是外部。
我尝试使用 Shapely 绘制多边形,有些是正确的。我的坐标:
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)]
代码:
from shapely.geometry import Point, Polygon
poly = Polygon(coords)
poly
输出:
我想将坐标作为输入传递,并且 return 坐标是否在给定坐标内。
预期输出:
您没有指定您的多边形坐标是否有序。从您的示例来看,它们似乎不是。如果没有,您可能希望使用 object.convex_hull
而不是多边形构造函数来创建多边形:
coords = [
(48.9276684941938, 9.148899187374205),
...
]
poly = MultiPoint(coords).convex_hull
除此之外,您似乎只是缺少使用 object.intersects()
来检查给定点是否在*您的多边形内:
x = 48.929234
y = 9.147870
Point(x, y).intersects(poly)
Returns True
*为了“内”的共同理解。在技术术语中,“内”将排除多边形边界上的点,而“相交”包括它们。如果你不希望边界上的点(比如你原来的 coords
中的点)到 return True
,用 object.within()
代替 object.intersects()
.
我有一组坐标,想从中创建一个多边形,然后,我将输入经纬度并检查给定坐标是在多边形内部还是外部。
我尝试使用 Shapely 绘制多边形,有些是正确的。我的坐标:
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)]
代码:
from shapely.geometry import Point, Polygon
poly = Polygon(coords)
poly
输出:
我想将坐标作为输入传递,并且 return 坐标是否在给定坐标内。
预期输出:
您没有指定您的多边形坐标是否有序。从您的示例来看,它们似乎不是。如果没有,您可能希望使用 object.convex_hull
而不是多边形构造函数来创建多边形:
coords = [
(48.9276684941938, 9.148899187374205),
...
]
poly = MultiPoint(coords).convex_hull
除此之外,您似乎只是缺少使用 object.intersects()
来检查给定点是否在*您的多边形内:
x = 48.929234
y = 9.147870
Point(x, y).intersects(poly)
Returns True
*为了“内”的共同理解。在技术术语中,“内”将排除多边形边界上的点,而“相交”包括它们。如果你不希望边界上的点(比如你原来的 coords
中的点)到 return True
,用 object.within()
代替 object.intersects()
.