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

The following examples show how to use com.vividsolutions.jts.geom.Geometry#union() . 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: PathEditor2.java    From coordination_oru with GNU General Public License v3.0 6 votes vote down vote up
public Geometry createEnvelope(ArrayList<PoseSteering> path) {
	Geometry onePoly = null;
	Geometry prevPoly = null;
	for (PoseSteering ps : path) {
		Geometry rect = makeFootprint(ps.getX(), ps.getY(), ps.getTheta());			
		if (onePoly == null) {
			onePoly = rect;
			prevPoly = rect;
		}
		else {
			Geometry auxPoly = prevPoly.union(rect);
			onePoly = onePoly.union(auxPoly.convexHull());
			prevPoly = rect;
		}
	}
	return onePoly;
	//return onePoly.getCoordinates();
}
 
Example 2
Source File: JTSServiceImpl.java    From geowe-core with GNU General Public License v3.0 6 votes vote down vote up
@Override
public List<String> getEnvelope(final List<String> wkts)
		throws IllegalArgumentException {
	Geometry resultGeometry = null;

	final List<String> result = new ArrayList<String>();

	for (final String wkt : wkts) {
		Geometry geom = getGeometry(wkt);
		geom = geom.buffer(TOLERANCIA_ENVELOPE);

		if (resultGeometry == null) {
			resultGeometry = geom;
		} else {
			resultGeometry = resultGeometry.union(geom);
		}
	}

	if (resultGeometry != null) {
		result.add(resultGeometry.getEnvelope().toText());
	}

	return result;
}
 
Example 3
Source File: LineNoder.java    From geowe-core with GNU General Public License v3.0 5 votes vote down vote up
public Collection<Geometry> nodeLines(final Collection<Geometry> lines) {
	final GeometryFactory gf = new GeometryFactory();
	final Geometry linesGeom = gf.createMultiLineString(GeometryFactory
			.toLineStringArray(lines));
	Geometry unionInput = gf.createMultiLineString(null);
	final Geometry point = extractPoint(lines);
	if (point != null)
		unionInput = point;

	final Geometry noded = linesGeom.union(unionInput);
	final List<Geometry> nodedList = new ArrayList<Geometry>();
	nodedList.add(noded);
	return nodedList;
}
 
Example 4
Source File: DivideLineStringTool.java    From geowe-core with GNU General Public License v3.0 5 votes vote down vote up
public List<String> divide(String divisionLine, String lineToDivide)
		throws IllegalArgumentException {

	Geometry geomToDivide = getGeometry(lineToDivide);
	Geometry divisionGeom = getGeometry(divisionLine);
	Geometry unionGeom = geomToDivide.union(divisionGeom);

	List<Geometry> lines = linealize(unionGeom);
	List<Geometry> segments = getSegments(geomToDivide, lines);
	return getWkts(segments);
}
 
Example 5
Source File: BufferByUnionFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
* Buffer polygons by buffering the individual boundary segments and
* either unioning or differencing them.
* 
* @param g
* @param distance
* @return the buffer geometry
*/
public static Geometry bufferBySegments(Geometry g, double distance)
{
  Geometry segs = LineHandlingFunctions.extractSegments(g);
  double posDist = Math.abs(distance);
  Geometry segBuf = bufferByComponents(segs, posDist);
  if (distance < 0.0) 
    return g.difference(segBuf);
  return g.union(segBuf);
}
 
Example 6
Source File: BufferByUnionFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry bufferByChains(Geometry g, double distance, int maxChainSize)
{
  if (maxChainSize <= 0)
    throw new IllegalArgumentException("Maximum Chain Size must be specified as an input parameter");
  Geometry segs = LineHandlingFunctions.extractChains(g, maxChainSize);
  double posDist = Math.abs(distance);
  Geometry segBuf = bufferByComponents(segs, posDist);
  if (distance < 0.0) 
    return g.difference(segBuf);
  return g.union(segBuf);
}
 
Example 7
Source File: MiscellaneousTest2.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testNoding() throws Exception {
  Geometry a = reader.read("LINESTRING(0 0, 100 100)");
  Geometry b = reader.read("LINESTRING(0 100, 100 0)");
  List lineStrings = Arrays.asList(new Object[] {a, b});
  Geometry nodedLineStrings = (LineString) lineStrings.get(0);
  for (int i = 1; i < lineStrings.size(); i++) {
    nodedLineStrings = nodedLineStrings.union((LineString)lineStrings.get(i));
  }
  assertEquals("MULTILINESTRING ((0 0, 50 50), (50 50, 100 100), (0 100, 50 50), (50 50, 100 0))", nodedLineStrings.toString());
}
 
