001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002, 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:
018: package org.geotools.feature.iso;
019:
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.geotools.factory.Factory;
026: import org.geotools.geometry.jts.ReferencedEnvelope;
027: import org.opengis.feature.Feature;
028: import org.opengis.feature.FeatureCollection;
029: import org.opengis.geometry.BoundingBox;
030:
031: /**
032: * A utility class for working with FeatureCollections. Provides a mechanism for
033: * obtaining a FeatureCollection instance.
034: *
035: * @author Ian Schneider
036: * @source $URL:
037: * http://svn.geotools.org/geotools/branches/fm/module/main/src/org/geotools/feature/FeatureCollections.java $
038: */
039: public abstract class FeatureCollections implements Factory {
040:
041: /**
042: * Holds a reference to a FeatureCollections implementation once one has
043: * been requested for the first time using instance().
044: */
045: private static FeatureCollections instance = null;
046:
047: private static FeatureCollections instance() {
048: if (instance == null) {
049: /*
050: * instance = (FeatureCollections) FactoryFinder.findFactory(
051: * "org.geotools.feature.FeatureCollections",
052: * "org.geotools.feature.DefaultFeatureCollections");
053: */
054: }
055: return instance;
056: }
057:
058: /**
059: * create a new FeatureCollection using the current default factory.
060: *
061: * @return A FeatureCollection instance.
062: */
063: public static FeatureCollection newCollection() {
064: return instance().createCollection();
065: }
066:
067: /**
068: * Subclasses must implement this to return a new FeatureCollection object.
069: *
070: * @return A new FeatureCollection
071: */
072: protected abstract FeatureCollection createCollection();
073:
074: /**
075: * Returns the implementation hints. The default implementation returns en
076: * empty map.
077: */
078: public Map getImplementationHints() {
079: return Collections.EMPTY_MAP;
080: }
081:
082: /**
083: * Calculates the bounds of a feature iterator. Obtains crs information from
084: * the first feature in the iteration.
085: */
086: public static ReferencedEnvelope getBounds(
087: Iterator/* <Feature> */iterator) {
088:
089: ReferencedEnvelope bounds = null;
090: while (iterator.hasNext()) {
091: Feature f = (Feature) iterator.next();
092: BoundingBox e = f.getBounds();
093:
094: if (bounds == null) {
095: bounds = new ReferencedEnvelope(e);
096: // bounds.init(e);
097: } else {
098: bounds.include(e);
099: }
100: }
101:
102: return bounds;
103: }
104:
105: /**
106: * Returns the <code>collection</code>'s element count. May need to
107: * traverse the collection.
108: *
109: * @param collection
110: * @return
111: */
112: public static int getSize(FeatureCollection collection) {
113: int size = -1;
114: if (collection instanceof Collection) {
115: size = ((Collection) collection).size();
116: } else {
117: int count = 0;
118: Iterator features = collection.iterator();
119: try {
120: while (features.hasNext()) {
121: features.next();
122: count++;
123: }
124: } finally {
125: collection.close(features);
126: }
127: size = count;
128: }
129: return size;
130: }
131:
132: }
|