Sunday, 11 December 2016

openlayers 2 - How to Obtain the feature attributes from feature collection



I am able to obtain the features of the layer which is selected by the user in the Layers table and then intersected by a user defined polygon, using e.features. I was able to obtain the attribute values of the feature as a property, like this e.features[0].attributes.Family or e.features[0].attributes.Species. The e.features[0].attributes object looks like this, Object { Family="Pinaceae", Species="Pinus cembroides"}.


Now the problem that I am facing is when a user selects a different layer, say states, the attributes properties change, the object now looks like this, Object { STATE_NAME="Arizona", STATE_FIPS="04", SUB_REGION="Mtn", more...}. So now I need to fetch the attribute data by again changing the properties, like e.features[0].attributes.STATE_NAME.


So I am trying to create a common function where I can obtain the properties of the attributes object and then directly fetch the values for any layer. This is the code that I am trying to implement,


var attrData = [];
for (var i = 0; i < e.features.length; i++) {
//Work on how to fetch properties for all the layers attributes
feat = e.features[i];
for (prop in feat.attributes) { alert(prop); } //this gives all the property values
var values = "";
for (prop in feat.attributes) {

value = (value === "") ? feat.attributes.prop : value += feat.attributes.prop; //But feat.attributes.prop is undefined

}
attrData.push([value])
}

As you can see, for (prop in feat.attributes) { alert(prop); } gives me all the property names but when I assign this prop to feat.attributes.prop to fetch the value, it returns undefined.


Not sure where am I going wrong? This is my application, http://128.196.142.12/geo/test/test_new.html. Click on the 'draw poly' in the toolbar and then select 'Pima Pine' layer in the Map Layers column and create a polygon and intersect it with the blue stars (Pima Pine), you should get the table with the info of the stars which intersect with the vector polygon. Now I am trying to replicate the same for the USA States and that is where I am failing.


How should I change the code to fetch the attribute values for all the layers?



Answer




You should write:


feat.attributes[prop]

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...