Howdy,

Over the last couple of days I've been working on displaying graphics like 
Circle and lines on a JMapPane

In the below example I have the following:
1) my example integrates a SWT/AWT bridge.  so to run it you will need proper 
eclipse jars
2) extended JMapPane for Override of paintComponent for drawing graphics with 
out impacting mapContext-map.  The example draws connected lines via mouse 
clicks.
3) I also utilize the createCircle method from the Create Geometry section of 
the site:
http://docs.codehaus.org/display/GEOTDOC/01+How+to+Create+a+Geometry#01HowtoCreateaGeometry-Arcs%2CCirclesandCurves
to create a circle and display it in its own Layer.

In ESRI there is the ability to have an AcetateLayer defined and draw Graphics 
on it Like Squares/Rectangles/Arcs etc...  using Graphics2D


How would I go about building the option for an AcetateLayer in geotools and 
displaying Graphics on it?
Would it be really harry to integrate the AcetateLayer as an option into a 
mapContext?
Is option 3) above my only realistic solution for getting graphics into a 
specific Layer that can be displayed/manipulated?

I'm willing to do the work to integrate an AcetateLayer option and post to the 
forum.  I just need your expert direction to get 'er done :-)

Thanks,
Oliver


package org.geotools.demo;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.util.Iterator;


import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.GTRenderer;
import org.geotools.renderer.lite.StreamingRenderer;
import org.geotools.styling.StyleFactory;
import org.geotools.swing.JMapPane;
import org.geotools.swing.event.MapMouseAdapter;
import org.geotools.swing.event.MapMouseEvent;
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.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.Point;
import com.vividsolutions.jts.geom.Polygon;

public class View2 extends Composite {

    static StyleFactory styleFactory = 
CommonFactoryFinder.getStyleFactory(null);
    static FilterFactory filterFactory = 
CommonFactoryFinder.getFilterFactory(null);
    static CoordinateReferenceSystem crs = DefaultGeographicCRS.WGS84;
    static final int MAX_NAME_LENGTH = 20;
    static MapContext map;
    static MyPane mpane;

    public View2(Composite parent) {
        this(parent, SWT.NONE);  // must always supply parent
    }
 
    public View2(Composite parent, int style) {
        super(parent, style);
        createPartControl(parent);
        
    }

