001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-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; either
009: * version 2.1 of the License, or (at your option) any later version.
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.renderer.lite;
017:
018: import java.io.IOException;
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.NoSuchElementException;
022:
023: import org.geotools.data.FeatureReader;
024: import org.geotools.data.store.DataFeatureCollection;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.FeatureCollections;
028: import org.geotools.feature.FeatureIterator;
029: import org.geotools.feature.FeatureType;
030: import org.geotools.feature.IllegalAttributeException;
031: import org.geotools.geometry.jts.ReferencedEnvelope;
032:
033: import com.vividsolutions.jts.geom.Envelope;
034: import com.vividsolutions.jts.index.strtree.STRtree;
035:
036: /**
037: * IndexedFeatureReader
038: *
039: * @author wolf
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/render/src/main/java/org/geotools/renderer/lite/IndexedFeatureResults.java $
041: */
042: public final class IndexedFeatureResults extends DataFeatureCollection {
043: STRtree index = new STRtree();
044: FeatureType schema;
045: Envelope bounds;
046: int count;
047: private Envelope queryBounds;
048:
049: public IndexedFeatureResults(FeatureCollection results)
050: throws IOException, IllegalAttributeException {
051: // copy results attributes
052: this .schema = results.getSchema();
053:
054: // load features into the index
055: FeatureIterator reader = null;
056: bounds = new Envelope();
057: count = 0;
058: try {
059: reader = results.features();
060: Feature f;
061: Envelope env;
062: while (reader.hasNext()) {
063: f = reader.next();
064: env = f.getDefaultGeometry().getEnvelopeInternal();
065: bounds.expandToInclude(env);
066: count++;
067: index.insert(env, f);
068: }
069: } finally {
070: if (reader != null)
071: reader.close();
072: }
073: }
074:
075: /**
076: * @see org.geotools.data.FeatureResults#getSchema()
077: */
078: public FeatureType getSchema() {
079: return this .schema;
080: }
081:
082: /**
083: * @see org.geotools.data.FeatureResults#reader()
084: */
085: public FeatureReader reader(Envelope envelope) throws IOException {
086: List results = index.query(envelope);
087: final Iterator resultsIterator = results.iterator();
088:
089: return new FeatureReader() {
090: /**
091: * @see org.geotools.data.FeatureReader#getFeatureType()
092: */
093: public FeatureType getFeatureType() {
094: return schema;
095: }
096:
097: /**
098: * @see org.geotools.data.FeatureReader#next()
099: */
100: public Feature next() throws IOException,
101: IllegalAttributeException, NoSuchElementException {
102: return (Feature) resultsIterator.next();
103: }
104:
105: /**
106: * @see org.geotools.data.FeatureReader#hasNext()
107: */
108: public boolean hasNext() throws IOException {
109: return resultsIterator.hasNext();
110: }
111:
112: /**
113: * @see org.geotools.data.FeatureReader#close()
114: */
115: public void close() throws IOException {
116: }
117: };
118: }
119:
120: /**
121: * @see org.geotools.data.FeatureResults#getBounds()
122: */
123: public ReferencedEnvelope getBounds() {
124: return ReferencedEnvelope.reference(bounds);
125: }
126:
127: /**
128: * @see org.geotools.data.FeatureResults#getCount()
129: */
130: public int getCount() throws IOException {
131: return count;
132: }
133:
134: /**
135: * @see org.geotools.data.FeatureResults#collection()
136: */
137: public FeatureCollection collection() throws IOException {
138: FeatureCollection fc = FeatureCollections.newCollection();
139: List results = index.query(bounds);
140: for (Iterator it = results.iterator(); it.hasNext();) {
141: fc.add(it.next());
142: }
143: return fc;
144: }
145:
146: /**
147: * @see org.geotools.data.FeatureResults#reader()
148: */
149: public FeatureReader reader() throws IOException {
150: if (queryBounds != null)
151: return reader(queryBounds);
152: else
153: return reader(bounds);
154: }
155:
156: /**
157: * @param queryBounds an Envelope defining the boundary of the query
158: *
159: */
160: public void setQueryBounds(Envelope queryBounds) {
161: this.queryBounds = queryBounds;
162: }
163: }
|