001: package org.geotools.demo;
002:
003: import java.io.File;
004: import java.io.FileNotFoundException;
005: import java.util.HashMap;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import javax.swing.JFileChooser;
010: import javax.swing.JOptionPane;
011: import javax.swing.filechooser.FileFilter;
012:
013: import org.geotools.data.DataStore;
014: import org.geotools.data.DataStoreFactorySpi;
015: import org.geotools.data.DataStoreFinder;
016: import org.geotools.data.DataUtilities;
017: import org.geotools.data.DefaultQuery;
018: import org.geotools.data.DefaultTransaction;
019: import org.geotools.data.FeatureSource;
020: import org.geotools.data.FeatureStore;
021: import org.geotools.data.Transaction;
022: import org.geotools.data.shapefile.ShapefileDataStoreFactory;
023: import org.geotools.factory.GeoTools;
024: import org.geotools.feature.FeatureCollection;
025: import org.geotools.feature.FeatureType;
026: import org.geotools.referencing.CRS;
027: import org.opengis.referencing.crs.CoordinateReferenceSystem;
028:
029: public class Shp2Shp {
030: public static void main(String[] args) throws Exception {
031: System.out.println("Welcome to GeoTools:"
032: + GeoTools.getVersion());
033:
034: File file = getShapeFile(args);
035:
036: Map connect = new HashMap();
037: connect.put("url", file.toURI().toURL());
038:
039: DataStore dataStore = DataStoreFinder.getDataStore(connect);
040: String[] typeNames = dataStore.getTypeNames();
041: String typeName = typeNames[0];
042:
043: System.out.println("Opening file " + typeName);
044: FeatureSource featureSource = dataStore
045: .getFeatureSource(typeName);
046:
047: FeatureType schema = featureSource.getSchema();
048: System.out.println("Header: " + DataUtilities.spec(schema));
049:
050: DefaultQuery query = new DefaultQuery();
051: query.setTypeName(typeName);
052:
053: CoordinateReferenceSystem prj = schema.getDefaultGeometry()
054: .getCoordinateSystem();
055: if (prj == null) {
056: prj = getCoordinateReferenceSystem("No projection fround for "
057: + file + " please choose one:");
058: query.setCoordinateSystem(prj);
059: }
060:
061: CoordinateReferenceSystem crs = getCoordinateReferenceSystem("Project "
062: + file + " to:");
063: query.setCoordinateSystemReproject(crs);
064:
065: FeatureCollection collection = featureSource.getFeatures(query);
066: File newFile = getNewShapeFile(file);
067:
068: DataStoreFactorySpi factory = new ShapefileDataStoreFactory();
069:
070: Map create = new HashMap();
071: create.put("url", newFile.toURI().toURL());
072: create.put("create spatial index", Boolean.TRUE);
073: DataStore newDataStore = factory.createNewDataStore(create);
074:
075: newDataStore.createSchema(collection.getSchema());
076: Transaction transaction = new DefaultTransaction("Reproject");
077: FeatureStore featureStore = (FeatureStore) newDataStore
078: .getFeatureSource(typeName);
079: featureStore.setTransaction(transaction);
080: try {
081: featureStore.addFeatures(collection);
082: transaction.commit();
083: } catch (Exception problem) {
084: problem.printStackTrace();
085: transaction.rollback();
086: } finally {
087: transaction.close();
088: }
089: System.exit(0);
090: }
091:
092: private static File getNewShapeFile(File file) {
093: String path = file.getAbsolutePath();
094: String newPath = path.substring(0, path.length() - 4) + "2.shp";
095:
096: JFileChooser chooser = new JFileChooser();
097: chooser.setDialogTitle("Save reprojected shapefile");
098: chooser.setSelectedFile(new File(newPath));
099: chooser.setFileFilter(new FileFilter() {
100: public boolean accept(File f) {
101: return f.isDirectory() || f.getPath().endsWith("shp")
102: || f.getPath().endsWith("SHP");
103: }
104:
105: public String getDescription() {
106: return "Shapefiles";
107: }
108: });
109: int returnVal = chooser.showSaveDialog(null);
110:
111: if (returnVal != JFileChooser.APPROVE_OPTION) {
112: System.exit(0);
113: }
114: File newFile = chooser.getSelectedFile();
115: if (newFile.equals(file)) {
116: System.out.println("Cannot replace " + file);
117: System.exit(0);
118: }
119: return newFile;
120: }
121:
122: private static CoordinateReferenceSystem getCoordinateReferenceSystem(
123: String message) throws Exception {
124: Set codes = CRS.getSupportedCodes("EPSG");
125: String selected = (String) JOptionPane.showInputDialog(null,
126: message, "Choose a Projection",
127: JOptionPane.QUESTION_MESSAGE, null, codes.toArray(),
128: "4326");
129: if (selected == null) {
130: System.exit(0);
131: }
132: return CRS.decode("EPSG:" + selected);
133: }
134:
135: private static File getShapeFile(String[] args)
136: throws FileNotFoundException {
137: File file;
138: if (args.length == 0) {
139: JFileChooser chooser = new JFileChooser();
140: chooser.setDialogTitle("Open Shapefile for Reprojection");
141: chooser.setFileFilter(new FileFilter() {
142: public boolean accept(File f) {
143: return f.isDirectory()
144: || f.getPath().endsWith("shp")
145: || f.getPath().endsWith("SHP");
146: }
147:
148: public String getDescription() {
149: return "Shapefiles";
150: }
151: });
152: int returnVal = chooser.showOpenDialog(null);
153:
154: if (returnVal != JFileChooser.APPROVE_OPTION) {
155: System.exit(0);
156: }
157: file = chooser.getSelectedFile();
158:
159: System.out.println("You chose to open this file: "
160: + file.getName());
161: } else {
162: file = new File(args[0]);
163: }
164: if (!file.exists()) {
165: throw new FileNotFoundException(file.getAbsolutePath());
166: }
167: return file;
168: }
169: }
|