[GIS] Create multiple vector layers with one GeoJSON in OpenLayers

geojsonlayersopenlayers

I am a beginner to OpenLayers,and what i'm trying to do is to create multiple vector layers with one GeoJSON in OpenLayers 3.
Here is a small part of the GeoJSON file.

{"type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
           "type": "Point",
           "coordinates":  [ 7.07,44.91 ]
        },
    "properties": {
        "Type":"Animals",
        "JS_Date":1470433175,
        }
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ 5.97,44.78 ]
    },
    "properties": {
    "Type":"Contaminations",
    "JS_Date":1470433145,
    }
  },
……]
}

I read an old topic about that, but i didn't manage to adapt his solution to mine.

What I’m trying to do, is to create two different layers depending on the GeoJSON properties. therefor I want a layer with all features which have as properties type “Animals” and another layer with all features which have as properties type “Contaminations”.

Currently the code I have to display features in one layer is this one:

var GeoJsonDATA=new ol.layer.Vector({           
    source: new ol.source.Vector({
        url: '../Data/Observations.geojson',
        format: new ol.format.GeoJSON()
            }),
    name: ‘UserData’,
});

Best Answer

If you are OK to use QGIS, you can follow these steps :

  • Open your file in QGIS;
  • Select the entries where Type is "Animals" in the properties;
  • Right-click on the layer in the Legend and choose "Save As";
  • Save as GeoJSON and check the box that says "Save only selected features".

You don't need to change the CRS.

Repeat the steps with the entries where "Type" = "Contaminations" and you will have your two different GeoJSON files.