0

i want to load the coordinates of a point that is stored inside a vector layer and the vector layer is stored inside a layergroup. I do load the vector layers dynamically to the layergroup "Runs". So i can't just do:

var source = layer.getSource();
source.forEachFeature(function(feature){
  var coord = feature.getGeometry().getCoordinates();
  // ...
});

At the moment it is totally unclear how to filter for the layer and then for the point by name to get the coordinates.

So what i have available is the layer name, layergroup name and the feature name. I tried different approaches like:

My layergroup is called 'runs' so to get the layers inside runs i did:

i tried also:

runs.getLayers().getSource();

But i get:

Uncaught TypeError: runs.getLayers(...).getSource is not a function.

I'm having a hard time to get my head around this.


So i made it a little furhter:

      function getActiveLayer(layerName,picID) {
            // get layerGroups
            var layers = map.getLayers();
            var length = layers.getLength(), l;
            for (var i = 0; i < length; i++) {
                l = layers.item(i);
                var lt = l.get('title');
                // check for layers within groups
                if (lt === 'Runs') { // Title of Group
                  // get layers from Runs
                  var layers = l.getLayers();
                  //get length
                  var length = layers.getLength(), l;
                  if (length > 0) {
                    for (var i = 0; i < length; i++) {

                      l = layers.item(i);
                      var lt = l.get('title');
                      // check for Layer Title
                      if (lt === layerName) { // Title if Layer
                        var innerLayers = l.getSource().getFeatureById(picID).getGeometry().getCoordinates();
                        return innerLayers;
                      }
                    }
            }
                }
            }


      };

With this function i get the LayerGroup list that i can parse and identify the Group "Runs" i want. Until this point it is working.

The Problem starts at the second Point. The layers.getLength() is always zero for the layers that are inside the "Runs" Group. enter image description here

The Upper one is the getLayers from map. The lower one is the getLauers from "Runs".

Why is length 0 if there are Layers inside the array?

EDIT

      function getActiveLayer(layerName,picID) {
        var layers = map.getLayers();
        console.log(layers);
            var length = layers.getLength(), l;
            console.log(length);
            for (var i = 0; i < length; i++) {
                l = layers.item(i);
                var lt = l.get('title');
                // check for layers within groups
                if (lt === 'Runs') { // Title of Group
                  var layers = l.getLayers().getArray();
                  console.log(layers);
                  //if (length > 0) {
                    for (var i = 0; i < 99; i++) {

                      l = layers[i];
                      console.log(l);
                      console.log(layerName);
                      var lt = l.get('title');
                      console.log(lt);
                      // check for Layer Title
                      if (lt === layerName) { // Title if Layer
                        var innerLayers = l.getSource().getFeatureById(picID).getGeometry().getCoordinates();
                        return innerLayers;
                      }
                    }
                }
            }


      };

This function works. The only Problem i have that i can not figure out how the get the length of the object to config the second for loop.

1

Just in case someone else is starting with openlayes. This is a working function i came up with:

 function setActivePicMarker(layerName,layerTitle, featureName) {
   var layers = map.getLayers();
   var length = layers.getLength();
   for (var i = 0; i < length; i++) {
       l = layers.item(i);
       var lt = l.get('title');

       // check for layers within groups
       if (lt === layerTitle) { // Title of Group
         var layers = l.getLayers().getArray();
         var length = l.getLayers().getArray().length;

           // loop over layers
           for (var i = 0; i < length; i++) {
             l = layers[i];
             var lt = l.get('title');

             // check for Layer Title
             if (lt === layerName) { // Title if Layer
               var coords = l.getSource().forEachFeature(function (feature){

                    // check for feature and get coordinates
                    if (feature.get('Name') == featureName{
                       var Coords = feature.getGeometry().getCoordinates();
                       console.log(Coords);
                    }    
               })
               return Coords;
             }
           }
       }
   }
}; // end 

The function searches for a specified feature inside a specified layer inside a specified layer group and returns the features coordinates.

0

I was spacing out at work today... So here is a way of doing what you described above. There are for sure other ways of doing this.

// OpenLayers V4.6.5
// demo data
const feature = new ol.Feature({
  geometry: new ol.geom.Point([3,5]), 
  name: 'myFeatureName'
});
feature.setId(5);

const map = new ol.Map({
  view: new ol.View({}),
  layers: [
    new ol.layer.Group({
      title: 'Runs',
      layers: [
        new ol.layer.Vector({
          title: 'layerName',
          source: new ol.source.Vector({
            features: [feature]
          })
        })
      ]
    })
  ]
});

//// Return data by Feature ID
// Internal function for returning coordinates from a layer, from a feature by featureID
const byFeatureId = (layer, layerName, picID) => {
  if(layer.get('title') === layerName) {
    return layer.getSource().getFeatureById(picID).getGeometry().getCoordinates();
  }
};

// Public function for returning coordinates from a feature by featureID
// layerName: string, the name of the layer that will be querried
// picID: string/number, the featureID of the feature that will be querried
// groupLayerName: string, 'Runs'
function getCoordinatesByFeatureID(layerName, picID, groupLayerName) {
  var result = [];

  map.getLayers().forEach(groupLayer => {
    if (groupLayer.get('title') === groupLayerName) {
      groupLayer.getLayers().getArray().forEach(innerLayer => {
        result = byFeatureId(innerLayer, layerName, picID);
      });
    }
  });

  return result;
}

console.log('coodinatesById', getCoordinatesByFeatureID('layerName', 5, 'Runs'));

//// Return data by Feature NAME
// Internal function for returning coordinates from a layer, from a feature by featureName
const byFeatureName = (layer, layerName, featureName) => {
  var result = new ol.Feature();

  if(layer.get('title') === layerName) {
    layer.getSource().getFeatures().forEach(feature => {
      if (feature.get('name') === featureName) {
        result = feature.getGeometry().getCoordinates();
      }
    });
  }

  return result;
};

// Public function for returning coordinates from a feature by featureName
// layerName: string, the name of the layer that will be querried
// featureName: string, the featureName of the feature that will be querried
// groupLayerName: string, 'Runs'
function getCoordinatesByFeatureName(layerName, featureName, groupLayerName) {
  var coordinates = [];

  map.getLayers().forEach(groupLayer => {
    if (groupLayer.get('title') === groupLayerName) {
      groupLayer.getLayers().getArray().forEach(innerLayer => {
        coordinates = byFeatureName(innerLayer, layerName, featureName);
      });
    }
  });

  return coordinates;
}

console.log('coodinatesByName', getCoordinatesByFeatureName('layerName', 'myFeatureName', 'Runs'));

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.