Class TaggedLineString

  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.simplify;
 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.LineSegment;
 20 import org.locationtech.jts.geom.LineString;
 21 import org.locationtech.jts.geom.LinearRing;
 22  
 23 /**
 24  * Represents a {@link LineString} which can be modified to a simplified shape.  
 25  * This class provides an attribute which specifies the minimum allowable length
 26  * for the modified result.
 27  * 
 28  * @version 1.7
 29  */
 30 class TaggedLineString
 31 {
 32  
 33   private LineString parentLine;
 34   private TaggedLineSegment[] segs;
 35   private List resultSegs = new ArrayList();
 36   private int minimumSize;
 37  
 38   public TaggedLineString(LineString parentLine) {
 39     this(parentLine, 2);
 40   }
 41  
 42   public TaggedLineString(LineString parentLine, int minimumSize) {
 43     this.parentLine = parentLine;
 44     this.minimumSize = minimumSize;
 45     init();
 46   }
 47  
 48   public int getMinimumSize()  {    return minimumSize;  }
 49   public LineString getParent() { return parentLine; }
 50   public Coordinate[] getParentCoordinates() { return parentLine.getCoordinates(); }
 51   public Coordinate[] getResultCoordinates() { return extractCoordinates(resultSegs); }
 52  
 53   public int getResultSize()
 54   {
 55     int resultSegsSize = resultSegs.size();
 56     return resultSegsSize == 0 ? 0 : resultSegsSize + 1;
 57   }
 58  
 59   public TaggedLineSegment getSegment(int i) { return segs[i]; }
 60  
 61   private void init()
 62   {
 63     Coordinate[] pts = parentLine.getCoordinates();
 64     segs = new TaggedLineSegment[pts.length - 1];
 65     for (int i = 0; i < pts.length - 1; i++) {
 66       TaggedLineSegment seg
 67                = new TaggedLineSegment(pts[i], pts[i + 1], parentLine, i);
 68       segs[i] = seg;
 69     }
 70   }
 71  
 72   public TaggedLineSegment[] getSegments() { return segs; }
 73  
 74   public void addToResult(LineSegment seg)
 75   {
 76     resultSegs.add(seg);
 77   }
 78  
 79   public LineString asLineString()
 80   {
 81     return parentLine.getFactory().createLineString(extractCoordinates(resultSegs));
 82   }
 83  
 84   public LinearRing asLinearRing() {
 85     return parentLine.getFactory().createLinearRing(extractCoordinates(resultSegs));
 86   }
 87  
 88   private static Coordinate[] extractCoordinates(List segs)
 89   {
 90     Coordinate[] pts = new Coordinate[segs.size() + 1];
 91     LineSegment seg = null;
 92     for (int i = 0; i < segs.size(); i++) {
 93       seg = (LineSegment) segs.get(i);
 94       pts[i] = seg.p0;
 95     }
 96     // add last point
 97     pts[pts.length - 1] = seg.p1;
 98     return pts;
 99   }
100  
101  
102 }
103