transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(tx); transformers.put(crs, transformer); object = transformer.transform(geometry); } catch (TransformException e) { String msg = "Error occured transforming " + geometry.toString();
/** * 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)); }
/** * @param point * @throws TransformException */ public Point transformPoint(Point point, GeometryFactory gf) throws TransformException { // if required, init csTransformer using geometry's CSFactory init(gf); CoordinateSequence cs = projectCoordinateSequence(point.getCoordinateSequence()); Point transformed = gf.createPoint(cs); transformed.setUserData(point.getUserData()); return transformed; }
public ReprojectingFeatureCollection( SimpleFeatureCollection delegate, CoordinateReferenceSystem target) throws SchemaException, OperationNotFoundException, FactoryRegistryException, FactoryException { super(delegate); this.target = target; this.schema = FeatureTypes.transform(delegate.getSchema(), target); // create transform cache transformers = new HashMap(); // cache "default" transform CoordinateReferenceSystem source = delegate.getSchema().getCoordinateReferenceSystem(); if (source != null) { MathTransform tx = ReferencingFactoryFinder.getCoordinateOperationFactory(hints) .createOperation(source, target) .getMathTransform(); GeometryCoordinateSequenceTransformer transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(tx); transformers.put(source, transformer); } else { throw new RuntimeException( "Source was null in trying to create a reprojected feature collection!"); } }
public ReprojectFeatureIterator( FeatureIterator<SimpleFeature> reader, SimpleFeatureType schema, MathTransform transform) { this.reader = reader; this.schema = schema; transformer.setMathTransform(transform); // set hte target coordinate system transformer.setCoordinateReferenceSystem(schema.getCoordinateReferenceSystem()); }
protected void setUp() throws Exception { super.setUp(); target = CRS.parseWKT( "PROJCS[\"BC_Albers\",GEOGCS[\"GCS_North_American_1983\",DATUM[\"North_American_Datum_1983\",SPHEROID[\"GRS_1980\",6378137,298.257222101],TOWGS84[0,0,0]],PRIMEM[\"Greenwich\",0],UNIT[\"Degree\",0.017453292519943295]],PROJECTION[\"Albers_Conic_Equal_Area\"],PARAMETER[\"False_Easting\",1000000],PARAMETER[\"False_Northing\",0],PARAMETER[\"Central_Meridian\",-126],PARAMETER[\"Standard_Parallel_1\",50],PARAMETER[\"Standard_Parallel_2\",58.5],PARAMETER[\"Latitude_Of_Origin\",45],UNIT[\"Meter\",1],AUTHORITY[\"EPSG\",\"42102\"]]"); MathTransform2D tx = (MathTransform2D) ReferencingFactoryFinder.getCoordinateOperationFactory(null) .createOperation(crs, target) .getMathTransform(); transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(tx); transformer.setCoordinateReferenceSystem(target); }
private Geometry invertGeometryCoordinates(Geometry geometry) throws TransformException { return transformer.transform(geometry); }
public ReprojectingIterator( Iterator<SimpleFeature> delegate, MathTransform transform, SimpleFeatureType schema, GeometryCoordinateSequenceTransformer transformer) throws OperationNotFoundException, FactoryRegistryException, FactoryException { this.delegate = delegate; this.schema = schema; tx = transformer; tx.setMathTransform((MathTransform2D) transform); }
init(factory); transformed = transformPoint((Point) g, factory); } else if (g instanceof MultiPoint) { MultiPoint mp = (MultiPoint) g; points[i] = transformPoint((Point) mp.getGeometryN(i), factory); transformed = transformLineString((LineString) g, factory); } else if (g instanceof MultiLineString) { MultiLineString mls = (MultiLineString) g; lines[i] = transformLineString((LineString) mls.getGeometryN(i), factory); transformed = transformPolygon((Polygon) g, factory); } else if (g instanceof MultiPolygon) { MultiPolygon mp = (MultiPolygon) g; polygons[i] = transformPolygon((Polygon) mp.getGeometryN(i), factory); geoms[i] = transform(gc.getGeometryN(i));
public ReprojectingFeatureCollection( SimpleFeatureCollection delegate, CoordinateReferenceSystem source, CoordinateReferenceSystem target) { super(delegate); this.target = target; SimpleFeatureType schema = delegate.getSchema(); this.schema = reType(schema, target); if (source == null) { throw new NullPointerException("source crs"); } if (target == null) { throw new NullPointerException("destination crs"); } this.transform = transform(source, target); transformer = new GeometryCoordinateSequenceTransformer(); }
private static void transformFeatureCollection(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, CoordinateReferenceSystem targetCRS) throws TransformException { final GeometryCoordinateSequenceTransformer transform = FeatureUtils.getTransform(DefaultGeographicCRS.WGS84, targetCRS); final FeatureIterator<SimpleFeature> features = featureCollection.features(); final GeometryFactory geometryFactory = new GeometryFactory(); while (features.hasNext()) { final SimpleFeature simpleFeature = features.next(); final Point sourcePoint = (Point) simpleFeature.getDefaultGeometry(); final Point targetPoint = transform.transformPoint(sourcePoint, geometryFactory); simpleFeature.setDefaultGeometry(targetPoint); } }
/** * @param point * * @throws TransformException */ public Point transformPoint(Point point, GeometryFactory gf) throws TransformException { CoordinateSequence cs = projectCoordinateSequence(point.getCoordinateSequence()); return gf.createPoint(cs); }
public static GeometryCoordinateSequenceTransformer getTransform(CoordinateReferenceSystem sourceCrs, CoordinateReferenceSystem targetCrs) { GeometryCoordinateSequenceTransformer transformer; try { MathTransform transform = CRS.findMathTransform(sourceCrs, targetCrs, true); transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(transform); transformer.setCoordinateReferenceSystem(targetCrs); } catch (FactoryException e) { throw new IllegalStateException(e); } return transformer; }
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); } }
public PullParserFeatureReader( final Configuration wfsConfiguration, final InputStream getFeatureResponseStream, final FeatureType featureType, String axisOrder) throws IOException { this.inputStream = getFeatureResponseStream; this.featureType = featureType; this.axisOrder = axisOrder; this.parser = new PullParser( wfsConfiguration, getFeatureResponseStream, new QName( featureType.getName().getNamespaceURI(), featureType.getName().getLocalPart())); transformer = new GeometryCoordinateSequenceTransformer(); transformer.setMathTransform(new AffineTransform2D(0, 1, 1, 0, 0, 0)); }
public ReprojectingFeatureIterator( SimpleFeatureIterator delegate, MathTransform transform, SimpleFeatureType schema, GeometryCoordinateSequenceTransformer transformer) throws OperationNotFoundException, FactoryRegistryException, FactoryException { this.delegate = delegate; this.schema = schema; tx = transformer; tx.setMathTransform((MathTransform2D) transform); }
public ReprojectFeatureIterator(FeatureIterator<SimpleFeature> reader, SimpleFeatureType schema, MathTransform transform) { this.reader = reader; this.schema = schema; transformer.setMathTransform((MathTransform2D)transform); //set hte target coordinate system transformer.setCoordinateReferenceSystem(schema.getCoordinateReferenceSystem()); }
transformed = transformPoint((Point) g, factory); } else if (g instanceof MultiPoint) { MultiPoint mp = (MultiPoint) g; points[i] = transformPoint((Point) mp.getGeometryN(i), factory); transformed = transformLineString((LineString) g, factory); } else if (g instanceof MultiLineString) { MultiLineString mls = (MultiLineString) g; lines[i] = transformLineString((LineString) mls.getGeometryN(i), factory); transformed = transformPolygon((Polygon) g, factory); } else if (g instanceof MultiPolygon) { MultiPolygon mp = (MultiPolygon) g; polygons[i] = transformPolygon((Polygon) mp.getGeometryN(i), factory); geoms[i] = transform(gc.getGeometryN(i));
public ReprojectingFeatureCollection( SimpleFeatureCollection delegate, CoordinateReferenceSystem source, CoordinateReferenceSystem target) { super(delegate); this.target = target; SimpleFeatureType schema = delegate.getSchema(); this.schema = reType(schema, target); if (source == null) { throw new NullPointerException("source crs"); } if ( target == null ) { throw new NullPointerException("destination crs"); } this.transform = transform(source, target); transformer = new GeometryCoordinateSequenceTransformer(); }
private static void transformFeatureCollection(FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection, CoordinateReferenceSystem targetCRS) throws TransformException { final GeometryCoordinateSequenceTransformer transform = FeatureUtils.getTransform(DefaultGeographicCRS.WGS84, targetCRS); final FeatureIterator<SimpleFeature> features = featureCollection.features(); final GeometryFactory geometryFactory = new GeometryFactory(); while (features.hasNext()) { final SimpleFeature simpleFeature = features.next(); final Point sourcePoint = (Point) simpleFeature.getDefaultGeometry(); final Point targetPoint = transform.transformPoint(sourcePoint, geometryFactory); simpleFeature.setDefaultGeometry(targetPoint); } }