English

How To: Select features from one layer, using a feature geometry from another

Summary

Instructions provided describe selecting features from one layer based on a feature geometry, from another layer using the Java Connector.

Procedure

This code is written as a sample Java class. Modify it to suit user needs and port to JSP if desired.


Code:

import com.esri.aims.mtier.model.map.layer.*;
import com.esri.aims.mtier.model.map.layer.query.*;
import com.esri.aims.mtier.io.ConnectionProxy;
import com.esri.aims.mtier.model.map.Map;
import com.esri.aims.mtier.model.acetate.*;

public class SelectByFeature {

public static void main(String[] args) {

ConnectionProxy connection = new ConnectionProxy();
Map map = new Map();

connection.setHost("localhost");
connection.setPort(5300);
connection.setService("SantaClara");
connection.setDisplayMessages(false);
try {
map.initMap(connection, 0, true, true, true, true);
} catch (Exception ex) {
}
map.getLayers().setGeometry(true);

// we'll be selecting all the hospitals within a specific topo polygon
FeatureLayer pointsLayer = getLayerByName("hospital", map);
FeatureLayer polygonLayer = getLayerByName("topoq24", map);

// create a Filter object and send the GET_FEATURES request
Filter filter = new Filter();
filter.setWhereExpression("#ID# = 10");
filter.setGlobalEnvelope(true);
polygonLayer.setFilterObject(filter);
map.refresh();

// get the first returned from the GET_FEATURES
Geometry geom = polygonLayer.getRecordset().getGeometry(0);

// create a polygon shape from the feature geometry
Points points = geom.getPoints();
Ring ring = new Ring();
ring.setPoints(points);
Polygon polygon = new Polygon();
polygon.addRing(ring);

// remove the filter object
// this is required or the next request will include the previous filter
polygonLayer.setFilterObject(null);

// apply the new filter with the shape
filter.setWhereExpression("");
filter.setGlobalEnvelope(true);
filter.setSpatialShape(polygon);
filter.setRelation(Filter.AREA_INTERSECTION);
pointsLayer.setFilterObject(filter);

// send the GET_FEATURES request
map.refresh();

// output the resultset
Recordset recordset = pointsLayer.getRecordset();
System.out.println("Selected " + recordset.getCount() + " records");


// print out field names
int fieldCount = recordset.getTableDesc().getCount();
String fieldName = "";
for (int i = 0; i < fieldCount; i++) {
fieldName = recordset.getTableDesc().getFieldName(i);
System.out.print(fieldName + "\t");
}
System.out.print("\n");
// print field values
int recordCount = recordset.getCount();
String fieldValue = "";
for (int i = 0; i < recordCount; i++) {
for (int j = 0; j < fieldCount; j++) {
fieldValue = recordset.getRecords(i).getFieldValue(j);
System.out.print(fieldValue + '\t');
}
System.out.print('\n');
}

}

// getLayer usually requires that you know the index number of the specific layer
// this is a utility function to return a layer by the name instead
private static FeatureLayer getLayerByName(String layerName, Map map) {
FeatureLayer layer = null;
for (int i=0;i<map.getLayers().getCount();i++) {
if (map.getLayers().item(i).getName().equalsIgnoreCase(layerName))
{layer = (FeatureLayer) map.getLayers().item(i);}
}
return layer;
}

}