Below is the complete example with filter applied for the Feature Reference:
Given a radius the example prints out points within a circle via the filter for
a multi layer collection:
[javascript:%20void(0)]
package org.geotools.demo;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.swing.JMapFrame;
import com.vividsolutions.jts.geom.Point;
import org.geotools.demo.handler.*;
import java.io.IOException;
import java.util.*;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.styling.StyleFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.Filter;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.GeometryFactory;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JToolBar;
public class FilterFeatureReferenceAction {
static StyleFactory styleFactory =
CommonFactoryFinder.getStyleFactory(null);
static FilterFactory filterFactory =
CommonFactoryFinder.getFilterFactory(null);
static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
static final int FEATURE_REFERENCE_OUTER = 5;
static JMapFrame mapFrame = new JMapFrame();
static MapContext map;
public static void main(String[] args) throws Exception {
TreeMapHandler tmHandler = new TreeMapHandler();
TreeMap<Integer, XyzPoint> allPoints =
tmHandler.getTreeMap("/projects/Yard3.svy");
FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
getPointFeature(allPoints);
FeatureSource<SimpleFeatureType, SimpleFeature> featureSource =
DataUtilities.source(collection);
map = new DefaultMapContext();
map.setTitle("The Points");
map.addLayer(featureSource, null);
displayMap(map);
}
public static FeatureCollection<SimpleFeatureType, SimpleFeature>
getPointFeature(TreeMap<Integer, XyzPoint> allPoints) throws Exception {
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
typeBuilder.setName("mytype");
typeBuilder.setCRS(crs);
typeBuilder.add("route", Point.class);
final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();
FeatureCollection<SimpleFeatureType, SimpleFeature> collection =
FeatureCollections.newCollection();
GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
Collection<XyzPoint> cl = allPoints.values();
Iterator<XyzPoint> it = cl.iterator();
while (it.hasNext()){
XyzPoint xyz = (XyzPoint)it.next();
Point point = factory.createPoint( new
Coordinate(xyz.getX(),xyz.getY()));
featureBuilder.add(point);
collection.add(featureBuilder.buildFeature(null));
}
return collection;
}
public static void displayMap(MapContext map) throws Exception {
mapFrame = new JMapFrame(map);
mapFrame.enableToolBar(true);
JToolBar toolBar = mapFrame.getToolBar();
JButton btn = new JButton("Select");
toolBar.addSeparator();
toolBar.add(btn);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mapFrame.getMapPane().setCursorTool(
new CursorTool() {
@Override
public void onMouseClicked(MapMouseEvent ev) {
//p would be actual x,y pixel point: Point p =
ev.getLocationOnScreen();
DirectPosition2D pos = ev.getMapPosition(); //pos
is the lon,lat point
try {
getCoordinates(pos);
} catch(IOException ioe){
System.err.println(ioe);
}
}
});
}
});
mapFrame.setSize(600, 600);
mapFrame.setVisible(true);
}
@SuppressWarnings("deprecation")
public static FeatureCollection<SimpleFeatureType, SimpleFeature> findFeatures(
FeatureCollection<SimpleFeatureType, SimpleFeature> collection,
DirectPosition2D pos, String geomAttributeName) {
FilterFactory2 ff2 = CommonFactoryFinder.getFilterFactory2(null);
GeometryFactory gf = JTSFactoryFinder.getGeometryFactory(null);
Point jtsPoint = gf.createPoint(new Coordinate(pos.x, pos.y));
Filter filter = ff2.dwithin(ff2.property(geomAttributeName),
ff2.literal(jtsPoint), getRadius(pos.x,pos.y), null);
return collection.subCollection(filter);
}
public static double getRadius(double cx, double cy){
double px = cx+FEATURE_REFERENCE_OUTER; //outer parameter x
double py = cy+FEATURE_REFERENCE_OUTER; //outer parameter y
double dx = px - cx;
double dy = py - cy;
double radius = Math.sqrt(dx*dx + dy*dy);
return radius;
}
@SuppressWarnings("unchecked")
public static void getCoordinates(DirectPosition2D pos) throws IOException {
MapLayer[] layers = map.getLayers();
for (int i=0; i<layers.length; i++){
System.out.println("Layer["+i+"]");
FeatureSource<SimpleFeatureType, SimpleFeature> fs =
(FeatureSource<SimpleFeatureType, SimpleFeature>) layers[i].getFeatureSource();
FeatureCollection<SimpleFeatureType, SimpleFeature> fc =
fs.getFeatures();
FeatureCollection<SimpleFeatureType, SimpleFeature> subCollection =
findFeatures(fc,pos,"route");
Iterator<SimpleFeature> f = subCollection.iterator();
while (f.hasNext()){
SimpleFeature sf = (SimpleFeature)f.next();
Point p = (Point)sf.getAttribute("route");
System.out.println("X: "+p.getX()+" Y: "+p.getY());
}
}
}
}
-----Original Message-----
From: "Michael Bedward" <[email protected]>
Sent: Wednesday, November 25, 2009 10:25pm
To: "Oliver Gottwald" <[email protected]>
Cc: [email protected]
Subject: Re: [Geotools-gt2-users] Feature Reference - Better Way?
And here's that method again without the spurious hack :-)
FeatureCollection<SimpleFeatureType, SimpleFeature> findFeatures(
FeatureCollection<SimpleFeatureType, SimpleFeature> collection,
DirectPosition2D pos, double maxDistance, String geomAttributeName) {
Point jtsPoint = gf.createPoint(new Coordinate(pos.x, pos.y));
Filter filter = ff2.dwithin(ff2.property(geomAttributeName),
ff2.literal(jtsPoint), radius, null);
return fc.subCollection(filter);
}
Michael
------------------------------------------------------------------------------
Join us December 9, 2009 for the Red Hat Virtual Experience,
a free event focused on virtualization and cloud computing.
Attend in-depth sessions from your desk. Your couch. Anywhere.
http://p.sf.net/sfu/redhat-sfdev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users