    public void createPartControl(Composite parent) {
         
        Composite composit = new Composite(parent, SWT.EMBEDDED | 
SWT.NO_BACKGROUND);
 
        Frame awtFrame = SWT_AWT.new_Frame(composit);
 
        Panel awtPanel = new Panel();
        
        awtPanel.setLayout(new BorderLayout());
                     
        //GEOTOOLS MAP GENERATION BRUT FORCE!!!
            try {
            final SimpleFeatureType TYPE = DataUtilities.createType("Location", 
"location:Point,name:String"); // see createFeatureType();
            FeatureCollection<SimpleFeatureType, SimpleFeature> collection = 
FeatureCollections.newCollection(); 
            
            //DATA
            double lon[] = {123.31, 12, 20.222};
            double lat[] = {48.4, 52, 30.333};
            String name[] = { "Point1", "Point2", "Point3" };
             //Define Collection
             GeometryFactory factory = 
JTSFactoryFinder.getGeometryFactory(null);    
            
             for(int i=0;i<=2;i++){            
                    Point point = factory.createPoint( new 
Coordinate(lon[i],lat[i]));
                    SimpleFeature feature = SimpleFeatureBuilder.build( TYPE, 
new Object[]{point, name[i]}, null );
                    collection.add( feature );
             }
             
             //The below creates a Geometry Circle
            SimpleFeature featurePoly = 
SimpleFeatureBuilder.build((SimpleFeatureType)DataUtilities.createType("Location",
 "Location:Polygon,name:String"), new 
Object[]{createCircle(factory,10.00,10.00,20.00),""},null);
            FeatureCollection<SimpleFeatureType, SimpleFeature> collection2 = 
FeatureCollections.newCollection();
            collection2.add(featurePoly);
            FeatureSource<SimpleFeatureType, SimpleFeature> featureSource2 = 
DataUtilities.source(collection2);
            //end create Geometry Circle
             
            
            
            
            
             //Define FeatureSource
             FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = 
DataUtilities.source(collection);        

             map = new DefaultMapContext();
             map.setTitle("The Point");
             map.addLayer(featureSource, null);     
             map.setTitle("The Circle");
             map.addLayer(featureSource2,null);
             
             
             //Iterate Collection
             FeatureIterator<SimpleFeature> iterator = collection.features();
             try {
                 while (iterator.hasNext()) {
                     SimpleFeature feature = iterator.next();

                     Geometry geometry = (Geometry) 
feature.getDefaultGeometry();
                     
                     Coordinate[] coords = geometry.getCoordinates();
                     for( int i = 0; i < coords.length; i++ ) {
                         
System.out.println("================================================");
                         System.out.println("Actual\t x: "+coords[i].x + " \ty: 
" + coords[i].y);
                     }
                 }
             }
             finally {
                 if( iterator != null ){
                     // YOU MUST CLOSE THE ITERATOR!
                     iterator.close();
                 }
             }
            } catch (Exception e){
                
            }
            //END GEOTOOLS MAPBUILD BRUT FORCE
            
            mpane = new MyPane(new StreamingRenderer(), map);

            
            
            mpane.addMouseListener(new MapMouseAdapter() {
                @Override
                public void onMouseClicked(MapMouseEvent ev) {
                    //DirectPosition2D p = ev.getMapPosition();
                    java.awt.Point p = ev.getPoint();
                    if(mpane.getP()==null){
                        mpane.setP(p);
                    } else if(mpane.getP()!=null){
                        mpane.setP(p);
                        mpane.repaint();
                    }
                }
            });
            
            awtPanel.add(mpane,BorderLayout.CENTER);
            
            awtFrame.setSize(200,400);
            awtFrame.add(awtPanel); 
    }
    private static Geometry createCircle(GeometryFactory factory, double x, 
double y, final double RADIUS) {
        final int SIDES = 32;
        Coordinate coords[] = new Coordinate[SIDES+1];
        for( int i = 0; i < SIDES; i++){
            double angle = ((double) i / (double) SIDES) * Math.PI * 2.0;
            double dx = Math.cos( angle ) * RADIUS;
            double dy = Math.sin( angle ) * RADIUS;
            coords[i] = new Coordinate( (double) x + dx, (double) y + dy );
        }
        coords[SIDES] = coords[0];

        LinearRing ring = factory.createLinearRing( coords );
        Polygon polygon = factory.createPolygon( ring, null );

        return polygon;
    }
 
    
    public static void main(String[] args) {
        // the display allows access to the host display device
        final Display display = new Display();

        // the shell is the top level window (AKA frame)
        final Shell shell = new Shell(display);
        shell.setText("Example SWT Shell");   // Give me a title

        // all children split space equally
        shell.setLayout( new FillLayout());
        
        View2 basic = new View2(shell);

        shell.pack();       // make shell take minimum size
        shell.open();       // open shell for user access

        // process all user input events until the shell is disposed (i.e., 
closed)
        while ( !shell.isDisposed()) {
            if (!display.readAndDispatch()) {  // process next message
                display.sleep();              // wait for next message
            }
        }
        display.dispose();  // must always clean up
    }
}

class MyPane extends JMapPane {
    
    private java.awt.Point p1;
    
    private java.util.List<java.awt.Point> pList = new 
java.util.ArrayList<java.awt.Point>();
    
    public MyPane(GTRenderer renderer, MapContext context) {
        super(renderer, context);
    }

    
    public void setP(java.awt.Point point){
        this.p1=point;
        pList.add(point);
    }
    
    
    
    public java.awt.Point getP(){
        return this.p1;
    }
        
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        java.awt.Point dp1 = new java.awt.Point();
        java.awt.Point dp2 = new java.awt.Point();
        if(p1!=null){
            Iterator<java.awt.Point> pi = pList.iterator();
            while (pi.hasNext()){
                if(pList!=null && pList.size()>0){
                    dp1=dp2;
                } else {
                    dp1=pi.next();
                }
                if(pi.hasNext()){
                    dp2=pi.next();
                    g.drawLine((int)dp1.getX(), (int)dp1.getY(), 
(int)dp2.getX(), (int)dp2.getY());
                }    
            }
           }
    }    
}

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to