com.vividsolutions.jts.geom.CoordinateSequence Java Examples

The following examples show how to use com.vividsolutions.jts.geom.CoordinateSequence. 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: FacetSequenceTreeBuilder.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Creates facet sequences
 * 
 * @param g
 * @return List<GeometryFacetSequence>
 */
private static List computeFacetSequences(Geometry g) {
  final List sections = new ArrayList();

  g.apply(new GeometryComponentFilter() {

    public void filter(Geometry geom) {
      CoordinateSequence seq = null;
      if (geom instanceof LineString) {
        seq = ((LineString) geom).getCoordinateSequence();
        addFacetSequences(seq, sections);
      }
      else if (geom instanceof Point) {
        seq = ((Point) geom).getCoordinateSequence();
        addFacetSequences(seq, sections);
      }
    }
  });
  return sections;
}
 
Example #3
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 #4
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 #5
Source File: WKBWriter.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void writeCoordinate(CoordinateSequence seq, int index, OutStream os)
throws IOException
{
  ByteOrderValues.putDouble(seq.getX(index), buf, byteOrder);
  os.write(buf, 8);
  ByteOrderValues.putDouble(seq.getY(index), buf, byteOrder);
  os.write(buf, 8);
  
  // only write 3rd dim if caller has requested it for this writer
  if (outputDimension >= 3) {
    // if 3rd dim is requested, only write it if the CoordinateSequence provides it
  	double ordVal = Coordinate.NULL_ORDINATE;
  	if (seq.getDimension() >= 3)
  		ordVal = seq.getOrdinate(index, 2);
    ByteOrderValues.putDouble(ordVal, buf, byteOrder);
    os.write(buf, 8);
  }
}
 
Example #6
Source File: GeoJsonReader.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private Geometry createMultiPoint(Map<String, Object> geometryMap,
    GeometryFactory geometryFactory) throws ParseException {

  Geometry result = null;

  try {

    @SuppressWarnings("unchecked")
    List<List<Number>> coordinatesList = (List<List<Number>>) geometryMap
        .get(GeoJsonConstants.NAME_COORDINATES);

    CoordinateSequence coordinates = this
        .createCoordinateSequence(coordinatesList);

    result = geometryFactory.createMultiPoint(coordinates);

  } catch (RuntimeException e) {
    throw new ParseException(
        "Could not parse MultiPoint from GeoJson string.", e);
  }

  return result;
}
 
Example #7
Source File: GamaGeometryType.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public static IShape buildTriangle(final double side_size, final ILocation location) {
	final double h = Math.sqrt(3) / 2 * side_size;
	final Coordinate[] points = new Coordinate[4];
	final double x = location == null ? 0 : location.getX();
	final double y = location == null ? 0 : location.getY();
	final double z = location == null ? 0 : location.getZ();
	points[0] = new GamaPoint(x - side_size / 2.0, y + h / 3, z);
	points[1] = new GamaPoint(x, y - 2 * h / 3, z);
	points[2] = new GamaPoint(x + side_size / 2.0, y + h / 3, z);
	points[3] = points[0];
	final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
	final CoordinateSequence cs = fact.create(points);
	final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
	final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
	return new GamaShape(p);
}
 
Example #8
Source File: GamaGeometryType.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public static IShape buildTriangle(final double base, final double height, final ILocation location) {
	final Coordinate[] points = new Coordinate[4];
	final double z = location == null ? 0.0 : location.getZ();
	points[0] = new GamaPoint(-base / 2.0, height / 2, z);
	points[1] = new GamaPoint(0, -height / 2, z);
	points[2] = new GamaPoint(base / 2.0, height / 2, z);
	points[3] = points[0];
	final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
	final CoordinateSequence cs = fact.create(points);
	final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
	final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
	final IShape s = new GamaShape(p);
	if (location != null) {
		s.setLocation(location);
	}
	return s;
}
 
