python 点到围栏距离_Python一行代码处理地理围栏

最近在工作中遇到了这个一个需求,用户设定地理围栏,后台获取到实时位置信息后通过与围栏比较,判断是否越界等。

这个过程需要用到数据协议为GEOjson,通过查阅资料后,发现python的shapely库可以非常简单的解决这个问题,接下来演示一下我处理这个问题的过程。

测试数据:

得到GEOjson数据如下:

{

"type": "FeatureCollection",

"features": [

{

"type": "Feature",

"properties": {},

"geometry": {

"type": "Polygon",

"coordinates": [

[

[

114.3458104133606,

30.476167529462785

],

[

114.34512376785278,

30.475575748963195

],

[

114.34576749801636,

30.474540124433936

],

[

114.3467652797699,

30.475363076967565

],

[

114.34693694114685,

30.476102803645833

],

[

114.3458104133606,

30.476167529462785

]

]

]

}

},

{

"type": "Feature",

"properties": {},

"geometry": {

"type": "Point",

"coordinates": [

114.34605717658997,

30.475584995561178

]

}

},

{

"type": "Feature",

"properties": {},

"geometry": {

"type": "Point",

"coordinates": [

114.346604347229,

30.476518897432545

]

}

}

]

}

安装shapely

本测试基于python——python3.6

$ pip install shapely

windows安装shapely会报错

shapely解析地理围栏

话不多说直接上代码

from shapely.geometry import Point

from shapely.geometry.polygon import Polygon

point = Point(0.5, 0.5)

polygon = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])

print(polygon.contains(point))

下面是实际的实例:

from shapely.geometry import Point

from shapely.geometry.polygon import Polygon

polygon_data= [

[

114.3458104133606,

30.476167529462785

],

[

114.34512376785278,

30.475575748963195

],

[

114.34576749801636,

30.474540124433936

],

[

114.3467652797699,

30.475363076967565

],

[

114.34693694114685,

30.476102803645833

],

[

114.3458104133606,

30.476167529462785

]

]

point1 = Point([114.34605717658997,30.475584995561178])

point2 = Point([114.346604347229,30.476518897432545])

polygon = Polygon(polygon_data)

print(polygon.contains(point1))

print(polygon.contains(point2))

输出结果:

True

False

这样一来我们就快速的实现了,目标点是否在地理围栏内的判断。

总结

Python还是挺好用的:)

参考资料:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值