Java Code Examples for com.vividsolutions.jts.geom.Geometry#intersection()

The following examples show how to use com.vividsolutions.jts.geom.Geometry#intersection() . 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: BasicExample.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void main(String[] args)
    throws Exception
{
  // read a geometry from a WKT string (using the default geometry factory)
  Geometry g1 = new WKTReader().read("LINESTRING (0 0, 10 10, 20 20)");
  System.out.println("Geometry 1: " + g1);

  // create a geometry by specifying the coordinates directly
  Coordinate[] coordinates = new Coordinate[]{new Coordinate(0, 0),
    new Coordinate(10, 10), new Coordinate(20, 20)};
  // use the default factory, which gives full double-precision
  Geometry g2 = new GeometryFactory().createLineString(coordinates);
  System.out.println("Geometry 2: " + g2);

  // compute the intersection of the two geometries
  Geometry g3 = g1.intersection(g2);
  System.out.println("G1 intersection G2: " + g3);
}
 
Example 2
Source File: TiledFeatureService.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Apply clipping to the features in a tile. The tile and its features should already be in map space.
 *
 * @param tile
 *            tile to put features in
 * @param scale
 *            scale
 * @param panOrigin
 *            When panning on the client, only this parameter changes. So we need to be aware of it as we calculate
 *            the maxScreenEnvelope.
 * @throws GeomajasException oops
 */
public void clipTile(InternalTile tile, double scale, Coordinate panOrigin) throws GeomajasException {
	log.debug("clipTile before {}", tile);
	List<InternalFeature> orgFeatures = tile.getFeatures();
	tile.setFeatures(new ArrayList<InternalFeature>());
	Geometry maxScreenBbox = null; // The tile's maximum bounds in screen space. Used for clipping.
	for (InternalFeature feature : orgFeatures) {
		// clip feature if necessary
		if (exceedsScreenDimensions(feature, scale)) {
			log.debug("feature {} exceeds screen dimensions", feature);
			InternalFeatureImpl vectorFeature = (InternalFeatureImpl) feature.clone();
			tile.setClipped(true);
			vectorFeature.setClipped(true);
			if (null == maxScreenBbox) {
				maxScreenBbox = JTS.toGeometry(getMaxScreenEnvelope(tile, panOrigin));
			}
			Geometry clipped = maxScreenBbox.intersection(feature.getGeometry());
			vectorFeature.setClippedGeometry(clipped);
			tile.addFeature(vectorFeature);
		} else {
			tile.addFeature(feature);
		}
	}
	log.debug("clipTile after {}", tile);
}
 
Example 3
Source File: GeoServiceImpl.java    From geomajas-project-server with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION")
public Geometry transform(Geometry source, CrsTransform crsTransform) {
	try {
		if (crsTransform.isTransforming()) {
			Geometry transformableArea = crsTransform.getTransformableGeometry();
			if (null != transformableArea && !transformableArea.covers(source)) {
				source = source.intersection(transformableArea);
			}
			return JTS.transform(source, crsTransform);
		} else {
			return source;
		}
	} catch (Exception e) { // NOSONAR typically TopologyException, TransformException or FactoryException
		logEnvelopeSuggestCrsTransformInfo(crsTransform.getId(), source, e);
		return createEmptyGeometryForClass(source.getClass());
	}
}
 
Example 4
Source File: ShapeExecuter.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param scope
 * @param shape
 * @return
 */
private Geometry addToroidalParts(final IScope scope, final Geometry shape) {
	Geometry result = shape;
	final ITopology t = scope.getTopology();
	if (t != null && t.isTorus()) {
		final List<Geometry> geoms = t.listToroidalGeometries(shape);
		final Geometry all = GEOMETRY_FACTORY.buildGeometry(geoms);
		final Geometry world = scope.getSimulation().getInnerGeometry();
		result = all.intersection(world);
		// WARNING Does not correctly handle rotations or translations
	}
	return result;
}
 
Example 5
Source File: FixedPrecisionSnappingTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testTriangles()
	throws ParseException
{
	Geometry a = rdr.read("POLYGON ((545 317, 617 379, 581 321, 545 317))");
	Geometry b = rdr.read("POLYGON ((484 290, 558 359, 543 309, 484 290))");
	a.intersection(b);
}
 
Example 6
Source File: CommonBitsOpTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Tests an issue where CommonBitsRemover was not persisting changes to some kinds of CoordinateSequences
 */
public void testPackedCoordinateSequence() {
  GeometryFactory pcsFactory = new GeometryFactory(PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
  Geometry geom0 = read(pcsFactory, "POLYGON ((210 210, 210 220, 220 220, 220 210, 210 210))");
  Geometry geom1 = read("POLYGON ((225 225, 225 215, 215 215, 215 225, 225 225))");
  CommonBitsOp cbo = new CommonBitsOp(true);
  Geometry result = cbo.intersection(geom0, geom1);
  Geometry expected = geom0.intersection(geom1);
  ///Geometry expected = read("POLYGON ((220 215, 215 215, 215 220, 220 220, 220 215))");
  checkEqual(expected, result);
}
 
Example 7
Source File: DefaultSecurityContext.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
private Geometry areaCombine(String layerId, AreaCombineGetter areaGetter) {
	if (null == authentications || authentications.size() == 0) {
		// no authorizations, so nothing should be allowed
		return null;
	}

	Layer<?> layer = configurationService.getLayer(layerId);
	if (null == layer) {
		log.error("areaCombine on unknown layer " + layerId);
		return null;
	}
	LayerInfo layerInfo = layer.getLayerInfo();

	// base is the max bounds of the layer
	Envelope maxBounds = converterService.toInternal(layerInfo.getMaxExtent());
	PrecisionModel precisionModel = new PrecisionModel(PrecisionModel.FLOATING);
	int srid = geoService.getSridFromCrs(layer.getLayerInfo().getCrs());
	GeometryFactory geometryFactory = new GeometryFactory(precisionModel, srid);
	Geometry geometry = geometryFactory.toGeometry(maxBounds);

	// limit based on authorizations
	for (Authentication authentication : authentications) {
		for (BaseAuthorization authorization : authentication.getAuthorizations()) {
			if (authorization instanceof AreaAuthorization) {
				geometry = geometry.intersection(areaGetter.get((AreaAuthorization) authorization));
			}
		}
	}
	geometry.setSRID(srid); // force srid, even when not set correctly by security service
	return geometry;
}
 
Example 8
Source File: OverlayFunctions.java    From jts with GNU Lesser General Public License v2.1 votes vote down vote up
public static Geometry intersection(Geometry a, Geometry b)		{		return a.intersection(b);	}