A,
Thanks for the info. Now that I discovered the JTS I guess I have
another question. My original idea was to walk around my farm with a
gps unit, take points on the corner of a building, put them in my
software convert them to UTM and then plot them on a canvas. This is
so that I can do this all over my farm mapping greenhouses,
vegetables fields and every fruit tree. Since some will be square
(ie buildings) and some will be circles (ie trees) and some will not
be defined shapes ( access paths) is my approach still valid? Do you
have abetter way to create a map of all the objects in my farm using
JTS?
Also in the code you gave you say:
Call convertToUTM() on your Geometries to convert them accordingly.
so if i have 4 sets of gps coordinates representing the corners of a
building, how should I call convertToUTM?
thanks so much for the help
On 14/01/2009, at 5:34 AM, Alexandre Pretyman wrote:
Ari,
Geometry is a class from the popular Java Topology Suite (JTS) -
jts-1.10.jar ( http://docs.codehaus.org/display/GEOTDOC/03+JTS+Topology+Suite
), NoSuchAuthorityCodeException and FactoryException is from the
geoapi jar (I use geoapi-2.2-M1.jar), CRS is from the referencing
jar (I use gt-referencing-2.5-RC1.jar). The imports are these:
import org.apache.log4j.Logger;
import org.geotools.factory.Hints;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
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 com.vividsolutions.jts.geom.PrecisionModel;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.linearref.LinearLocation;
import com.vividsolutions.jts.linearref.LocationIndexedLine;
you will need the epsg-hsql-2.5-SNAPSHOT.jar and probably the gt-
api-2.5-RC1.jar, gt-main-2.5-RC1.jar, gt-metadata-2.5-RC1.jar. I'm
not sure these are the latest versions available for the respetive
jars, but it is what maven downloaded for me with the following pom
dependencies (you will know what the pom dependencies are if you
follow the first project tutorial at the wiki: http://docs.codehaus.org/display/GEOTDOC/03+First+Project
)
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>2.5-RC1</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>epsg-hsql</artifactId>
<version>2.5-SNAPSHOT</version>
</dependency>
If anyone sees that these versions are incorrect, please shout out (-:
Regards,
Alexandre Pretyman
On Mon, Jan 12, 2009 at 8:09 PM, ari fainchtein
<[email protected]> wrote:
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
------------------------------------------------------------------------------
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