Java Identifying Clicked Feature

824
6
Jump to solution
08-13-2012 07:03 AM
AndrewRohne
New Contributor II
I am trying to identify the feature a user clicks on.  This would be similar to the identify tool, but instead of showing a window, I want to take some of the feature attributes and put them into another table.

The problem I'm having is that every way I try to identify the feature that was clicked on, I get a null result.  The code I'm using is below:

for(int x=0;x<focusMap.getLayerCount();x++){  if(focusMap.getLayer(x).getName().equals("Origin Locations")){   FeatureLayer featLayer=(FeatureLayer) focusMap.getLayer(x);   IIdentify ident=featLayer;   IPoint point=new Point();   point=activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(me.getX(), me.getY());  /*  * The point object has the correct geographic coordinates at this point, so I'm thinking it is not the issue (and at  *   one point, I thought that was the issue because I was passing screen coordinates to the identify commands  *   below).  */        IArray result=ident.identify(point);    IArray result2=featLayer.identify(point,null);    IArray result3=featLayer.identify(point); /*  * The result, result2, and result3 objects (above) are all null here.  I've tried passing an IGeometry object to these  *   commands as well, and that was to no avail.  */   if(result!=null){    for(int i=0;i<result.getCount();i++){     System.out.println(result.getElement(i).toString());    }   }  } }


If anyone can get me pointed in the right direction here, I would appreciate it!

Andrew Rohne
OKI Regional Council
0 Kudos
1 Solution

Accepted Solutions
EricWeber
New Contributor III
Rather than passing the point to the Identify method, you'll probably want to pass a small envelope or circle (centered on the click point) to tolerate the click being a slight distance from the feature. What type of features are you identifying? Polygons, lines, points? If points or lines, then passing a small envelope could be your solution. If you're working with polygons and a click right in the center is still resulting in a null array, there must be some other problem.

View solution in original post

0 Kudos
6 Replies
EricWeber
New Contributor III
Rather than passing the point to the Identify method, you'll probably want to pass a small envelope or circle (centered on the click point) to tolerate the click being a slight distance from the feature. What type of features are you identifying? Polygons, lines, points? If points or lines, then passing a small envelope could be your solution. If you're working with polygons and a click right in the center is still resulting in a null array, there must be some other problem.
0 Kudos
AndrewRohne
New Contributor II
Thanks!  That's what I needed.  Based on some tests I did, it appears that if you pass it a point object, you have to have the EXACT POINT or it will fail.  Passing it an envelope worked.

For the sake of anyone finding this later on, here is the code I'm using:

for(int x=0;x<focusMap.getLayerCount();x++){
 if(focusMap.getLayer(x).getName().equals("Origin Locations")){ 
  FeatureLayer featLayer=(FeatureLayer) focusMap.getLayer(x);
  IIdentify ident=featLayer;
  IEnvelope envelope = new Envelope();
  IPoint envLL=new Point();
  IPoint envUR=new Point();
  envLL=activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(me.getX()-5, me.getY()+7);
  envUR=activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(me.getX()+5, me.getY()-7);
  envelope.setLowerLeft(envLL);
  envelope.setUpperRight(envUR);
  IArray result=ident.identify(envelope);
  if(result!=null){
   for(int i=0;i<result.getCount();i++){
    System.out.println(result.getElement(i).toString());
   }
  }
 }
}


I based the envelope on the screen coordinates and converted them to geographic coordinates for the points for the envelope upper-right and lower-left corners of the envelope.  This hopefully will serve me well at different zoom levels.  The X +/- 5 and Y+/- 7 works for me, but others may want to run a few tests to see if their mileage varies.

Andrew
0 Kudos
LeoDonahue
Occasional Contributor III
Andrew,

Change your zoom level with the code you have.  Does it catch the feature you want to identify?
0 Kudos
LeoDonahue
Occasional Contributor III
Also, I dont' think that code will work if you have to change the projection of your data frame to WGS coordinates.  +/- 5 in WGS will probably not give you what you expect.  Does it?
0 Kudos
EricWeber
New Contributor III
Also, I dont' think that code will work if you have to change the projection of your data frame to WGS coordinates.  +/- 5 in WGS will probably not give you what you expect.  Does it?


Since he's adding and subtracting from screen coordinates (before the toMapPoint transformation), the type of coordinate system shouldn't matter, right? I think that's the best way to do it; it should work as expected regardless of zoom level or of coordinate system.
0 Kudos
AndrewRohne
New Contributor II
Change your zoom level with the code you have.  Does it catch the feature you want to identify?


It does!

envLL=activeView.getScreenDisplay().getDisplayTransformation().toMapPoint(me.getX()-5, me.getY()+7);


In the above, me.getX() and me.getY() are screen coordinates.  They are independent of zoom.  The .toMapPoint(x,y) function changes those screen coordinates to geographic coordinates.

To answer your other question, I haven't tried geographic coordinates or any projection other than NAD83 State Plane Ohio South.  I THINK (operative word!) that it would still work the same way.

Andrew
0 Kudos