MEASUREMENT
COORDINATE MUTATION
TRANSFORMATION
FEATURE_CONVERSION
MISC
HELPER
RANDOM
GRIDS
AGGREGATION
META
ASSERTIONS
BOOLEANS
UNIT CONVERSION
DATA
JOINS
CLASSIFICATION

pointsWithinPolygon

该函数用于以地理空间的角度将点和多边形相结合。它接收两个参数:1)一个点的属性集和经纬度,2)一个多边形的属性集和经纬度。多边形可以是一个单独的多边形,也可以是一个多面体。 此函数仅接受两个参数,如果传递了第三个参数,则它将被忽略。

参数

  • points (FeatureCollection.<Point>) - 一个包含点的属性集和经纬度的要素集合。
  • polygons (FeatureCollection.<Polygon>|FeatureCollection.<MultiPolygon>) - 一个包含多边形属性集和经纬度的要素集合。

返回

  • FeatureCollection.<Point> - 一个包含所有点在多边形内的要素集合。

示例


var fs = require('fs');
var turf = require('@turf/turf');
var points = fs.readFileSync(path.join(__dirname, 'points.geojson'), 'utf8');
var polygons = fs.readFileSync(path.join(__dirname, 'polygons.geojson'), 'utf8');
points = JSON.parse(points);
polygons = JSON.parse(polygons);
var pointsWithinPolygon = turf.pointsWithinPolygon(points, polygons);
console.log(pointsWithinPolygon);

示例输入

  • points

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "id": 1 
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -28.833038,
          -71.255107
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "id": 2
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -29.348072,
          -70.827637
        ]
      }
    }
  ]
}

  • polygons

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "Colorado",
        "id": 1
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -109.028673,
              37.000263
            ],
            [
              -109.049118,
              36.999861
            ],
            [
              -109.053098,
              36.999154
            ],
            ...
          ]
        ]
      }
    }
  ]
}

示例输出


{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "id": 1
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -29.348072,
          -70.827637
        ]
      }
    }
  ]
}