Example 8
Source File: LineDissolvePerfTest.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
private Geometry dissolveLines(Geometry lines) {
  Geometry dissolved = lines.union();
  LineMerger merger = new LineMerger();
  merger.add(dissolved);
  Collection mergedColl = merger.getMergedLineStrings();
  Geometry merged = lines.getFactory().buildGeometry(mergedColl);
  return merged;
}
 
Example 9
Source File: MergePolygonCommand.java    From geomajas-project-server with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void execute(MergePolygonRequest request, MergePolygonResponse response) throws Exception {
	Polygon[] polygons = new Polygon[request.getPolygons().length];
	for (int i = 0; i < request.getPolygons().length; i++) {
		try {
			polygons[i] = (Polygon) converter.toInternal(request.getPolygons()[i]);
		} catch (Exception e) {
			throw new GeomajasException(e, ExceptionCode.MERGE_NO_POLYGON);
		}
	}
	int precision = polygons[0].getPrecisionModel().getMaximumSignificantDigits() - 1;
	PrecisionModel precisionModel = new PrecisionModel(Math.pow(10.0, precision));
	GeometryFactory factory = new GeometryFactory(precisionModel, polygons[0].getSRID());

	Geometry temp = factory.createGeometry(polygons[0]);
	for (int i = 1; i < polygons.length; i++) {
		Geometry polygon = factory.createGeometry(polygons[i]);
		temp = temp.union(polygon.buffer(Math.pow(10.0, -(precision - 1))));
	}
	if (temp instanceof Polygon) {
		MultiPolygon mp = factory.createMultiPolygon(new Polygon[] { (Polygon) temp });
		response.setGeometry(converter.toDto(mp));
	} else if (temp instanceof MultiPolygon && temp.getNumGeometries() != 0
			&& (request.isAllowMultiPolygon() || temp.getNumGeometries() == 1)) {
		response.setGeometry(converter.toDto(temp));
	} else {
		throw new GeomajasException(ExceptionCode.MERGE_NO_POLYGON);
	}
}
 
Example 10
Source File: AbstractTrajectoryEnvelopeCoordinator.java    From coordination_oru with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Get the path index beyond which a robot should not navigate, given the {@link TrajectoryEnvelope} of another robot.  
 * @param te1 The {@link TrajectoryEnvelope} of the leading robot.
 * @param te2 The {@link TrajectoryEnvelope} of the yielding robot.
 * @param currentPIR1 The current path index of the leading robot.
 * @param te1Start The path index
 * @param te1End
 * @param te2Start
 * @return The path index beyond which a robot should not navigate, given the {@link TrajectoryEnvelope} of another robot.
 */
protected int getCriticalPoint(int yieldingRobotID, CriticalSection cs, int leadingRobotCurrentPathIndex) {

	//Number of additional path points robot 2 should stay behind robot 1
	int TRAILING_PATH_POINTS = 3;

	int leadingRobotStart = -1;
	int yieldingRobotStart = -1;
	int leadingRobotEnd = -1;
	int yieldingRobotEnd = -1;
	TrajectoryEnvelope leadingRobotTE = null;
	TrajectoryEnvelope yieldingRobotTE = null;
	if (cs.getTe1().getRobotID() == yieldingRobotID) {
		leadingRobotStart = cs.getTe2Start();
		yieldingRobotStart = cs.getTe1Start();
		leadingRobotEnd = cs.getTe2End();
		yieldingRobotEnd = cs.getTe1End();
		leadingRobotTE = cs.getTe2();
		yieldingRobotTE = cs.getTe1();
	}
	else {
		leadingRobotStart = cs.getTe1Start();
		yieldingRobotStart = cs.getTe2Start();
		leadingRobotEnd = cs.getTe1End();
		yieldingRobotEnd = cs.getTe2End();
		leadingRobotTE = cs.getTe1();
		yieldingRobotTE = cs.getTe2();			
	}

	if (leadingRobotCurrentPathIndex < leadingRobotStart) {
		return Math.max(0, yieldingRobotStart-TRAILING_PATH_POINTS);
	}

	//Compute sweep of robot 1's footprint from current position to LOOKAHEAD
	Pose leadingRobotPose = leadingRobotTE.getTrajectory().getPose()[leadingRobotCurrentPathIndex];
	Geometry leadingRobotInPose = TrajectoryEnvelope.getFootprint(leadingRobotTE.getFootprint(), leadingRobotPose.getX(), leadingRobotPose.getY(), leadingRobotPose.getTheta());
	if (leadingRobotCurrentPathIndex <= leadingRobotEnd) {
		for (int i = leadingRobotCurrentPathIndex+1; i <= leadingRobotEnd; i++) {
			Pose leadingRobotNextPose = leadingRobotTE.getTrajectory().getPose()[i];
			try {
				leadingRobotInPose = leadingRobotInPose.union(TrajectoryEnvelope.getFootprint(leadingRobotTE.getFootprint(), leadingRobotNextPose.getX(), leadingRobotNextPose.getY(), leadingRobotNextPose.getTheta()));			
			} catch (Exception e) { e.printStackTrace(); }
		}
	}

	//Return pose at which yielding robot should stop given driving robot's projected sweep
	for (int i = yieldingRobotStart; i <= yieldingRobotEnd; i++) {
		Pose yieldingRobotPose = yieldingRobotTE.getTrajectory().getPose()[i];
		Geometry yieldingRobotInPose = TrajectoryEnvelope.getFootprint(yieldingRobotTE.getFootprint(), yieldingRobotPose.getX(), yieldingRobotPose.getY(), yieldingRobotPose.getTheta());
		if (leadingRobotInPose.intersects(yieldingRobotInPose)) {
			return Math.max(0, i-TRAILING_PATH_POINTS);
		}
	}

	//The only situation where the above has not returned is when robot 2 should
	//stay "parked", therefore wait at index 0
	return Math.max(0, yieldingRobotStart-TRAILING_PATH_POINTS);

}
 
