Class SegmentStringUtil

  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.noding;
 14  
 15 import java.util.ArrayList;
 16 import java.util.Collection;
 17 import java.util.Iterator;
 18 import java.util.List;
 19  
 20 import org.locationtech.jts.geom.Coordinate;
 21 import org.locationtech.jts.geom.Geometry;
 22 import org.locationtech.jts.geom.GeometryFactory;
 23 import org.locationtech.jts.geom.LineString;
 24 import org.locationtech.jts.geom.MultiLineString;
 25 import org.locationtech.jts.geom.util.LinearComponentExtracter;
 26  
 27  
 28 /**
 29  * Utility methods for processing {@link SegmentString}s.
 30  * 
 31  * @author Martin Davis
 32  *
 33  */
 34 public class SegmentStringUtil 
 35 {
 36   /**
 37    * Extracts all linear components from a given {@link Geometry}
 38    * to {@link SegmentString}s.
 39    * The SegmentString data item is set to be the source Geometry.
 40    * 
 41    * @param geom the geometry to extract from
 42    * @return a List of SegmentStrings
 43    */
 44   public static List extractSegmentStrings(Geometry geom)
 45   {
 46     return extractNodedSegmentStrings(geom);
 47   }
 48  
 49   /**
 50    * Extracts all linear components from a given {@link Geometry}
 51    * to {@link SegmentString}s.
 52    * The SegmentString data item is set to be the source Geometry.
 53    * 
 54    * @param geom the geometry to extract from
 55    * @return a List of SegmentStrings
 56    */
 57   public static List extractNodedSegmentStrings(Geometry geom)
 58   {
 59     List segStr = new ArrayList();
 60     List lines = LinearComponentExtracter.getLines(geom);
 61     for (Iterator i = lines.iterator(); i.hasNext(); ) {
 62       LineString line = (LineString) i.next();
 63       Coordinate[] pts = line.getCoordinates();
 64       segStr.add(new NodedSegmentString(pts, geom));
 65     }
 66     return segStr;
 67   }
 68  
 69   /**
 70    * Converts a collection of {@link SegmentString}s into a {@link Geometry}.
 71    * The geometry will be either a {@link LineString} or a {@link MultiLineString} (possibly empty).
 72    *
 73    * @param segStrings a collection of SegmentStrings
 74    * @return a LineString or MultiLineString
 75    */
 76   public static Geometry toGeometry(Collection segStrings, GeometryFactory geomFact)
 77   {
 78     LineString[] lines = new LineString[segStrings.size()];
 79     int index = 0;
 80     for (Iterator i = segStrings.iterator(); i.hasNext(); ) {
 81       SegmentString ss = (SegmentString) i.next();
 82       LineString line = geomFact.createLineString(ss.getCoordinates());
 83       lines[index++] = line;
 84     }
 85     if (lines.length == 1return lines[0];
 86     return geomFact.createMultiLineString(lines);
 87   }
 88  
 89   public static String toString(List segStrings)
 90   {
 91     StringBuffer buf = new StringBuffer();
 92     for (Iterator i = segStrings.iterator(); i.hasNext(); ) {
 93         SegmentString segStr = (SegmentString) i.next();
 94         buf.append(segStr.toString());
 95         buf.append("\n");
 96         
 97     }
 98     return buf.toString();
 99   }
100 }
101