Hi,

Today I've been working on a simple forced collection example for world(coord) 
to screen(pixel) and screen(pixel) to world(coord)

I have 3 questions:
#1  Am I on the right track with the below methods getScreentoWorld and 
getWorldtoScreen
#2  The major question I have is regarding the Rectangle width and height.  Did 
I use a proper approach for that???
#3  One thing I noticed is that the JMapPane has a:
public AffineTransform getWorldToScreenTransform()
public AffineTransform getScreenToWorldTransform()
Are these usable in my getScreentoWorld and getWorldtoScreen methods?
It seems like since they are already present in JMapPane that use of them would 
be realistic.
I tried getting a direct call from the JMapPane to work with no luck.

The following is my OUTPUT:

================================================
Actual     x: 20.222     y: 30.333
Pixel     x: 44.31946815200793     Pixel y:     600.0000000000001
Coord     x: 20.222     Coord y:     30.332999999999995
================================================
Actual     x: 12.0     y: 52.0
Pixel     x: 0.0     Pixel y:     0.0
Coord     x: 11.999999999999998     Coord y:     52.0
================================================
Actual     x: 123.31     y: 48.4
Pixel     x: 600.0000000000001     Pixel y:     99.69077398809259
Coord     x: 123.31000000000002     Coord y:     48.4

It seems like my pixel values are way off.  Possibly explain....

The following is my EXAMPLE:

package org.geotools.demo;

import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.data.DataUtilities;
import org.geotools.data.FeatureSource;

import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.renderer.lite.RendererUtilities;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.JMapPane;

import org.geotools.factory.CommonFactoryFinder;
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.geometry.jts.ReferencedEnvelope;
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.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.io.IOException;

public class WorldScreen {
    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 JMapFrame mapFrame;
    
public static void main(String[] args) throws Exception {
    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 );
     }
     
     //Define FeatureSource
     FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = 
DataUtilities.source(collection);        

     map = new DefaultMapContext();
     map.setTitle("The Point");
     map.addLayer(featureSource, null);     
     mapFrame = new JMapFrame(map);
     mapFrame.setSize(600, 600);
     //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);
                //Get Pixel Value of Coordinates
                 Point2D pixelValue=getWorldtoScreen(coords[i].x, coords[i].y);
                 System.out.println("Pixel\t x: "+pixelValue.getX()+" \tPixel 
y: \t"+pixelValue.getY());
                 //Get Coordinates from Pixel
                 Point2D coordValue=getScreentoWorld(pixelValue.getX(), 
pixelValue.getY());
                 System.out.println("Coord\t x: "+coordValue.getX()+" \tCoord 
y: \t"+coordValue.getY());
             }
         }
     }
     finally {
         if( iterator != null ){
             // YOU MUST CLOSE THE ITERATOR!
             iterator.close();
         }
     }
     mapFrame.enableStatusBar(true);
     mapFrame.setVisible(true);
}

public static Point2D getWorldtoScreen(double x, double y){
    
    Rectangle imageBounds=null;
    ReferencedEnvelope mapBounds=null;
    try{
        mapBounds=map.getLayerBounds();
        imageBounds = mapFrame.getBounds();
        int width = (int)imageBounds.getWidth();
        int height = (int)imageBounds.getHeight();
    }catch(Exception e){
        
    } 
    
    
    AffineTransform world2screen =
    RendererUtilities.worldToScreenTransform(mapBounds, imageBounds);
    Point2D pointScreenAbsolute = new Point2D.Double(x, y);
    Point2D pointScreen = world2screen.transform(pointScreenAbsolute, null);
    return pointScreen;
}


public static Point2D getScreentoWorld(double x, double y) throws Exception {
    Rectangle imageBounds=null;
    ReferencedEnvelope mapBounds=null;
    try{
        mapBounds=map.getLayerBounds();
        imageBounds = mapFrame.getBounds();
        int width = (int)imageBounds.getWidth();
        int height = (int)imageBounds.getHeight();
    }catch(Exception e){
        
    } 
    
    AffineTransform world2screen =
    RendererUtilities.worldToScreenTransform(mapBounds, imageBounds);
    
    AffineTransform screen2world = world2screen.createInverse();
    Point2D pointScreenAbsolute = new Point2D.Double(x, y);
    Point2D pointScreen = screen2world.transform(pointScreenAbsolute, null); 
    return pointScreen;
} 
}


Thanks,
Oliver






------------------------------------------------------------------------------
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