001: package org.geotools.data.gml;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.util.Iterator;
006: import java.util.NoSuchElementException;
007:
008: import org.geotools.data.FeatureReader;
009: import org.geotools.data.store.DataFeatureCollection;
010: import org.geotools.feature.Feature;
011: import org.geotools.feature.FeatureCollection;
012: import org.geotools.feature.FeatureType;
013: import org.geotools.feature.IllegalAttributeException;
014: import org.geotools.geometry.jts.ReferencedEnvelope;
015: import org.geotools.gml3.GMLConfiguration;
016: import org.geotools.xml.StreamingParser;
017: import org.opengis.filter.Filter;
018:
019: import com.vividsolutions.jts.geom.Envelope;
020:
021: public class GMLFeatureCollection extends DataFeatureCollection {
022:
023: GMLTypeEntry entry;
024:
025: GMLFeatureCollection(GMLTypeEntry entry) {
026: this .entry = entry;
027: }
028:
029: public FeatureType getSchema() {
030: return entry.getFeatureType();
031: }
032:
033: protected Iterator openIterator() throws IOException {
034: return new GMLIterator(entry);
035: }
036:
037: protected void closeIterator(Iterator close) throws IOException {
038: ((GMLIterator) close).close();
039: }
040:
041: public ReferencedEnvelope getBounds() {
042: //, look for bounds on feature collection
043: InputStream input = null;
044: try {
045: input = entry.parent().document();
046: } catch (IOException e) {
047: throw new RuntimeException(e);
048: }
049:
050: Envelope bounds = null;
051: try {
052: StreamingParser parser = new StreamingParser(entry.parent()
053: .configuration(), input, "/boundedBy");
054: bounds = (Envelope) parser.parse();
055: } catch (Exception e) {
056: throw new RuntimeException(e);
057: } finally {
058: try {
059: input.close();
060: } catch (IOException e) {
061: }
062: }
063:
064: if (bounds == null) {
065: //bounds must have not been declared, calculate manually
066: try {
067: bounds = new Envelope();
068: FeatureReader reader = reader();
069: if (!reader.hasNext()) {
070: bounds.setToNull();
071: } else {
072: bounds.init(reader.next().getBounds());
073: while (reader.hasNext()) {
074: bounds.expandToInclude(reader.next()
075: .getBounds());
076: }
077: }
078:
079: reader.close();
080: } catch (Exception e) {
081: throw new RuntimeException(e);
082: }
083: }
084:
085: return ReferencedEnvelope.reference(bounds);
086: }
087:
088: public int getCount() throws IOException {
089: FeatureReader reader = reader();
090: int count = 0;
091:
092: try {
093: while (reader.hasNext()) {
094: reader.next();
095: count++;
096: }
097: } catch (Exception e) {
098: throw (IOException) new IOException().initCause(e);
099: } finally {
100: reader.close();
101: }
102:
103: return count;
104: }
105:
106: public FeatureCollection collection() throws IOException {
107: return this;
108: }
109:
110: }
|