Class ComponentCoordinateExtracter

 1  
 2 /*
 3  * Copyright (c) 2016 Vivid Solutions.
 4  *
 5  * All rights reserved. This program and the accompanying materials
 6  * are made available under the terms of the Eclipse Public License 2.0
 7  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 8  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 9  * and the Eclipse Distribution License is available at
10  *
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  */
13 package org.locationtech.jts.geom.util;
14  
15 import java.util.ArrayList;
16 import java.util.List;
17  
18 import org.locationtech.jts.geom.Coordinate;
19 import org.locationtech.jts.geom.Geometry;
20 import org.locationtech.jts.geom.GeometryComponentFilter;
21 import org.locationtech.jts.geom.LineString;
22 import org.locationtech.jts.geom.Point;
23  
24 /**
25  * Extracts a representative {@link Coordinate} 
26  * from each connected component of a {@link Geometry}.
27  *
28  * @version 1.9
29  */
30 public class ComponentCoordinateExtracter
31   implements GeometryComponentFilter
32 {
33  
34   /**
35    * Extracts a representative {@link Coordinate}
36    * from each connected component in a geometry.
37    * <p>
38    * If more than one geometry is to be processed, it is more
39    * efficient to create a single {@link ComponentCoordinateExtracter} instance
40    * and pass it to each geometry.
41    *
42    * @param geom the Geometry from which to extract
43    * @return a list of representative Coordinates
44    */
45   public static List getCoordinates(Geometry geom)
46   {
47     List coords = new ArrayList();
48     geom.apply(new ComponentCoordinateExtracter(coords));
49     return coords;
50   }
51  
52   private List coords;
53  
54   /**
55    * Constructs a LineExtracterFilter with a list in which to store LineStrings found.
56    */
57   public ComponentCoordinateExtracter(List coords)
58   {
59     this.coords = coords;
60   }
61  
62   public void filter(Geometry geom)
63   {
64     // add coordinates from connected components
65     if (geom instanceof LineString
66         || geom instanceof Point) 
67       coords.add(geom.getCoordinate());
68   }
69  
70 }
71