Product: MapXtreme Java
Version: 3.1
Platform: All Platforms
Category: Code Samples
Summary:
Adding a VectorGeometry to an Annotation layer.
Question:
The following code sample does a searchAtPoint on a layer and puts a duplicate feature in an Annotation layer.
Answer:
private void searchForFeatures(MapJ map, String layerName, DoublePoint pnt) {
try {
// get the search layer
Layer searchLayer = map.getLayers().getLayer(layerName);
// get the annotation layer
Layer annLayer = map.getLayers().getLayer("FEATURE");
// if the annotation layer named FEATURE doesn't exist, add it
if (annLayer==null) {
annLayer = map.getLayers().insert(new LocalDataProviderRef(new AnnotationDataProviderHelper()),
new AnnotationTableDescHelper("FEATURE"), 0, "FEATURE");
}
// create a Vector for the search columns
Vector cols = new Vector();
cols.add("STATE");
// search at point and store the results in a new feature set
FeatureSet results = searchLayer.searchAtPoint(cols, pnt, null);
// grab the first feature
Feature foundFeature = results.getNextFeature();
// if a feature exists in the search results
if (foundFeature!=null) {
// get all the Features in the annotation layer
FeatureSet tempFs = annLayer.searchAll(new Vector(), null);
if (tempFs!=null) {
// remove the only feature in the layer (we only ever add one)
annLayer.removeFeature(tempFs.getNextFeature().getPrimaryKey());
tempFs.dispose();
}
// should check the Feature with getType() to make sure it's a VectorGeometry
// get the Geometry as a VectorGeometry and get it's point list
VectorGeometry geom = (VectorGeometry)foundFeature.getGeometry();
PointList pntList = geom.getNextPointList();
// create a double array and get the points from the Feature
double [] pnts = new double[pntList.getPointCount()*2];
pntList.getNextPoints(pnts, 0, pntList.getPointCount()*2);
// create a new Rendition
Rendition rend = new Rendition();
rend.setValue(Rendition.STROKE, Color.red);
// create an attributes array
Attribute [] attrs = new Attribute[1];
attrs[0] = new Attribute(foundFeature.getAttribute(0).getString());
// add a new feature to the annotation layer
annLayer.addFeature(map.getFeatureFactory().createRegion(pnts, rend, attrs,
new PrimaryKey(attrs[0])));
}
// free the FeatureSet resources
results.dispose();
}
catch (Exception e) {
e.printStackTrace();
}
}
Last Modified: 05/22/2001 02:11:23 PM
|