001: package org.geotools.feature.iso.collection;
002:
003: import java.util.Collection;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import org.geotools.feature.iso.FeatureCollections;
008: import org.opengis.feature.Feature;
009: import org.opengis.feature.FeatureCollection;
010: import org.opengis.feature.FeatureVisitor;
011: import org.opengis.feature.GeometryAttribute;
012: import org.opengis.feature.type.AttributeDescriptor;
013: import org.opengis.feature.type.AttributeType;
014: import org.opengis.feature.type.FeatureCollectionType;
015: import org.opengis.feature.type.Name;
016: import org.opengis.feature.type.PropertyDescriptor;
017: import org.opengis.filter.Filter;
018: import org.opengis.filter.FilterFactory;
019: import org.opengis.geometry.BoundingBox;
020: import org.opengis.referencing.crs.CoordinateReferenceSystem;
021: import org.opengis.util.ProgressListener;
022:
023: /**
024: * Used as a reasonable default implementation for subCollection.
025: * <p>
026: * Note: to implementors, this is not optimal, please do your own thing - your
027: * users will thank you.
028: * </p>
029: *
030: * @author Jody Garnett, Refractions Research, Inc.
031: * @author Justin Deoliveira, The Open Planning Project
032: */
033: public class SubFeatureCollection extends AbstractResourceCollection
034: implements FeatureCollection {
035: /**
036: * The unfiltered feature collection.
037: */
038: protected FeatureCollection collection;
039:
040: /**
041: * Filters the unfiltered feature collection.
042: */
043: protected Filter filter;
044:
045: /**
046: * Filter used to contstruct new filters.
047: */
048: protected FilterFactory factory;
049:
050: public SubFeatureCollection(FeatureCollection collection,
051: Filter filter, FilterFactory factory) {
052: if (filter.equals(Filter.EXCLUDE)) {
053: throw new IllegalArgumentException(
054: "A subcollection with Filter.EXCLUDE is a null operation");
055: }
056: if (filter.equals(Filter.INCLUDE)) {
057: throw new IllegalArgumentException(
058: "A subcollection with Filter.INCLUDE should be a FeatureCollectionEmpty");
059: }
060: if (collection instanceof SubFeatureCollection) {
061: SubFeatureCollection sub = (SubFeatureCollection) collection;
062: collection = sub.collection;
063: this .filter = factory.and(sub.filter, filter);
064: } else {
065: this .collection = collection;
066: this .filter = filter;
067: }
068: }
069:
070: public int size() {
071: int count = 0;
072: for (Iterator itr = this .collection.iterator(); itr.hasNext();) {
073: Feature f = (Feature) itr.next();
074: if (this .filter.evaluate(f)) {
075: count++;
076: }
077: }
078: return count;
079: }
080:
081: protected Iterator openIterator() {
082: Iterator/* <Feature> */filtered = new FilteredIterator(
083: collection, filter);
084: return filtered;
085: }
086:
087: protected void closeIterator(Iterator close) {
088: if (close instanceof FilteredIterator) {
089: ((FilteredIterator) close).close();
090: } else {
091: collection.close(close);
092: }
093: }
094:
095: public FeatureCollection subCollection(
096: org.opengis.filter.Filter filter) {
097: return new SubFeatureCollection(this , filter, factory);
098: }
099:
100: public FeatureCollection sort(org.opengis.filter.sort.SortBy order) {
101: // return new SubFeatureList(this,filter,factory,order);
102: throw new UnsupportedOperationException();
103: }
104:
105: public boolean nillable() {
106: return collection.nillable();
107: }
108:
109: public Collection memberTypes() {
110: return collection.memberTypes();
111: }
112:
113: public void putUserData(Object key, Object value) {
114: collection.putUserData(key, value);
115:
116: }
117:
118: public Object getUserData(Object key) {
119: return collection.getUserData(key);
120: }
121:
122: public String getID() {
123: return collection.getID();
124: }
125:
126: public CoordinateReferenceSystem getCRS() {
127: return collection.getCRS();
128: }
129:
130: public void setCRS(CoordinateReferenceSystem crs) {
131: collection.setCRS(crs);
132: }
133:
134: public BoundingBox getBounds() {
135: return FeatureCollections.getBounds(iterator());
136: }
137:
138: public GeometryAttribute getDefaultGeometry() {
139: return collection.getDefaultGeometry();
140: }
141:
142: public void setDefaultGeometry(GeometryAttribute geom) {
143: collection.setDefaultGeometry(geom);
144:
145: }
146:
147: public void setValue(List newValue) throws IllegalArgumentException {
148: collection.setValue(newValue);
149:
150: }
151:
152: public List getValue(Name name) {
153: return collection.get(name);
154: }
155:
156: public AttributeType getType() {
157: return collection.getType();
158: }
159:
160: public AttributeDescriptor getDescriptor() {
161: return collection.getDescriptor();
162: }
163:
164: public PropertyDescriptor descriptor() {
165: return collection.descriptor();
166: }
167:
168: public Name name() {
169: return collection.name();
170: }
171:
172: public Object getValue() {
173: return collection.getValue();
174: }
175:
176: public Collection attributes() {
177: return collection.attributes();
178: }
179:
180: public Collection associations() {
181: return collection.associations();
182: }
183:
184: public void setValue(Object newValue)
185: throws IllegalArgumentException {
186: collection.setValue(newValue);
187: }
188:
189: /**
190: * Accepts a visitor, which then visits each feature in the collection.
191: */
192: public void accepts(FeatureVisitor visitor,
193: ProgressListener progress) {
194: Iterator iterator = null;
195: // if( progress == null ) progress = new NullProgressListener();
196: try {
197: float size = size();
198: float position = 0;
199: progress.started();
200: visitor.init((FeatureCollectionType) collection.getType());
201: for (iterator = iterator(); !progress.isCanceled()
202: && iterator.hasNext(); progress.progress(position++
203: / size)) {
204: try {
205: Feature feature = (Feature) iterator.next();
206: visitor.visit(feature);
207: } catch (Exception erp) {
208: progress.exceptionOccurred(erp);
209: }
210: }
211: } finally {
212: progress.complete();
213: close(iterator);
214: }
215: }
216:
217: public Object operation(Name arg0, List arg1) {
218: throw new UnsupportedOperationException(
219: "operation not supported yet");
220: }
221:
222: /**
223: * TODO: implement
224: */
225: public List get(Name name) {
226: throw new UnsupportedOperationException(
227: "operation not supported yet");
228: }
229:
230: }
|