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

The following examples show how to use com.vividsolutions.jts.geom.CoordinateSequence#getCoordinate() . 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: 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 2
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 3
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 4
Source File: OraReader.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create LinearRing for exterior/interior polygon ELEM_INFO triplets.
 *
 * @param oraGeom SDO_GEOMETRY attributes being read
 * @param elemIndex the element being read
 * @param coords the coordinates of the entire geometry
 * @return LinearRing
 *
 * @throws IllegalArgumentException If circle, or curve is requested
 */
private LinearRing readLinearRing(OraGeom oraGeom, int elemIndex) 
{
  int etype = oraGeom.eType(elemIndex);
  int interpretation = oraGeom.interpretation(elemIndex);

	checkOrdinates(oraGeom, elemIndex, "Polygon");
	checkETYPE(etype,OraGeom.ETYPE.POLYGON, OraGeom.ETYPE.POLYGON_EXTERIOR,  OraGeom.ETYPE.POLYGON_INTERIOR, "Polygon");
	checkInterpretation(interpretation, OraGeom.INTERP.POLYGON, OraGeom.INTERP.RECTANGLE, "Polygon");

  CoordinateSequence seq = extractCoords(oraGeom, elemIndex);
	LinearRing ring;
  if (interpretation == OraGeom.INTERP.POLYGON) {
    ring = geometryFactory.createLinearRing(seq);
  } 
  else { 
  	// interpretation == OraSDO.INTERP.RECTANGLE
    // rectangle does not maintain measures
    Coordinate min = seq.getCoordinate(0);
    Coordinate max = seq.getCoordinate(1);
    ring = geometryFactory.createLinearRing(new Coordinate[] {
                min, new Coordinate(max.x, min.y), 
                max, new Coordinate(min.x, max.y), 
                min
            });
  }
  return ring;
}
 
Example 5
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 6
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 7
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 8
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 9
Source File: SimpleRayCrossingStressTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void filter(CoordinateSequence seq, int i)
{
	if (i == 0) return;
	seq.getCoordinate(i - 1, p0);
	seq.getCoordinate(i, p1);
	rcc.countSegment(p0, p1);
}
 
Example 10
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;
}