001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032: package com.vividsolutions.jump.feature;
033:
034: import java.util.ArrayList;
035: import java.util.Collection;
036: import java.util.Comparator;
037: import java.util.Iterator;
038: import java.util.List;
039:
040: import com.vividsolutions.jts.geom.Geometry;
041:
042: /**
043: * Useful utility functions for working with Features.
044: * @see Feature
045: */
046: public class FeatureUtil {
047:
048: /**
049: * Creates a new Feature from the given Geometry, with nominal values for
050: * the attributes.
051: * @param g the Geometry to convert
052: * @param schema metadata for the Feature to create
053: * @return a new Feature containing the Geometry and default values for the
054: * attributes
055: */
056: public static Feature toFeature(Geometry g, FeatureSchema schema) {
057: Feature feature = new BasicFeature(schema);
058: feature.setGeometry(g);
059: return feature;
060: }
061:
062: /**
063: * Returns the n Geometries extracted from the given n Features
064: */
065: public static List toGeometries(Collection features) {
066: ArrayList list = new ArrayList();
067:
068: for (Iterator i = features.iterator(); i.hasNext();) {
069: Feature feature = (Feature) i.next();
070: list.add(feature.getGeometry());
071: }
072:
073: return list;
074: }
075:
076: /**
077: * Compares two Features for order based on their feature ID.
078: * @see Feature#getID()
079: */
080: public static class IDComparator implements Comparator {
081: public int compare(Object o1, Object o2) {
082: Feature f1 = (Feature) o1;
083: Feature f2 = (Feature) o2;
084:
085: if (f1.getID() < f2.getID()) {
086: return -1;
087: }
088:
089: if (f1.getID() > f2.getID()) {
090: return 1;
091: }
092:
093: return 0;
094: }
095: }
096:
097: private static int lastID = 0;
098:
099: /**
100: * Increments and returns the feature-ID counter
101: * @see Feature#getID()
102: */
103: public static int nextID() {
104: return ++lastID;
105: }
106:
107: /**
108: * Although Feature implements Cloneable, this method is useful
109: * when the two Features are implemented with different classes.
110: */
111: public static void copyAttributes(Feature a, Feature b) {
112: for (int i = 0; i < a.getSchema().getAttributeCount(); i++) {
113: b.setAttribute(i, a.getAttribute(i));
114: }
115: }
116:
117: /**
118: * Returns whether all attributes are null (other than the Geometry
119: * attribute, which is not checked)
120: */
121: public static boolean areAllNonSpatialAttributesNull(Feature feature) {
122: for (int i = 0; i < feature.getSchema().getAttributeCount(); i++) {
123: if (AttributeType.GEOMETRY == feature.getSchema()
124: .getAttributeType(i)) {
125: continue;
126: }
127: if (feature.getAttribute(i) != null) {
128: return false;
129: }
130: }
131: return true;
132: }
133:
134: }
|