Hang on a minute I think I'm missing the obvious - I still have to
select a sensible UTM projection don't I as the destination CRS don't I?

 

DefaultGeocentricCRS.CARTESIAN was never going to work....

 

________________________________

From: Douglas Waights [mailto:[email protected]] 
Sent: 05 December 2009 14:24
To: [email protected]
Subject: [Geotools-gt2-users] new user struggling with simple tramsforms
andlat, lon to x, y

 

Hi,

I'm new to GeoTools and i'm getting myself into a bit of a tangle even
though i thought i had a reasonable handle on coordinate reference
systems...

I have a map application which currently works onan arbitary xy grid.
This map has no CRS attached to it and currently assumes any data loaded
in by the user is internally consistent. I would now like this be
capable of rendering culture data (shape files). To begin with, the
assumption is that the CRS of the shape file (yes it has the .prj file)
is the same as that of the data which is currently loaded into the
arbitrary x,y grid. Therefore all i want to do at the moment is project
this onto my grid. At some point i will make the application CRS aware
and so transform the shape file (should the transform be possible) into
the CRS of the application before displaying on the grid.

The code ive been playing around with is below along with the output it
generates...

So what is the correct/best way to convert my lat, lons to xy?

Do I want to be creating a transform from source to
DefaultGeocentricCRS. CARTESIAN?

With the first transform, I'm confused about the data in the array
(whether it needs the z dimension or not) and the output i'm getting
from tit which looks a bit odd to me.

I'm also confused as to how to use the second transform with the
DirectPosition which is currently blowing up as you'll see from the
below.

The commented out line was where I wondered if i could  get a transform
after doing CRS.getProjected(crs) but this returned null...

Apologies as this is all a bit noddy, but i'm beginning to get
frustrated and cant figure out whether what i'm getting is nonsense or
if i'm even doing things the right way even despite looking at the
docmentation and trawling the user group emails.

Thanks for you help

Doug

******************************

********************* CODE
************************************************************************
***


public static void main( String[] args )
    {
        System.out.println( "Hello GeoTools:" + GeoTools.getVersion() );
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        
        try
        {
            FileDataStore store =
FileDataStoreFinder.getDataStore(file);
            FeatureSource featureSource = store.getFeatureSource();
            ReferencedEnvelope envelope = featureSource.getBounds();
            
            CoordinateReferenceSystem crs =
envelope.getCoordinateReferenceSystem();
            
            double minLon = envelope.getMinX();
            double maxLon = envelope.getMaxX();
            double minLat = envelope.getMinY();
            double maxLat = envelope.getMaxY();
 
System.out.println(envelope.getCoordinateReferenceSystem().toWKT());
            
            
            System.out.println(CRS.lookupIdentifier(crs, true));
            String code = CRS.lookupIdentifier(crs, true);
            crs = CRS.decode(code);
            System.out.println(crs.toWKT());
           
            CoordinateReferenceSystem projected =
CRS.getProjectedCRS(crs);
            
            MathTransform mathTransform = CRS.findMathTransform(
crs,DefaultGeocentricCRS.CARTESIAN,true);
//            MathTransform mathTransform = CRS.findMathTransform(
crs,projected);
            
            System.out.println();
            double[] sphericalPoints = new double[]
                                                 {minLon,maxLat,0,
                                                  maxLon,maxLat,0,
                                                  maxLon,minLat,0,
                                                  minLon,minLon,0,
                                                 };
            
            double[] result = new double[12];
             mathTransform.transform(sphericalPoints, 0,result, 0, 4);

            for(int i=0; i<result.length;i++)
            {
                System.out.println(result[i]);
            }
            
            System.out.println();
            DirectPosition dp = new DirectPosition2D(crs, maxLon,
minLat);
            double[] coordinates = dp.getCoordinate();
            for(int i=0; i<coordinates.length;i++)
            {
                System.out.println(coordinates[i]);
            }
         
          DirectPosition newdp = new DirectPosition2D(0,0);
          mathTransform.transform(dp, dp);
          
          coordinates = newdp.getCoordinate();
          for(int i=0; i<coordinates.length;i++)
          {
              System.out.println(coordinates[i]);
          }
          
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (FactoryException e)
        {
            e.printStackTrace();
        }
        catch (MismatchedDimensionException e)
        {
            e.printStackTrace();
        }
        catch (TransformException e)
        {
            e.printStackTrace();
        }
        
    }

/**************************************************** OUTPUT
***********************************************************************
Hello GeoTools:2.6.0
GEOGCS["GCS_European_1950", 
  DATUM["D_European_1950", 
    SPHEROID["International_1924", 6378388.0, 297.0]], 
  PRIMEM["Greenwich", 0.0], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Longitude", EAST], 
  AXIS["Latitude", NORTH]]
EPSG:4230
GEOGCS["ED50", 
  DATUM["European Datum 1950", 
    SPHEROID["International 1924", 6378388.0, 297.0,
AUTHORITY["EPSG","7022"]], 
    TOWGS84[-157.89, -17.16, -78.41, 2.118, 2.697, -1.434,
-1.1097046576093785], 
    AUTHORITY["EPSG","6230"]], 
  PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], 
  UNIT["degree", 0.017453292519943295], 
  AXIS["Geodetic latitude", NORTH], 
  AXIS["Geodetic longitude", EAST], 
  AUTHORITY["EPSG","4230"]]

2861049.1088070613
5631542.099018573
-881846.7512652623
6367755.58081158
365251.52841702907
-157.92197895122462
2896775.777359277
-95.45411266252938
5663350.799459978
4040401.453112458
4921879.397968464
362780.3806419499

3.2833333600000003
50.61666667
org.opengis.geometry.MismatchedDimensionException: Argument "ptDst" has
2 dimensions, while 3 was expected.
    at
org.geotools.referencing.operation.transform.AbstractMathTransform.trans
form(AbstractMathTransform.java:220)
    at
org.geotools.referencing.operation.transform.ConcatenatedTransform.trans
form(ConcatenatedTransform.java:416)
    at org.geotools.demo.App.main(App.java:87)



This email is from Ikon Science Ltd.  It may contain
confidential or privileged information and is for use only by
the intended recipient(s).  If you have received it in error,
please reply and delete all copies from your system.  It is
your responsibility to conduct your own virus checks on all
parts of this email.
------------------------------------------------------------------------------
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

Reply via email to