01: /*
02: * GeoTools - OpenSource mapping toolkit
03: * http://geotools.org
04: * (C) 2005-2006, GeoTools Project Managment Committee (PMC)
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation;
09: * version 2.1 of the License.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: */
16: package org.geotools.feature.visitor;
17:
18: import com.vividsolutions.jts.geom.Envelope;
19: import com.vividsolutions.jts.geom.Geometry;
20: import org.geotools.feature.Feature;
21:
22: /**
23: * Calculates the extents (envelope) of the features it visits.
24: *
25: * @author Cory Horner, Refractions
26: *
27: * @since 2.2.M2
28: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/main/java/org/geotools/feature/visitor/BoundsVisitor.java $
29: */
30: public class BoundsVisitor implements FeatureCalc {
31: Envelope bounds = new Envelope();
32:
33: public void visit(Feature feature) {
34: Geometry geom = feature.getDefaultGeometry();
35: Envelope bbox = geom.getEnvelopeInternal();
36:
37: bounds.expandToInclude(bbox);
38: }
39:
40: public Envelope getBounds() {
41: return bounds;
42: }
43:
44: public void reset(Envelope bounds) {
45: this .bounds = new Envelope();
46: }
47:
48: public CalcResult getResult() {
49: return new BoundsResult(bounds);
50: }
51:
52: public static class BoundsResult extends AbstractCalcResult {
53: private Envelope bbox;
54:
55: public BoundsResult(Envelope bbox) {
56: this .bbox = bbox;
57: }
58:
59: public Object getValue() {
60: return new Envelope(bbox);
61: }
62:
63: public boolean isCompatible(CalcResult targetResults) {
64: //list each calculation result which can merge with this type of result
65: if (targetResults instanceof BoundsResult) {
66: return true;
67: }
68:
69: return false;
70: }
71:
72: public CalcResult merge(CalcResult resultsToAdd) {
73: if (!isCompatible(resultsToAdd)) {
74: throw new IllegalArgumentException(
75: "Parameter is not a compatible type");
76: }
77:
78: if (resultsToAdd instanceof BoundsResult) {
79: //add one set to the other (to create one big unique list)
80: Envelope newBounds = new Envelope(bbox);
81: newBounds.expandToInclude((Envelope) resultsToAdd
82: .getValue());
83:
84: return new BoundsResult(newBounds);
85: } else {
86: throw new IllegalArgumentException(
87: "The CalcResults claim to be compatible, but the appropriate merge method has not been implemented.");
88: }
89: }
90: }
91: }
|