001: /*
002: * Geotools2 - 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;
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: */
017: package org.geotools.data.feature.memory;
018:
019: import java.io.IOException;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.Set;
024:
025: import org.geotools.catalog.GeoResourceInfo;
026: import org.geotools.data.DataStore;
027: import org.geotools.data.FeatureListener;
028: import org.geotools.data.Query;
029: import org.geotools.data.Transaction;
030: import org.geotools.data.feature.FeatureSource2;
031: import org.geotools.feature.FeatureCollection;
032: import org.geotools.feature.iso.collection.AbstractFeatureCollection;
033: import org.geotools.feature.iso.collection.MemorySimpleFeatureCollection;
034: import org.geotools.geometry.jts.ReferencedEnvelope;
035: import org.opengis.feature.Feature;
036: import org.opengis.feature.type.FeatureCollectionType;
037: import org.opengis.feature.type.FeatureType;
038: import org.opengis.feature.type.Name;
039: import org.opengis.filter.Filter;
040: import org.opengis.filter.capability.FilterCapabilities;
041:
042: import com.vividsolutions.jts.geom.Envelope;
043:
044: /**
045: *
046: * @author Gabriel Roldan, Axios Engineering
047: * @version $Id: MemorySource.java 26609 2007-08-20 15:29:33Z groldan $
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/feature/memory/MemorySource.java $
049: * @since 2.4
050: */
051: public class MemorySource implements FeatureSource2 {
052:
053: private FeatureType type;
054:
055: private Collection content;
056:
057: private MemoryDataAccess dataStore;
058:
059: public MemorySource(MemoryDataAccess dataStore, FeatureType type,
060: Collection collection) {
061: this .dataStore = dataStore;
062: this .type = type;
063: this .content = collection;
064: }
065:
066: public Collection content() {
067: MemorySimpleFeatureCollection collection = new MemorySimpleFeatureCollection(
068: null, null);
069: collection.addAll(content);
070: return collection;
071: }
072:
073: public Collection content(String query, String queryLanguage) {
074: throw new UnsupportedOperationException();
075: }
076:
077: public Collection content(Filter filter) {
078: return content(filter, Integer.MAX_VALUE);
079: }
080:
081: private class LimitingFeatureCollection extends
082: AbstractFeatureCollection {
083:
084: private final Collection contents;
085:
086: private final int maxFeatures;
087:
088: private int cachedSize = Integer.MIN_VALUE;
089:
090: public LimitingFeatureCollection(Collection contents,
091: int maxFeatures) {
092: super (null, (FeatureCollectionType) null, null);
093: this .contents = contents;
094: this .maxFeatures = maxFeatures;
095: }
096:
097: protected Iterator openIterator() throws IOException {
098: return new LimitingIterator();
099: }
100:
101: protected void closeIterator(Iterator close) throws IOException {
102: LimitingIterator iterator = (LimitingIterator) close;
103: iterator.close();
104: }
105:
106: public int size() {
107: if (cachedSize == Integer.MIN_VALUE) {
108: int contentSize = contents.size();
109: cachedSize = Math.min(contentSize, maxFeatures);
110: }
111: return cachedSize;
112: }
113:
114: /**
115: * Iterator wraper that limits the number of features to maxFeatures
116: */
117: class LimitingIterator implements Iterator {
118: private int count = 0;
119:
120: Iterator subject = LimitingFeatureCollection.this .contents
121: .iterator();
122:
123: public boolean hasNext() {
124: boolean hasNext = subject.hasNext();
125: return hasNext
126: && count <= LimitingFeatureCollection.this .maxFeatures;
127: }
128:
129: public Object next() {
130: Object next = subject.next();
131: count++;
132: return next;
133: }
134:
135: public void remove() {
136: throw new UnsupportedOperationException();
137: }
138:
139: public void close() {
140: ((org.opengis.feature.FeatureCollection) LimitingFeatureCollection.this .contents)
141: .close(subject);
142: }
143: }
144: }
145:
146: public Collection content(Filter filter, final int countLimit) {
147: org.opengis.feature.FeatureCollection collection = (org.opengis.feature.FeatureCollection) content();
148: if (!Filter.INCLUDE.equals(filter)) {
149: collection = collection.subCollection(filter);
150: }
151: if (countLimit < Integer.MAX_VALUE) {
152: collection = new LimitingFeatureCollection(
153: (Collection) collection, countLimit);
154: }
155: return (Collection) collection;
156: }
157:
158: public Object describe() {
159: return dataStore.describe(type.getName());
160: }
161:
162: public void dispose() {
163: }
164:
165: public FilterCapabilities getFilterCapabilities() {
166: throw new UnsupportedOperationException();
167: }
168:
169: public GeoResourceInfo getInfo() {
170: throw new UnsupportedOperationException();
171: }
172:
173: public Name getName() {
174: return type.getName();
175: }
176:
177: public void setTransaction(Transaction t) {
178: throw new UnsupportedOperationException();
179: }
180:
181: public void addFeatureListener(FeatureListener listener) {
182: throw new UnsupportedOperationException();
183: }
184:
185: public Envelope getBounds() throws IOException {
186: return getBounds(Filter.INCLUDE);
187: }
188:
189: public Envelope getBounds(Query query) throws IOException {
190: return getBounds(query.getFilter());
191: }
192:
193: private Envelope getBounds(Filter filter) throws IOException {
194: org.opengis.feature.FeatureCollection collection = (org.opengis.feature.FeatureCollection) content(filter);
195: Feature f;
196: ReferencedEnvelope env = new ReferencedEnvelope(this .type
197: .getCRS());
198: Iterator it = collection.iterator();
199: for (; it.hasNext();) {
200: f = (Feature) it.next();
201: env.include(f.getBounds());
202: }
203: collection.close(it);
204: return env;
205: }
206:
207: public int getCount(Query query) throws IOException {
208: org.opengis.feature.FeatureCollection collection;
209: collection = (org.opengis.feature.FeatureCollection) content(query
210: .getFilter());
211: int count = 0;
212: Iterator it = collection.iterator();
213: for (; it.hasNext();) {
214: it.next();
215: count++;
216: }
217: collection.close(it);
218: return count;
219: }
220:
221: public DataStore getDataStore() {
222: return dataStore;
223: }
224:
225: public FeatureCollection getFeatures(Query query)
226: throws IOException {
227: throw new UnsupportedOperationException();
228: }
229:
230: public FeatureCollection getFeatures(Filter filter)
231: throws IOException {
232: throw new UnsupportedOperationException();
233: }
234:
235: public FeatureCollection getFeatures() throws IOException {
236: throw new UnsupportedOperationException();
237: }
238:
239: public org.geotools.feature.FeatureType getSchema() {
240: throw new UnsupportedOperationException();
241: }
242:
243: public void removeFeatureListener(FeatureListener listener) {
244: throw new UnsupportedOperationException();
245: }
246:
247: public Set getSupportedHints() {
248: return Collections.EMPTY_SET;
249: }
250:
251: }
|