All,

   I'm relatively new to GeoTools, and trying to find my way around.  
Hopefully others can help me out.

   After much API hunting, I believe I have constructed something that can 
go from WGS-84 Lat/Lon/Alt to XY Mercator projection.  (I might have the 
technical naming of that wrong, essentially, I'd like to project from LLA 
points that are in WGS-84 and render them on a screen, so I want to 
projection them onto any old Cartesian coordinate system which I can then 
translate into screen coordinates).

   Below is the sample code.  What I really want to do is render portions of 
the to a screen.  So I'd like to convert from Lat/Lon to Pixels.  I tried to 
use DISPLAY_X/DISPLAY_Y instead of X/Y for the axis of the Cartesian system. 
  That threw an exception about the DISPLAY_RIGHT axis.  I gave up on that 
thinking I could finish it another way.

   I now have the X/Y coordinates from the conversion, but I'm fuzzy on what 
the actual numbers mean.  Can I just use the upper left and lower right 
coordinates to linearly interpolate to pixels?  So if the Upper Left is at,
(40, 0) and the Lower right is at (0, 40) which translates to:
(4452779.631730943, 7.081154551613622E-10) and (0.0, 4838471.398061137).

If the screen is 320x400 can I translate just use interpolation of a point 
like LL of (20, 20) ->
xy of (2226389.8158654715, 2258423.6490963805), and then interpolate to 
figure out where
each point is?

Something like this essentially:
(toXY(point) - toXY(lowerRight))/ (toXY(UpperLeft) - toXY(lowerRight)) * 
heightInPixels;

I probably have to account for wrap around cases and other nasties, but 
that's par for the course in rendering the earth onto a 2D surface.

So the y screen coordinate of 20 is something like:
(2258423.6490963805 - 7.081154551613622E-10) / (2258423.6490963805 - 
7.081154551613622E-10) * 200 ~= 93.35 pixels.


(Knowing that I have to account for the Y being up in one direction in the 
XY and down in the screen coordinate system).

Is that math "right"?  It's not at all obvious to me that it is correct.  It 
seems fairly reasonable to me, but I'm not exactly sure what the values 
coming out of the transformation means.

Curious if there is an easier way that I can use to put use the 
Transformation in GeoTools to do more of the heavy lifting for me.  This 
seems like something a GridCoverage or some such would have to have done at 
some point.  Maybe I should go take a look at that.

Thanks in Advance,
    Kirby

Here is the sample code to show how I'm constructing the transformations, it 
was used to do the above conversions from LL to XY.  If is is correct it'd 
be nice to know, if it's wrong that'd be even better.  Finding sample code 
via Google for this was difficult.  The couple of things that did come up 
were nice descriptions, but no complete source listings.  Until I included 
"Mercator_SP1" in my Google search, I couldn't find honest to goodness 
complete and runnable code that did this.


import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geotools.referencing.FactoryFinder;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.referencing.cs.DefaultCartesianCS;
import org.geotools.referencing.cs.DefaultCoordinateSystemAxis;
import org.geotools.referencing.factory.FactoryGroup;
import org.geotools.referencing.operation.DefaultMathTransformFactory;
import org.geotools.referencing.operation.DefiningConversion;
import org.opengis.parameter.ParameterValueGroup;
import org.opengis.referencing.IdentifiedObject;
import org.opengis.referencing.crs.ProjectedCRS;
import org.opengis.referencing.cs.CartesianCS;
import org.opengis.referencing.operation.CoordinateOperation;
import org.opengis.referencing.operation.CoordinateOperationFactory;
import org.opengis.referencing.operation.MathTransform;

/**
* Simple sample code to test how to use GeoTools to project from
* WGS-84 Lat/Lon to Mercator XY.
*
* @author kbohling
* @dateCreated Mar 12, 2007
*/
public class ProjectionSample {

