com.vividsolutions.jts.geom.prep.PreparedGeometry Java Examples

The following examples show how to use com.vividsolutions.jts.geom.prep.PreparedGeometry. 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: GeometryUtils.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public static void iterateOverTriangles(final Polygon polygon, final Consumer<Geometry> action) {
	final double elevation = getContourCoordinates(polygon).averageZ();
	final double sizeTol = Math.sqrt(polygon.getArea()) / 100.0;
	final DelaunayTriangulationBuilder dtb = new DelaunayTriangulationBuilder();
	final PreparedGeometry buffered = PREPARED_GEOMETRY_FACTORY.create(polygon.buffer(sizeTol, 5, 0));
	final Envelope3D env = Envelope3D.of(buffered.getGeometry());
	try {
		dtb.setSites(polygon);
		dtb.setTolerance(sizeTol);
		applyToInnerGeometries(dtb.getTriangles(GEOMETRY_FACTORY), (gg) -> {
			final ICoordinates cc = getContourCoordinates(gg);
			if (cc.isCoveredBy(env) && buffered.covers(gg)) {
				cc.setAllZ(elevation);
				gg.geometryChanged();
				action.accept(gg);
			}
		});
	} catch (final LocateFailureException | ConstraintEnforcementException e) {
		final IScope scope = GAMA.getRuntimeScope();
		GamaRuntimeException.warning("Impossible to triangulate: " + new WKTWriter().write(polygon), scope);
		iterateOverTriangles((Polygon) DouglasPeuckerSimplifier.simplify(polygon, 0.1), action);
		return;
	} finally {
		env.dispose();
	}
}
 
Example #2
Source File: InvalidHoleRemover.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Polygon getResult()
{
  GeometryFactory gf = poly.getFactory();
  Polygon shell = gf.createPolygon((LinearRing) poly.getExteriorRing());
  PreparedGeometry shellPrep = PreparedGeometryFactory.prepare(shell);
  
  List holes = new ArrayList();
  for (int i = 0; i < poly.getNumInteriorRing(); i++) {
    LinearRing hole = (LinearRing) poly.getInteriorRingN(i);
    if (shellPrep.covers(hole)) {
      holes.add(hole);
    }
  }
  // all holes valid, so return original
  if (holes.size() == poly.getNumInteriorRing())
    return poly;
  
  // return new polygon with covered holes only
  Polygon result = gf.createPolygon((LinearRing) poly.getExteriorRing(), 
      GeometryFactory.toLinearRingArray(holes));
  return result;
}
 
Example #3
Source File: WebMercatorTile.java    From xyz-hub with Apache License 2.0 5 votes vote down vote up
public PreparedGeometry getAsPolygon() {
  if (polygon != null) {
    return polygon;
  }

  final BBox bbox = getBBox(false);
  final Envelope envelope = new Envelope(bbox.minLon(), bbox.maxLon(), bbox.minLat(), bbox.maxLat());
  polygon = PreparedGeometryFactory.prepare(JTSHelper.factory.toGeometry(envelope));
  return polygon;
}
 
Example #4
Source File: WebMercatorTile.java    From xyz-hub with Apache License 2.0 5 votes vote down vote up
public PreparedGeometry getExtendedBBoxAsPolygon(int buffer) {
  if (ePolygon != null && ePolygonBuffer == buffer) {
    return ePolygon;
  }

  final BBox bbox = getExtendedBBox(buffer);
  final Envelope envelope = new Envelope(bbox.minLon(), bbox.maxLon(), bbox.minLat(), bbox.maxLat());

  ePolygonBuffer = buffer;
  ePolygon = PreparedGeometryFactory.prepare(JTSHelper.factory.toGeometry(envelope));
  return ePolygon;
}
 
