9

How do you add a point to a polygon as a single feature? According to the GeoJson specs, this is known as a "GeometryCollection".

Example of a 'GeometryCollection':

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

I tried adding a point to a polygon feature, but I couldn't get it to show on my mapbox map because I guess it is invalid GeoJson.

Anyone know what the proper way of doing this is? There are not many examples to follow on the web.

My take: [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
            ]
          ]
        ]
      }
   ]
  }
 ]
};
redshift
  • 4,815
  • 13
  • 75
  • 138

2 Answers2

16

As said in GeoJSON spec, a Feature object has exactly one geometry member, which is a Geometry object (or null).

A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value.

Among the possible geometry's you can indeed use a GeometryCollection, which must have a member geometries. The latter is an array of other geometries, i.e. your point, polygon, etc., or even another GeometryCollection.

A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.

So in your case you could simply do something like:

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
              ]
            ]
          ]
        }
      ]
    }
  }]
};

Updated jsfiddle: http://jsfiddle.net/rh8ok5t8/18/

ghybs
  • 47,565
  • 6
  • 74
  • 99
4

I'm unsure to what you're actually trying to accomplish because you say you want to create a geometrycollection but in your example you're creating a featurecollection which is not the same by far.

A featurecollection is a collection of features:

A GeoJSON object with the type "FeatureCollection" is a feature collection object. An object of type "FeatureCollection" must have a member with the name "features". The value corresponding to "features" is an array.

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

Here's an example of a 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]]]
        }
    }]
}

A geometrycollection is a single feature (which you could contain in a featurecollection):

A GeoJSON object with the type "Feature" is a feature object. A feature object must have a member with the name "geometry". The value of the geometry member is a geometry object as defined above or a JSON null value. A feature object must have a member with the name "properties". The value of the properties member is an object (any JSON object or a JSON null value). If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id".

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

with multiple geometries:

A GeoJSON object with type "GeometryCollection" is a geometry object which represents a collection of geometry objects. A geometry collection must have a member with the name "geometries". The value corresponding to "geometries" is an array. Each element in this array is a GeoJSON geometry object.

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

And here's an example of a geometrycollection feature:

{
    "type": "GeometryCollection",
    "properties": {
        "value": "foo"
    },
    "geometries": [{
        "type": "Point",
        "coordinates": [0, 0]
    }, {
        "type": "Polygon",
        "coordinates": [[[45, 45], [45, -45], [-45, -45], [-45, 45], [45,45]]]
    }]
}
iH8
  • 27,722
  • 4
  • 67
  • 76
  • Damn a few seconds earlier! :-) – ghybs Dec 02 '15 at 14:59
  • Except that only `Feature`'s are expected to have `properties`… of course it does not harm a `GeometryCollection` having `properties`, but Leaflet will not read them. – ghybs Dec 02 '15 at 15:01
  • ? A geometrycollection is a feature, thus it must have a `properties` member or i'm interpreting the spec wrong. – iH8 Dec 02 '15 at 15:04
  • As far as I understand, a GeometryCollection is a Geometry object, like Point, Polygon, etc? – ghybs Dec 02 '15 at 15:11
  • Ok, i meant a featureobject with a geometrycollection as geometry object must have properties. The feature object spec is wrong btw, it states that it must have a `geometry` member which isn't true in the case when it holds a geometrycollection, then the member is called `geometries`. And FYI the spec we're linking to is outdated. Current draft can be found here: https://github.com/geojson/draft-geojson/blob/master/middle.mkd – iH8 Dec 02 '15 at 15:22
  • 1
    Thank you for the link! I do not think there is a mistake on that point in the spec? To me you have a Feature, which `geometry` is a Geometry object with type "GeometryCollection". Then the GeometryCollection has `geometries` which is an array of Geometry objects (some or all could be also of GeometryCollection type). – ghybs Dec 03 '15 at 02:53