2015-12-02 130 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/