Java Code Examples for com.vividsolutions.jts.geom.CoordinateSequence#size()

The following examples show how to use com.vividsolutions.jts.geom.CoordinateSequence#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PlanarPolygon3D.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Computes an average normal vector from a list of polygon coordinates.
 * Uses Newell's method, which is based
 * on the fact that the vector with components
 * equal to the areas of the projection of the polygon onto 
 * the Cartesian axis planes is normal.
 * 
 * @param seq the sequence of coordinates for the polygon
 * @return a normal vector
 */
private Vector3D averageNormal(CoordinateSequence seq) 
{
	int n = seq.size();
	Coordinate sum = new Coordinate(0,0,0);
	Coordinate p1 = new Coordinate(0,0,0);
	Coordinate p2 = new Coordinate(0,0,0);
	for (int i = 0; i < n - 1; i++) {
		seq.getCoordinate(i, p1);
		seq.getCoordinate(i+1, p2);
		sum.x += (p1.y - p2.y)*(p1.z + p2.z);
		sum.y += (p1.z - p2.z)*(p1.x + p2.x);
		sum.z += (p1.x - p2.x)*(p1.y + p2.y);
	}
	sum.x /= n;
	sum.y /= n;
	sum.z /= n;
	Vector3D norm = Vector3D.create(sum).normalize();
	return norm;
}
 
Example 2
Source File: WKTWriter.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Generates the WKT for a <tt>LINESTRING</tt>
 * specified by a {@link CoordinateSequence}.
 *
 * @param seq the sequence to write
 *
 * @return the WKT string
 */
public static String toLineString(CoordinateSequence seq)
{
  StringBuffer buf = new StringBuffer();
  buf.append("LINESTRING ");
  if (seq.size() == 0)
    buf.append(" EMPTY");
  else {
    buf.append("(");
    for (int i = 0; i < seq.size(); i++) {
      if (i > 0)
        buf.append(", ");
      buf.append(seq.getX(i) + " " + seq.getY(i));
    }
    buf.append(")");
  }
  return buf.toString();
}
 
Example 3
Source File: PolygonConverter.java    From game-server with MIT License 6 votes vote down vote up
/**
 *  生成KPolygon
 * @param lineString
 * @return
 */
public KPolygon makeKPolygonFrom(com.vividsolutions.jts.geom.LineString lineString) {
    CoordinateSequence coordinateSequence = lineString.getCoordinateSequence();
    ArrayList<Vector3> points = new ArrayList<>();
    // The loop stops at the second-last coord since the last coord will be
    // the same as the start coord.
    Vector3 lastAddedPoint = null;
    for (int i = 0; i < coordinateSequence.size() - 1; i++) {
        Coordinate coord = coordinateSequence.getCoordinate(i);
        Vector3 p = new Vector3((float)coord.x, (float)coord.z, (float)coord.y);
        if (lastAddedPoint != null && p.x == lastAddedPoint.x && p.z == lastAddedPoint.z) {
            // Don't add the point since it's the same as the last one
            continue;
        } else {
            points.add(p);
            lastAddedPoint = p;
        }
    }
    if (points.size() < 3) {
        return null;
    }
    KPolygon polygon = new KPolygon(points);
    return polygon;
}
 
Example 4
Source File: LineDissolver.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void add(LineString lineString) {
  if (factory == null) {
    this.factory = lineString.getFactory();
  }
  CoordinateSequence seq = lineString.getCoordinateSequence();
  boolean doneStart = false;
  for (int i = 1; i < seq.size(); i++) {
    DissolveHalfEdge e = (DissolveHalfEdge) graph.addEdge(seq.getCoordinate(i-1), seq.getCoordinate(i));
    // skip zero-length edges
    if (e == null) continue;
    /**
     * Record source initial segments, so that they can be reflected in output when needed
     * (i.e. during formation of isolated rings)
     */
    if (! doneStart) {
      e.setStart();
      doneStart = true;
    }
  }
}
 
Example 5
Source File: CGAlgorithms.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Computes the length of a linestring specified by a sequence of points.
 * 
 * @param pts
 *          the points specifying the linestring
 * @return the length of the linestring
 */
public static double length(CoordinateSequence pts)
{
  // optimized for processing CoordinateSequences
  int n = pts.size();
  if (n <= 1)
    return 0.0;

  double len = 0.0;

  Coordinate p = new Coordinate();
  pts.getCoordinate(0, p);
  double x0 = p.x;
  double y0 = p.y;

  for (int i = 1; i < n; i++) {
    pts.getCoordinate(i, p);
    double x1 = p.x;
    double y1 = p.y;
    double dx = x1 - x0;
    double dy = y1 - y0;

    len += Math.sqrt(dx * dx + dy * dy);

    x0 = x1;
    y0 = y1;
  }
  return len;
}
 
