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.workbench.ui.plugin;
034:
035: import java.util.Iterator;
036:
037: import com.vividsolutions.jts.geom.Coordinate;
038: import com.vividsolutions.jts.geom.Geometry;
039: import com.vividsolutions.jts.geom.GeometryCollection;
040: import com.vividsolutions.jts.geom.Polygon;
041: import com.vividsolutions.jump.feature.AttributeType;
042: import com.vividsolutions.jump.feature.BasicFeature;
043: import com.vividsolutions.jump.feature.Feature;
044: import com.vividsolutions.jump.feature.FeatureDataset;
045: import com.vividsolutions.jump.feature.FeatureSchema;
046: import com.vividsolutions.jump.util.StringUtil;
047: import com.vividsolutions.jump.workbench.model.Layer;
048: import com.vividsolutions.jump.workbench.model.StandardCategoryNames;
049: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
050: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
051:
052: /**
053: * Computes various statistics for selected layers.
054: */
055: public class FeatureStatisticsPlugIn extends AbstractPlugIn {
056: public static final String nPtsAttr = "nPts";
057: public static final String nHolesAttr = "nHoles";
058: public static final String nCompsAttr = "nComponents";
059: public static final String areaAttr = "area";
060: public static final String lengthAttr = "length";
061: public static final String typeAttr = "type";
062: private static final String jtsGeometryClassPackagePrefix = "com.vividsolutions.jts.geom";
063:
064: public FeatureStatisticsPlugIn() {
065: }
066:
067: public static FeatureSchema getStatisticsSchema() {
068: FeatureSchema featureSchema = new FeatureSchema();
069: featureSchema.addAttribute("GEOMETRY", AttributeType.GEOMETRY);
070: featureSchema.addAttribute(nPtsAttr, AttributeType.INTEGER);
071: featureSchema.addAttribute(nHolesAttr, AttributeType.INTEGER);
072: featureSchema.addAttribute(nCompsAttr, AttributeType.INTEGER);
073: featureSchema.addAttribute(areaAttr, AttributeType.DOUBLE);
074: featureSchema.addAttribute(lengthAttr, AttributeType.DOUBLE);
075: featureSchema.addAttribute(typeAttr, AttributeType.STRING);
076:
077: return featureSchema;
078: }
079:
080: /**
081: * Removes the JTS geometry package prefix from a classname
082: * @param fullClassName
083: * @return the simplified class name
084: */
085: public static String removeGeometryPackage(String fullClassName) {
086: if (fullClassName.startsWith(jtsGeometryClassPackagePrefix)) {
087: return StringUtil.classNameWithoutQualifiers(fullClassName);
088: } else {
089: return fullClassName;
090: }
091: }
092:
093: public boolean execute(PlugInContext context) throws Exception {
094: //Call #getSelectedLayers before #clear, because #clear will surface
095: //output window. [Jon Aquino]
096: Layer[] selectedLayers = context.getSelectedLayers();
097:
098: for (int i = 0; i < selectedLayers.length; i++) {
099: featureStatistics(selectedLayers[i], context);
100: }
101:
102: return true;
103: }
104:
105: private void featureStatistics(final Layer layer,
106: PlugInContext context) {
107: FeatureSchema statsSchema = getStatisticsSchema();
108: FeatureDataset statsFC = new FeatureDataset(statsSchema);
109:
110: for (Iterator i = layer.getFeatureCollectionWrapper()
111: .iterator(); i.hasNext();) {
112: Feature f = (Feature) i.next();
113: Geometry g = f.getGeometry();
114: double area = g.getArea();
115: double length = g.getLength();
116:
117: // these both need work - need to recurse into geometries
118: int comps = 1;
119:
120: if (g instanceof GeometryCollection) {
121: comps = ((GeometryCollection) g).getNumGeometries();
122: }
123:
124: Coordinate[] pts = g.getCoordinates();
125: int holes = 0;
126:
127: if (g instanceof Polygon) {
128: holes = ((Polygon) g).getNumInteriorRing();
129: }
130:
131: Feature statsf = new BasicFeature(statsSchema);
132:
133: // this aliases the geometry of the input feature, but this shouldn't matter,
134: // since if geometries are edited they should be completely replaced
135: statsf.setAttribute("GEOMETRY", g);
136: statsf.setAttribute(nPtsAttr, new Integer(pts.length));
137: statsf.setAttribute(nHolesAttr, new Integer(holes));
138: statsf.setAttribute(nCompsAttr, new Integer(comps));
139: statsf.setAttribute(areaAttr, new Double(area));
140: statsf.setAttribute(lengthAttr, new Double(length));
141: statsf.setAttribute(typeAttr, removeGeometryPackage(g
142: .getClass().getName()));
143: statsFC.add(statsf);
144: }
145:
146: Layer statsLayer = context.addLayer(StandardCategoryNames.QA,
147: "Statistics-" + layer.getName(), statsFC);
148: statsLayer.setStyles(layer.cloneStyles());
149: }
150: }
|