From: <jga...@sv...> - 2004-09-30 00:43:42
|
Author: jgarnett Date: 2004-09-29 17:45:30 -0700 (Wed, 29 Sep 2004) New Revision: 8264 Added: geotools/trunk/gt/module/main/src/org/geotools/data/crs/ForceCoordinat= eSystemFeatureReader.java geotools/trunk/gt/module/main/src/org/geotools/data/crs/ReprojectFeatu= reReader.java Removed: geotools/trunk/gt/module/main/src/org/geotools/data/ForceCoordinateSys= temFeatureReader.java geotools/trunk/gt/module/main/src/org/geotools/data/ReprojectFeatureRe= ader.java Modified: geotools/trunk/gt/module/main/src/org/geotools/data/crs/CRSService.jav= a Log: Set up for simple reprojections Deleted: geotools/trunk/gt/module/main/src/org/geotools/data/ForceCoordin= ateSystemFeatureReader.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/data/ForceCoordinateSy= stemFeatureReader.java 2004-09-29 23:07:47 UTC (rev 8263) +++ geotools/trunk/gt/module/main/src/org/geotools/data/ForceCoordinateSy= stemFeatureReader.java 2004-09-30 00:45:30 UTC (rev 8264) @@ -1,207 +0,0 @@ -/* Copyright (c) 2001, 2003 TOPP - www.openplans.org. All rights reserv= ed. - * This code is licensed under the GPL 2.0 license, availible at the roo= t - * application directory. - */ -/* - * Geotools2 - OpenSource mapping toolkit - * http://geotools.org - * (C) 2003, Geotools Project Managment Committee (PMC) - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; - * version 2.1 of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ -package org.geotools.data; - -import org.geotools.feature.AttributeType; -import org.geotools.feature.AttributeTypeFactory; -import org.geotools.feature.Feature; -import org.geotools.feature.FeatureType; -import org.geotools.feature.FeatureTypeFactory; -import org.geotools.feature.GeometryAttributeType; -import org.geotools.feature.IllegalAttributeException; -import org.geotools.feature.SchemaException; -import org.opengis.referencing.crs.CoordinateReferenceSystem; -import java.io.IOException; -import java.util.NoSuchElementException; - - -/** - * ForceCoordinateSystemFeatureReader provides a CoordinateReferenceSyst= em for - * FeatureTypes. - *=20 - * <p> - * ForceCoordinateSystemFeatureReader is a wrapper used to force - * GeometryAttributes to a user supplied CoordinateReferenceSystem rathe= r then - * the default supplied by the DataStore. - * </p> - *=20 - * <p> - * Example Use: - * <pre><code> - * ForceCoordinateSystemFeatureReader reader =3D - * new ForceCoordinateSystemFeatureReader( origionalReader, forceCS = ); - *=20 - * CoordinateReferenceSystem orgionalCS =3D - * origionalReader.getFeatureType().getDefaultGeometry().getCoordian= teSystem(); - *=20 - * CoordinateReferenceSystem newCS =3D - * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem(= ); - *=20 - * assertEquals( forceCS, newCS ); - * </code></pre> - * </p> - * - * @author jgarnett, Refractions Research, Inc. - * @author $Author: jive $ (last modification) - * @version $Id: ForceCoordinateSystemFeatureReader.java,v 1.1 2003/12/1= 9 01:05:08 jive Exp $ - */ -public class ForceCoordinateSystemFeatureReader implements FeatureReader= { - private FeatureReader reader; - private FeatureType schema; - private CoordinateReferenceSystem coordianteSystem; - - public ForceCoordinateSystemFeatureReader(FeatureReader reader, - CoordinateReferenceSystem cs) throws SchemaException { - if (cs =3D=3D null) { - throw new NullPointerException("CoordinateSystem required"); - } - - FeatureType type =3D reader.getFeatureType(); - CoordinateReferenceSystem origional =3D type.getDefaultGeometry(= ) - .getCoordinateSystem()= ; - - if (cs.equals(origional)) { - throw new IllegalArgumentException("CoordinateSystem " + cs - + " already used (check before using wrapper)"); - } - - coordianteSystem =3D cs; - - FeatureTypeFactory typeFactory =3D FeatureTypeFactory.newInstanc= e(type - .getTypeName()); - =20 - typeFactory.setNamespace(type.getNamespace()); - typeFactory.setName(type.getTypeName()); - =20 - GeometryAttributeType defaultGeometryType =3D null; - for( int i=3D0; i<type.getAttributeCount(); i++ ){ - AttributeType attributeType =3D type.getAttributeType( i ); - if( attributeType instanceof GeometryAttributeType ){ - GeometryAttributeType geometryType =3D - (GeometryAttributeType) attributeType; - GeometryAttributeType forcedGeometry; - =20 - ; - forcedGeometry =3D (GeometryAttributeType) - AttributeTypeFactory.newAttributeType( - geometryType.getName(), - geometryType.getClass(), - geometryType.isNillable(), - geometryType.getFieldLength(), - geometryType.createDefaultValue(), - cs - ); - if( defaultGeometryType =3D=3D null || - geometryType =3D=3D type.getDefaultGeometry() ){ - defaultGeometryType =3D forcedGeometry; = =20 - } - typeFactory.addType( forcedGeometry ); =20 - } - else { - typeFactory.addType( attributeType ); - } =20 - } - typeFactory.setDefaultGeometry( defaultGeometryType ); - schema =3D typeFactory.getFeatureType(); - this.reader =3D reader; - } - - /** - * Implement getFeatureType. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @see org.geotools.data.FeatureReader#getFeatureType() - */ - public FeatureType getFeatureType() { - if( schema =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } - return schema; - } - - /** - * Implement next. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @throws IOException - * @throws IllegalAttributeException - * @throws NoSuchElementException - * - * @see org.geotools.data.FeatureReader#next() - */ - public Feature next() - throws IOException, IllegalAttributeException, NoSuchElementExce= ption { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } =20 - Feature next =3D reader.next(); =20 - return schema.create( next.getAttributes( null ), next.getID() )= ; - } - - /** - * Implement hasNext. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @throws IOException - * - * @see org.geotools.data.FeatureReader#hasNext() - */ - public boolean hasNext() throws IOException { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } =20 - return reader.hasNext(); - } - - /** - * Implement close. - *=20 - * <p> - * Description ... - * </p> - * - * @throws IOException - * - * @see org.geotools.data.FeatureReader#close() - */ - public void close() throws IOException { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } - reader.close(); - reader =3D null; - schema =3D null; - } -} Deleted: geotools/trunk/gt/module/main/src/org/geotools/data/ReprojectFea= tureReader.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/data/ReprojectFeatureR= eader.java 2004-09-29 23:07:47 UTC (rev 8263) +++ geotools/trunk/gt/module/main/src/org/geotools/data/ReprojectFeatureR= eader.java 2004-09-30 00:45:30 UTC (rev 8264) @@ -1,210 +0,0 @@ -/* Copyright (c) 2001, 2003 TOPP - www.openplans.org. All rights reserv= ed. - * This code is licensed under the GPL 2.0 license, availible at the roo= t - * application directory. - */ -/* - * Geotools2 - OpenSource mapping toolkit - * http://geotools.org - * (C) 2003, Geotools Project Managment Committee (PMC) - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; - * version 2.1 of the License. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - */ -package org.geotools.data; - -import org.geotools.feature.AttributeType; -import org.geotools.feature.AttributeTypeFactory; -import org.geotools.feature.Feature; -import org.geotools.feature.FeatureType; -import org.geotools.feature.FeatureTypeFactory; -import org.geotools.feature.GeometryAttributeType; -import org.geotools.feature.IllegalAttributeException; -import org.geotools.feature.SchemaException; -import org.opengis.referencing.crs.CoordinateReferenceSystem; -import java.io.IOException; -import java.util.NoSuchElementException; - - -/** - * ReprojectFeatureReader provides a reprojection for FeatureTypes. - *=20 - * <p> - * ReprojectFeatureReader is a wrapper used to reproject=20 - * GeometryAttributes to a user supplied CoordinateReferenceSystem from - * the Origional CoordinateReferenceSystem supplied by the origional - * FeatureReader. - * </p> - *=20 - * <p> - * Example Use: - * <pre><code> - * ReprojectFeatureReader reader =3D - * new ReprojectFeatureReader( origionalReader, newCS ); - *=20 - * CoordinateReferenceSystem orgionalCS =3D - * origionalReader.getFeatureType().getDefaultGeometry().getCoordian= teSystem(); - *=20 - * CoordinateReferenceSystem newCS =3D - * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem(= ); - *=20 - * assertEquals( forceCS, newCS ); - * </code></pre> - * </p> - * - * @author jgarnett, Refractions Research, Inc. - * @author $Author: jive $ (last modification) - * @version $Id: ReprojectFeatureReader.java,v 1.1 2003/12/19 01:08:58 j= ive Exp $ - */ -public class ReprojectFeatureReader implements FeatureReader { - private FeatureReader reader; - private FeatureType schema; - private CoordinateReferenceSystem coordianteSystem; - - public ReprojectFeatureReader(FeatureReader reader, - CoordinateReferenceSystem cs) throws SchemaException { - if (cs =3D=3D null) { - throw new NullPointerException("CoordinateSystem required"); - } - - FeatureType type =3D reader.getFeatureType(); - CoordinateReferenceSystem origional =3D type.getDefaultGeometry(= ) - .getCoordinateSystem()= ; - - if (cs.equals(origional)) { - throw new IllegalArgumentException("CoordinateSystem " + cs - + " already used (check before using wrapper)"); - } - - coordianteSystem =3D cs; - - FeatureTypeFactory typeFactory =3D FeatureTypeFactory.newInstanc= e(type - .getTypeName()); - =20 - typeFactory.setNamespace(type.getNamespace()); - typeFactory.setName(type.getTypeName()); - =20 - GeometryAttributeType defaultGeometryType =3D null; - for( int i=3D0; i<type.getAttributeCount(); i++ ){ - AttributeType attributeType =3D type.getAttributeType( i ); - if( attributeType instanceof GeometryAttributeType ){ - GeometryAttributeType geometryType =3D - (GeometryAttributeType) attributeType; - GeometryAttributeType forcedGeometry; - =20 - ; - forcedGeometry =3D (GeometryAttributeType) - AttributeTypeFactory.newAttributeType( - geometryType.getName(), - geometryType.getClass(), - geometryType.isNillable(), - geometryType.getFieldLength(), - geometryType.createDefaultValue(), - cs - ); - if( defaultGeometryType =3D=3D null || - geometryType =3D=3D type.getDefaultGeometry() ){ - defaultGeometryType =3D forcedGeometry; = =20 - } - typeFactory.addType( forcedGeometry ); =20 - } - else { - typeFactory.addType( attributeType ); - } =20 - } - typeFactory.setDefaultGeometry( defaultGeometryType ); - schema =3D typeFactory.getFeatureType(); - this.reader =3D reader; - } - - /** - * Implement getFeatureType. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @see org.geotools.data.FeatureReader#getFeatureType() - */ - public FeatureType getFeatureType() { - if( schema =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } - return schema; - } - - /** - * Implement next. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @throws IOException - * @throws IllegalAttributeException - * @throws NoSuchElementException - * - * @see org.geotools.data.FeatureReader#next() - */ - public Feature next() - throws IOException, IllegalAttributeException, NoSuchElementExce= ption { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } =20 - Feature next =3D reader.next(); =20 - // - // Add Reprojection Code here! - // - return schema.create( next.getAttributes( null ), next.getID() )= ; - } - - /** - * Implement hasNext. - *=20 - * <p> - * Description ... - * </p> - * - * @return - * - * @throws IOException - * - * @see org.geotools.data.FeatureReader#hasNext() - */ - public boolean hasNext() throws IOException { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } =20 - return reader.hasNext(); - } - - /** - * Implement close. - *=20 - * <p> - * Description ... - * </p> - * - * @throws IOException - * - * @see org.geotools.data.FeatureReader#close() - */ - public void close() throws IOException { - if( reader =3D=3D null ){ - throw new IllegalStateException("Reader has already been clo= sed"); - } - reader.close(); - reader =3D null; - schema =3D null; - } -} \ No newline at end of file Modified: geotools/trunk/gt/module/main/src/org/geotools/data/crs/CRSServ= ice.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/data/crs/CRSService.ja= va 2004-09-29 23:07:47 UTC (rev 8263) +++ geotools/trunk/gt/module/main/src/org/geotools/data/crs/CRSService.ja= va 2004-09-30 00:45:30 UTC (rev 8264) @@ -24,13 +24,48 @@ import java.util.List; import java.util.Set; =20 +import org.geotools.cs.AxisInfo; import org.geotools.cs.CoordinateSystem; import org.geotools.cs.CoordinateSystemAuthorityFactory; +import org.geotools.cs.CoordinateSystemFactory; +import org.geotools.cs.HorizontalDatum; import org.geotools.cs.NoSuchAuthorityCodeException; +import org.geotools.cs.PrimeMeridian; +import org.geotools.ct.CannotCreateTransformException; +import org.geotools.ct.CoordinateTransformation; +import org.geotools.ct.CoordinateTransformationFactory; +import org.geotools.ct.MathTransform; +import org.geotools.data.FeatureReader; import org.geotools.factory.FactoryFinder; +import org.geotools.feature.AttributeType; +import org.geotools.feature.AttributeTypeFactory; +import org.geotools.feature.Feature; +import org.geotools.feature.FeatureType; +import org.geotools.feature.FeatureTypeFactory; +import org.geotools.feature.GeometryAttributeType; +import org.geotools.feature.IllegalAttributeException; +import org.geotools.feature.SchemaException; +import org.geotools.pt.CoordinatePoint; +import org.geotools.units.Unit; import org.opengis.referencing.FactoryException; import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.TransformException; +import org.opengis.spatialschema.geometry.MismatchedDimensionException; =20 +import com.vividsolutions.jts.geom.Coordinate; +import com.vividsolutions.jts.geom.CoordinateSequence; +import com.vividsolutions.jts.geom.CoordinateSequenceFactory; +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.LinearRing; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.MultiPoint; +import com.vividsolutions.jts.geom.MultiPolygon; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; + /** * Utility method isolating data source providers from CRS production. * <p> @@ -41,8 +76,39 @@ * @author Jody Garnett, Refractions Research */ public class CRSService { - =20 + /** + * GeoGraphicCoordinateSystem sutiable for distance on sphere calcua= ltions. + * <p> + * We will use a geographic coordinate system, i.e. one that use (l= ongitude,latitude) + * coordinates. Latitude values are increasing north and longitude= values area + * increasing east. Angular units are degrees and prime meridian is= Greenwich. + * Ellipsoid is WGS 84 (a commonly used one for remote sensing data= and GPS). + * </p> + */ + static public final CoordinateReferenceSystem GEOGRAPHIC; + static { + CoordinateSystemFactory csFactory =3D CoordinateSystemFactory.ge= tDefault(); =20 + /* + * Construct the source CoordinateSystem. Note that th= e Geotools library + * provides simpler ways to construct geographic coordinate syst= ems using default + * values for some arguments. But we show here the complete way= in order to show + * the range of possibilities and to stay closer to the OpenGIS'= s specification. + */ + Unit angularUnit =3D Unit.DEGREE; + HorizontalDatum datum =3D HorizontalDatum.WGS84; + PrimeMeridian meridian =3D PrimeMeridian.GREENWICH; + =20 + CoordinateReferenceSystem geographic; + try { + geographic =3D csFactory.createGeographicCoordinateSystem( + "geographic", angularUnit, datum, meridian, AxisInfo= .LONGITUDE, AxisInfo.LATITUDE ); + } catch (FactoryException e) { + geographic =3D null; + } + GEOGRAPHIC =3D geographic; + } + /** * List of CoordinateSystemAuthorityFactory. * <p> * With a normal geotools install plugins exist for AUTO and EPSG. @@ -146,4 +212,264 @@ public CoordinateReferenceSystem createCRS( String code ) throws Factor= yException { return createCoordianteSystem( code ); } + /**=20 + * A "safe" cast to the old CoordinateSystem class. + *=20 + * @param crs CoordinateReferenceSystem + * @return CoordianteSystem for provided CRS, or null if this is not po= sssible. + */ + public static CoordinateSystem cs( CoordinateReferenceSystem crs ){ + if( crs instanceof CoordinateSystem ){ + return (CoordinateSystem) crs; + } + String wkt =3D crs.toWKT(); + CoordinateSystemFactory factory =3D CoordinateSystemFactory.getDefa= ult();=20 + try { + return factory.createFromWKT( wkt ); + } catch (FactoryException huh) { + huh.printStackTrace(); + return null; + } + } + =09 + public static MathTransform reproject( CoordinateReferenceSystem from, = CoordinateReferenceSystem to ) throws CannotCreateTransformException{ + return reproject( cs( from ), cs( to ) ); + } +=09 + public static MathTransform reproject( CoordinateSystem from, Coordinat= eSystem to ) throws CannotCreateTransformException{ + CoordinateTransformationFactory factory =3D + CoordinateTransformationFactory.getDefault(); + =09 + CoordinateTransformation transformation; + + transformation =3D factory.createFromCoordinateSystems( from, to= ); + return transformation.getMathTransform(); =20 + } +=09 + static FeatureType transform( FeatureType schema, CoordinateReferenceSy= stem crs ) throws SchemaException { + FeatureTypeFactory factory =3D FeatureTypeFactory.newInstance( s= chema.getTypeName() ); + =20 + factory.setNamespace( schema.getNamespace() ); + factory.setName( schema.getTypeName() ); + =20 + GeometryAttributeType defaultGeometryType =3D null; + for( int i=3D0; i<schema.getAttributeCount(); i++ ){ + AttributeType attributeType =3D schema.getAttributeType( i )= ; + if( attributeType instanceof GeometryAttributeType ){ + GeometryAttributeType geometryType =3D (GeometryAttribut= eType) attributeType; + GeometryAttributeType geometry; + =20 + geometry =3D (GeometryAttributeType) AttributeTypeFactor= y.newAttributeType( + geometryType.getName(), + geometryType.getClass(), + geometryType.isNillable(), + geometryType.getFieldLength(), + geometryType.createDefaultValue(), + crs + ); + =20 + if( defaultGeometryType =3D=3D null ||=20 + geometryType =3D=3D schema.getDefaultGeometry() ){ + defaultGeometryType =3D geometry; + } + factory.addType( geometry ); =20 + } + else { + factory.addType( attributeType ); + } =20 + } + factory.setDefaultGeometry( defaultGeometryType ); + return factory.getFeatureType(); + } +=09 + /** + * Applies transform to all geometry attribute. + * =20 + * @throws TransformException + * @throws MismatchedDimensionException + */ + static Feature transform( Feature feature, FeatureType schema, MathTran= sform transform ) throws MismatchedDimensionException, TransformException= { + FeatureType type =3D feature.getFeatureType(); + GeometryAttributeType geomType =3D schema.getDefaultGeometry(); + Geometry geom =3D (Geometry) feature.getAttribute( geomType.getName= () ); + =20 + geom =3D transform( geom, transform ); =20 + try { =20 + feature.setAttribute( geomType.getName(), geom ); + } catch (IllegalAttributeException shouldNotHappen) { + // we are expecting the transform to return the same geometr= y type + } + return feature; + } +=09 + public static Geometry transform( Geometry geom, MathTransform transfor= m ) throws MismatchedDimensionException, TransformException{ + if( transform.isIdentity() ) return geom; + if( geom instanceof LineString ){ + return transform( (LineString) geom, transform ); =20 + } + if( geom instanceof MultiLineString ){ + return transform( (MultiLineString) geom, transform ); =20 + } + if( geom instanceof Polygon ){ + return transform( (Polygon) geom, transform ); =20 + } + if( geom instanceof Point ){ + return transform( (Point) geom, transform ); =20 + } + if( geom instanceof MultiPoint ){ + return transform( (MultiPoint) geom, transform ); =20 + } + if( geom instanceof MultiPolygon ){ + return transform( (MultiPolygon) geom, transform ); =20 + } + return null; // could not transform! + } +=09 + public static Envelope transform( Envelope envelope, MathTransform tran= sform ) throws MismatchedDimensionException, TransformException { + CoordinatePoint pt; + Envelope bbox =3D new Envelope(); + pt =3D transform.transform( new CoordinatePoint( envelope.getMinX()= , envelope.getMinY() ), null ); + bbox.expandToInclude( pt.getOrdinate( 0 ), pt.getOrdinate( 1 )); + =20 + pt =3D transform.transform( new CoordinatePoint( envelope.getMaxX()= , envelope.getMinY() ), null ); + bbox.expandToInclude( pt.getOrdinate( 0 ), pt.getOrdinate( 1 )); + =20 + pt =3D transform.transform( new CoordinatePoint( envelope.getMaxX()= , envelope.getMaxY() ), null ); + bbox.expandToInclude( pt.getOrdinate( 0 ), pt.getOrdinate( 1 )); + =20 + pt =3D transform.transform( new CoordinatePoint( envelope.getMinX()= , envelope.getMaxY() ), null ); + bbox.expandToInclude( pt.getOrdinate( 0 ), pt.getOrdinate( 1 )); + =20 + return bbox; =20 + } + public static Point transform( Point point, MathTransform transform ) t= hrows MismatchedDimensionException, TransformException { + GeometryFactory factory =3D point.getFactory(); + =20 + Coordinate coords[] =3D point.getCoordinateSequence().toCoordinateA= rray(); + =20 + for( int i=3D0; i<coords.length; i++ ){ + CoordinatePoint pt =3D new CoordinatePoint( coords[i].x, coords= [i].y ); + pt =3D transform.transform( pt, null ); + coords[i].x =3D pt.getOrdinate( 0 ); + coords[i].y =3D pt.getOrdinate( 1 ); =20 + } + return factory.createPoint( coords[0] ); =20 + }=09 + public static LineString transform( LineString line, MathTransform tran= sform ) throws MismatchedDimensionException, TransformException { + GeometryFactory factory =3D line.getFactory(); + Coordinate coords[] =3D line.getCoordinateSequence().toCoordinateAr= ray(); + =20 + for( int i=3D0; i<coords.length; i++ ){ + CoordinatePoint pt =3D new CoordinatePoint( coords[i].x, coords= [i].y ); + pt =3D transform.transform( pt, null ); + coords[i].x =3D pt.getOrdinate( 0 ); + coords[i].y =3D pt.getOrdinate( 1 ); =20 + } + return factory.createLineString( coords ); =20 + } + public static LinearRing transform( LinearRing ring, MathTransform tran= sform ) throws MismatchedDimensionException, TransformException { + GeometryFactory factory =3D ring.getFactory(); + Coordinate coords[] =3D ring.getCoordinateSequence().toCoordinateAr= ray(); + =20 + for( int i=3D0; i<coords.length; i++ ){ + CoordinatePoint pt =3D new CoordinatePoint( coords[i].x, coords= [i].y ); + pt =3D transform.transform( pt, null ); + coords[i].x =3D pt.getOrdinate( 0 ); + coords[i].y =3D pt.getOrdinate( 1 ); =20 + } + return factory.createLinearRing( coords ); =20 + } + public static Polygon transform( Polygon polygon, MathTransform transfo= rm ) throws MismatchedDimensionException, TransformException { + GeometryFactory factory =3D polygon.getFactory(); + =20 + LinearRing ring =3D transform( (LinearRing) polygon.getExteriorRing= (), transform ); + LinearRing holes[] =3D new LinearRing[ polygon.getNumInteriorRing(= ) ]; + for( int i=3D0; i< holes.length; i++ ){ + holes[i] =3D transform( (LinearRing) polygon.getInteriorRingN( = i ), transform ); + } + return factory.createPolygon( ring, holes ); + } +=09 + public static MultiPoint transform( MultiPoint multi, MathTransform tra= nsform ) throws MismatchedDimensionException, TransformException { + GeometryFactory factory =3D multi.getFactory(); + =20 + Coordinate coords[] =3D multi.getCoordinates(); + =20 + for( int i=3D0; i<coords.length; i++ ){ + CoordinatePoint pt =3D new CoordinatePoint( coords[i].x, coords= [i].y ); + pt =3D transform.transform( pt, null ); + coords[i].x =3D pt.getOrdinate( 0 ); + coords[i].y =3D pt.getOrdinate( 1 ); =20 + } + return factory.createMultiPoint( coords ); + } + public static MultiLineString transform( MultiLineString multi, MathTra= nsform transform ) throws MismatchedDimensionException, TransformExceptio= n { + GeometryFactory factory =3D multi.getFactory(); + =20 + LineString geoms[] =3D new LineString[ multi.getNumGeometries() ]; + for( int i=3D0; i< geoms.length; i++ ){ + geoms[i] =3D transform( (LineString) multi.getGeometryN( i ), t= ransform ); + } + return factory.createMultiLineString( geoms ); + } + public static MultiPolygon transform( MultiPolygon multi, MathTransform= transform ) throws MismatchedDimensionException, TransformException { + GeometryFactory factory =3D multi.getFactory(); + =20 + Polygon geoms[] =3D new Polygon[ multi.getNumGeometries() ]; + for( int i=3D0; i< geoms.length; i++ ){ + geoms[i] =3D transform( (Polygon) multi.getGeometryN( i ), tran= sform ); + } + return factory.createMultiPolygon( geoms ); + } +=09 + /** + * Force GeometryAttributes to a user supplied CoordinateReferenceSyste= m + * <p> + * Opperates as a FeatureReader wrapper (well Decorator pattern since t= he result is + * also a FeatureReader). + * </p> + * Example Use: + * <pre><code> + * FeatureReader reader =3D CRSSerivce.readerForce( origionalReader, fo= rceCS ); + *=20 + * CoordinateReferenceSystem orgionalCS =3D + * origionalReader.getFeatureType().getDefaultGeometry().getCoordia= nteSystem(); + *=20 + * CoordinateReferenceSystem newCS =3D + * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem= (); + *=20 + * assertEquals( forceCS, newCS ); + * </code></pre> + * </p> + * + * @author jgarnett, Refractions Research, Inc. + * @author $Author: jive $ (last modification) + */ + public static FeatureReader readerForce( FeatureReader reader, Coordina= teReferenceSystem crs ) throws SchemaException { + return new ForceCoordinateSystemFeatureReader( reader, crs ); + } + /** + * Reproject GeometryAttributes to a user supplied CoordinateReferenceS= ystem. + * </p> + *=20 + * <p> + * Example Use: + * <pre><code> + * FeatureReader reader =3D CRSService.readerReproject( origionalReader= , newCS ); + *=20 + * CoordinateReferenceSystem orgionalCS =3D + * origionalReader.getFeatureType().getDefaultGeometry().getCoordia= nteSystem(); + *=20 + * CoordinateReferenceSystem newCS =3D + * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem= (); + *=20 + * assertEquals( forceCS, newCS ); + * </code></pre> + * </p> + * @throws SchemaException + * @throws CannotCreateTransformException + */ + public static FeatureReader readerReproject( FeatureReader reader, Coor= dinateReferenceSystem crs ) throws CannotCreateTransformException, Schema= Exception { + return new ReprojectFeatureReader( reader, crs ); + } } Copied: geotools/trunk/gt/module/main/src/org/geotools/data/crs/ForceCoor= dinateSystemFeatureReader.java (from rev 8222, geotools/trunk/gt/module/m= ain/src/org/geotools/data/ForceCoordinateSystemFeatureReader.java) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/data/ForceCoordinateSy= stemFeatureReader.java 2004-09-27 18:50:27 UTC (rev 8222) +++ geotools/trunk/gt/module/main/src/org/geotools/data/crs/ForceCoordina= teSystemFeatureReader.java 2004-09-30 00:45:30 UTC (rev 8264) @@ -0,0 +1,172 @@ +/* Copyright (c) 2001, 2003 TOPP - www.openplans.org. All rights reserv= ed. + * This code is licensed under the GPL 2.0 license, availible at the roo= t + * application directory. + */ +/* + * Geotools2 - OpenSource mapping toolkit + * http://geotools.org + * (C) 2003, Geotools Project Managment Committee (PMC) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package org.geotools.data.crs; + +import org.geotools.data.FeatureReader; +import org.geotools.feature.AttributeType; +import org.geotools.feature.AttributeTypeFactory; +import org.geotools.feature.Feature; +import org.geotools.feature.FeatureType; +import org.geotools.feature.FeatureTypeFactory; +import org.geotools.feature.GeometryAttributeType; +import org.geotools.feature.IllegalAttributeException; +import org.geotools.feature.SchemaException; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import java.io.IOException; +import java.util.NoSuchElementException; + + +/** + * ForceCoordinateSystemFeatureReader provides a CoordinateReferenceSyst= em for + * FeatureTypes. + *=20 + * <p> + * ForceCoordinateSystemFeatureReader is a wrapper used to force + * GeometryAttributes to a user supplied CoordinateReferenceSystem rathe= r then + * the default supplied by the DataStore. + * </p> + *=20 + * <p> + * Example Use: + * <pre><code> + * ForceCoordinateSystemFeatureReader reader =3D + * new ForceCoordinateSystemFeatureReader( origionalReader, forceCS = ); + *=20 + * CoordinateReferenceSystem orgionalCS =3D + * origionalReader.getFeatureType().getDefaultGeometry().getCoordian= teSystem(); + *=20 + * CoordinateReferenceSystem newCS =3D + * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem(= ); + *=20 + * assertEquals( forceCS, newCS ); + * </code></pre> + * </p> + * + * @author jgarnett, Refractions Research, Inc. + * @author $Author: jive $ (last modification) + * @version $Id: ForceCoordinateSystemFeatureReader.java,v 1.1 2003/12/1= 9 01:05:08 jive Exp $ + */ +public class ForceCoordinateSystemFeatureReader implements FeatureReader= { + private FeatureReader reader; + private FeatureType schema; + private CoordinateReferenceSystem coordianteSystem; + + public ForceCoordinateSystemFeatureReader(FeatureReader reader, + CoordinateReferenceSystem cs) throws SchemaException { + if (cs =3D=3D null) { + throw new NullPointerException("CoordinateSystem required"); + } + + FeatureType type =3D reader.getFeatureType(); + CoordinateReferenceSystem origional =3D type.getDefaultGeometry(= ) + .getCoordinateSystem()= ; + + if (cs.equals(origional)) { + throw new IllegalArgumentException("CoordinateSystem " + cs + + " already used (check before using wrapper)"); + } + + coordianteSystem =3D cs; + schema =3D CRSService.transform( type, cs ); =20 + this.reader =3D reader; + } + + /** + * Implement getFeatureType. + *=20 + * <p> + * Description ... + * </p> + * + * @return + * + * @see org.geotools.data.FeatureReader#getFeatureType() + */ + public FeatureType getFeatureType() { + if( schema =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } + return schema; + } + + /** + * Implement next. + *=20 + * <p> + * Description ... + * </p> + * + * @return + * + * @throws IOException + * @throws IllegalAttributeException + * @throws NoSuchElementException + * + * @see org.geotools.data.FeatureReader#next() + */ + public Feature next() + throws IOException, IllegalAttributeException, NoSuchElementExce= ption { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } =20 + Feature next =3D reader.next(); =20 + return schema.create( next.getAttributes( null ), next.getID() )= ; + } + + /** + * Implement hasNext. + *=20 + * <p> + * Description ... + * </p> + * + * @return + * + * @throws IOException + * + * @see org.geotools.data.FeatureReader#hasNext() + */ + public boolean hasNext() throws IOException { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } =20 + return reader.hasNext(); + } + + /** + * Implement close. + *=20 + * <p> + * Description ... + * </p> + * + * @throws IOException + * + * @see org.geotools.data.FeatureReader#close() + */ + public void close() throws IOException { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } + reader.close(); + reader =3D null; + schema =3D null; + } +} Copied: geotools/trunk/gt/module/main/src/org/geotools/data/crs/Reproject= FeatureReader.java (from rev 8222, geotools/trunk/gt/module/main/src/org/= geotools/data/ReprojectFeatureReader.java) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- geotools/trunk/gt/module/main/src/org/geotools/data/ReprojectFeatureR= eader.java 2004-09-27 18:50:27 UTC (rev 8222) +++ geotools/trunk/gt/module/main/src/org/geotools/data/crs/ReprojectFeat= ureReader.java 2004-09-30 00:45:30 UTC (rev 8264) @@ -0,0 +1,187 @@ +/* Copyright (c) 2001, 2003 TOPP - www.openplans.org. All rights reserv= ed. + * This code is licensed under the GPL 2.0 license, availible at the roo= t + * application directory. + */ +/* + * Geotools2 - OpenSource mapping toolkit + * http://geotools.org + * (C) 2003, Geotools Project Managment Committee (PMC) + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + */ +package org.geotools.data.crs; + +import org.geotools.ct.CannotCreateTransformException; +import org.geotools.ct.MathTransform; +import org.geotools.data.DataSourceException; +import org.geotools.data.FeatureReader; +import org.geotools.feature.AttributeType; +import org.geotools.feature.AttributeTypeFactory; +import org.geotools.feature.Feature; +import org.geotools.feature.FeatureType; +import org.geotools.feature.FeatureTypeFactory; +import org.geotools.feature.GeometryAttributeType; +import org.geotools.feature.IllegalAttributeException; +import org.geotools.feature.SchemaException; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.referencing.operation.TransformException; +import org.opengis.spatialschema.geometry.MismatchedDimensionException; + +import java.io.IOException; +import java.util.NoSuchElementException; + + +/** + * ReprojectFeatureReader provides a reprojection for FeatureTypes. + *=20 + * <p> + * ReprojectFeatureReader is a wrapper used to reproject=20 + * GeometryAttributes to a user supplied CoordinateReferenceSystem from + * the Origional CoordinateReferenceSystem supplied by the origional + * FeatureReader. + * </p> + *=20 + * <p> + * Example Use: + * <pre><code> + * ReprojectFeatureReader reader =3D + * new ReprojectFeatureReader( origionalReader, newCS ); + *=20 + * CoordinateReferenceSystem orgionalCS =3D + * origionalReader.getFeatureType().getDefaultGeometry().getCoordian= teSystem(); + *=20 + * CoordinateReferenceSystem newCS =3D + * reader.getFeatureType().getDefaultGeometry().getCoordianteSystem(= ); + *=20 + * assertEquals( forceCS, newCS ); + * </code></pre> + * </p> + * + * @author jgarnett, Refractions Research, Inc. + * @author $Author: jive $ (last modification) + * @version $Id: ReprojectFeatureReader.java,v 1.1 2003/12/19 01:08:58 j= ive Exp $ + */ +public class ReprojectFeatureReader implements FeatureReader { + private FeatureReader reader; + private FeatureType schema; + private CoordinateReferenceSystem cs; + private MathTransform transform; + =20 + public ReprojectFeatureReader(FeatureReader reader, + CoordinateReferenceSystem crs) throws SchemaException, CannotCre= ateTransformException=20 + { =20 + if (crs =3D=3D null) { + throw new NullPointerException("CoordinateSystem required"); + } + + FeatureType type =3D reader.getFeatureType(); + CoordinateReferenceSystem origional =3D type.getDefaultGeometry(= ).getCoordinateSystem(); + + if (crs.equals(origional)) { + throw new IllegalArgumentException("CoordinateSystem " + cs + + " already used (check before using wrapper)"); + } + cs =3D crs; + =20 + transform =3D CRSService.reproject( origional, cs ); + =20 + schema =3D CRSService.transform( type, crs ); =20 + this.reader =3D reader; + } + + /** + * Implement getFeatureType. + *=20 + * <p> + * Description ... + * </p> + * + * @return + * + * @see org.geotools.data.FeatureReader#getFeatureType() + */ + public FeatureType getFeatureType() { + if( schema =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } + return schema; + } + + /** + * Implement next. + *=20 + * <p> + * Uses CRSService.transform( next, transform ) for very simple repr= ojection. + * </p> + * + * @return + * + * @throws IOException + * @throws IllegalAttributeException + * @throws NoSuchElementException + * + * @see org.geotools.data.FeatureReader#next() + */ + public Feature next() + throws IOException, IllegalAttributeException, NoSuchElementExce= ption { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } =20 + Feature next =3D reader.next(); + try { + return CRSService.transform( next, schema, transform ); + } catch (MismatchedDimensionException dimensionException ) { + throw new DataSourceException( dimensionException ); + } catch (TransformException transformException) { + throw new DataSourceException( transformException ); + } + } + + /** + * Implement hasNext. + *=20 + * <p> + * Description ... + * </p> + * + * @return + * + * @throws IOException + * + * @see org.geotools.data.FeatureReader#hasNext() + */ + public boolean hasNext() throws IOException { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } =20 + return reader.hasNext(); + } + + /** + * Implement close. + *=20 + * <p> + * Description ... + * </p> + * + * @throws IOException + * + * @see org.geotools.data.FeatureReader#close() + */ + public void close() throws IOException { + if( reader =3D=3D null ){ + throw new IllegalStateException("Reader has already been clo= sed"); + } + reader.close(); + reader =3D null; + schema =3D null; + } +} \ No newline at end of file |