001: package org.geotools.feature.iso.collection;
002:
003: import java.util.Collection;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009:
010: import org.opengis.feature.Feature;
011: import org.opengis.feature.FeatureCollection;
012: import org.opengis.feature.FeatureVisitor;
013: import org.opengis.feature.GeometryAttribute;
014: import org.opengis.feature.simple.SimpleFeatureCollection;
015: import org.opengis.feature.simple.SimpleFeatureCollectionType;
016: import org.opengis.feature.type.AttributeDescriptor;
017: import org.opengis.feature.type.AttributeType;
018: import org.opengis.feature.type.FeatureType;
019: import org.opengis.feature.type.Name;
020: import org.opengis.feature.type.PropertyDescriptor;
021: import org.opengis.geometry.BoundingBox;
022: import org.opengis.referencing.crs.CoordinateReferenceSystem;
023: import org.opengis.util.ProgressListener;
024:
025: /**
026: * Pure implementation of SimpleFeatureCollection.
027: * <p>
028: * Because a SimpleFeatureCollection cannot contain attributes many methods are
029: * stubbed to return appropriate null values, or throw
030: * IllegalArgumentExceptions.
031: * </p>
032: * To make use of this class pelase extend with the following:
033: * <ul>
034: * <li>openIterator()
035: * <li>closeIterator()
036: * <li>size()
037: * </ul>
038: *
039: * @author Jody Garnett (Refractions Research)
040: */
041: public abstract class AbstractSimpleFeatureCollection extends
042: AbstractResourceCollection implements SimpleFeatureCollection {
043:
044: SimpleFeatureCollectionType type;
045:
046: String id;
047:
048: BoundingBox bbox;
049:
050: Map userData;
051:
052: public AbstractSimpleFeatureCollection(
053: SimpleFeatureCollectionType type, String id) {
054: this .type = type;
055: this .id = id;
056: this .bbox = null;
057: }
058:
059: public FeatureType getMemberType() {
060: return type.getMemberType();
061: }
062:
063: public Object getUserData(Object key) {
064: if (userData != null) {
065: return userData.get(key);
066: }
067: return null;
068: }
069:
070: public void putUserData(Object key, Object value) {
071: if (userData == null) {
072: userData = new HashMap();
073: }
074: userData.put(key, value);
075: }
076:
077: public FeatureCollection subCollection(
078: org.opengis.filter.Filter filter) {
079: return new SubFeatureCollection(this , filter, null);
080: }
081:
082: public FeatureCollection sort(org.opengis.filter.sort.SortBy order) {
083: throw new UnsupportedOperationException();
084: }
085:
086: public Collection memberTypes() {
087: return Collections.singleton(type);
088: }
089:
090: public void putClientProperty(Object key, Object value) {
091: }
092:
093: public Object getClientProperty(Object key) {
094: return null;
095: }
096:
097: public SimpleFeatureCollectionType getFeatureCollectionType() {
098: return type;
099: }
100:
101: public AttributeType getType() {
102: return getFeatureCollectionType();
103: }
104:
105: public boolean nillable() {
106: return false;
107: }
108:
109: public String getID() {
110: return id;
111: }
112:
113: public CoordinateReferenceSystem getCRS() {
114: return type.getCRS();
115: }
116:
117: public void setCRS(CoordinateReferenceSystem crs) {
118: throw new UnsupportedOperationException();
119: }
120:
121: /**
122: * Gets the bounding box for the features in this feature collection.
123: *
124: * @return the envelope of the geometries contained by this feature
125: * collection.
126: */
127: public BoundingBox getBounds() {
128: /*
129: if (bbox == null) {
130: BoundsVisitor bounds = new BoundsVisitor();
131:
132: accepts(bounds, null);
133: bbox = bounds.getBounds();
134: }
135: */
136: return bbox;
137: }
138:
139: public GeometryAttribute getDefaultGeometry() {
140: return null;
141: }
142:
143: public void setDefaultGeometry(GeometryAttribute geom) {
144: }
145:
146: public void setValue(List newValue) {
147: throw new IllegalArgumentException(
148: "SimpleFeatureCollection does not support properties");
149: }
150:
151: public List getValue(Name name) {
152: return Collections.EMPTY_LIST;
153: }
154:
155: public AttributeDescriptor getDescriptor() {
156: return null;
157: }
158:
159: public PropertyDescriptor descriptor() {
160: return null;
161: }
162:
163: public Name name() {
164: return type.getName();
165: }
166:
167: public Object getValue() {
168: return Collections.EMPTY_LIST;
169: }
170:
171: public Collection attributes() {
172: return Collections.EMPTY_LIST;
173: }
174:
175: public Collection associations() {
176: return Collections.EMPTY_LIST;
177: }
178:
179: public void setValue(Object newValue)
180: throws IllegalArgumentException {
181: throw new IllegalArgumentException(
182: "SimpleFeatureCollection does not support properties");
183: }
184:
185: /**
186: * Accepts a visitor, which then visits each feature in the collection.
187: */
188: public void accepts(FeatureVisitor visitor,
189: ProgressListener progress) {
190: Iterator iterator = null;
191: // if( progress == null ) progress = new NullProgressListener();
192: try {
193: float size = size();
194: float position = 0;
195: progress.started();
196: visitor.init(type);
197: for (iterator = iterator(); !progress.isCanceled()
198: && iterator.hasNext(); progress.progress(position++
199: / size)) {
200: try {
201: Feature feature = (Feature) iterator.next();
202: visitor.visit(feature);
203: } catch (Exception erp) {
204: progress.exceptionOccurred(erp);
205: }
206: }
207: } finally {
208: progress.complete();
209: close(iterator);
210: }
211: }
212: }
|