001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://www.geotools.org
004: * (C) 2004,2005, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017:
018: package org.geotools.demo.referencing;
019:
020: //java dependancies
021: import java.io.File;
022: import java.net.URL;
023: import java.util.Iterator;
024:
025: //geotools dedendancies
026: import org.geotools.data.DataStore;
027: import org.geotools.data.FeatureSource;
028: import org.geotools.feature.FeatureCollection;
029: import org.geotools.data.FeatureWriter;
030: import org.geotools.data.Transaction;
031: import org.geotools.data.shapefile.ShapefileDataStore;
032: import org.geotools.feature.Feature;
033: import org.geotools.feature.FeatureType;
034: import org.geotools.referencing.FactoryFinder;
035:
036: //geoapi dependancies
037: import org.opengis.referencing.crs.CRSFactory;
038: import org.opengis.referencing.crs.CoordinateReferenceSystem;
039: import org.opengis.referencing.operation.CoordinateOperationFactory;
040: import org.opengis.referencing.operation.CoordinateOperation;
041:
042: // JTS dependanceis
043: import com.vividsolutions.jts.geom.CoordinateFilter;
044: import com.vividsolutions.jts.geom.Geometry;
045:
046: /**
047: * A simple demo to transform the geometries in a shapefile from the source
048: * coordinate reference system (CRS) to a target CRS. Source and target
049: * CRS's are given here as well know text (WKT) strings. The default output path
050: * is the users home directory.
051: *
052: * The following method is rather low-level. Typically the higher-level
053: * DefaultQuery method demoed in org.geotools.demo.data.ShapeReprojector would
054: * be used instead.
055: *
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/referencing/src/main/java/org/geotools/demo/referencing/TransformData.java $
057: * @version $Id: TransformData.java 17699 2006-01-22 22:42:41Z desruisseaux $
058: * @author rschulz
059: */
060: public class TransformData {
061:
062: //hardcoded WKT strings
063: private static String SOURCE_WKT = "GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]";
064: private static String TARGET_WKT = "PROJCS[\"UTM Zone 14N\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"central_meridian\", -99.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"scale_factor\", 0.9996], PARAMETER[\"false_easting\", 500000.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
065: //private static String TARGET_WKT = "PROJCS[\"Lambert\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Lambert_Conformal_Conic_2SP\"], PARAMETER[\"semi_major\", 6378137.0], PARAMETER[\"semi_minor\", 6356752.314245179], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"standard_parallel_1\", 20.0], PARAMETER[\"standard_parallel_2\", 20.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"false_easting\", 100000.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
066:
067: //private static String WKT_2 = "PROJCS[\"Mercator\", GEOGCS[\"WGS84\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6378137.0, 298.257223563]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Mercator_2SP\"], PARAMETER[\"semi_major\", 6378137.0], PARAMETER[\"semi_minor\", 6356752.314245179], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"standard_parallel_1\", 20.0], PARAMETER[\"false_easting\", 0.0], PARAMETER[\"false_northing\", 1000000.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
068: //private static String WKT_3 = "GEOGCS[\"Sphere\", DATUM[\"WGS84\", SPHEROID[\"WGS84\", 6370997.0, 0],TOWGS84[0,0,0,0,0,0,0]], PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]]";
069: //private static String WKT_4 = "PROJCS[\"TransverseMercator\", GEOGCS[\"Sphere\", DATUM[\"Sphere\", SPHEROID[\"Sphere\", 6370997.0, 0],TOWGS84[0,0,0,0,0,0,0]],PRIMEM[\"Greenwich\", 0.0], UNIT[\"degree\",0.017453292519943295], AXIS[\"Longitude\",EAST], AXIS[\"Latitude\",NORTH]], PROJECTION[\"Transverse_Mercator\"], PARAMETER[\"semi_major\", 6370997], PARAMETER[\"semi_minor\", 6370997], PARAMETER[\"central_meridian\", 0.0], PARAMETER[\"latitude_of_origin\", 0.0], PARAMETER[\"scale_factor\", 1.0], PARAMETER[\"false_easting\", 0.0], PARAMETER[\"false_northing\", 0.0], UNIT[\"metre\",1.0], AXIS[\"x\",EAST], AXIS[\"y\",NORTH]]";
070:
071: /** Factory to create coordinate reference systems from WKT strings*/
072: private CRSFactory crsFactory = FactoryFinder.getCRSFactory(null);
073:
074: /** Factory to create transformations from a source and target CS */
075: private CoordinateOperationFactory coFactory = FactoryFinder
076: .getCoordinateOperationFactory(null);
077:
078: /** Creates a new instance of TransformData */
079: public TransformData(URL inURL, URL outURL, String inWKT,
080: String outWKT) {
081: FeatureCollection fc = null;
082: Iterator i = null;
083: FeatureWriter outFeatureWriter = null;
084: try {
085: //create the CS's and transformation
086: CoordinateReferenceSystem inCRS = crsFactory
087: .createFromWKT(inWKT);
088: CoordinateReferenceSystem outCRS = crsFactory
089: .createFromWKT(outWKT);
090: System.out.println("source CRS: "
091: + inCRS.getName().getCode());
092: System.out.println("target CRS: "
093: + outCRS.getName().getCode());
094: CoordinateOperation co = coFactory.createOperation(inCRS,
095: outCRS);
096: System.out.println("transform: "
097: + co.getMathTransform().toWKT());
098: CoordinateFilter transFilter = new TransformationCoordinateFilter(
099: co.getMathTransform());
100:
101: //get the input shapefile
102: DataStore inStore = new ShapefileDataStore(inURL);
103: String name = inStore.getTypeNames()[0];
104: FeatureSource inSource = inStore.getFeatureSource(name);
105: fc = inSource.getFeatures();
106: FeatureType inSchema = inSource.getSchema();
107:
108: //create the output shapefile
109: DataStore outStore = new ShapefileDataStore(outURL);
110: Object[] outAttributes = new Object[inSchema
111: .getAttributeCount()];
112: outStore.createSchema(inSchema);
113: outFeatureWriter = outStore.getFeatureWriter(outStore
114: .getTypeNames()[0], Transaction.AUTO_COMMIT);
115:
116: for (i = fc.iterator(); i.hasNext();) {
117: Feature inFeature = (Feature) i.next();
118: // create a new feature
119: Feature outFeature = outFeatureWriter.next();
120: for (int j = 0; j < inFeature.getNumberOfAttributes(); j++) {
121: Object inAttribute = inFeature.getAttribute(j);
122: if (inAttribute instanceof Geometry) {
123: Geometry geom = (Geometry) inAttribute;
124: geom.apply(transFilter);
125: outFeature.setAttribute(j, geom);
126: } else {
127: outFeature.setAttribute(j, inAttribute);
128: }
129: }
130: // write the new feature
131: outFeatureWriter.write();
132: }
133:
134: //close stuff
135: fc.close(i);
136: outFeatureWriter.close();
137: System.out.println("Done");
138: } catch (Exception e) {
139: System.out.println("Exception: " + e);
140: e.printStackTrace();
141: }
142: }
143:
144: /**
145: * Command line arguments are:
146: * <ul>
147: * <li>in file - input shapefile
148: * <li>out file - output shapefile
149: * <li>in WKT - WKT string for the source coordinate system
150: * <li>out WKT - WKT string for the target coordinate system
151: * </ul>
152: *
153: * @param args the command line arguments
154: *
155: */
156: public static void main(String[] args) throws Exception {
157: URL inURL, outURL;
158:
159: if (args.length == 0) {
160: inURL = TransformData.class.getClassLoader().getResource(
161: "org/geotools/sampleData/statepop.shp");
162: outURL = new File(System.getProperty("user.home")
163: + "/statepopTransform.shp").toURL();
164: new TransformData(inURL, outURL, SOURCE_WKT, TARGET_WKT);
165: } else if (args.length == 2) {
166: inURL = new File(args[0]).toURL();
167: outURL = new File(args[1]).toURL();
168: new TransformData(inURL, outURL, SOURCE_WKT, TARGET_WKT);
169: } else if (args.length == 4) {
170: inURL = new File(args[0]).toURL();
171: outURL = new File(args[1]).toURL();
172: new TransformData(inURL, outURL, args[2], args[4]);
173: } else {
174: System.out
175: .println("Usage: java TransformData <in file> <out file> <in WKT> <out WKT>");
176: }
177: }
178:
179: }
|