使用Turf.js解析GEOJSON并将地址与多边形匹配

时间:2018-10-19 22:43:24

标签: javascript polygon geojson turfjs

我有一堆具有以下结构的标记的GEOJSON。这只是一个例子

{
"type": "FeatureCollection",
  "features": [
    {`enter code here`
      "type": "Feature",
      "id": 1,
      "geometry": {
        "type": "Point",
        "coordinates": [
          -95.980577,
          41.287989
        ]
      },
      "properties": {
        "CommunityPartner": "Abide Omaha",
        "Address": "3223 N 45th St",
        "City": "Omaha",
        "State": "NE",
        "Zip": "68104",
        "Location": "Omaha, NE",
        "Latitude": 41.287989,
        "Longitude": -95.980577,
        "PhoneNumber": "402-455-7807",
        "Website": "www.abideomaha.org",
        "PrimaryMissionFocus": "Social Justice",
        "SecondaryMissionFocus": "Economic Sufficiency",
        "WeitzCECPartner": "No",
        "Enteredby": "Service Learning Academy",
        "Notes": "",
        "semester": [
          "Spring 2018"
        ],
        "time": "Spring 2018"
      }
    }
]}

GEOJSON文件称为communityDatas。

我还有一个名为regionData的多边形GEOJSON文件。多边形结构为:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
      "geometry": {
        "type": "Polygon",
        "coordinates": []
      },
      "properties": {}
    }
  ]
}

我要做的是弄清楚标记属于哪个多边形,并在“属性”下创建一个称为“区”的新键,并为其指定多边形的“ id”值。例如,如果标记位于ID为1的多边形中,则该标记在属性下的“区”键将为1。这就是我使用turf.js

进行的操作
for (var i = 0; i < communityDatas.length; i++) {
    var pt = communityDatas.features[i].geometry.coordinates;
    pt = turf.point(pt)
    for (var j = 0; j < districtData.length; j++) {
        var polyg = districtData.features[j].geometry.coordinates;
        polyg = turf.polygon(polyg)
        if (turf.booleanPointInPolygon(point, polyg)) {
            communityDatas.features[i].properties['district'] = districtData.features[j]['id']
        }
    }
}

这是我在标记弹出窗口中显示文本的地方

function parseDescription(message) {
    var string = ""


    for (var i in message) {

        if (i == "CommunityPartner") {
            string += '<span style="font-weight:bold">' + i + '</span>' + ": " + message[i] + "<br>"
        }if (i == "K-12 Partner") {
            string += '<span style="font-weight:bold">' + i + '</span>' + ": " + message[i] + "<br>"
        } else if (i == "PhoneNumber") {
            string += '<span style="font-weight:bold">' + i + '</span>' + ": " + message[i] + "<br>"
        } else if (i == "Website"){
            var website = message[i]
            var base = "http://"
            if (!website.includes("http")){
                website = base.concat(website)
            }
            //string += '<span style="font-weight:bold">' + i + '</span>: <a target="_blank" href="' + message[i] + '">' + message[i] + '</a><br>';
            string += `<span style="font-weight:bold">${i}</span>:<a target="_blank" href="${website}" class="popup">${website}</a><br>`;
        } else if (i= "district"){
            string += '<span style="font-weight:bold">' + i + '</span>' + ": " + message[i] + "<br>"
        }
    }
    return string;
}

但是它没有按预期工作。这是现在的样子。

enter image description here

任何反馈/建议都非常感谢。谢谢

0 个答案:

没有答案