    /**
     * The driver for the sample code.
     *
     * This code should be "cookbook" style on how to convert from an WGS84 
to XY using
     * the a Mercator projection using the GeoTools/GeoAPI libraries.
     *
     * This <a 
href="http://geotools.codehaus.org/TransformationConsole";>code</a>
     * appears to be higher quality sample code that does roughtly the same
     * thing.  I couldn't find it until after implementing this when I used 
the
     * keywords "GeoTools Mercator_SP1 transform".  Until I got done with 
this,
     * no idea that Mercator_SP1 was important (using Mercator instead 
doesn't
     * get me there).
     *
     * @param args Ignored.
     */
    public static void main(String[] args) {
        try {

            // Want to use "DefaultCoordinateSystemAxis.DISPLAY_X" and 
"DefaultCoordinateSystemAxis.DISPLAY_Y", but
            // that doesn't appear to work, an exception is thrown.

            // Construct a Cartesian coordinate system (Presumably, there is 
one of these around someplace, but oh well finding
            // it is harder then constructing it.
            CartesianCS cartesianCS = new DefaultCartesianCS("screen", 
DefaultCoordinateSystemAxis.X, DefaultCoordinateSystemAxis.Y);

            // WGS84 are the coordinates to convert to and from.
            DefaultGeographicCRS wgs84 =  DefaultGeographicCRS.WGS84;

            // Grab the transformFactory, we'll need it later.
            DefaultMathTransformFactory mathXFormFactory = new 
DefaultMathTransformFactory();

            /*
             * Use the following command to find out about all of the 
available projections, they should
             * be printed out onto stdout.
             *
             * DefaultMathTransformFactory.main(new String[] { 
"-projections" });
             *
             * It appears that the "Identifier" column, or using "EPSG:XXXX" 
works, did not verify
             * with the other names or not.
             *
             * Use:
             *
             * DefaultMathTransformFactory.main(new String[] { 
"Mercator_1SP" });
             *
             * To print out more of the "magic strings" that can be added to 
the parameter group.
             *
             * See this e-mail for more information:
             * 
http://www.arcknowledge.com/gmane.comp.gis.geotools2.user/2006-08/msg00090.html
             *

             *
             */
            String projectionType = "Mercator_1SP";
            ParameterValueGroup pg = 
mathXFormFactory.getDefaultParameters(projectionType);

            // Construct a "conversion" with this parameter group.
            DefiningConversion conversion = new DefiningConversion("Mercator 
Conversion", pg);

            FactoryGroup factoryGroup = FactoryGroup.createInstance(null);

            // Whenever you create something you must give it a name.
            Map<String, Object> props = new HashMap<String, Object>();
            props.put(IdentifiedObject.NAME_KEY, "projected crs name");

            // Construct a Projected Coordinate Reference System named from 
the given properties,
            // and translates from WGS84 to our cartesian coordinate system.
            ProjectedCRS myCRS = factoryGroup.createProjectedCRS(props, 
wgs84, conversion, cartesianCS);

            // Pull the operation factory, so we can generate things that go 
back and forth between
            // WGS84 (LLA) and our cartesian system (XY).
            CoordinateOperationFactory coFactory = 
FactoryFinder.getCoordinateOperationFactory(null);

            // Create the backwards and forwards operations.
            CoordinateOperation op = coFactory.createOperation(wgs84, 
myCRS);
            MathTransform forward = op.getMathTransform();
            MathTransform inverse = forward.inverse();

            double[] lla = new double[] { 40.0, 40.0 };
            double[] xy = new double[2];

            // The offset into the lla to start converting at.
            int llaOffset = 0;

            // The offset into the xy to store the converted values at.
            int xyOffset = 0;

            // The number of point (not coordinates to convert).
            int numPoints = 1;

            // Go from LLA to XY.
            forward.transform(lla, llaOffset, xy, xyOffset, numPoints);
            System.out.println("xy: " + Arrays.toString(xy));

            // Go from the newly constructed XY to LLA.
            inverse.transform(xy, xyOffset, lla, llaOffset, numPoints);
            System.out.println("LLA: " + Arrays.toString(lla));

        } catch (Throwable t) {

            
Logger.getLogger(ProjectionSample.class.getName()).log(Level.INFO, "Caught 
exception: ", t);
        }
    }

}

_________________________________________________________________
Rates near 39yr lows!  $430K Loan for $1,399/mo - Paying Too Much? Calculate 
new payment 
http://www.lowermybills.com/lre/index.jsp?sourceid=lmb-9632-18226&moid=7581


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to