2015-12-02 208 views
2

如何将点添加到多边形作为单个要素?根据GeoJson规范,这被称为“GeometryCollection”。的“的GeometryCollection”如何使用单点+多边形在GeoJSON中创建GeometryCollection?

例子:

{ "type": "GeometryCollection", 
"geometries": [ 
    { "type": "Point", 
    "coordinates": [100.0, 0.0] 
    }, 
    { "type": "LineString", 
    "coordinates": [ [101.0, 0.0], [102.0, 1.0] ] 
    } 
] 
} 

我尝试添加一个点到面要素,但我无法得到它显示在我的mapbox地图,因为我猜它是无效的GeoJson。

任何人都知道这样做的正确方法是什么?网络上没有太多示例要遵循。

我的看法:[jsfilddle]

var myRegions = { 
"type": "FeatureCollection", 
    "features": [ 
    { 
     "type": "Feature", 
     "properties": {}, 
     "geometries": [ 
     { 
      "type": "Point", 
      "coordinates": [ 
      61.34765625, 
      48.63290858589535 
      ] 
     }, 
     { 
     "type": "Polygon", 
     "coordinates": [ 
      [ 
      [ 
       59.94140624999999, 
       50.65294336725709 
      ], 
      [ 
       54.931640625, 
       50.90303283111257 
      ], 
      [ 
       51.943359375, 
       51.04139389812637 
      ], 
      [ 
       50.9765625, 
       48.19538740833338 
      ], 
      [ 
       52.55859375, 
       46.46813299215554 
      ], 
      [ 
       52.998046875, 
       43.8028187190472 
      ], 
      [ 
       54.4921875, 
       42.391008609205045 
      ], 
      [ 
       57.041015625, 
       43.29320031385282 
      ], 
      [ 
       59.8974609375, 
       45.398449976304086 
      ], 
      [ 
       62.5341796875, 
       44.08758502824516 
      ], 
      [ 
       65.6982421875, 
       45.73685954736049 
      ], 
      [ 
       68.37890625, 
       48.3416461723746 
      ], 
      [ 
       65.8740234375, 
       49.18170338770663 
      ], 
      [ 
       63.720703125, 
       49.97948776108648 
      ], 
      [ 
       63.80859374999999, 
       52.348763181988076 
      ], 
      [ 
       61.4794921875, 
       52.32191088594773 
      ], 
      [ 
       59.9853515625, 
       51.86292391360244 
      ], 
      [ 
       61.9189453125, 
       51.09662294502995 
      ], 
      [ 
       60.5126953125, 
       50.51342652633956 
      ], 
      [ 
       59.94140624999999, 
       50.65294336725709 
      ] 
      ] 
     ] 
     } 
    ] 
    } 
] 
}; 

回答

0

我不确定什么你其实想完成,因为你说你想创建一个GEOMETRYCOLLECTION但在你的例子中,你正在创建一个远远不一样的特征集合。

阿是的FeatureCollection的特征的集合:

与类型 “的FeatureCollection” A GeoJSON的对象是一个特征集合对象。 “FeatureCollection”类型的对象必须具有名称为“features”的成员。与“功能”对应的值是一个数组。

http://geojson.org/geojson-spec.html#feature-collection-objects

这里有一个的FeatureCollection的例子:

{ 
    type: "FeatureCollection", 
    features: [{ 
     "type": "Feature", 
     "properties": { 
      "value": "foo" 
     }, 
     "geometry": { 
      "type": "Point", 
      "coordinates": [0,0] 
     } 
    }, { 
     "type": "Feature", 
     "properties": { 
      "value": "bar" 
     }, 
     "geometry": { 
      "type": "Polygon", 
      "coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]] 
     } 
    }] 
} 

一个GEOMETRYCOLLECTION是一个单一的功能(该功能你可以包含一个的FeatureCollection):

一个GeoJSON的对象类型“功能”是一个功能对象。要素对象必须具有名称为“几何”的成员。几何成员的值是上面定义的几何对象或JSON空值。要素对象必须具有名称为“属性”的成员。属性成员的值是一个对象(任何JSON对象或JSON空值)。如果某个要素具有常用的标识符,那么该标识符应该作为名称为“id”的要素对象的成员包含在内。

http://geojson.org/geojson-spec.html#feature-objects

具有多个几何形状:

与类型 “的GeometryCollection” A GeoJSON的对象是一个几何对象,表示几何对象的集合。几何集合必须具有名称为“几何”的成员。对应于“几何”的值是一个数组。该数组中的每个元素都是一个GeoJSON几何对象。

http://geojson.org/geojson-spec.html#geometry-collection

这里还有一个GEOMETRYCOLLECTION特性的一个例子:

{ 
    "type": "GeometryCollection", 
    "properties": { 
     "value": "foo" 
    }, 
    "geometries": [{ 
     "type": "Point", 
     "coordinates": [0, 0] 
    }, { 
     "type": "Polygon", 
     "coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]] 
    }] 
} 
+0

该死的几更早秒! :-) – ghybs

+0

Highfives @ghybs:D – iH8

+0

除了只有'Feature'预计会有'properties' ...当然它不会损害具有'properties'的GeometryCollection',但Leaflet将不会读取它们。 – ghybs

3

如以GeoJSON规范说,一个Feature object恰好有一个geometry成员,这是一个Geometry object(或null )。

要素对象必须具有名称为"geometry"的成员。几何成员的值是上面定义的几何对象或JSON空值。

在可能的geometry的,你的确可以使用GeometryCollection,其中必须有一名成员geometries。后者是其他几何图形的数组,即您的点,多边形等,甚至另一个GeometryCollection。

几何集合必须有名称为"geometries"的成员。对应于"geometries"的值是一个数组。该数组中的每个元素都是一个GeoJSON几何对象。

所以你的情况,你可以简单地这样做:

var myRegions = { 
    "type": "FeatureCollection", 
    "features": [{ 
    "type": "Feature", // single feature 
    "properties": {}, 
    "geometry": { // unique geometry member 
     "type": "GeometryCollection", // the geometry can be a GeometryCollection 
     "geometries": [ // unique geometries member 
     { // each array item is a geometry object 
      "type": "Point", 
      "coordinates": [ 
      61.34765625, 
      48.63290858589535 
      ] 
     }, 
     { 
      "type": "Polygon", 
      "coordinates": [ 
      [ 
       [ 
       59.94140624999999, 
       50.65294336725709 
       ], 
       // more points… 
       [ 
       59.94140624999999, 
       50.65294336725709 
       ] 
      ] 
      ] 
     } 
     ] 
    } 
    }] 
}; 

更新的jsfiddle:http://jsfiddle.net/rh8ok5t8/18/

相关问题