8

I could not find any relevant information in the spec: http://geojson.org/geojson-spec.html

Is it allowed to have a properties key in a FeatureCollection? Or is it only possible for Features?

If it's possible, how can I access the properties within openlayers? Thanks in advance.

bardiir
  • 14,556
  • 9
  • 41
  • 66
jllodra
  • 1,348
  • 1
  • 14
  • 22

2 Answers2

11

The specification does not forbid it explicitly, but feature collections do not have properties. GeoJSON spec only mentions the following keys for FeatureCollection:

  • type - must be `FeatureCollection
  • features - array of feaures
  • bbox - bounding box of the whole feature collection

As I see from OpenLayers code, properties is only processed for feature instances, not for the feature collection.

lexicore
  • 42,748
  • 17
  • 132
  • 221
-2

Switch to Leaflet is easier and it accepts GeoJSON with FeatureCollection. You can get attributes of the in the Popup as well when you click the objects.

However, Openlayers do as well access FeatureCollection. Mobile (jQuery Mobile) example for Openlayers demonstrated accessing properties.

http://openlayers.org/dev/examples/mobile-jq.html#mappage

Click the black icons and you will see the popup form with these attributes.

This is part of the GeoJSON in the mobile example.

{
    "type": "FeatureCollection",
    "features": [
        { "type": "Feature", 
          "geometry": {"type": "Point","coordinates": [1332700, 7906300]},
          "properties": {"Name": "Igor Tihonov","Country":"Sweden", "City":"Gothenburg"}
        },
        { "type": "Feature", 
          "geometry": {"type": "Point","coordinates": [790300, 6573900]},
          "properties": {"Name": "Marc Jansen","Country":"Germany", "City":"Bonn"}
        },
        { "type": "Feature", 
          "geometry": {"type": "Point","coordinates": [568600, 6817300]},
          "properties": {"Name": "Bart van den Eijnden","Country":"Netherlands", "City":"Utrecht"}
        },
        { "type": "Feature", 
          "geometry": {"type": "Point","coordinates": [-7909900, 5215100]}
        }
    ]
}
jllodra
  • 1,348
  • 1
  • 14
  • 22
ns-1m
  • 341
  • 4
  • 10