如何将点转换为多边形?

时间:2017-03-25 01:37:07

标签: polygon mapbox-gl-js mapbox-gl

我在GeoJSON中有一个大数据,Point类型如下:

    {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          2.8125,
          47.040182144806664
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Point",
        "coordinates": [
          14.765625,
          48.69096039092549
        ]
      }
    }
  ]
}

据我所知,为了在地图上创建多边形,我的GeoJSON应该将Polygon作为Type而不是Points。我该怎么做?有人有想法吗?我应该将它们转换成多边形吗?怎么样?我真的需要你的帮助......(我顺便使用Mapbox-Gl-Js)

谢谢!

2 个答案:

答案 0 :(得分:0)

这很简单:我在csv中有关于纬度,经度的恐怖主义数据。当我将它转换为geojson时,我有一个这样的结构:     {   "输入":" FeatureCollection",   "功能":[     {       "输入":"功能",       "属性":{},       " geometry":{         "输入":" Point",         "坐标":[           2.8125,           47.040182144806664         ]       }     }

问题是:我如何使用这些数据创建这样的地图:terrorism_map

我知道我的geojson在这种情况下没用,但我需要一种使用这些数据创建地图的方法。很抱歉混淆......

答案 1 :(得分:0)

在搜索年龄时遇到了同样的问题,然后找到了这个lib Terraformer(http://terraformer.io/core/#terraformercircle),其中一种方法可让您指定一个长点和点,并将其转换为围绕该点的圆。

请在下面查看我的实现,或者您也可以浏览文档 http://terraformer.io/core/#terraformercircle

//get the Terraformer lib
var Terraformer = require("terraformer");

const getPolygonFromPoints = async => {
    //get the geoJSON
    const getPointBasedGeoJSON = await require({localresource}); //or use a fetch if it is an external resource.

    //loop over all the featues using ES6 map, to generate a new array of polygon based coordinates
    const polygonBasedGeoJSON = getPointBasedGeoJSON.features.map(d => ({
        type: "Feature",
        ...new Terraformer.Circle(
            [d.geometry.coordinates[0], d.geometry.coordinates[1]],
            500, //The radius of the circle in meters.
            5 //How many steps will be used to create the polygon that represents the circle. i.e number of poins in the polyon array
        ),
        /*
        Terraformer.Circle will retrun an object similar to this, which we need to spread into our parent object, hence the ES6 spread
        "geometry": {
            "type": "Point",
            "coordinates": [array of polygon coordinates based on the point you referenced]
        },
        */
        point: [d.geometry.coordinates[0], d.geometry.coordinates[1]], //so that you can still reference a point
        properties: {
            //specify any properties
        }
    }));

    if (polygonBasedGeoJSON.length > 0){
        return polygonBasedGeoJSON
    }

    return []
}

//Points to Polygon collection ready for use
console.log(getPolygonFromPoints())
相关问题