/
CentroidProcess.java
136 lines (118 loc) · 4.97 KB
/
CentroidProcess.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* GeoTools - The Open Source Java GIS Toolkit
* http://geotools.org
*
* (C) 2011, Open Source Geospatial Foundation (OSGeo)
* (C) 2001-2007 TOPP - www.openplans.org.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
package org.geotools.process.vector;
import java.util.NoSuchElementException;
import org.geotools.data.DataUtilities;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.process.ProcessException;
import org.geotools.process.factory.DescribeParameter;
import org.geotools.process.factory.DescribeProcess;
import org.geotools.process.factory.DescribeResult;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.feature.type.GeometryDescriptor;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.Point;
/**
* A process that returns the centroids for the geometries in the
* input feature collection.
*
* @author Rohan Singh
*
*
* @source $URL: http://svn.osgeo.org/geotools/trunk/modules/unsupported/process-feature/src/main/java/org/geotools/process/feature/gs/CentroidProcess.java $
*/
@DescribeProcess(title = "Centroid", description = "Computes the geometric centroids of features")
public class CentroidProcess implements VectorProcess {
@DescribeResult(name = "result", description = "Centroids of input features")
public SimpleFeatureCollection execute(
@DescribeParameter(name = "features", description = "Input feature collection") SimpleFeatureCollection features)
throws ProcessException {
return DataUtilities.simple(new CentroidFeatureCollection(features));
}
static class CentroidFeatureCollection extends SimpleProcessingCollection {
SimpleFeatureCollection delegate;
public CentroidFeatureCollection(SimpleFeatureCollection delegate) {
this.delegate = delegate;
}
@Override
public SimpleFeatureIterator features() {
return new CentroidFeatureIterator(delegate.features(), getSchema());
}
@Override
public ReferencedEnvelope getBounds() {
return getFeatureBounds();
}
@Override
protected SimpleFeatureType buildTargetFeatureType() {
SimpleFeatureTypeBuilder tb = new SimpleFeatureTypeBuilder();
for (AttributeDescriptor ad : delegate.getSchema().getAttributeDescriptors()) {
if(ad instanceof GeometryDescriptor) {
GeometryDescriptor gd = (GeometryDescriptor) ad;
Class<?> binding = ad.getType().getBinding();
if(Point.class.isAssignableFrom(binding)) {
tb.add(ad);
} else {
tb.minOccurs(ad.getMinOccurs());
tb.maxOccurs(ad.getMaxOccurs());
tb.nillable(ad.isNillable());
tb.add(ad.getLocalName(), Point.class, gd.getCoordinateReferenceSystem());
}
} else {
tb.add(ad);
}
}
tb.setName(delegate.getSchema().getName());
return tb.buildFeatureType();
}
@Override
public int size() {
return delegate.size();
}
}
static class CentroidFeatureIterator implements SimpleFeatureIterator {
SimpleFeatureIterator delegate;
SimpleFeatureBuilder fb;
public CentroidFeatureIterator(SimpleFeatureIterator delegate, SimpleFeatureType schema) {
this.delegate = delegate;
fb = new SimpleFeatureBuilder(schema);
}
public void close() {
delegate.close();
}
public boolean hasNext() {
return delegate.hasNext();
}
public SimpleFeature next() throws NoSuchElementException {
SimpleFeature f = delegate.next();
for (Object attribute : f.getAttributes()) {
if ((attribute instanceof Geometry) &&
!(attribute instanceof Point)) {
attribute = ((Geometry) attribute).getCentroid();
}
fb.add(attribute);
}
return fb.buildFeature(f.getID());
}
}
}