I put together a rubberband example via the FeatureCollection and mouse 
(Press,Dragged,Released) this afternoon.
Is using Java2d a better way to do this?  If so is there an example out there 
with drawing and Java2d?
I load data into a TreeMap of XyzPoint
Based on mouse actions I build a FeatureCollection and display to the user as 
points are added with a rubberband effect.

onMousePressed: starts a new TreeMap of XyzPoints
onMouseDragged: does rubberband effect of line from last point
onMouseReleased: completes from last point released to the 0 element starting 
point

What I was thinking was build with a featureCollection convert to poly when 
complete and name.  Then from there be able to manipulate it more as part of 
the Collection.  The following is my code:

package org.geotools.demo;

import org.geotools.feature.FeatureCollection;

public class RubberbandAction {
    static StyleFactory styleFactory = 
CommonFactoryFinder.getStyleFactory(null);
    static FilterFactory filterFactory = 
CommonFactoryFinder.getFilterFactory(null);
    static final CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
    static final int MAX_NAME_LENGTH = 20;
    
    static JMapFrame mapFrame = new JMapFrame();
    static TreeMap <Integer, XyzPoint>eallPoints = new TreeMap<Integer, 
XyzPoint>();
    static MapContext mapper;
    static int startPoint=0;
    
public static void main(String[] args) throws Exception {
    
        TreeMapHandler tmHandler = new TreeMapHandler();
       
        TreeMap allPoints = tmHandler.getTreeMap("/projects/Yard3.svy");
        
        FeatureCollection collection = getLineFeature(allPoints);
        
        FeatureSource featureSource = DataUtilities.source(collection);
        
        Style lineStyle = createLineStyle();
        
        MapContext map = new DefaultMapContext();
        map.setTitle("The Line");
        map.addLayer(featureSource, lineStyle);
        
        //JMapFrame.showMap(map);
        displayMap(map);
        
}


public static void displayMap(MapContext map) throws Exception {

    mapFrame = new JMapFrame(map);
    
    /*
     * Before making the map frame visible we add a new button to its
     * toolbar for our custom feature selection tool
     */
    mapper=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 onMousePressed(MapMouseEvent ev) {
                            Point p = ev.getLocationOnScreen();
                            //p would be actual x,y pixel point
                            DirectPosition2D pos = ev.getMapPosition();
                            if(eallPoints!=null && eallPoints.size()>0){
                                System.out.println("startPoint="+startPoint);
                                try {
                                    XyzPoint xyz = new XyzPoint();
                                    xyz.setX(pos.getX());
                                    xyz.setY(pos.getY());
                                    System.out.println("onMousePressed 
X="+pos.getX()+" Y="+pos.getY());
                                    eallPoints.put(startPoint, xyz);
                                    } catch(Exception e){
                                        System.out.println("e" +e);
                                    }
                            } else if(eallPoints!=null && eallPoints.size()!=1){
                                System.out.println("FIRE eallPoints if");
                                eallPoints = new TreeMap<Integer, XyzPoint>();
                                try {
                                XyzPoint xyz = new XyzPoint();
                                xyz.setX(pos.getX());
                                xyz.setY(pos.getY());
                                System.out.println("onMousePressed 
X="+pos.getX()+" Y="+pos.getY());
                                eallPoints.put(startPoint, xyz);
                                } catch(Exception e){
                                    System.out.println("e" +e);
                                }
                            }
                        }
                        @Override
                        public void onMouseDragged(MapMouseEvent ev) {
                            
                            DirectPosition2D pos = ev.getMapPosition();
                            System.out.println("onMouseDragged X="+pos.getX()+" 
Y="+pos.getY());
                            try {
                                if(getLayerIndex("draw")>=0){
                                mapper.removeLayer(getLayerIndex("draw"));
                                }
                                XyzPoint xyz = new XyzPoint();
                                xyz.setX(pos.getX());
                                xyz.setY(pos.getY());
                                
                                eallPoints.put(startPoint+1, xyz);
                                FeatureCollection collection = 
getLineFeature(eallPoints);
                                FeatureSource featureSource = 
DataUtilities.source(collection);  
                                mapper.addLayer(featureSource, 
createLineStyle());
                                
mapper.getLayer(getLastIndex()).setTitle("draw");
                                mapFrame = new JMapFrame(mapper);
                                mapFrame.repaint();
                            } catch (Exception e) {
                                System.out.println("e: "+e);
                            }
                            
                        }
                        @Override
                        public void onMouseReleased(MapMouseEvent ev) {
                            
                            System.out.println("onMouseReleased");
                            try {
                                startPoint=startPoint+2;
                                
eallPoints.put(startPoint,(XyzPoint)eallPoints.get(0));
                                FeatureCollection collection = 
getLineFeature(eallPoints);
                                FeatureSource featureSource = 
DataUtilities.source(collection);  
                                mapper.addLayer(featureSource, 
createLineStyle());
                                
mapper.getLayer(getLastIndex()).setTitle("draw");
                                mapFrame = new JMapFrame(mapper);
                                mapFrame.repaint();
                            } catch (Exception e) {
                                System.out.println("e: "+e);
                            }
                            
                        }
                        
                        
                    });
        }
    });
    mapFrame.setSize(600, 600);
    mapFrame.setVisible(true);
}

