Source Code Cross Referenced for ShapeReader.java in  » GIS » GeoTools-2.4.1 » org » geotools » demo » data » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » GIS » GeoTools 2.4.1 » org.geotools.demo.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.