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.complex;
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.DefaultQuery;
028: import org.geotools.data.FeatureListener;
029: import org.geotools.data.Query;
030: import org.geotools.data.Transaction;
031: import org.geotools.data.feature.FeatureSource2;
032: import org.geotools.data.feature.adapter.GTFeatureTypeAdapter;
033: import org.geotools.feature.FeatureCollection;
034: import org.geotools.feature.iso.collection.AbstractFeatureCollection;
035: import org.opengis.feature.type.FeatureCollectionType;
036: import org.opengis.feature.type.FeatureType;
037: import org.opengis.feature.type.Name;
038: import org.opengis.filter.Filter;
039: import org.opengis.filter.capability.FilterCapabilities;
040:
041: import com.vividsolutions.jts.geom.Envelope;
042:
043: /**
044: * A FeatureSource2 that uses a
045: * {@linkplain org.geotools.data.complex.FeatureTypeMapping} to perform Feature
046: * fetching.
047: *
048: * <p>
049: * Note that the number of Features available from a MappingFeatureReader may
050: * not match the number of features that resulted of executing the incoming
051: * query over the surrogate FeatureSource. This will be the case when grouping
052: * attributes has configured on the FeatureTypeMapping this reader is based on.
053: * </p>
054: * <p>
055: * When a MappingFeatureReader is created, a delegated FeatureIterator will be
056: * created based on the information provided by the FeatureTypeMapping object.
057: * That delegate reader will be specialized in applying the appropiate mapping
058: * stratagy based on wether grouping has to be performed or not.
059: * </p>
060: *
061: * @author Gabriel Roldan, Axios Engineering
062: * @version $Id: MappingFeatureSource.java 28577 2008-01-03 15:44:29Z groldan $
063: * @source $URL:
064: * http://svn.geotools.org/geotools/branches/2.4.x/modules/unsupported/community-schemas/community-schema-ds/src/main/java/org/geotools/data/complex/MappingFeatureSource.java $
065: * @since 2.4
066: * @see org.geotools.data.complex.DefaultMappingFeatureIterator
067: * @see org.geotools.data.complex.GroupingFeatureIterator
068: */
069: class MappingFeatureSource implements FeatureSource2 {
070:
071: private ComplexDataStore store;
072:
073: private FeatureTypeMapping mappings;
074:
075: private org.geotools.feature.FeatureType gtType;
076:
077: public MappingFeatureSource(ComplexDataStore store,
078: FeatureTypeMapping mapping) {
079: this .store = store;
080: this .mappings = mapping;
081: FeatureType type = (FeatureType) mapping.getTargetFeature()
082: .type();
083: gtType = new GTFeatureTypeAdapter(type);
084: }
085:
086: public void addFeatureListener(FeatureListener listener) {
087: throw new UnsupportedOperationException();
088: }
089:
090: public Envelope getBounds() throws IOException {
091: Envelope bounds = store.getBounds(namedQuery(Filter.INCLUDE,
092: Integer.MAX_VALUE));
093: return bounds;
094: }
095:
096: private DefaultQuery namedQuery(Filter filter, int countLimit) {
097: DefaultQuery query = new DefaultQuery();
098: query.setTypeName(getName().getLocalPart());
099: query.setFilter(filter);
100: query.setMaxFeatures(countLimit);
101: return query;
102: }
103:
104: private DefaultQuery namedQuery(Query query) {
105: DefaultQuery namedQuery = namedQuery(query.getFilter(), query
106: .getMaxFeatures());
107: namedQuery.setPropertyNames(query.getPropertyNames());
108: namedQuery.setCoordinateSystem(query.getCoordinateSystem());
109: namedQuery.setHandle(query.getHandle());
110: namedQuery.setMaxFeatures(query.getMaxFeatures());
111: namedQuery.setSortBy(query.getSortBy());
112: return namedQuery;
113: }
114:
115: public Envelope getBounds(Query query) throws IOException {
116: DefaultQuery namedQuery = namedQuery(query);
117: Envelope bounds = store.getBounds(namedQuery);
118: return bounds;
119: }
120:
121: public int getCount(Query query) throws IOException {
122: DefaultQuery namedQuery = namedQuery(query);
123: int count = store.getCount(namedQuery);
124: return count;
125: }
126:
127: public DataStore getDataStore() {
128: return store;
129: }
130:
131: public org.geotools.feature.FeatureType getSchema() {
132: return gtType;
133: }
134:
135: public FeatureCollection getFeatures(Query query)
136: throws IOException {
137: throw new UnsupportedOperationException();
138: }
139:
140: public FeatureCollection getFeatures(Filter filter)
141: throws IOException {
142: throw new UnsupportedOperationException();
143: }
144:
145: public FeatureCollection getFeatures() throws IOException {
146: throw new UnsupportedOperationException();
147: }
148:
149: public void removeFeatureListener(FeatureListener listener) {
150: throw new UnsupportedOperationException(
151: "this is a read only feature source");
152: }
153:
154: public Collection content() {
155: return content(Filter.INCLUDE);
156: }
157:
158: public Collection content(String query, String queryLanguage) {
159: throw new UnsupportedOperationException();
160: }
161:
162: public Collection content(final Filter filter) {
163: return content(filter, Integer.MAX_VALUE);
164: }
165:
166: /**
167: * @return {@link org.opengis.feature.FeatureCollection}
168: */
169: public Collection content(final Filter filter, final int countLimit) {
170: Collection collection = new AbstractFeatureCollection(null,
171: (FeatureCollectionType) null, null) {
172: public int size() {
173: int count;
174: try {
175: count = store.getCount(namedQuery(filter,
176: countLimit));
177: } catch (IOException e) {
178: e.printStackTrace();
179: count = -1;
180: }
181: return count;
182: }
183:
184: /**
185: * @param close
186: * AbstractMappingFeatureIterator as returned by
187: * openIterator()
188: */
189: protected void closeIterator(Iterator close)
190: throws IOException {
191: AbstractMappingFeatureIterator iterator = (AbstractMappingFeatureIterator) close;
192: iterator.close();
193: }
194:
195: /**
196: * @return an AbstractMappingFeatureIterator
197: */
198: protected Iterator openIterator() throws IOException {
199: AbstractMappingFeatureIterator iterator;
200: Query query = namedQuery(filter, countLimit);
201: try {
202: if (0 == mappings.getGroupByAttNames().size()) {
203: iterator = new DefaultMappingFeatureIterator(
204: store, mappings, query);
205: } else {
206: iterator = new GroupingFeatureIterator3(store,
207: mappings, query);
208: }
209: } catch (IOException e) {
210: throw (RuntimeException) new RuntimeException()
211: .initCause(e);
212: }
213: return iterator;
214: }
215: };
216: return collection;
217: }
218:
219: public Object describe() {
220: return mappings.getTargetFeature();
221: }
222:
223: public void dispose() {
224: // TODO Auto-generated method stub
225:
226: }
227:
228: public FilterCapabilities getFilterCapabilities() {
229: throw new UnsupportedOperationException();
230: }
231:
232: public GeoResourceInfo getInfo() {
233: throw new UnsupportedOperationException();
234: }
235:
236: public Name getName() {
237: Name name = mappings.getTargetFeature().getName();
238: return name;
239: }
240:
241: public void setTransaction(Transaction t) {
242: throw new UnsupportedOperationException();
243: }
244:
245: public Set getSupportedHints() {
246: return Collections.EMPTY_SET;
247: }
248:
249: }
|