01: /*
02: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
03: * for visualizing and manipulating spatial features with geometry and attributes.
04: *
05: * Copyright (C) 2003 Vivid Solutions
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: *
21: * For more information, contact:
22: *
23: * Vivid Solutions
24: * Suite #1A
25: * 2328 Government Street
26: * Victoria BC V8T 5G5
27: * Canada
28: *
29: * (250)385-6040
30: * www.vividsolutions.com
31: */
32:
33: package com.vividsolutions.jump.geom.precision;
34:
35: import com.vividsolutions.jts.geom.*;
36: import com.vividsolutions.jts.geom.util.*;
37:
38: /**
39: * Reduces the precision of a {@link Geometry}
40: * according to the supplied CoordinatePrecisionReducer
41: */
42: public class GeometryPrecisionReducer {
43: private CoordinatePrecisionReducer coordPrecReducer;
44: private GeometryEditor geomEdit = new GeometryEditor();
45:
46: public GeometryPrecisionReducer(
47: NumberPrecisionReducer numberPrecReducer) {
48: this .coordPrecReducer = new CoordinatePrecisionReducer(
49: numberPrecReducer);
50: }
51:
52: public Geometry reduce(Geometry geom) {
53: return geomEdit.edit(geom,
54: new PrecisionReducerCoordinateOperation());
55: }
56:
57: private class PrecisionReducerCoordinateOperation extends
58: GeometryEditor.CoordinateOperation {
59: public Coordinate[] edit(Coordinate[] coordinates, Geometry geom) {
60: for (int i = 0; i < coordinates.length; i++) {
61: coordPrecReducer.reducePrecision(coordinates[i]);
62: }
63: // remove repeated points
64: CoordinateList noRepeatedCoordList = new CoordinateList(
65: coordinates, false);
66: Coordinate[] noRepeatedCoord = noRepeatedCoordList
67: .toCoordinateArray();
68:
69: /**
70: * Check to see if the removal of repeated points
71: * collapsed the coordinate List to an invalid length
72: * for the type of the parent geometry.
73: * If this is the case, return the orginal coordinate list.
74: * Note that the returned geometry will still be invalid, since it
75: * has fewer unique coordinates than required. This check simply
76: * ensures that the Geometry constructors won't fail.
77: * It is not necessary to check for Point collapses, since the coordinate list can
78: * never collapse to less than one point
79: */
80: if (geom instanceof LinearRing
81: && noRepeatedCoord.length <= 3)
82: return coordinates;
83: if (geom instanceof LineString
84: && noRepeatedCoord.length <= 1)
85: return coordinates;
86:
87: return noRepeatedCoord;
88: }
89: }
90: }
|