识别点是否在 GEOJSON 多边形内? JavaScript

问题描述 投票:0回答:0

afer 我将 KMZ 转换为 KML,然后转换为 JSON 文件,我得到了以下结构 { “类型”:“特征集合”, “特征”: [ { “类型”:“特征”, “几何学”: { “类型”:“多边形”, “坐标”:[ [ [lon1, lat1], [lon2, lat2], [lon3, lat3], ... ] ] }, “特性”: { “财产1”:“价值1”, “财产2”:“价值2”, ... } },

在我的脑海中找到 GPS 点我应该遍历坐标点并检查该点是否在 GEOJSON 多边形内所以我将此代码与 turf.js 库一起使用

                const myJSON = require('path/geojson.json');

                // define the point to check
                const pointToCheck = turf.point([-6.542, 34.224]);

                // loop through each feature in the GeoJSON data
                myJSON.features.forEach(feature => {
                  // get the polygon geometry for this feature
                  const polygon = feature.geometry;
                  
                  // check if the point is inside the polygon
                  const isInside = turf.booleanPointInPolygon(pointToCheck, polygon);
                  
                  // if the point is inside the polygon, log the feature's properties
                  if (isInside) {
                    console.log(feature.properties);
                  }
                });

有了这个解决方案,我首先遇到了两个问题,错误

  • 捕获 ReferenceError: require is not defined

这个错误是因为我的文件路径或其他原因而被触发的?

其次,如果一切顺利,逻辑和代码会起作用吗?

如果 GPS 点在多边形内,则期望从 JSON 中获取属性

json google-maps-api-3 polygon turfjs
© www.soinside.com 2019 - 2024. All rights reserved.