Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
Sam, I am needing to do something you did. I would like your guidance. I need to get the features of a polygon created by the user. These features appear under the active / visible layers. You did it using something besides OpenLayers? Thank you in advance. –  BC84 Jan 22 at 15:05
1  
This does not appear to be an answer to the current question. If you have a new question you should post it again, and link back here if you think it's related. –  Martin Jan 22 at 15:23
add comment

1 Answer 1

up vote 1 down vote accepted

You should write:

feat.attributes[prop]
share|improve this answer
    
Thanks, did not think it would be that easy. :) –  Sam007 Sep 27 '12 at 16:26
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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