Example #5
Source File: GeometryUtils.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
private static IList<IShape> filterGeoms(final GeometryCollection geom, final Geometry clip, final double sizeTol,
		final boolean approxClipping) {
	if (geom == null) { return null; }
	final double elevation = getContourCoordinates(clip).averageZ();
	final boolean setZ = elevation != 0.0;
	final IList<IShape> result = GamaListFactory.create(Types.GEOMETRY);
	final Geometry bufferClip = sizeTol != 0.0 ? clip.buffer(sizeTol, 5, 0) : clip;
	final PreparedGeometry buffered = PREPARED_GEOMETRY_FACTORY.create(bufferClip);
	final Envelope3D env = Envelope3D.of(buffered.getGeometry());
	try {
		for (int i = 0; i < geom.getNumGeometries(); i++) {
			final Geometry gg = geom.getGeometryN(i);
			if (!clip.covers(gg.getCentroid())) continue;
			final Coordinate[] coord = gg.getCoordinates();
			boolean cond = env.covers(gg.getCentroid().getCoordinate());
			cond = cond && (approxClipping
					? buffered.covers(gg.getCentroid()) && buffered.covers(GEOMETRY_FACTORY.createPoint(coord[0]))
							&& buffered.covers(GEOMETRY_FACTORY.createPoint(coord[1]))
							&& buffered.covers(GEOMETRY_FACTORY.createPoint(coord[2]))
					: bufferClip.covers(gg));
			if (cond) {
				if (setZ) {
					final ICoordinates cc = getContourCoordinates(gg);
					cc.setAllZ(elevation);
					gg.geometryChanged();
				}
				result.add(new GamaShape(gg));
			}
		}
	} finally {
		env.dispose();
	}
	/*
	 * applyToInnerGeometries(geom, (gg) -> { final ICoordinates cc = getContourCoordinates(gg); if
	 * (cc.isCoveredBy(env) && buffered.covers(gg)) {
	 *
	 * } });
	 */
	return result;
}
 
Example #6
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkAllPrepOps(Geometry g1, Geometry g2)
{
   PreparedGeometry prepGeom = PreparedGeometryFactory.prepare(g1);

	checkIntersects(prepGeom, g2);
	checkContains(prepGeom, g2);
	checkContainsProperly(prepGeom, g2);
	checkCovers(prepGeom, g2);	
}
 
Example #7
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkIntersects(PreparedGeometry pg, Geometry g2)
	{
		boolean pgResult = pg.intersects(g2);
		boolean expected = pg.getGeometry().intersects(g2);
		
		if (pgResult != expected) {
//			pg.intersects(g2);
			throw new IllegalStateException("PreparedGeometry.intersects result does not match expected");
		}
		
//		System.out.println("Results match!");
	}
 
Example #8
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkContains(PreparedGeometry pg, Geometry g2)
	{
		boolean pgResult = pg.contains(g2);
		boolean expected = pg.getGeometry().contains(g2);
		
		if (pgResult != expected)
			throw new IllegalStateException("PreparedGeometry.contains result does not match expected");
		
//		System.out.println("Results match!");
	}
 
Example #9
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkContainsProperly(PreparedGeometry pg, Geometry g2)
	{
		boolean pgResult = pg.containsProperly(g2);
		boolean expected = containsProperly(pg.getGeometry(), g2);
		
		if (pgResult != expected)
			throw new IllegalStateException("PreparedGeometry.containsProperly result does not match expected");
		
//		System.out.println("Results match!");
	}
 
Example #10
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkCovers(PreparedGeometry pg, Geometry g2)
	{
		boolean pgResult = pg.covers(g2);
		boolean expected = pg.getGeometry().covers(g2);
		
		if (pgResult != expected)
			throw new IllegalStateException("PreparedGeometry.covers result does not match expected");
		
//		System.out.println("Results match!");
	}
 
Example #11
Source File: PreparedGeometryFunctions.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static PreparedGeometry createPG(Geometry g)
{
  return (new PreparedGeometryFactory()).create(g);
}
 
Example #12
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static boolean intersects(Geometry g1, Geometry g2)
{
   PreparedGeometryFactory pgFact = new PreparedGeometryFactory();
   PreparedGeometry prepGeom = pgFact.create(g1);
   return prepGeom.intersects(g2);
}
 
Example #13
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static boolean contains(Geometry g1, Geometry g2)
{
   PreparedGeometryFactory pgFact = new PreparedGeometryFactory();
   PreparedGeometry prepGeom = pgFact.create(g1);
   return prepGeom.contains(g2);
}
 
Example #14
Source File: PreparedGeometryTeeOperation.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static boolean covers(Geometry g1, Geometry g2)
{
   PreparedGeometryFactory pgFact = new PreparedGeometryFactory();
   PreparedGeometry prepGeom = pgFact.create(g1);
   return prepGeom.contains(g2);
}