Source Code Cross Referenced for DefaultFeatureResults.java in  » GIS » GeoTools-2.4.1 » org » geotools » 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.data 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *    GeoTools - OpenSource mapping toolkit
003:         *    http://geotools.org
004:         *    (C) 2003-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.data;
017:
018:        import java.io.IOException;
019:        import java.util.ArrayList;
020:        import java.util.List;
021:        import java.util.logging.Level;
022:        import java.util.logging.Logger;
023:
024:        import org.geotools.data.crs.ReprojectFeatureReader;
025:        import org.geotools.data.store.DataFeatureCollection;
026:        import org.geotools.feature.AttributeType;
027:        import org.geotools.feature.Feature;
028:        import org.geotools.feature.FeatureCollection;
029:        import org.geotools.feature.FeatureCollections;
030:        import org.geotools.feature.FeatureType;
031:        import org.geotools.feature.IllegalAttributeException;
032:        import org.geotools.feature.SchemaException;
033:        import org.geotools.feature.type.GeometricAttributeType;
034:        import org.geotools.geometry.jts.ReferencedEnvelope;
035:        import org.geotools.referencing.CRS;
036:        import org.opengis.referencing.FactoryException;
037:        import org.opengis.referencing.crs.CoordinateReferenceSystem;
038:        import org.opengis.referencing.operation.MathTransform;
039:
040:        import com.vividsolutions.jts.geom.Envelope;
041:
042:        /**
043:         * Generic "results" of a query, class.
044:         * <p>
045:         * Please optimize this class when use with your own content.
046:         * For example a "ResultSet" make a great cache for a JDBCDataStore,
047:         * a temporary copy of an original file may work for shapefile etc. 
048:         * </p>
049:         *
050:         * @author Jody Garnett, Refractions Research
051:         * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/data/DefaultFeatureResults.java $
052:         */
053:        public class DefaultFeatureResults extends DataFeatureCollection {
054:            /** Shared package logger */
055:            private static final Logger LOGGER = org.geotools.util.logging.Logging
056:                    .getLogger("org.geotools.data");
057:
058:            /** Query used to define this subset of features from the feature source */
059:            protected Query query;
060:
061:            /**
062:             * Feature source used to aquire features, note we are only a
063:             * "view" of this FeatureSource, its contents, transaction and events
064:             * need to be forwarded through this collection api to simplier code
065:             * such as renderers.
066:             */
067:            protected FeatureSource featureSource;
068:
069:            protected FeatureType schema;
070:            protected MathTransform transform;
071:
072:            /**
073:             * FeatureResults query against featureSource.
074:             * <p>
075:             * Please note that is object will not be valid
076:             * after the transaction has closed.
077:             * </p>
078:             * <p>
079:             * Really? I think it would be, it would just reflect the
080:             * same query against the featuresource using AUTO_COMMIT.
081:             * </p>
082:             * 
083:             * @param source
084:             * @param query
085:             */
086:            public DefaultFeatureResults(FeatureSource source, Query query)
087:                    throws IOException {
088:                this .featureSource = source;
089:                FeatureType origionalType = source.getSchema();
090:
091:                String typeName = origionalType.getTypeName();
092:                if (typeName.equals(query.getTypeName())) {
093:                    this .query = query;
094:                } else {
095:                    // jg: this should be an error, we are deliberatly gobbling a mistake
096:                    // option 1: remove Query.getTypeName
097:                    // option 2: throw a warning
098:                    // option 3: restore exception code
099:                    this .query = new DefaultQuery(query);
100:                    ((DefaultQuery) this .query).setTypeName(typeName);
101:                    //((DefaultQuery) this.query).setCoordinateSystem(query.getCoordinateSystem());
102:                    //((DefaultQuery) this.query).setCoordinateSystemReproject(query.getCoordinateSystemReproject());
103:                }
104:                CoordinateReferenceSystem cs = null;
105:                if (query.getCoordinateSystemReproject() != null) {
106:                    cs = query.getCoordinateSystemReproject();
107:                } else if (query.getCoordinateSystem() != null) {
108:                    cs = query.getCoordinateSystem();
109:                }
110:                try {
111:                    if (cs == null) {
112:                        if (query.retrieveAllProperties()) { // we can use the origionalType as is                
113:                            schema = featureSource.getSchema();
114:                        } else {
115:                            schema = DataUtilities.createSubType(featureSource
116:                                    .getSchema(), query.getPropertyNames());
117:                        }
118:                    } else {
119:                        // we need to change the projection of the origional type
120:                        schema = DataUtilities.createSubType(origionalType,
121:                                query.getPropertyNames(), cs, query
122:                                        .getTypeName(), null);
123:                    }
124:                } catch (SchemaException e) {
125:                    // we were unable to create the schema requested!
126:                    //throw new DataSourceException("Could not create schema", e);
127:                    LOGGER.log(Level.WARNING, "Could not change projection to "
128:                            + cs, e);
129:                    schema = null; // client will notice something is amiss when getSchema() return null
130:                }
131:                if (origionalType.getDefaultGeometry() == null) {
132:                    return; // no transform needed
133:                }
134:                CoordinateReferenceSystem origionalCRS = origionalType
135:                        .getDefaultGeometry().getCoordinateSystem();
136:                if (query.getCoordinateSystem() != null) {
137:                    origionalCRS = query.getCoordinateSystem();
138:                }
139:                if (cs != null && cs != origionalCRS) {
140:                    try {
141:                        transform = CRS.findMathTransform(origionalCRS, cs,
142:                                true);
143:                    } catch (FactoryException noTransform) {
144:                        throw (IOException) new IOException(
145:                                "Could not reproject data to " + cs)
146:                                .initCause(noTransform);
147:                    }
148:                }
149:            }
150:
151:            /**
152:             * FeatureSchema for provided query.
153:             *
154:             * <p>
155:             * If query.retrieveAllProperties() is <code>true</code> the FeatureSource
156:             * getSchema() will be returned.
157:             * </p>
158:             *
159:             * <p>
160:             * If query.getPropertyNames() is used to limit the result of the Query a
161:             * sub type will be returned based on FeatureSource.getSchema().
162:             * </p>
163:             *
164:             * @return DOCUMENT ME!
165:             *
166:             * @throws IOException DOCUMENT ME!
167:             * @throws DataSourceException DOCUMENT ME!
168:             */
169:            public FeatureType getSchema() {
170:                return schema;
171:            }
172:
173:            /**
174:             * Returns transaction from featureSource (if it is a FeatureStore), or
175:             * Transaction.AUTO_COMMIT if it is not.
176:             *
177:             * @return Transacstion this FeatureResults opperates against
178:             */
179:            protected Transaction getTransaction() {
180:                if (featureSource instanceof  FeatureStore) {
181:                    FeatureStore featureStore = (FeatureStore) featureSource;
182:
183:                    return featureStore.getTransaction();
184:                } else {
185:                    return Transaction.AUTO_COMMIT;
186:                }
187:            }
188:
189:            /**
190:             * Retrieve a FeatureReader for this Query
191:             *
192:             * @return FeatureReader for this Query
193:             *
194:             * @throws IOException If results could not be obtained
195:             */
196:            public FeatureReader reader() throws IOException {
197:                FeatureReader reader = featureSource.getDataStore()
198:                        .getFeatureReader(query, getTransaction());
199:
200:                int maxFeatures = query.getMaxFeatures();
201:                if (maxFeatures != Integer.MAX_VALUE) {
202:                    reader = new MaxFeatureReader(reader, maxFeatures);
203:                }
204:                if (transform != null) {
205:                    reader = new ReprojectFeatureReader(reader, schema,
206:                            transform);
207:                }
208:                return reader;
209:            }
210:
211:            /**
212:             * Retrieve a FeatureReader for the geometry attributes only, designed for bounds computation
213:             */
214:            protected FeatureReader boundsReader() throws IOException {
215:                List attributes = new ArrayList();
216:                FeatureType schema = featureSource.getSchema();
217:                for (int i = 0; i < schema.getAttributeCount(); i++) {
218:                    AttributeType at = schema.getAttributeType(i);
219:                    if (at instanceof  GeometricAttributeType)
220:                        attributes.add(at.getName());
221:                }
222:
223:                DefaultQuery q = new DefaultQuery(query);
224:                q.setPropertyNames(attributes);
225:                FeatureReader reader = featureSource.getDataStore()
226:                        .getFeatureReader(q, getTransaction());
227:                int maxFeatures = query.getMaxFeatures();
228:
229:                if (maxFeatures == Integer.MAX_VALUE) {
230:                    return reader;
231:                } else {
232:                    return new MaxFeatureReader(reader, maxFeatures);
233:                }
234:            }
235:
236:            /**
237:             * Returns the bounding box of this FeatureResults
238:             *
239:             * <p>
240:             * This implementation will generate the correct results from reader() if
241:             * the provided FeatureSource does not provide an optimized result via
242:             * FeatureSource.getBounds( Query ).
243:             * </p>
244:             * If the feature has no geometry, then an empty envelope is returned.
245:             *
246:             *
247:             * @throws DataSourceException See IOException
248:             *
249:             * @see org.geotools.data.FeatureResults#getBounds()
250:             */
251:            public ReferencedEnvelope getBounds() {
252:                Envelope bounds;
253:
254:                try {
255:                    bounds = featureSource.getBounds(query);
256:                } catch (IOException e1) {
257:                    bounds = new Envelope();
258:                }
259:
260:                if (bounds == null) {
261:                    try {
262:                        Feature feature;
263:                        bounds = new Envelope();
264:
265:                        FeatureReader reader = boundsReader();
266:
267:                        while (reader.hasNext()) {
268:                            feature = reader.next();
269:                            bounds.expandToInclude(feature.getBounds());
270:                        }
271:
272:                        reader.close();
273:                    } catch (IllegalAttributeException e) {
274:                        //throw new DataSourceException("Could not read feature ", e);
275:                        bounds = new Envelope();
276:                    } catch (IOException e) {
277:                        bounds = new Envelope();
278:                    }
279:                }
280:
281:                return ReferencedEnvelope.reference(bounds);
282:            }
283:
284:            /**
285:             * Number of Features in this query.
286:             *
287:             * <p>
288:             * This implementation will generate the correct results from reader() if
289:             * the provided FeatureSource does not provide an optimized result via
290:             * FeatureSource.getCount( Query ).
291:             * </p>
292:             *
293:             *
294:             * @throws IOException If feature could not be read
295:             * @throws DataSourceException See IOException
296:             *
297:             * @see org.geotools.data.FeatureResults#getCount()
298:             */
299:            public int getCount() throws IOException {
300:                int count;
301:                count = featureSource.getCount(query);
302:
303:                if (count != -1) {
304:                    // optimization worked, return maxFeatures if count is
305:                    // greater.
306:                    int maxFeatures = query.getMaxFeatures();
307:                    return (count < maxFeatures) ? count : maxFeatures;
308:                }
309:
310:                // Okay lets count the FeatureReader
311:                try {
312:                    count = 0;
313:
314:                    FeatureReader reader = reader();
315:
316:                    for (; reader.hasNext(); count++) {
317:                        reader.next();
318:                    }
319:
320:                    reader.close();
321:
322:                    return count;
323:                } catch (IllegalAttributeException e) {
324:                    throw new DataSourceException("Could not read feature ", e);
325:                }
326:            }
327:
328:            public FeatureCollection collection() throws IOException {
329:                try {
330:                    FeatureCollection collection = FeatureCollections
331:                            .newCollection();
332:                    //Feature feature;
333:                    FeatureReader reader = reader();
334:                    //FeatureType type = reader.getFeatureType();
335:                    while (reader.hasNext()) {
336:                        collection.add(reader.next());
337:                    }
338:                    reader.close();
339:
340:                    return collection;
341:                } catch (IllegalAttributeException e) {
342:                    throw new DataSourceException("Could not read feature ", e);
343:                }
344:            }
345:
346:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.