判断经纬度数据是否在某个平面内

文章提供了一个静态方法isPointInPolygon,用于判断给定点是否位于一组平面经纬度数据定义的多边形内。通过对点与多边形边界进行相交测试,确定点的位置关系,从而筛选出在平面内的点。该方法可用于地理信息系统(GIS)或地图相关的数据处理。
摘要由CSDN通过智能技术生成

文章目录


需求:判断经纬度数据是否在某个平面内,将在面上的数据筛选出来。

例如:给出一组平面的经纬度数据,再给出一组点的经纬度数据,筛选出在面范围内的点的数据。

判断方法(直接复制可用)

  // 判断点位是否在区域内
  static isPointInPolygon(point, pts) {
  // point数据格式 ---{longitude: 114.28988366986654, latitude: 23.042948244464927} 
  // pts数据格式 ---[{latitude:xxx,longitude:xxx}
    var N = pts.length //pts [{latitude:xxx,longitude:xxx},{latitude:xxx,longitude:xxx}]
    var boundOrVertex = true //如果点位于多边形的顶点或边上,也算做点在多边形内,直接返回true
    var intersectCount = 0 // x的交叉点计数
    var precision = 2e-10 // 浮点类型计算时候与0比较时候的容差
    var p1, p2 // 相邻边界顶点
    var p = point // point {latitude:xxx,longitude:xxx}

    p1 = pts[0]
    for (var i = 1; i <= N; ++i) {
      if (p.latitude == p1.latitude && p.longitude == p1.longitude) {
        return boundOrVertex
      }
      p2 = pts[i % N] //右顶点
      if (
        p.latitude < Math.min(p1.latitude, p2.latitude) ||
        p.latitude > Math.max(p1.latitude, p2.latitude)
      ) {
        p1 = p2
        continue //下一条射线左点
      }
      if (
        p.latitude > Math.min(p1.latitude, p2.latitude) &&
        p.latitude < Math.max(p1.latitude, p2.latitude)
      ) {
        if (p.longitude <= Math.max(p1.longitude, p2.longitude)) {
          if (
            p1.latitude == p2.latitude &&
            p.longitude >= Math.min(p1.longitude, p2.longitude)
          ) {
            return boundOrVertex
          }
          if (p1.longitude == p2.longitude) {
            if (p1.longitude == p.longitude) {
              return boundOrVertex
            } else {
              ++intersectCount
            }
          } else {
            var xinters =
              ((p.latitude - p1.latitude) * (p2.longitude - p1.longitude)) /
                (p2.latitude - p1.latitude) +
              p1.longitude // 经度交点
            if (Math.abs(p.longitude - xinters) < precision) {
              return boundOrVertex
            }
            if (p.longitude < xinters) {
              ++intersectCount
            }
          }
        }
      } else {
        if (p.latitude == p2.latitude && p.longitude <= p2.longitude) {
          var p3 = pts[(i + 1) % N] //next vertex
          if (
            p.latitude >= Math.min(p1.latitude, p3.latitude) &&
            p.latitude <= Math.max(p1.latitude, p3.latitude)
          ) {
            ++intersectCount
          } else {
            intersectCount += 2
          }
        }
      }
      p1 = p2
    }
    if (intersectCount % 2 == 0) {
      //偶数在多边形外
      return false
    } else {
      //奇数在多边形内
      return true
    }
  }

使用:

 let includeData = [] // 存放筛选过后的点位数据
  pointData.forEach(item => {//pointData 点的数据
     let isInclude = GisDataConverter.isPointInPolygon(
        {
          longitude: item.lon,
          latitude: item.lat
        },fanwei)//fanwei 面的数据
      if (isInclude) {
        includeData.push(item)
      }
    })

今天开心,大吃一斤^ o ^

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Python 的 Shapely 库来实现判断某个经纬度是否在某个多边形范围内的功能。具体步骤如下: 1. 安装 Shapely 库:使用 pip 命令安装 Shapely 库,命令如下: ``` pip install shapely ``` 2. 导入 Shapely 库:在 Python 代码中导入 Shapely 库,命令如下: ``` from shapely.geometry import Point, Polygon ``` 3. 创建多边形对象:使用 Shapely 库的 Polygon 类创建多边形对象,多边形对象需要传入多个的坐标,格式为 (x, y)。 ``` polygon = Polygon([(x1, y1), (x2, y2), (x3, y3), ...]) ``` 4. 创建对象:使用 Shapely 库的 Point 类创建对象,对象需要传入的坐标,格式为 (x, y)。 ``` point = Point(x, y) ``` 5. 判断是否在多边形内:使用 Shapely 库的 contains 方法判断是否在多边形内,在多边形内返回 True,不在多边形内返回 False。 ``` if polygon.contains(point): print("在多边形内") else: print("不在多边形内") ``` 完整的示例代码如下: ``` from shapely.geometry import Point, Polygon # 创建多边形对象 polygon = Polygon([(116.397, 39.908), (116.410, 39.908), (116.410, 39.920), (116.397, 39.920)]) # 创建对象 point = Point(116.403, 39.914) # 判断是否在多边形内 if polygon.contains(point): print("在多边形内") else: print("不在多边形内") ``` 注意:在使用 Shapely 库时,经度和纬度的顺序需要注意,通常情况下经度在前,纬度在后,例如 (116.397, 39.908)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值