object = transformer.transform(geometry); } catch (TransformException e) { String msg = "Error occured transforming " + geometry.toString();
geoms[i] = transform(gc.getGeometryN(i));
private Geometry invertGeometryCoordinates(Geometry geometry) throws TransformException { return transformer.transform(geometry); }
SimpleFeature reproject(SimpleFeature feature) throws IOException { List<Object> attributes = feature.getAttributes(); for (int i = 0; i < attributes.size(); i++) { Object object = attributes.get(i); if (object instanceof Geometry) { // do the transformation Geometry geometry = (Geometry) object; try { attributes.set(i, tx.transform(geometry)); } catch (TransformException e) { String msg = "Error occured transforming " + geometry.toString(); throw (IOException) new IOException(msg).initCause(e); } } } try { return SimpleFeatureBuilder.build(schema, attributes, feature.getID()); } catch (IllegalAttributeException e) { String msg = "Error creating reprojeced feature"; throw (IOException) new IOException(msg).initCause(e); } }
/** * Transforms the geometry using the default transformer. * * @param geom The geom to transform * @param transform the transform to use during the transformation. * @return the transformed geometry. It will be a new geometry. * @throws MismatchedDimensionException if the geometry doesn't have the expected dimension for * the specified transform. * @throws TransformException if a point can't be transformed. */ public static Geometry transform(final Geometry geom, final MathTransform transform) throws MismatchedDimensionException, TransformException { final GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(transform); return transformer.transform(geom); }
SimpleFeature reproject(SimpleFeature feature) throws IOException { List<Object> attributes = feature.getAttributes(); for (int i = 0; i < attributes.size(); i++) { Object object = attributes.get(i); if (object instanceof Geometry) { // do the transformation Geometry geometry = (Geometry) object; try { attributes.set(i, tx.transform(geometry)); } catch (TransformException e) { String msg = "Error occured transforming " + geometry.toString(); throw (IOException) new IOException(msg).initCause(e); } } } try { return SimpleFeatureBuilder.build(schema, attributes, feature.getID()); } catch (IllegalAttributeException e) { String msg = "Error creating reprojeced feature"; throw (IOException) new IOException(msg).initCause(e); } } }
for (int i = 0; i < attributes.length; i++) { if (attributes[i] instanceof Geometry) { attributes[i] = transformer.transform((Geometry) attributes[i]);
for (int i = 0; i < attributes.length; i++) { if (attributes[i] instanceof Geometry) { attributes[i] = transformer.transform((Geometry) attributes[i]);
private Geometry invertGeometryCoordinates(Geometry geometry) throws TransformException { return transformer.transform(geometry); }
@Test public void testFlipAxisOnCurved() throws Exception { CircularString cs = (CircularString) new CurvedGeometryFactory(0.1).createCurvedGeometry(2, 0, 1, 1, 2, 2, 1); GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer(); CoordinateReferenceSystem crsLatLon = CRS.decode("urn:ogc:def:crs:EPSG:4326"); CoordinateReferenceSystem crsLonLat = CRS.decode("EPSG:4326", true); transformer.setCoordinateReferenceSystem(crsLatLon); MathTransform trans = CRS.findMathTransform(crsLonLat, crsLatLon); transformer.setMathTransform(trans); Geometry transformed = transformer.transform(cs); assertTrue(transformed instanceof CircularString); CircularString tcs = (CircularString) transformed; assertArrayEquals(new double[] {1, 0, 2, 1, 1, 2}, tcs.getControlPoints(), 0d); }
@Test public void testFullTransformOnCurved() throws Exception { CircularString cs = (CircularString) new CurvedGeometryFactory(0.1).createCurvedGeometry(2, 0, 1, 1, 2, 2, 1); GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer(); CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:3857"); CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:4326", true); transformer.setCoordinateReferenceSystem(targetCRS); MathTransform trans = CRS.findMathTransform(sourceCRS, targetCRS); transformer.setMathTransform(trans); Geometry transformed = transformer.transform(cs); assertFalse(transformed instanceof CircularString); LineString ls = (LineString) transformed; assertTrue(ls.getCoordinateSequence().size() > 3); }
/** * Check transformation correctness by transforming forwards and backwards using inverse * MathTransforms. * * @param g * @throws TransformException */ private void checkTransform(Geometry g) throws TransformException { GeometryCoordinateSequenceTransformer gcsTrans = new GeometryCoordinateSequenceTransformer(); gcsTrans.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); MathTransform trans = ProjectiveTransform.createTranslation(2, 100); gcsTrans.setMathTransform(trans); GeometryCoordinateSequenceTransformer gcsTransInv = new GeometryCoordinateSequenceTransformer(); gcsTransInv.setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84); MathTransform transInv = ProjectiveTransform.createTranslation(2, -100); gcsTransInv.setMathTransform(transInv); Geometry gTrans = gcsTrans.transform(g); Geometry g2 = gcsTransInv.transform(gTrans); // result better be a different geometry assertTrue(g != g2); assertTrue(hasSameValuesAndStructure(g, g2)); }
public void testNormal() throws Exception { Iterator reproject = new ReprojectFeatureResults(delegate, target).iterator(); Iterator reader = delegate.iterator(); while (reader.hasNext()) { SimpleFeature normal = (SimpleFeature) reader.next(); SimpleFeature reprojected = (SimpleFeature) reproject.next(); Point p1 = (Point) normal.getAttribute("defaultGeom"); Point p2 = (Point) reprojected.getAttribute("defaultGeom"); if (p1 != null) { assertEquals(crs, p1.getUserData()); assertEquals(target, p2.getUserData()); p1 = (Point) transformer.transform(p1); assertTrue(p1.equalsExact(p2)); } else { assertNull(p2); } LineString l1 = (LineString) normal.getAttribute("otherGeom"); LineString l2 = (LineString) reprojected.getAttribute("otherGeom"); if (l1 != null) { l1 = (LineString) transformer.transform(l1); assertTrue(l1.equalsExact(l2)); } else { assertNull(l2); } } }
protected Geometry transform(GeometryCoordinateSequenceTransformer transformer, Geometry source) { try { return transformer.transform(source); } catch (TransformException e) { LOGGER.log(Level.FINER, e.getMessage(), e); } return source; }
private Geometry transformGeometry(Geometry geometry) { if (isGeographic) { try { return transformer.transform(geometry); } catch (TransformException e) { LOGGER.log(Level.FINER, e.getMessage(), e); } } return geometry; }
protected Geometry transform(GeometryCoordinateSequenceTransformer transformer, Geometry source) { try { return transformer.transform(source); } catch (TransformException e) { LOGGER.log(Level.FINER, e.getMessage(), e); } return source; } }
public void testNormal() throws Exception { SimpleFeatureIterator reproject = new ReprojectingFeatureCollection(delegate, target).features(); SimpleFeatureIterator reader = delegate.features(); try { while (reader.hasNext()) { SimpleFeature normal = (SimpleFeature) reader.next(); SimpleFeature reprojected = (SimpleFeature) reproject.next(); Point p1 = (Point) normal.getAttribute("defaultGeom"); Point p2 = (Point) reprojected.getAttribute("defaultGeom"); if (p1 != null) { p1 = (Point) transformer.transform(p1); assertTrue(p1.equalsExact(p2)); } else { assertNull(p2); } LineString l1 = (LineString) normal.getAttribute("otherGeom"); LineString l2 = (LineString) reprojected.getAttribute("otherGeom"); if (l1 != null) { l1 = (LineString) transformer.transform(l1); assertTrue(l1.equalsExact(l2)); } else { assertNull(l2); } } } finally { reproject.close(); reader.close(); } }
expectedGeometry = transformer.transform(expectedGeometry);
private Geometry transformGeometry(Geometry sourceGeom, CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCrs) throws FactoryException, TransformException { MathTransform mt = CRS.findMathTransform(sourceCrs, targetCrs, true); GeometryCoordinateSequenceTransformer gcst = new GeometryCoordinateSequenceTransformer(); gcst.setMathTransform(mt); return gcst.transform(sourceGeom); }
private Geometry transformGeometry(Geometry sourceGeom, CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCrs) throws FactoryException, TransformException { MathTransform mt = CRS.findMathTransform(sourceCrs, targetCrs, true); GeometryCoordinateSequenceTransformer gcst = new GeometryCoordinateSequenceTransformer(); gcst.setMathTransform(mt); return gcst.transform(sourceGeom); }