public static int getLastIndex() throws IOException{
    MapLayer[] layers = mapper.getLayers();
    System.out.println("layers.lenght-1="+(layers.length-1));
    return layers.length-1;
}

public static int getLayerIndex(String name) throws IOException{
    MapLayer[] layers = mapper.getLayers();
    
    for (int i=0; i<layers.length; i++){    
        System.out.println(i);
        if(name.compareTo(layers[i].getTitle())==0){
            System.out.println("remove draw layer");
            return i;
        }
    }
    return -1;
    
}


public static FeatureCollection getLineFeature(TreeMap allPoints) throws 
Exception {
    SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
    typeBuilder.setName("mytype");
    typeBuilder.setCRS(crs);
    typeBuilder.add("route", LineString.class);
    //typeBuilder.length(MAX_NAME_LENGTH).add("name", String.class);
    final SimpleFeatureType TYPE = typeBuilder.buildFeatureType();
    
    FeatureCollection collection = FeatureCollections.newCollection();
    GeometryFactory factory = JTSFactoryFinder.getGeometryFactory(null);
    SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
    
      ArrayList coordList = new ArrayList();
      Collection cl = allPoints.values();
      Iterator it = cl.iterator();
      while (it.hasNext()){
          XyzPoint xyz = (XyzPoint)it.next();
          coordList.add(new Coordinate(xyz.getX(),xyz.getY()));
          //featureBuilder.set("name",xyz.getName());
      }
      
      Coordinate[] coords = CoordinateArrays.toCoordinateArray(coordList);
      LineString lineString = factory.createLineString(coords);

        
      //LineString lineString = factory.createLineString(cord);
      //SimpleFeature feature = SimpleFeatureBuilder.build( TYPE, new 
Object[]{lineString, "test"}, null );
      //featureBuilder.set("name", "testing123");
      featureBuilder.add(lineString);
      collection.add(featureBuilder.buildFeature(null));
      coordList.clear();
    return collection;
}


private static Style createLineStyle() {
    Stroke stroke = styleFactory.createStroke(
            filterFactory.literal(Color.BLUE),
            filterFactory.literal(1));

    LineSymbolizer sym = styleFactory.createLineSymbolizer(stroke, null);

    Rule rule = styleFactory.createRule();
    rule.symbolizers().add(sym);
    
    FeatureTypeStyle fts = styleFactory.createFeatureTypeStyle(new 
Rule[]{rule});
    Style style = styleFactory.createStyle();
    style.featureTypeStyles().add(fts);
    return style;
}

}


------------------------------------------------------------------------------
Return on Information:
Google Enterprise Search pays you back
Get the facts.
http://p.sf.net/sfu/google-dev2dev
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to