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:
033: package com.vividsolutions.jump.feature;
034:
035: import java.util.Collection;
036: import java.util.Iterator;
037:
038: import com.vividsolutions.jts.geom.Geometry;
039:
040: /**
041: * Utility functions to create different kinds of FeatureDatasets
042: */
043: public class FeatureDatasetFactory {
044: /**
045: * Creates a FeatureCollection from a Collection of {@link Geometry}s
046: * @param geoms a collection of {@link Geometry}s
047: */
048: public static FeatureCollection createFromGeometry(Collection geoms) {
049: FeatureSchema featureSchema = new FeatureSchema();
050: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
051:
052: FeatureCollection fc = new FeatureDataset(featureSchema);
053:
054: for (Iterator i = geoms.iterator(); i.hasNext();) {
055: Feature feature = new BasicFeature(fc.getFeatureSchema());
056: feature.setGeometry((Geometry) i.next());
057: fc.add(feature);
058: }
059:
060: return fc;
061: }
062:
063: /**
064: * Creates a FeatureCollection from a Collection of {@link Geometry}s
065: * and adds an attribute containing the length of the Geometry. The attribute
066: * name is given by the argument <code>attrName</code>
067: * @param geoms a collection of {@link Geometry}s
068: * @param attrName the name to use for the length attribute
069: */
070: public static FeatureDataset createFromGeometryWithLength(
071: Collection geoms, String attrName) {
072: FeatureSchema featureSchema = new FeatureSchema();
073: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
074: featureSchema.addAttribute(attrName, AttributeType.DOUBLE);
075:
076: FeatureDataset fc = new FeatureDataset(featureSchema);
077:
078: for (Iterator i = geoms.iterator(); i.hasNext();) {
079: Feature feature = new BasicFeature(fc.getFeatureSchema());
080: Geometry g = (Geometry) i.next();
081: feature.setGeometry(g);
082: feature.setAttribute(attrName, new Double(g.getLength()));
083: fc.add(feature);
084: }
085:
086: return fc;
087: }
088:
089: /**
090: * Creates a {@link FeatureDataset} from a Collection of {@link Geometry}s
091: * using the given {@link FeatureSchema}
092: *
093: * @param geoms a collection of {@link Geometry}s
094: * @param featureSchema the schema to use for the feature collection
095: *
096: * @return a FeatureDataset containing features for the geometries
097: */
098: public static FeatureDataset createFromGeometry(Collection geoms,
099: FeatureSchema featureSchema) {
100: FeatureDataset fc = new FeatureDataset(featureSchema);
101: for (Iterator i = geoms.iterator(); i.hasNext();) {
102: Feature feature = new BasicFeature(fc.getFeatureSchema());
103: feature.setGeometry((Geometry) i.next());
104: fc.add(feature);
105: }
106: return fc;
107: }
108:
109: /**
110: * Creates a FeatureCollection from a Collection of {@link Geometry}s
111: * and adds optional attributes containing the length and area of the Geometry.
112: *
113: * @param geoms a collection of {@link Geometry}s
114: * @param attrNameLen the name to use for the length attribute (or <code>null</code> if not required)
115: * @param attrNameArea the name to use for the length attribute (or <code>null</code> if not required)
116: *
117: */
118: public static FeatureDataset createFromGeometryWithLengthAndArea(
119: Collection geoms, String attrNameLen, String attrNameArea) {
120: FeatureSchema featureSchema = new FeatureSchema();
121: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
122: if (attrNameLen != null)
123: featureSchema.addAttribute(attrNameLen,
124: AttributeType.DOUBLE);
125: if (attrNameArea != null)
126: featureSchema.addAttribute(attrNameArea,
127: AttributeType.DOUBLE);
128:
129: FeatureDataset fc = new FeatureDataset(featureSchema);
130:
131: for (Iterator i = geoms.iterator(); i.hasNext();) {
132: Feature feature = new BasicFeature(fc.getFeatureSchema());
133: Geometry g = (Geometry) i.next();
134: feature.setGeometry(g);
135: if (attrNameLen != null)
136: feature.setAttribute(attrNameLen, new Double(g
137: .getLength()));
138: if (attrNameArea != null)
139: feature.setAttribute(attrNameArea, new Double(g
140: .getArea()));
141: fc.add(feature);
142: }
143: return fc;
144: }
145: }
|