Hi,

This morning i put together a quick example for defining a Feature Reference.
Data is loaded via a csv file in a TreeMap of XyzPoint objects.
Basically if the user does the following:
1) clicks the select button
2) Clicks on the displayed points it will tell you on a multi-layer level how 
many points are with in a given Feature Reference.

Is there a better way to do something like this???

The following is the output from clicking on a middle of 3 points:
x: 95.99 y: 120.233 NOT IN FEATURE REFERENCE CIRCLE
x: 85.99 y: 110.233 IN FEATURE REFERENCE CIRCLE
x: 75.99 y: 100.233 NOT IN FEATURE REFERENCE CIRCLE


The following is how I implemented the Feature Reference:
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.FilterFactory;
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 FeatureReferenceAction {
    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.getX(), pos.getY());
                            } catch(IOException ioe){
                                System.err.println(ioe);
                            }
                            
                        }
                    });
        }
    });
    mapFrame.setSize(600, 600);
    mapFrame.setVisible(true);
}

@SuppressWarnings("unchecked")
public static void getCoordinates(double cx, double cy) throws IOException {
    MapLayer[] layers = map.getLayers();
    
    for (int i=0; i<layers.length; i++){
        FeatureSource<SimpleFeatureType, SimpleFeature> fs = 
(FeatureSource<SimpleFeatureType, SimpleFeature>) layers[i].getFeatureSource();
        FeatureCollection<SimpleFeatureType, SimpleFeature> fc = 
fs.getFeatures();
        Iterator<SimpleFeature> f = fc.iterator();
        while (f.hasNext()){
            SimpleFeature sf = (SimpleFeature)f.next();
            Point p = (Point)sf.getAttribute("route");
            double xi = p.getX();
            double yi = p.getY();
            if(checkFeatureReference(cx,cy,xi,yi)){
                System.out.println("x: "+xi+" y: "+yi+" IN FEATURE REFERENCE 
CIRCLE");
            } else {
                System.out.println("x: "+xi+" y: "+yi+" NOT IN FEATURE 
REFERENCE CIRCLE");
            }
        }
    }
}

public static boolean checkFeatureReference(double cx, double cy, double xi, 
double yi){
    //cx: actual x point clicked with mouse
    //cy: actual y point clicked with mouse
    //xi: Point: p.getX()
    //yi: Point: p.getY()
    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);
    if(Math.sqrt(Math.pow((xi-cx),2) + Math.pow((yi-cy),2)) <= radius){
        return true; //in circle
    }
    return false; //not in circle    
}
}

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to