Example #9
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 #10
Source File: PlanarPolygon3D.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static CoordinateSequence project(CoordinateSequence seq, int facingPlane)
{
	switch (facingPlane) {
	case Plane3D.XY_PLANE: return AxisPlaneCoordinateSequence.projectToXY(seq);
	case Plane3D.XZ_PLANE: return AxisPlaneCoordinateSequence.projectToXZ(seq);
	default: return AxisPlaneCoordinateSequence.projectToYZ(seq);
	}
}
 
Example #11
Source File: SimpleMinimumClearance.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void filter(CoordinateSequence seq, int i) {
  // compare to vertex
  checkVertexDistance(seq.getCoordinate(i));
  
  // compare to segment, if this is one
  if (i > 0) {
    checkSegmentDistance(seq.getCoordinate(i - 1), seq.getCoordinate(i));
  }
}
 
Example #12
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 #13
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 #14
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 #15
Source File: BasicCoordinateSequenceTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCopy() {
    CoordinateSequence s1 = CoordinateArraySequenceFactory.instance().create(
        new Coordinate[] { new Coordinate(1, 2), new Coordinate(3, 4)});
    CoordinateSequence s2 = CoordinateSequences.copy(s1);
    assertTrue(s1.getCoordinate(0).equals(s2.getCoordinate(0)));
    assertTrue(s1.getCoordinate(0) != s2.getCoordinate(0));
}
 
Example #16
Source File: BasicCoordinateSequenceTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCloneDimension2() {
	CoordinateSequence s1 = CoordinateArraySequenceFactory.instance()
			.create(2, 2);
	s1.setOrdinate(0, 0, 1);
	s1.setOrdinate(0, 1, 2);
	s1.setOrdinate(1, 0, 3);
	s1.setOrdinate(1, 1, 4);

	CoordinateSequence s2 = (CoordinateSequence) CoordinateSequences.copy(s1);
	assertTrue(s1.getDimension() == s2.getDimension());
	assertTrue(s1.getCoordinate(0).equals(s2.getCoordinate(0)));
	assertTrue(s1.getCoordinate(0) != s2.getCoordinate(0));
}
 
Example #17
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testZeroLength()
{
  CoordinateSequence seq = getCSFactory().create(0, 3);
  assertTrue(seq.size() == 0);

  CoordinateSequence seq2 = getCSFactory().create((Coordinate[]) null);
  assertTrue(seq2.size() == 0);
}
 
Example #18
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 #19
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 #20
Source File: AxisPlaneCoordinateSequence.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a wrapper projecting to the XY plane.
 * 
 * @param seq the sequence to be projected
 * @return a sequence which projects coordinates
 */
public static CoordinateSequence projectToXY(CoordinateSequence seq)
{
	/**
	 * This is just a no-op, but return a wrapper
	 * to allow better testing
	 */
	return new AxisPlaneCoordinateSequence(seq, XY_INDEX);
}
 
Example #21
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 #22
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 #23
Source File: OraReader.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private CoordinateSequence extractCoords(OraGeom oraGeom, double[] ordinates, int start, int end)
{
  CoordinateSequenceFactory csFactory = geometryFactory.getCoordinateSequenceFactory();
  // handle empty case
  if ((ordinates == null) || (ordinates.length == 0)) {
    return csFactory.create(new Coordinate[0]);
  }
  int ordDim = oraGeom.ordDim();
  
  /**
   * The dimension created matches the input dim, unless it is explicitly set,
   * and unless the CoordinateSequence impl limits the dimension.
   */
  int csDim = ordDim;
  if(outputDimension != OraGeom.NULL_DIMENSION){
	  csDim = outputDimension;
  }
  int nCoord = (ordDim == 0 ? 0 : (end - start) / ordDim);

  CoordinateSequence cs = csFactory.create(nCoord, csDim);
  int actualCSDim = cs.getDimension();
  int readDim = Math.min(actualCSDim, ordDim);
  
  for (int iCoord = 0; iCoord < nCoord; iCoord++) {
    for (int iDim = 0; iDim < readDim; iDim++) {
      int ordIndex = start + iCoord * ordDim + iDim - 1;
      // TODO: be more lenient in handling invalid ordinates length
      cs.setOrdinate(iCoord, iDim, ordinates[ordIndex]);
    }
  }
  return cs;
}
 
