1 /* 2 * Copyright (c) 2016 Vivid Solutions. 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 13 package org.locationtech.jtstest.testbuilder.topostretch; 14 15 import java.util.*; 16 17 import org.locationtech.jts.geom.*; 18 import org.locationtech.jts.geom.util.*; 19 20 public class GeometryVerticesMover 21 { move(Geometry geom, Map moves)22 public static Geometry move(Geometry geom, Map moves) 23 { 24 GeometryVerticesMover mover = new GeometryVerticesMover(geom, moves); 25 return mover.move(); 26 } 27 28 private Geometry geom; 29 private Map moves; 30 private List modifiedCoords = new ArrayList(); 31 GeometryVerticesMover(Geometry geom, Map moves)32 public GeometryVerticesMover(Geometry geom, Map moves) 33 { 34 this.geom = geom; 35 this.moves = moves; 36 } 37 move()38 public Geometry move() 39 { 40 GeometryEditor editor = new GeometryEditor(); 41 MoveVerticesOperation op = new MoveVerticesOperation(moves); 42 Geometry movedGeom = editor.edit(geom, new MoveVerticesOperation(moves)); 43 return movedGeom; 44 } 45 getModifiedCoordinates()46 public List getModifiedCoordinates() 47 { 48 return modifiedCoords; 49 } 50 51 private class MoveVerticesOperation 52 extends GeometryEditor.CoordinateOperation 53 { 54 private Map moves; 55 MoveVerticesOperation(Map moves)56 public MoveVerticesOperation(Map moves) 57 { 58 this.moves = moves; 59 } 60 61 edit(Coordinate[] coords, Geometry geometry)62 public Coordinate[] edit(Coordinate[] coords, 63 Geometry geometry) 64 { 65 Coordinate[] newPts = new Coordinate[coords.length]; 66 for (int i = 0; i < coords.length; i++) { 67 newPts[i] = movedPt(coords[i]); 68 } 69 return newPts; 70 } 71 movedPt(Coordinate orig)72 private Coordinate movedPt(Coordinate orig) 73 { 74 Coordinate newLoc = (Coordinate) moves.get(orig); 75 if (newLoc == null) 76 return orig; 77 Coordinate mod = (Coordinate) newLoc.clone(); 78 modifiedCoords.add(mod); 79 return mod; 80 } 81 } 82 83 84 } 85