001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, 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: package org.geotools.demo.data;
017:
018: import java.io.File;
019: import java.net.URL;
020:
021: import javax.swing.JFileChooser;
022:
023: import org.geotools.data.DataUtilities;
024: import org.geotools.data.DefaultQuery;
025: import org.geotools.data.FeatureSource;
026: import org.geotools.data.FeatureStore;
027: import org.geotools.data.shapefile.ShapefileDataStore;
028: import org.geotools.feature.FeatureCollection;
029: import org.geotools.referencing.CRS;
030: import org.geotools.referencing.crs.DefaultGeographicCRS;
031: import org.opengis.referencing.crs.CoordinateReferenceSystem;
032:
033: /**
034: * Basic reprojection abilities demo: open a file, get the feature type, read the
035: * features and output their reprojected contents to the standard output
036: *
037: * @author aaime
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/data/src/main/java/org/geotools/demo/data/ShapeReprojector.java $
039: */
040: public class ShapeReprojector {
041: private static URL getResource(String path) {
042: return ShapeReprojector.class.getClassLoader()
043: .getResource(path);
044: }
045:
046: public static void main(String[] args) {
047: try {
048: // get the shapefile URL by either loading it from the file system
049: // or from the classpath
050: URL shapeURL = null;
051: JFileChooser fileChooser = new JFileChooser();
052: fileChooser
053: .setDialogTitle("Choose a shapefile, or press cancel to use the default one");
054: fileChooser.setFileFilter(new SimpleFileFilter("shp",
055: "Shapefile"));
056:
057: int result = fileChooser.showOpenDialog(null);
058:
059: if (result == JFileChooser.APPROVE_OPTION) {
060: File f = fileChooser.getSelectedFile();
061: shapeURL = f.toURL();
062: } else {
063: shapeURL = getResource("org/geotools/sampleData/statepop.shp");
064: }
065:
066: // get a origin CRS, let's say that data are expressed in WGS84
067: // if you want a list of codes, please look into the epsg.properties file
068: // included in the EPSG module
069: // Let's also create an auto crs based on the UTM with the standard parallel and meridian
070: // as the equator and Greenwich
071:
072: CoordinateReferenceSystem originCrs = DefaultGeographicCRS.WGS84; // crsService.createCRS("EPSG:4326");
073: CoordinateReferenceSystem destCrs = CRS
074: .decode("AUTO:42001,0.0,0.0");
075:
076: System.out.println("Origin CRS: " + originCrs);
077: System.out.println("Destination CRS: " + destCrs);
078:
079: // since we assume the data does not include a CRS, we need to force one, and
080: // then ask for reprojection
081: ShapefileDataStore store = new ShapefileDataStore(shapeURL);
082: String name = store.getTypeNames()[0];
083: DefaultQuery q = new DefaultQuery(name);
084: q.setCoordinateSystem(originCrs);
085: q.setCoordinateSystemReproject(destCrs);
086: FeatureSource reprojectedSource = store.getView(q);
087:
088: // now we need to write out the reprojected features.
089: // first ask the user where to save data
090: result = fileChooser.showSaveDialog(null);
091:
092: if (result != JFileChooser.APPROVE_OPTION)
093: return;
094:
095: File f = fileChooser.getSelectedFile();
096: if (!f.getName().toLowerCase().endsWith(".shp")) {
097: f = new File(f.getAbsolutePath() + ".shp");
098: }
099:
100: // then create the destination data store and write them all to
101: // to the disk
102: ShapefileDataStore dest = new ShapefileDataStore(f.toURL());
103: dest.createSchema(reprojectedSource.getSchema());
104: FeatureStore writer = (FeatureStore) dest
105: .getFeatureSource();
106: FeatureCollection features = reprojectedSource
107: .getFeatures();
108: writer.setFeatures(DataUtilities.reader(features));
109:
110: System.out.println("Reprojected shapefile "
111: + f.getAbsolutePath() + " successfully written");
112: } catch (Exception e) {
113: System.out.println("Ops! Something went wrong :-(");
114: e.printStackTrace();
115: }
116:
117: System.exit(0);
118: }
119: }
|