Class PrecisionReducerCoordinateOperation

 1 /*
 2  * Copyright (c) 2016 Martin Davis.
 3  *
 4  * All rights reserved. This program and the accompanying materials
 5  * are made available under the terms of the Eclipse Public License 2.0
 6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 8  * and the Eclipse Distribution License is available at
 9  *
10  * http://www.eclipse.org/org/documents/edl-v10.php.
11  */
12 package org.locationtech.jts.precision;
13  
14 import org.locationtech.jts.geom.Coordinate;
15 import org.locationtech.jts.geom.CoordinateList;
16 import org.locationtech.jts.geom.Geometry;
17 import org.locationtech.jts.geom.LineString;
18 import org.locationtech.jts.geom.LinearRing;
19 import org.locationtech.jts.geom.PrecisionModel;
20 import org.locationtech.jts.geom.util.GeometryEditor;
21  
22 public class PrecisionReducerCoordinateOperation extends
23         GeometryEditor.CoordinateOperation 
24 {
25   private PrecisionModel targetPM;
26   private boolean removeCollapsed = true;
27  
28     public PrecisionReducerCoordinateOperation(PrecisionModel targetPM, boolean removeCollapsed)
29     {
30         this.targetPM = targetPM;
31         this.removeCollapsed = removeCollapsed;
32     }
33     
34     public Coordinate[] edit(Coordinate[] coordinates, Geometry geom) {
35         if (coordinates.length == 0)
36             return null;
37  
38         Coordinate[] reducedCoords = new Coordinate[coordinates.length];
39         // copy coordinates and reduce
40         for (int i = 0; i < coordinates.length; i++) {
41             Coordinate coord = new Coordinate(coordinates[i]);
42             targetPM.makePrecise(coord);
43             reducedCoords[i] = coord;
44         }
45         // remove repeated points, to simplify returned geometry as much as possible
46         CoordinateList noRepeatedCoordList = new CoordinateList(reducedCoords,
47                 false);
48         Coordinate[] noRepeatedCoords = noRepeatedCoordList.toCoordinateArray();
49  
50         /**
51          * Check to see if the removal of repeated points collapsed the coordinate
52          * List to an invalid length for the type of the parent geometry. It is not
53          * necessary to check for Point collapses, since the coordinate list can
54          * never collapse to less than one point. If the length is invalid, return
55          * the full-length coordinate array first computed, or null if collapses are
56          * being removed. (This may create an invalid geometry - the client must
57          * handle this.)
58          */
59         int minLength = 0;
60         if (geom instanceof LineString)
61             minLength = 2;
62         if (geom instanceof LinearRing)
63             minLength = 4;
64  
65         Coordinate[] collapsedCoords = reducedCoords;
66         if (removeCollapsed)
67             collapsedCoords = null;
68  
69         // return null or original length coordinate array
70         if (noRepeatedCoords.length < minLength) {
71             return collapsedCoords;
72         }
73  
74         // ok to return shorter coordinate array
75         return noRepeatedCoords;
76     }
77 }
78