Example 11
Source File: SubjectUtils.java    From TomboloDigitalConnector with MIT License 4 votes vote down vote up
private static Query queryFromSubjectSpecification(Session session, SubjectRecipe subjectRecipe) {
	SubjectType subjectType = SubjectTypeUtils.getSubjectTypeByProviderAndLabel(subjectRecipe.getProvider(), subjectRecipe.getSubjectType());

	String hqlQuery = "from Subject s where s.subjectType = :subjectType";

	// Add Attribute Match Rule if exists
	if (null != subjectRecipe.getMatchRule()){
		if (subjectRecipe.getMatchRule().attribute == SubjectAttributeMatchRule.MatchableAttribute.label) {
			hqlQuery += " and lower(label) like :pattern";
		} else if (subjectRecipe.getMatchRule().attribute == SubjectAttributeMatchRule.MatchableAttribute.name) {
			hqlQuery += " and lower(name) like :pattern";
		} else {
			throw new IllegalArgumentException(
					"SubjectAttributeMatchRule attribute is not a valid type (can be either name or label)");
		}
	}

	// Add Geo Match Rule if exists
	if (null != subjectRecipe.getGeoMatchRule()){
		List<SubjectRecipe.SubjectGeoMatchRule.GeoRelation> geoRel = new ArrayList<>();
		Collections.addAll(geoRel, SubjectRecipe.SubjectGeoMatchRule.GeoRelation.values());
		SubjectRecipe.SubjectGeoMatchRule.GeoRelation gr = subjectRecipe.getGeoMatchRule().geoRelation;
		if (geoRel.contains(subjectRecipe.getGeoMatchRule().geoRelation)){
			hqlQuery += " and " + subjectRecipe.getGeoMatchRule().geoRelation.name() + "(shape, :geom) = true";
		} else {
			throw new IllegalArgumentException(String.format(
					"SubjectGeoMatchRule geoRelation is not a valid type.\nSupported spatial joins: %s.",
					Stream.of(SubjectRecipe.SubjectGeoMatchRule.GeoRelation.values()).map(Enum::name)
							.collect(Collectors.toList()).toString()));
		}
	}

	// Create the basic query with obligatory paramaters
	Query query = session.createQuery(hqlQuery, Subject.class);

	for (Parameter parameter : query.getParameters()) {
		if (Objects.equals(parameter.getName(), "subjectType")) {
			query.setParameter("subjectType", subjectType);
		} else if (Objects.equals(parameter.getName(), "pattern")) {
			query.setParameter("pattern", subjectRecipe.getMatchRule().pattern.toLowerCase());
		} else if (Objects.equals(parameter.getName(), "geom")) {
			List<Subject> parents = getSubjectBySpecifications(subjectRecipe.getGeoMatchRule().subjects);

			Geometry union = null;
			for (Subject parent : parents){
				if (union == null)
					union = parent.getShape();
				else
					union = union.union(parent.getShape());
			}
			union.setSRID(Subject.SRID);
			query.setParameter("geom", union);
		}
	}

	return query;
}
 
Example 12
Source File: OverlayFunctions.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Geometry unionUsingGeometryCollection(Geometry a, Geometry b)                 
{   
  Geometry gc = a.getFactory().createGeometryCollection(
      new Geometry[] { a, b});
  return gc.union(); 
}
 
Example 13
Source File: OverlayFunctions.java    From jts with GNU Lesser General Public License v2.1 votes vote down vote up
public static Geometry union(Geometry a, Geometry b)					{		return a.union(b);	} 
Example 14
Source File: OverlayFunctions.java    From jts with GNU Lesser General Public License v2.1 votes vote down vote up
public static Geometry unaryUnion(Geometry a)                 {   return a.union(); }