Just getting into using the GeoTools framework, after having done a lot of things manually.

One of my previously-written functions transforms a given lat/lon/height between CRS.

I found this link: How to Transform a Geometry

I have generating my custom CRSs set up using WKT text and all - that's fine. But I'm not 100% about the Geometry part. Referenced in the JTS.transform(sourceGeometry, transform) line.

I have what I THOUGHT was what it was expecting:

GeometryBuilder builder = new GeometryBuilder(sourceHCRS);
Point sourceGeometry = builder.createPoint(34.5678, -77.45343);

But I'm getting an error on the JTS.transform that reads:

Error: no suitable method found for transform(Point,MathTransform)
 method JTS.transform(Envelope,MathTransform) is not applicable (argument mismatch; Point cannot be converted to Envelope)
 method JTS.transform(Geometry,MathTransform) is not applicable (argument mismatch; Point cannot be converted to Geometry)

Now, Point is apparently a superclass of Geometry, so I figured that would work. I've dug down enogh to realize there are TWO classes of 'Geometry' - org.locationtech.jts.geom.Geometry which is what JTS.transform() is expecting, and org.opengis.geometry.Geometry, which is what Point is superclassing.

Searching around I haven't been able to deduce how to generate a Geometry with my location/point in it to send to the transform().


I think (as you suspect) you are mixing the two types of Geometry used by GeoTools. org.opengis.geometry.Geometry is an OGC geometry as defined by GeoAPI which is useful for interacting with other libraries that implement GeoAPI. org.locationtech.jts.geom.Geometry is a JTS geometry and is what GeoTools uses as it's default internal geometry. In general, you can ignore the OGC geometries in day to day use.

So what you actually want to do to create your point is:

import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.opengis.geometry.MismatchedDimensionException;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

GeometryFactory geomFactory = new GeometryFactory();
Point point = geomFactory.createPoint(new Coordinate(0.0, 50.0));
CoordinateReferenceSystem src = CRS.decode("EPSG:31287");
CoordinateReferenceSystem dest = CRS.decode("EPSG:4326");
MathTransform transform = CRS.findMathTransform(src, dest, false);
Geometry forward = JTS.transform(point, transform);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.