Example 6
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tests for equality using all supported accessors,
 * to provides test coverage for them.
 * 
 * @param seq
 * @param coords
 * @return
 */
boolean isEqual(CoordinateSequence seq, Coordinate[] coords)
{
  if (seq.size() != coords.length) return false;

  Coordinate p = new Coordinate();
  
  for (int i = 0; i < seq.size(); i++) {
    if (! coords[i].equals(seq.getCoordinate(i)))  return false;

    // Ordinate named getters
    if (coords[i].x != seq.getX(i))  return false;
    if (coords[i].y != seq.getY(i))  return false;

    // Ordinate indexed getters
    if (coords[i].x != seq.getOrdinate(i, CoordinateSequence.X))  return false;
    if (coords[i].y != seq.getOrdinate(i, CoordinateSequence.Y))  return false;
    if (coords[i].z != seq.getOrdinate(i, CoordinateSequence.Z))  return false;
    
    // Coordinate getter
    seq.getCoordinate(i, p);
    if (coords[i].x != p.x) return false;
    if (coords[i].y != p.y)  return false;
    if (coords[i].z != p.z)  return false;
    
  }
  return true;
}
 
Example 7
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
boolean isAllCoordsEqual(CoordinateSequence seq, Coordinate coord)
{
  for (int i = 0; i < seq.size(); i++) {
    if (! coord.equals(seq.getCoordinate(i)))  return false;

    if (coord.x != seq.getOrdinate(i, CoordinateSequence.X))  return false;
    if (coord.y != seq.getOrdinate(i, CoordinateSequence.Y))  return false;
    if (coord.z != seq.getOrdinate(i, CoordinateSequence.Z))  return false;
  }
  return true;
}
 
Example 8
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void test2DZOrdinate()
{
  Coordinate[] coords = createArray(SIZE);

  CoordinateSequence seq = getCSFactory().create(SIZE, 2);
  for (int i = 0; i < seq.size(); i++) {
    seq.setOrdinate(i, 0, coords[i].x);
    seq.setOrdinate(i, 1, coords[i].y);
  }

  for (int i = 0; i < seq.size(); i++) {
    Coordinate p = seq.getCoordinate(i);
    assertTrue(Double.isNaN(p.z));
  }
}
 
Example 9
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCreateBySizeAndModify()
{
  Coordinate[] coords = createArray(SIZE);

  CoordinateSequence seq = getCSFactory().create(SIZE, 3);
  for (int i = 0; i < seq.size(); i++) {
    seq.setOrdinate(i, 0, coords[i].x);
    seq.setOrdinate(i, 1, coords[i].y);
    seq.setOrdinate(i, 2, coords[i].z);
  }

  assertTrue(isEqual(seq, coords));
}
 
Example 10
Source File: WKBWriter.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void writeCoordinateSequence(CoordinateSequence seq, boolean writeSize, OutStream os)
    throws IOException
{
  if (writeSize)
    writeInt(seq.size(), os);

  for (int i = 0; i < seq.size(); i++) {
    writeCoordinate(seq, i, os);
  }
}
 
Example 11
Source File: CGAlgorithms.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Computes the signed area for a ring. The signed area is:
 * <ul>
 * <li>positive if the ring is oriented CW
 * <li>negative if the ring is oriented CCW
 * <li>zero if the ring is degenerate or flat
 * </ul>
 * 
 * @param ring
 *          the coordinates forming the ring
 * @return the signed area of the ring
 */
public static double signedArea(CoordinateSequence ring)
{
  int n = ring.size();
  if (n < 3)
    return 0.0;
  /**
   * Based on the Shoelace formula.
   * http://en.wikipedia.org/wiki/Shoelace_formula
   */
  Coordinate p0 = new Coordinate();
  Coordinate p1 = new Coordinate();
  Coordinate p2 = new Coordinate();
  ring.getCoordinate(0, p1);
  ring.getCoordinate(1, p2);
  double x0 = p1.x;
  p2.x -= x0;
  double sum = 0.0;
  for (int i = 1; i < n - 1; i++) {
    p0.y = p1.y;
    p1.x = p2.x;
    p1.y = p2.y;
    ring.getCoordinate(i + 1, p2);
    p2.x -= x0;
    sum += p1.x * (p0.y - p2.y);
  }
  return sum / 2.0;
}
 
