点"内部"许多多边形

时间:2016-10-20 07:32:42

标签: javascript leaflet turfjs

我正在尝试使用turf-inside()turf.jsleafletjs功能。我用一个geojson点和一个多边形来预测基本示例,但我不能对两个以上的多边形(geojson)做同样的事情。有什么想法吗?

<!DOCTYPE html>
<html>
<head>

    <title>Mobile tutorial - Leaflet</title>

    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />

    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.0.1/dist/leaflet.css" />
    <script src="https://unpkg.com/leaflet@1.0.1/dist/leaflet.js"></script>
    <script src='https://api.mapbox.com/mapbox.js/plugins/turf/v2.0.2/turf.min.js'></script>

    <style>
        #map {
            width: 600px;
            height: 400px;
        }
    </style>

    <style>body { padding: 0; margin: 0; } html, body, #map { height: 100vh; width: 100vw; }</style>
</head>
<body>

<div id='map'></div>

<script>

var pt1 = {
    "type" : "Feature",
    "properties" : {
        "marker-color" : "#f00"
    },
    "geometry" : {
        "type" : "Point",
        "coordinates" : [15.853271484375, 52.649729197309426]  //   19.16015625,52.119998657638156

    }
};

        var poly = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              13.842773437499998,
              50.42951794712287
            ],
            [
              13.842773437499998,
              54.213861000644926
            ],
            [
              16.89697265625,
              54.213861000644926
            ],
            [
              16.89697265625,
              50.42951794712287
            ],
            [
              13.842773437499998,
              50.42951794712287
            ]
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              19.094238281249996,
              52.669720383688166
            ],
            [
              18.544921875,
              52.16045455774706
            ],
            [
              18.43505859375,
              51.08282186160978
            ],
            [
              20.06103515625,
              50.65294336725709
            ],
            [
              20.80810546875,
              51.467696956223364
            ],
            [
              20.830078125,
              52.29504228453735
            ],
            [
              19.094238281249996,
              52.669720383688166
            ]
          ]
        ]
      }
    }
  ]
};

        var features = {"type" : "FeatureCollection", "features" : [pt1, poly]};

        //=features

        var isInside1 = turf.inside(pt1, poly);
        //=isInside1


        console.log(isInside1)



    var map = L.map('map').fitWorld().setView([52.119998657638156, 19.16015625], 6); 
    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpandmbXliNDBjZWd2M2x6bDk3c2ZtOTkifQ._QA7i5Mpkd_m30IGElHziw', {
        maxZoom: 18,
        attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
            '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
            'Imagery © <a href="http://mapbox.com">Mapbox</a>',
        id: 'mapbox.streets'
    }).addTo(map);

    L.geoJSON(poly).addTo(map);
    L.geoJSON(pt1).addTo(map);

</script>



</body>
</html>

1 个答案:

答案 0 :(得分:3)

关于文档,不可能直接将featureCollection作为参数添加到turf-inside()中。 turf-inside()需要多边形或多边形。但是你可以编写自己的小函数来检查一个点是否在你的featureCollection中,如下所示:

function checkIsInside(poly) {
  for(poly of poly.features) {
    var isInside = turf.inside(pt1, poly);
    if(isInside) {
      return true
    } else {
      return false
    }
  } 
};

在这里,我创建了一个JS FIDDLE,如果该点位于您的特征集合中的某些多边形中,则会提示true或false。