001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.data.wfs;
017:
018: import java.io.IOException;
019:
020: import org.geotools.data.AbstractFeatureSource;
021: import org.geotools.data.DataStore;
022: import org.geotools.data.DefaultFeatureResults;
023: import org.geotools.data.DefaultQuery;
024: import org.geotools.data.FeatureListener;
025: import org.geotools.data.FeatureReader;
026: import org.geotools.data.Query;
027: import org.geotools.data.Transaction;
028: import org.geotools.data.store.EmptyFeatureCollection;
029: import org.geotools.feature.FeatureCollection;
030: import org.geotools.feature.FeatureType;
031: import org.geotools.filter.Filter;
032: import org.geotools.geometry.jts.ReferencedEnvelope;
033:
034: import com.vividsolutions.jts.geom.Envelope;
035:
036: /**
037: * DOCUMENT ME!
038: *
039: * @author dzwiers
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wfs/src/main/java/org/geotools/data/wfs/WFSFeatureSource.java $
041: */
042: public class WFSFeatureSource extends AbstractFeatureSource {
043: protected WFSDataStore ds;
044: protected String fname;
045:
046: protected WFSFeatureSource(WFSDataStore ds, String fname) {
047: this .ds = ds;
048: this .fname = fname;
049: }
050:
051: /**
052: *
053: * @see org.geotools.data.FeatureSource#getDataStore()
054: */
055: public DataStore getDataStore() {
056: return ds;
057: }
058:
059: /**
060: *
061: * @see org.geotools.data.FeatureSource#addFeatureListener(org.geotools.data.FeatureListener)
062: */
063: public void addFeatureListener(FeatureListener listener) {
064: ds.listenerManager.addFeatureListener(this , listener);
065: }
066:
067: /**
068: *
069: * @see org.geotools.data.FeatureSource#removeFeatureListener(org.geotools.data.FeatureListener)
070: */
071: public void removeFeatureListener(FeatureListener listener) {
072: ds.listenerManager.removeFeatureListener(this , listener);
073: }
074:
075: /**
076: *
077: * @see org.geotools.data.FeatureSource#getSchema()
078: */
079: public FeatureType getSchema() {
080: try {
081: return ds.getSchema(fname);
082: } catch (IOException e) {
083: return null;
084: }
085: }
086:
087: /**
088: *
089: * @see org.geotools.data.FeatureSource#getBounds()
090: */
091: public Envelope getBounds() throws IOException {
092: return getBounds((fname == null) ? Query.ALL
093: : new DefaultQuery(fname));
094: }
095:
096: /**
097: *
098: * @see org.geotools.data.FeatureSource#getBounds(org.geotools.data.Query)
099: */
100: public Envelope getBounds(Query query) throws IOException {
101: return ds.getBounds(namedQuery(query));
102: }
103:
104: /**
105: *
106: * @see org.geotools.data.FeatureSource#getFeatures()
107: */
108: public FeatureCollection getFeatures() throws IOException {
109: return getFeatures(new DefaultQuery(getSchema().getTypeName(),
110: Filter.INCLUDE));
111: }
112:
113: /**
114: *
115: * @see org.geotools.data.FeatureSource#getFeatures(org.geotools.filter.Filter)
116: */
117: public FeatureCollection getFeatures(Filter filter)
118: throws IOException {
119: return getFeatures(new DefaultQuery(getSchema().getTypeName(),
120: filter));
121: }
122:
123: /**
124: *
125: * @see org.geotools.data.FeatureSource#getFeatures(org.geotools.data.Query)
126: */
127: public FeatureCollection getFeatures(Query query)
128: throws IOException {
129: FeatureType schema = getSchema();
130: String typeName = schema.getTypeName();
131:
132: if (query.getTypeName() == null) { // typeName unspecified we will "any" use a default
133: DefaultQuery defaultQuery = new DefaultQuery(query);
134: defaultQuery.setTypeName(typeName);
135: }
136:
137: if (!typeName.equals(query.getTypeName())) {
138: return new EmptyFeatureCollection(schema);
139: } else {
140: return new DefaultFeatureResults(this , query);
141: }
142: }
143:
144: /**
145: *
146: * @see org.geotools.data.AbstractFeatureSource#getTransaction()
147: */
148: public Transaction getTransaction() {
149: return Transaction.AUTO_COMMIT;
150: }
151:
152: /**
153: *
154: * @author dzwiers
155: */
156: public static class WFSFeatureResults extends DefaultFeatureResults {
157: private WFSFeatureSource fs;
158: private Query query;
159:
160: /**
161: *
162: * @param fs
163: * @param query
164: */
165: public WFSFeatureResults(WFSFeatureSource fs, Query query)
166: throws IOException {
167: super (fs, query);
168: this .query = query;
169: this .fs = fs;
170: }
171:
172: /**
173: *
174: * @see org.geotools.data.FeatureResults#getSchema()
175: */
176: public FeatureType getSchema() {
177: return fs.getSchema();
178: }
179:
180: /**
181: *
182: * @see org.geotools.data.FeatureResults#reader()
183: */
184: public FeatureReader reader() throws IOException {
185: return fs.ds.getFeatureReader(query, fs.getTransaction());
186: }
187:
188: /**
189: *
190: * @see org.geotools.data.FeatureResults#getBounds()
191: */
192: public ReferencedEnvelope getBounds() {
193: try {
194: return ReferencedEnvelope
195: .reference(fs.getBounds(query));
196: } catch (IOException e) {
197: e.printStackTrace();
198: return null;
199: }
200: }
201:
202: /**
203: *
204: * @see org.geotools.data.FeatureResults#getCount()
205: */
206: public int getCount() throws IOException {
207: return fs.getCount(query);
208: }
209: }
210: }
|