Class VertexTaggedGeometryDataMapper

 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.jts.triangulate;
14  
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.TreeMap;
21  
22 import org.locationtech.jts.geom.Coordinate;
23 import org.locationtech.jts.geom.Geometry;
24  
25 /**
26  * Creates a map between the vertex {@link Coordinate}s of a 
27  * set of {@link Geometry}s,
28  * and the parent geometry, and transfers the source geometry
29  * data objects to geometry components tagged with the coordinates.
30  * <p>
31  * This class can be used in conjunction with {@link VoronoiDiagramBuilder}
32  * to transfer data objects from the input site geometries
33  * to the constructed Voronoi polygons.
34  * 
35  * @author Martin Davis
36  * @see VoronoiDiagramBuilder
37  *
38  */
39 public class VertexTaggedGeometryDataMapper 
40 {
41     private Map coordDataMap = new TreeMap();
42     
43     public VertexTaggedGeometryDataMapper()
44     {
45         
46     }
47     
48     public void loadSourceGeometries(Collection geoms)
49     {
50         for (Iterator i = geoms.iterator(); i.hasNext(); ) {
51             Geometry geom = (Geometry) i.next();
52             loadVertices(geom.getCoordinates(), geom.getUserData());
53         }
54     }
55     
56     public void loadSourceGeometries(Geometry geomColl)
57     {
58         for (int i = 0; i < geomColl.getNumGeometries(); i++) {
59             Geometry geom = geomColl.getGeometryN(i);
60             loadVertices(geom.getCoordinates(), geom.getUserData());
61         }
62     }
63     
64     private void loadVertices(Coordinate[] pts, Object data)
65     {
66         for (int i = 0; i < pts.length; i++) {
67             coordDataMap.put(pts[i], data);
68         }
69     }
70     
71     public List getCoordinates()
72     {
73         return new ArrayList(coordDataMap.keySet());
74     }
75     
76     /**
77      * Input is assumed to be a multiGeometry
78      * in which every component has its userData
79      * set to be a Coordinate which is the key to the output data.
80      * The Coordinate is used to determine
81      * the output data object to be written back into the component. 
82      * 
83      * @param targetGeom
84      */
85     public void transferData(Geometry targetGeom)
86     {
87         for (int i = 0; i < targetGeom.getNumGeometries(); i++) {
88             Geometry geom = targetGeom.getGeometryN(i);
89             Coordinate vertexKey = (Coordinate) geom.getUserData();
90             if (vertexKey == nullcontinue;
91             geom.setUserData(coordDataMap.get(vertexKey));
92         }
93     }
94 }
95