Select to view content in your preferred language

Problem in reading attribute of the selected feature on Map

762
1
05-29-2017 02:42 AM
SignalGroup
Deactivated User

Hi,

I have selected a feature on Map , and i am trying to read the attribute  of the selected feature .

But all attributes are null.

I am using

ArcGIS 10.4 Engine

Postgresql 9.4 64 bit

Following is my code snippet.

public void onMouseDown(IMapControlEvents2OnMouseDownEvent event) throws AutomationException, IOException {

double x = event.getMapX();

double y = event.getMapY();

if (event.getButton() == 1)

{

try {

selectFeaturesScreenPoint(mapBean.getMap(),event.getX(),event.getY(),20);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

public void selectFeaturesScreenPoint(IMap map, int x, int y, int pixelTol)throws Exception

{

tagRECT r[] = new tagRECT[1];

r[0]=new tagRECT();

// Construct a small rectangle out of the x,y coordinates' pixel

// tolerance.

r[0].left = x - pixelTol; // Upper left x, top left is 0,0.

r[0].top = y - pixelTol; // Upper left y, top left is 0,0.

r[0].right = x + pixelTol; // Lower right x, top left is 0,0.

r[0].bottom = y + pixelTol; // Lower right y, top left is 0,0.

// Transform the device rectangle into a geographic rectangle via the

// display transformation.

IEnvelope envelope = new Envelope();

IActiveView activeView = (IActiveView)map;

IDisplayTransformation displayXform = activeView.getScreenDisplay()

.getDisplayTransformation();

displayXform.transformRect(envelope, r, 100);

// 5 = esriTransformPosition + esriTransformToMap.

envelope.setSpatialReferenceByRef(map.getSpatialReference());

ISelectionEnvironment selectionEnv = new SelectionEnvironment();

selectionEnv.setCombinationMethod(esriSelectionResultEnum.esriSelectionResultNew);

map.selectByShape(envelope, selectionEnv, true);

activeView.refresh();

if(map.getSelectionCount()>0)

{

System.out.println("Item is selected ");

MapSelection mapSelection = new MapSelection(map.getFeatureSelection());

mapSelection.reset();

IFeature feature = mapSelection.next();

System.out.println("Selected Feature is "+feature.getFields().getFieldCount());

Fields fields = (Fields) feature.getFields();

int fieldCount = fields.getFieldCount();

// Go through each field and print to the console

for (int index = 0; index < fieldCount; index++) {

System.out.print( "\t" + " " +fields.getField(index).getDefaultValue());

}
}

}

Can anybody help me.

0 Kudos
1 Reply
DuncanHornby
MVP Notable Contributor

Your logic is incorrect and I believe the final error is with this line:

System.out.print( "\t" + " " +fields.getField(index).getDefaultValue());

You are returning the DEFAULT value, which is a geodatabase property, so if your layer is a shapefile this won't exist. Also the default value is a property that assigns a value to the field when you create a feature, more information is here. What you want to be returning is the value for the row or in this case the feature.

It looks like you ASSUME you only ever select one feature; but as you are running the select on your map (rather than an individual layer), so you could potentially be selecting many features across many layers. Was that your intention?

So assuming you do select a single feature you have got that by creating your feature object. You would then read the values from each field for that row via IFeature.Value(). Currently you get the feature, ignore it completely and cycle through fields looking at an inappropriate property setting which may not even exist depending upon what the source of the data is.

0 Kudos