Example #24
Source File: CoordinateSequenceTestBase.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testCreateByInitAndCopy()
{
  Coordinate[] coords = createArray(SIZE);
  CoordinateSequence seq = getCSFactory().create(coords);
  CoordinateSequence seq2 = getCSFactory().create(seq);
  assertTrue(isEqual(seq2, coords));
}
 
Example #25
Source File: DynamicLineString.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void apply(final CoordinateSequenceFilter filter) {
	final CoordinateSequence points = getCoordinateSequence();
	filter.filter(points, 0);
	if (filter.isDone()) { return; }
	filter.filter(points, 1);
	if (filter.isGeometryChanged()) {
		geometryChanged();
	}
}
 
Example #26
Source File: GamaGeometryFactory.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
public Polygon buildRectangle(final Coordinate[] points) {
	final CoordinateSequenceFactory fact = GamaGeometryFactory.COORDINATES_FACTORY;
	final CoordinateSequence cs = fact.create(points);
	final LinearRing geom = GeometryUtils.GEOMETRY_FACTORY.createLinearRing(cs);
	final Polygon p = GeometryUtils.GEOMETRY_FACTORY.createPolygon(geom, null);
	return p;
}
 
Example #27
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 #28
Source File: SimpleDemo.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void testGwtRpcAndWktAndWkb() {
	FeatureServiceAsync features = GWT.create(FeatureService.class);

	CoordinateSequence cs = new CoordinateArraySequence(new Coordinate[] {
			new Coordinate(1, 2, 3), new Coordinate(4, 5) });

	sLogger.info("CoordinateSequence: " + cs);
	sLogger.info("Copied CS: " + CoordinateSequences.copy(cs));

	final Geometry g = demonstrateGeometryAndWktWriter();
	demonstrateWkb();

	Feature feature = new Feature();
	feature.fid = "feature.1";
	feature.geometry = g;
	feature.properties = new HashMap<String, String>();
	feature.properties.put("name", "JTS!");

	sLogger.info("Original feature: " + feature);

	features.echo(feature, new AsyncCallback<Feature>() {

		public void onSuccess(Feature result) {
			sLogger.info("Echoed feature: " + result);
		}

		public void onFailure(Throwable caught) {
			sLogger.log(Level.WARNING, "Unable to make GWT-RPC request",
					caught);
		}
	});
}
 
Example #29
Source File: GeoJsonReader.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Geometry createPolygon(Map<String, Object> geometryMap,
    GeometryFactory geometryFactory) throws ParseException {

  Geometry result = null;

  try {

    @SuppressWarnings("unchecked")
    List<List<List<Number>>> ringsList = (List<List<List<Number>>>) geometryMap
        .get(GeoJsonConstants.NAME_COORDINATES);

    List<CoordinateSequence> rings = new ArrayList<CoordinateSequence>();

    for (List<List<Number>> coordinates : ringsList) {

      rings.add(createCoordinateSequence(coordinates));
    }

    if (rings.isEmpty()) {
      throw new IllegalArgumentException("Polygon specified with no rings.");
    }

    LinearRing outer = geometryFactory.createLinearRing(rings.get(0));
    LinearRing[] inner = null;
    if (rings.size() > 1) {
      inner = new LinearRing[rings.size() - 1];
      for (int i = 1; i < rings.size(); i++) {
        inner[i - 1] = geometryFactory.createLinearRing(rings.get(i));
      }
    }

    result = geometryFactory.createPolygon(outer, inner);

  } catch (RuntimeException e) {
    throw new ParseException("Could not parse Polygon from GeoJson string.",
        e);
  }

  return result;
}
 
Example #30
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);
}