qwerttt 发表于 2024-3-20 16:01:02

怎样判断GPS坐标在电子围栏内

怎样判断GPS坐标在电子围栏内? 有没什么算法参考下

假设电子围栏是多个GPS坐标构成的凸多边形

leafstamen 发表于 2024-3-20 16:12:45

To determine whether a GPS coordinate is within an electronic fence, which is a convex polygon formed by multiple GPS coordinates, we can use the Ray Casting algorithm. The main idea is to draw a line segment from the coordinate to a point outside the polygon, and count how many edges it intersects. If the number of intersections is even, the coordinate is outside the polygon, otherwise it's inside.

This algorithm can accurately judge whether the coordinate is within the convex polygon. However, since the Earth is not a flat plane, if the GPS data in the vertices of the polygon crosses the change line (the boundary between 360 degrees and -360 degrees), it may cause problems, and may need to add or subtract in some corner data.

So, the implementation process is to traverse each edge of the polygon in turn, and determine whether the connection point of the coordinate and the edge is on the left or right of the edge vector. If it's on the right, it's considered to be intersected, otherwise it's not. When the last edge is judged, if the result is intersect, it means that the coordinate is outside the polygon, otherwise it's inside.

In conclusion, through the above algorithm, we can determine whether a GPS coordinate is within a given convex polygon (electronic fence).

leafstamen 发表于 2024-3-20 16:18:37

竟然有这么神奇的算法,从这个点随便往一个方向画一条线,数这条线和围栏的变交点的个数,奇数个就是在围栏里面,偶数个就是在围栏外面。

amazing030 发表于 2024-3-20 16:20:45

leafstamen 发表于 2024-3-20 16:18
竟然有这么神奇的算法,从这个点随便往一个方向画一条线,数这条线和围栏的变交点的个数,奇数个就是在围栏 ...
(引用自3楼)

很不错的办法,那问题又来了,怎么判断相交点?

amwjz 发表于 2024-3-20 16:35:21

ChatGPT

确定 GPS 坐标是否位于由多个 GPS 坐标形成的凸多边形电子围栏内,我们可以使用射线投射算法。其主要思想是从坐标向多边形外的点画一条线段,并计算它与多边形的边的交点数。如果交点数为偶数,则坐标在多边形外部,否则在内部。

该算法能够准确判断坐标是否在凸多边形内。然而,由于地球不是一个平面,如果多边形顶点的 GPS 数据穿过变化线(360 度和 -360 度之间的边界),可能会导致问题,可能需要在某些角落数据中添加或减去。

因此,实现过程是依次遍历多边形的每条边,并确定坐标与边的连接点是位于边向量的左侧还是右侧。如果在右侧,则被认为是相交的,否则不是。当判断最后一条边时,如果结果为相交,则表示坐标在多边形外部,否则在内部。

综上所述,通过以上算法,我们可以确定一个 GPS 坐标是否位于给定的凸多边形(电子围栏)内。




leafstamen 发表于 2024-3-20 16:37:08

amazing030 发表于 2024-3-20 16:20
很不错的办法,那问题又来了,怎么判断相交点?
(引用自4楼)

要计算的点(x,y),假如竖着向下画线,边的两个点的x坐标一个比x大一个比x小就会相交,由于是往下画线,所以还需要再比较一下边的Y是不是有在点y下方的部分。估计搜索一下就能找到关于电子围栏的高效的并且成熟的计算方法。

Doding 发表于 2024-3-20 16:41:18

判断一点是否在给定的多边形内,搜索point in polygon有很多资料,多数应该是用射线法。

zouzhichao 发表于 2024-3-20 16:47:39

我可以搞算法,你肯付费不?

智涅 发表于 2024-3-20 16:48:09

如果用高级语言,应该直接找方法调用,不用造轮子。
你的问题就是转化成“判断一个点是否在多边形(面)内”
如果用python,就是一行代码{:titter:}

iamseer 发表于 2024-3-21 00:07:14

https://www.amobbs.com/thread-5736339-1-1.html

王二小 发表于 2024-3-27 14:47:46

请教一下楼主搞GPS围栏都有哪些难点?我还以为只是把模块给的经纬度丢进函数里面计算两点直接距离那么简单。

qwerttt 发表于 2024-3-27 20:21:07

王二小 发表于 2024-3-27 14:47
请教一下楼主搞GPS围栏都有哪些难点?我还以为只是把模块给的经纬度丢进函数里面计算两点直接距离那么简单 ...
(引用自11楼)

还不知道会遇到什么难点,项目才开始,监测采砂船是否在开采区域内

目前也只是把gps坐标丢进函数判断,是否在多边形内

王二小 发表于 2024-3-27 21:04:32

qwerttt 发表于 2024-3-27 20:21
还不知道会遇到什么难点,项目才开始,监测采砂船是否在开采区域内

目前也只是把gps坐标丢进函数判断, ...
(引用自12楼)

你用的哪个模块?我也是刚开始。连模块选型都不懂。我用的ATGM336H-5NR-32。不知道这个模块行不行。

qwerttt 发表于 2024-3-28 09:35:46

王二小 发表于 2024-3-27 21:04
你用的哪个模块?我也是刚开始。连模块选型都不懂。我用的ATGM336H-5NR-32。不知道这个模块行不行。 ...
(引用自13楼)

我用的是维特智能公司的WTGPS+BD

王二小 发表于 2024-4-10 02:19:47

qwerttt 发表于 2024-3-28 09:35
我用的是维特智能公司的WTGPS+BD
(引用自14楼)


好贵啊。你用的说陶瓷短天线还是外接长天线的?模拟器你们买了吗?

qwerttt 发表于 2024-4-10 12:38:28

王二小 发表于 2024-4-10 02:19
好贵啊。你用的说陶瓷短天线还是外接长天线的?模拟器你们买了吗?
(引用自15楼)

外接长天线,模拟器没买

王二小 发表于 2024-4-10 20:55:19

qwerttt 发表于 2024-4-10 12:38
外接长天线,模拟器没买
(引用自16楼)

那你怎么调试?去户外实测?

zouzhichao 发表于 2024-4-10 21:54:10

王二小 发表于 2024-4-10 20:55
那你怎么调试?去户外实测?
(引用自17楼)

直接伪造nmea帧发串口给单片机不就好了?
页: [1]
查看完整版本: 怎样判断GPS坐标在电子围栏内