Class PointExtracter

 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.Collections;
17 import java.util.List;
18  
19 import org.locationtech.jts.geom.Geometry;
20 import org.locationtech.jts.geom.GeometryCollection;
21 import org.locationtech.jts.geom.GeometryFilter;
22 import org.locationtech.jts.geom.Point;
23  
24  
25 /**
26  * Extracts all the 0-dimensional ({@link Point}) components from a {@link Geometry}.
27  *
28  * @version 1.7
29  * @see GeometryExtracter
30  */
31 public class PointExtracter
32   implements GeometryFilter
33 {
34   /**
35    * Extracts the {@link Point} elements from a single {@link Geometry}
36    * and adds them to the provided {@link List}.
37    * 
38    * @param geom the geometry from which to extract
39    * @param list the list to add the extracted elements to
40    */
41   public static List getPoints(Geometry geom, List list)
42   {
43       if (geom instanceof Point) {
44           list.add(geom);
45       }
46       else if (geom instanceof GeometryCollection) {
47           geom.apply(new PointExtracter(list));
48       }
49       // skip non-Polygonal elemental geometries
50       
51     return list;
52   }
53  
54   /**
55    * Extracts the {@link Point} elements from a single {@link Geometry}
56    * and returns them in a {@link List}.
57    * 
58    * @param geom the geometry from which to extract
59    */
60   public static List getPoints(Geometry geom) {
61     if (geom instanceof Point) {
62       return Collections.singletonList(geom);
63     }
64     return getPoints(geom, new ArrayList());
65   }
66  
67   private List pts;
68   /**
69    * Constructs a PointExtracterFilter with a list in which to store Points found.
70    */
71   public PointExtracter(List pts)
72   {
73     this.pts = pts;
74   }
75  
76   public void filter(Geometry geom)
77   {
78     if (geom instanceof Point) pts.add(geom);
79   }
80  
81 }
82