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.io.IOException;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.swing.JFileChooser;
027:
028: import org.geotools.data.DataStore;
029: import org.geotools.data.DataStoreFactorySpi;
030: import org.geotools.data.DataStoreFinder;
031: import org.geotools.data.FeatureSource;
032: import org.geotools.data.DataStoreFactorySpi.Param;
033: import org.geotools.data.shapefile.ShapefileDataStore;
034: import org.geotools.feature.AttributeType;
035: import org.geotools.feature.Feature;
036: import org.geotools.feature.FeatureCollection;
037: import org.geotools.feature.FeatureType;
038: import org.geotools.feature.GeometryAttributeType;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041:
042: /**
043: * A demo of basic reading abilities exemplified by the shapefile access system.
044: * The demo opens a file, gets the feature type, reads the first ten features
045: * and outputs their contents to the standard output.
046: *
047: * @author aaime
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/demo/data/src/main/java/org/geotools/demo/data/ShapeReader.java $
049: */
050: public class ShapeReader {
051:
052: private static URL getResource(String path) {
053: return ShapeReader.class.getResource(path);
054: }
055:
056: public static void main(String[] args) {
057: try {
058: URL url = null;
059: if (args.length == 1) {
060: url = getResource(args[0]);
061: } else {
062: // What shapefile (or database) do you want?
063: url = openURL();
064: }
065: // Open a connection to the shapefile (or database...)
066: DataStore dataStore = openShapefile3(url);
067:
068: String typeName = dataStore.getTypeNames()[0];
069: FeatureSource featureSource = dataStore
070: .getFeatureSource(typeName);
071: FeatureCollection featureCollection = featureSource
072: .getFeatures();
073:
074: FeatureType featureType = featureSource.getSchema();
075: System.out.println("FID\t");
076:
077: // print out the normal (non geometry) attributes
078: //
079: for (int i = 0; i < featureType.getAttributeCount(); i++) {
080: AttributeType attributeType = featureType
081: .getAttributeType(i);
082:
083: if (!(attributeType instanceof GeometryAttributeType)) {
084: System.out.print(attributeType.getType().getName()
085: + "\t");
086: }
087: }
088:
089: System.out.println();
090: // print out the geometry attributes
091: //
092: for (int i = 0; i < featureType.getAttributeCount(); i++) {
093: AttributeType at = featureType.getAttributeType(i);
094:
095: if (at instanceof GeometryAttributeType) {
096: System.out.print(at.getName() + "\t");
097: }
098: }
099:
100: System.out.println();
101:
102: // now print out the first 10 features (non geometric attribute)
103: //
104: Iterator iterator = featureCollection.iterator();
105: try {
106: for (int count = 0; iterator.hasNext(); count++) {
107: Feature feature = (Feature) iterator.next();
108: System.out.print(feature.getID() + "\t");
109:
110: for (int i = 0; i < feature.getNumberOfAttributes(); i++) {
111: Object attribute = feature.getAttribute(i);
112:
113: if (!(attribute instanceof Geometry)) {
114: System.out.print(attribute + "\t");
115: }
116: }
117: System.out.println();
118: if (count == 10)
119: break; // only 10 please
120: }
121: } finally {
122: featureCollection.close(iterator);
123: }
124: System.out.println();
125:
126: // and finally print out every geometry in wkt format
127: iterator = featureCollection.iterator();
128: try {
129: for (int count = 0; iterator.hasNext(); count++) {
130: Feature feature = (Feature) iterator.next();
131: System.out.print(feature.getID() + "\t");
132: System.out.println(feature.getDefaultGeometry());
133: System.out.println();
134:
135: if (count == 10)
136: break; // only 10
137: }
138: } finally {
139: featureCollection.close(iterator);
140: }
141: } catch (Exception e) {
142: System.out.println("Ops! Something went wrong :-(");
143: e.printStackTrace();
144: }
145:
146: System.exit(0);
147: }
148:
149: /**
150: * This method will prompt the user for a shapefile.
151: * <p>
152: * This method will call System.exit() if the user presses cancel.
153: * </p>
154: *
155: * @return url to selected shapefile.
156: * @throws MalformedURLException
157: */
158: public static URL openURL() throws MalformedURLException {
159: URL shapeURL = null;
160: JFileChooser fileChooser = new JFileChooser();
161: fileChooser.setFileFilter(new SimpleFileFilter("shp",
162: "Shapefile"));
163:
164: int result = fileChooser.showOpenDialog(null);
165:
166: if (result == JFileChooser.APPROVE_OPTION) {
167: File f = fileChooser.getSelectedFile();
168: shapeURL = f.toURL();
169: } else {
170: System.out.println("Goodbye");
171: System.exit(0);
172: }
173: return shapeURL;
174: }
175:
176: /**
177: * This method will open a shapefile directly using the ShapefileDatastore.
178: * <p>
179: * Please note this is "wrong" and limits your code to working with
180: * shapefiles for no reason. Look at openShapefile2 for a better example.
181: * @see #openShapefile2
182: * @param shapeURL An URL for the shapefile
183: * @return the ShapefileDataStore which contains the shapefile data.
184: * @throws MalformedURLException
185: */
186: public static ShapefileDataStore openShapefile(URL shapeURL)
187: throws MalformedURLException {
188: return new ShapefileDataStore(shapeURL);
189: }
190:
191: /**
192: * You can use the DataStoreFinder to find additional kinds of data access (including shapefile).
193: * <p>
194: * This will check the available jars on your class path, and return the first one that can
195: * work with the provided url. This example goes through each data store, geoserver actually
196: * includes two datastores that can read shapefiles (one with indexing one without).
197: * </p>
198: * <p>
199: * For your amusement we print out the parameters for the datastore used.
200: * </p>
201: * @param url
202: * @return DataStore for the provided URL
203: * @throws IOException If connection could not be made
204: */
205: public static DataStore openShapefile2(URL url) throws IOException {
206: // Step 1: Connection Parameters
207: // We will place the url into a Map, some datastores require several paramters
208: // for now we will just use one.
209: Map params = new HashMap();
210: params.put("url", url);
211:
212: DataStoreFactorySpi found = null;
213: // Step 2: grab the list of available datastores
214: for (Iterator iterator = DataStoreFinder
215: .getAvailableDataStores(); iterator.hasNext();) {
216: DataStoreFactorySpi factory = (DataStoreFactorySpi) iterator
217: .next();
218: if (factory.canProcess(params)) {
219: System.out.println(factory.getDisplayName() + ":("
220: + factory.getDescription() + ")");
221: found = factory;
222: }
223: }
224: if (found != null) {
225: System.out.println("Using " + found.getDisplayName());
226: Param[] parameters = found.getParametersInfo();
227: for (int i = 0; i < parameters.length; i++) {
228: System.out.print(parameters[i]);
229: }
230: return found.createDataStore(params);
231: }
232: throw new IOException("Could not connect to:" + params);
233: }
234:
235: /**
236: * You can use the DataStoreFinder to find additional kinds of data access (including shapefile).
237: * <p>
238: * This will check the available jars on your class path, and return the first one that can
239: * work with the provided url.
240: * </p>
241: * @param url
242: * @return DataStore for the provided URL
243: * @throws IOException If connection could not be made
244: */
245: public static DataStore openShapefile3(URL url) throws IOException {
246: // Step 1: Connection Parameters
247: // We will place the url into a Map, some datastores require several paramters
248: // for now we will just use one.
249: Map params = new HashMap();
250: params.put("url", url);
251:
252: // Step 2: ask the DataStoreFinder for the "best" match
253: return DataStoreFinder.getDataStore(params);
254: }
255: }
|