I have a service that return data in json format as the following snippet:

{"type":"FeatureCollection", "features":[{"type": "Feature", "id": 1, "sheet_no": "NF4011D", "geometry": {"type":"MultiPolygon","coordinates":[[[[57.25,21],[57,21],[57,21.25],[57,21.5],[57.25,21.5],[57.5,21.5],[57.5,21.25],[57.5,21],[57.25,21]]]]}}, ...

All I want is to get the sheet_no of the selected feature, example: NF4011D

I added the selection method as follows:

selectClick = new ol.interaction.Select({
    condition: ol.events.condition.click,

I added also the click event to this layer as follows:

selectClick.on('select', function(e) 
      //I tried
      var x = e.element.get('sheet_no');// Uncaught TypeError: Cannot read property 'get' of undefined
      var features = selectClick.getFeatures();
      var feature = features.item(0); 
      var no = feature.get('sheet_no');  //results in undefined
      var name = features[0].attributes.id // attributes not defined
      //and others  
      var sheets = selectedFeatures.getArray().map(function(feature) 
            return feature.get('sheet_no');

        if (sheets.length > 0) 
            console.log("names: "+sheets); //returns nothing

I have tried for a day almost every solution that is market as correct in stackoverflow but unfortunately I could never get one working:

This this question has been there for a while and got answered. But I think that there is something in my json file that is different. Please help

  • try e.selected[0].get('sheet_no') – Mike Sep 4 '19 at 17:13
  • that results in undefined – Hilal Al-Rajhi Sep 4 '19 at 17:39
  • 1
    It's because your json isn't a standard geojson which should look like {"type":"FeatureCollection", "features":[{"type": "Feature", "id": 1, "properties": { "sheet_no": "NF4011D" }, "geometry": { Some pre-processing will be needed if you are using ol.format.GeoJSON – Mike Sep 4 '19 at 19:04
  • Oh my god, that just solved my problem. I just added the properties column in my query with the value sheet_no. – Hilal Al-Rajhi Sep 4 '19 at 19:26
  • how to mark that as correct answer? Should I add the answer myself? – Hilal Al-Rajhi Sep 4 '19 at 19:29

The JSON should use a properties collection for non-standard properties to comply with the GeoJSON standard:

  { "type": "FeatureCollection",
    "features": [{
      "type": "Feature",
      "id": 1,
      "properties": { "sheet_no": "NF4011D" },
      "geometry": {
        "coordinates": [[[[57.25,21],[57,21],[57,21.25],[57,21.5],[57.25,21.5],[57.5,21.5],[57.5,21.25],[57.5,21],[57.25,21]]]]
    ... ]
  • That is correct. Thank you Mr. Mike – Hilal Al-Rajhi Sep 6 '19 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.