A,
Thanks for the code. What version are you running? I am using 2.5.1 and objects like Geometry, NoSuchAuthorityCodeException, FactoryException, etc don't seem to be part of it. I tried looking in the javadoc and those objects dont seem to be there either. Could you send me your package statements?

thanks

On 13/01/2009, at 3:23 AM, Alexandre Pretyman wrote:

Hi Ari,

Try the following code and see if it suffice your needs, it is what I wrote to convert from WGS84 to UTM, I'm not sure this is the best method, but has been working for me so far.

=============================

/**
 * Conversion utility class to convert from WGS84 to UTM
 * @author Alexandre Walter Pretyman
 */

public class ConversionUtil
{
private static final Map<Integer, MathTransform> transformMap = new HashMap<Integer, MathTransform>();

private static Logger logger = Logger.getLogger(ConversionUtil.class);
        
        public static Geometry convertWGS84ToUTM(Geometry geom)
        {

                if (geom.getSRID() != 4326)
throw new RuntimeException("Geometry's SRID is not 4326 (WGS84), Geometry: " + geom);

                int utmzone = utmzone(geom.getCoordinate());
                final Integer utmZoneInteger = new Integer(utmzone);

                try
                {
                        synchronized (transformMap)
                        {
                                MathTransform transform = 
transformMap.get(utmZoneInteger);
                                if (transform == null)
                                {
CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:" + utmzone);
                                                transform = 
CRS.findMathTransform(wgs84CRS, targetCRS);
                                                
transformMap.put(utmZoneInteger, transform);
                                }
                                final Geometry transformedGeom = 
JTS.transform(geom, transform);
                                transformedGeom.setSRID(utmzone);
                                return transformedGeom;                         
                        }
                }
                catch (NoSuchAuthorityCodeException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
                catch (FactoryException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
                catch (MismatchedDimensionException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
                catch (TransformException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
        }

        /**
* Returns the EPSG id for the UTM zone of the given coordinate, this is ported from the code
     * http://wiki.postgis.org/support/wiki/index.php?plpgsqlfunctions
         * @param coord
         * @return
         */
        public static int utmzone(Coordinate coord)
        {
                int pref;
                if (coord.y > 0)
                {
                        pref = 32600;
                }
                else
                {
                        pref = 32700;
                }
                final int zone = (int) Math.floor((coord.x+180)/6)+1;
                return zone + pref;
        }
        
        static CoordinateReferenceSystem wgs84CRS;
        static CRSAuthorityFactory factory;
        static {

                try
                {
Hints hints = new Hints(Hints.FORCE_LONGITUDE_FIRST_AXIS_ORDER, Boolean.TRUE);
                        factory = CRS.getAuthorityFactory(true);
                                        
                        wgs84CRS = 
factory.createCoordinateReferenceSystem("EPSG:4326");
                }
                catch (NoSuchAuthorityCodeException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
                catch (FactoryException e)
                {
                        logger.error(e.getMessage(), e);
                        throw new RuntimeException(e);
                }
        }
}

=============================

Call convertToUTM() on your Geometries to convert them accordingly.
There is a problem with this method though, the GeometryFactory of the Geometries created by JTS.transform(geom, transform); have the SRID set to 0, so even I explicitly set them with transformedGeom.setSRID(utmzone); spatial function called on the geometry (for example, buffer), generate other geometries with SRID set to 0, but this is something I have not yet managed to find a solution for.

Regards,
Alexandre Pretyman

On Mon, Jan 12, 2009 at 11:38 AM, Martin Desruisseaux <[email protected] > wrote:
ari fainchtein a écrit :
> Exception in thread "main" java.lang.IncompatibleClassChangeError:
> Implementing class

Exception ending in "Error" are usually a compilation problem. In this case, we lack information about the cause of this error - I don't know what is happening. But if you happen to have a chance to recompile GeoTools on your platform (Mac OS), it may be worth. If you have the source code and Maven installed, you only need to execute "mvn install" from the command-line at the root of source directory.

       Martin

------------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It is the best place to buy or sell services for
just about anything Open Source.
http://p.sf.net/sfu/Xq1LFB
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users


------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to