Example 12
Source File: PlanarPolygon3D.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Computes a point which is the average of all coordinates
 * in a sequence.
 * If the sequence lies in a single plane,
 * the computed point also lies in the plane.
 * 
 * @param seq a coordinate sequence
 * @return a Coordinate with averaged ordinates
 */
private Coordinate averagePoint(CoordinateSequence seq) {
	Coordinate a = new Coordinate(0,0,0);
	int n = seq.size();
	for (int i = 0; i < n; i++) {
		a.x += seq.getOrdinate(i, CoordinateSequence.X);
		a.y += seq.getOrdinate(i, CoordinateSequence.Y);
		a.z += seq.getOrdinate(i, CoordinateSequence.Z);
	}
	a.x /= n;
	a.y /= n;
	a.z /= n;
	return a;
}
 
Example 13
Source File: FacetSequenceTreeBuilder.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void addFacetSequences(CoordinateSequence pts, List sections) {
  int i = 0;
  int size = pts.size();
  while (i <= size - 1) {
    int end = i + FACET_SEQUENCE_SIZE + 1;
    // if only one point remains after this section, include it in this
    // section
    if (end >= size - 1)
      end = size;
    FacetSequence sect = new FacetSequence(pts, i, end);
    sections.add(sect);
    i = i + FACET_SEQUENCE_SIZE;
  }
}
 
Example 14
Source File: GeoJsonWriter.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String getJsonString(CoordinateSequence coordinateSequence) {
  StringBuffer result = new StringBuffer();

  if (coordinateSequence.size() > 1) {
    result.append("[");
  }
  for (int i = 0; i < coordinateSequence.size(); i++) {
    if (i > 0) {
      result.append(",");
    }
    result.append("[");
    result.append(formatOrdinate(coordinateSequence.getOrdinate(i, CoordinateSequence.X))); 
    result.append(",");
    result.append(formatOrdinate(coordinateSequence.getOrdinate(i, CoordinateSequence.Y)));

    if (coordinateSequence.getDimension() > 2 ) {
      double z = coordinateSequence.getOrdinate(i, CoordinateSequence.Z);
      if (!  Double.isNaN(z)) {
        result.append(",");
        result.append(formatOrdinate(z));
      }
    }

    result.append("]");

  }

  if (coordinateSequence.size() > 1) {
    result.append("]");
  }

  return result.toString();
}
 
Example 15
Source File: GamaCoordinateSequenceFactory.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Method create()
 * 
 * @see com.vividsolutions.jts.geom.CoordinateSequenceFactory#create(com.vividsolutions.jts.geom.CoordinateSequence)
 */
@Override
public ICoordinates create(final CoordinateSequence coordSeq) {
	if (coordSeq.size() == 1) { return new UniqueCoordinateSequence(coordSeq.getCoordinate(0)); }
	if (coordSeq instanceof GamaCoordinateSequence) { return ((GamaCoordinateSequence) coordSeq).clone(); }
	return new GamaCoordinateSequence(coordSeq.toCoordinateArray());
}
 
Example 16
Source File: EdgeGraphBuilder.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void add(LineString lineString) {
  CoordinateSequence seq = lineString.getCoordinateSequence();
  for (int i = 1; i < seq.size(); i++) {
    graph.addEdge(seq.getCoordinate(i-1), seq.getCoordinate(i));
  }
}
 
Example 17
Source File: Distance3DOp.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private Coordinate intersection(PlanarPolygon3D poly,LineString line) {
	CoordinateSequence seq = line.getCoordinateSequence();
	if (seq.size() == 0)
		return null;

	// start point of line
	Coordinate p0 = new Coordinate();
	seq.getCoordinate(0, p0);
	double d0 = poly.getPlane().orientedDistance(p0);
	
	// for each segment in the line
	Coordinate p1 = new Coordinate();
	for (int i = 0; i < seq.size() - 1; i++) {
		seq.getCoordinate(i, p0);
		seq.getCoordinate(i + 1, p1);
		double d1 = poly.getPlane().orientedDistance(p1);

		/**
		 * If the oriented distances of the segment endpoints have the same sign, 
		 * the segment does not cross the plane, and is skipped.
		 */
		if (d0 * d1 > 0)
			continue;

		/**
		 * Compute segment-plane intersection point
		 * which is then used for a point-in-polygon test.
		 * The endpoint distances to the plane d0 and d1 
		 * give the proportional distance of the intersection point 
		 * along the segment.
		 */
		Coordinate intPt = segmentPoint(p0, p1, d0, d1);
		// Coordinate intPt = polyPlane.intersection(p0, p1, s0, s1);
		if (poly.intersects(intPt)) {
			return intPt;
		}

		// shift to next segment
		d0 = d1;
	}
	return null;
}