com.vividsolutions.jts.operation.buffer.BufferParameters Java Examples

The following examples show how to use com.vividsolutions.jts.operation.buffer.BufferParameters. 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 Geometry cleanGeometry(final Geometry g) {
	//follow the proposition of https://locationtech.github.io/jts/jts-faq.html#G1
	if (g == null || g.isEmpty()) return g; 
	Geometry g2 = g.buffer(0.0, BufferParameters.DEFAULT_QUADRANT_SEGMENTS,
			BufferParameters.CAP_FLAT);
	if (g2.isEmpty()) {
		if (g instanceof Polygon) {
			Polygon p = (Polygon) g;
			Geometry g3 = GeometryUtils.GEOMETRY_FACTORY.createPolygon(p.getExteriorRing().getCoordinates());
			for (int i = 0; i < p.getNumInteriorRing(); i++) {
				Geometry g4 = GeometryUtils.GEOMETRY_FACTORY.createPolygon(p.getInteriorRingN(i).getCoordinates());
				g3 = g3.difference(g4);
			}
			return g3;
		}else {
			return GeometryUtils.GEOMETRY_FACTORY.createGeometry(g);
		}
	}
	return g2;
}
 
Example #2
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static Geometry buildCurveSet(Geometry g, double dist, BufferParameters bufParams)
{
  // --- now construct curve
  OffsetCurveBuilder ocb = new OffsetCurveBuilder(
      g.getFactory().getPrecisionModel(),
      bufParams);
  OffsetCurveSetBuilder ocsb = new OffsetCurveSetBuilder(g, dist, ocb);
  List curves = ocsb.getCurves();
  
  List lines = new ArrayList();
  for (Iterator i = curves.iterator(); i.hasNext(); ) {
  	SegmentString ss = (SegmentString) i.next();
  	Coordinate[] pts = ss.getCoordinates();
  	lines.add(g.getFactory().createLineString(pts));
  }
  Geometry curve = g.getFactory().buildGeometry(lines);
  return curve;
}
 
Example #3
Source File: JTSFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry logoBuffer(Geometry g, double distance)
{
  Geometry lines = logoLines(g);
  BufferParameters bufParams = new BufferParameters();
  bufParams.setEndCapStyle(BufferParameters.CAP_SQUARE);   
  return BufferOp.bufferOp(lines, distance, bufParams);
}
 
Example #4
Source File: OffsetCurveFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry offsetCurve(Geometry geom, double distance)
{
  BufferParameters bufParams = new BufferParameters();
  OffsetCurveBuilder ocb = new OffsetCurveBuilder(
      geom.getFactory().getPrecisionModel(), bufParams
      );
  Coordinate[] pts = ocb.getOffsetCurve(geom.getCoordinates(), distance);
  Geometry curve = geom.getFactory().createLineString(pts);
  return curve;
}
 
Example #5
Source File: TestCaseGeometryFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry bufferMitredJoin(Geometry g, double distance)	
{
   BufferParameters bufParams = new BufferParameters();
   bufParams.setJoinStyle(BufferParameters.JOIN_MITRE);
   
   return BufferOp.bufferOp(g, distance, bufParams);
}
 
Example #6
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry bufferWithParams(Geometry g, Double distance, 
		Integer quadrantSegments, Integer capStyle, Integer joinStyle, Double mitreLimit)	
{
    double dist = 0;
    if (distance != null) dist = distance.doubleValue();
    
    BufferParameters bufParams = new BufferParameters();
    if (quadrantSegments != null)	bufParams.setQuadrantSegments(quadrantSegments.intValue());
    if (capStyle != null)	bufParams.setEndCapStyle(capStyle.intValue());
    if (joinStyle != null) 	bufParams.setJoinStyle(joinStyle.intValue());
    if (mitreLimit != null) 	bufParams.setMitreLimit(mitreLimit.doubleValue());
    
    return BufferOp.bufferOp(g, dist, bufParams);
}
 
Example #7
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry bufferWithSimplify(Geometry g, Double distance, 
		Double simplifyFactor)	
{
    double dist = 0;
    if (distance != null) dist = distance.doubleValue();
    
    BufferParameters bufParams = new BufferParameters();
    if (simplifyFactor != null)	bufParams.setSimplifyFactor(simplifyFactor.doubleValue());
    
    return BufferOp.bufferOp(g, dist, bufParams);
}
 
Example #8
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry bufferCurveWithParams(Geometry g, Double distance, 
		Integer quadrantSegments, Integer capStyle, Integer joinStyle, Double mitreLimit)	
{
   double dist = 0;
   if (distance != null) dist = distance.doubleValue();
   
   BufferParameters bufParams = new BufferParameters();
   if (quadrantSegments != null)	bufParams.setQuadrantSegments(quadrantSegments.intValue());
   if (capStyle != null)	bufParams.setEndCapStyle(capStyle.intValue());
   if (joinStyle != null) 	bufParams.setJoinStyle(joinStyle.intValue());
   if (mitreLimit != null) 	bufParams.setMitreLimit(mitreLimit.doubleValue());
   
   return buildCurveSet(g, dist, bufParams);
}
 
Example #9
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static Geometry singleSidedBufferCurve(Geometry geom, double distance) {
  BufferParameters bufParam = new BufferParameters();
  bufParam.setSingleSided(true);
  OffsetCurveBuilder ocb = new OffsetCurveBuilder(
      geom.getFactory().getPrecisionModel(), bufParam
      );
  Coordinate[] pts = ocb.getLineCurve(geom.getCoordinates(), distance);
  Geometry curve = geom.getFactory().createLineString(pts);
  return curve;
}
 
Example #10
Source File: GeoJSON.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object inside = stack.pop();
  Object pcterror = stack.pop();
  Object geoJson = stack.pop();

  if (!(geoJson instanceof String) || !(inside instanceof Boolean) || (!(pcterror instanceof Double) && !(pcterror instanceof Long))) {
    throw new WarpScriptException(getName() + " expects a GeoJSON string, an error percentage or resolution (even number between 2 and 30) and a boolean as the top 3 elements of the stack.");
  }

  // Check the resolution is even and in 2..30, if relevant
  if (pcterror instanceof Long) {
    long res = ((Number) pcterror).longValue();
    if (1 == (res % 2) || res > 30 || res < 2) {
      throw new WarpScriptException(getName() + " expects the resolution to be an even number between 2 and 30");
    }
  }

  //
  // Read Geo JSON
  //

  GeoJSONReader reader = new GeoJSONReader();
  Geometry geometry = null;

  try {
    geometry = reader.read((String) geoJson);
  } catch (UnsupportedOperationException uoe) {
    throw new WarpScriptException(uoe);
  }

  //
  // Apply buffer if defined
  //
  Map<Object,Object> buffer = (Map<Object,Object>) stack.getAttribute(GEOBUFFER.ATTR_GEOBUFFER);
  
  if (null != buffer) {
    // Clear the buffer
    stack.setAttribute(GEOBUFFER.ATTR_GEOBUFFER, null);
    // Apply the buffer operation
    BufferOp bop = new BufferOp(geometry, (BufferParameters) buffer.get(GEOBUFFER.KEY_PARAMS));
    geometry = bop.getResultGeometry(((Double) buffer.get(GEOBUFFER.KEY_DIST)).doubleValue());
  }

  //
  // Convert Geometry to a GeoXPShape
  //

  int maxcells = ((Number) stack.getAttribute(WarpScriptStack.ATTRIBUTE_MAX_GEOCELLS)).intValue();
  Object shape = null;

  if (!this.uniform) {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  } else {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  }

  if (null == shape) {
    throw new WarpScriptException("Maximum number of cells exceeded in a geographic shape (warpscript.maxgeocells=" + maxcells + ")");
  }

  stack.push(shape);
  return stack;
}
 
Example #11
Source File: GeoWKT.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object inside = stack.pop();
  Object pcterror = stack.pop();
  Object wkt = stack.pop();

  if (!(wkt instanceof String) || !(inside instanceof Boolean) || (!(pcterror instanceof Double) && !(pcterror instanceof Long))) {
    throw new WarpScriptException(getName() + " expects a WKT string, an error percentage or resolution (even number between 2 and 30) and a boolean as the top 3 elements of the stack.");
  }

  // Check the resolution is even and in 2..30, if relevant
  if (pcterror instanceof Long) {
    long res = ((Number) pcterror).longValue();
    if (1 == (res % 2) || res > 30 || res < 2) {
      throw new WarpScriptException(getName() + " expects the resolution to be an even number between 2 and 30");
    }
  }

  //
  // Read WKT
  //

  WKTReader reader = new WKTReader();
  Geometry geometry = null;

  try {
    geometry = reader.read(wkt.toString());
  } catch (ParseException pe) {
    throw new WarpScriptException(pe);
  }

  //
  // Apply buffer if defined
  //
  Map<Object,Object> buffer = (Map<Object,Object>) stack.getAttribute(GEOBUFFER.ATTR_GEOBUFFER);
  
  if (null != buffer) {
    // Clear the buffer
    stack.setAttribute(GEOBUFFER.ATTR_GEOBUFFER, null);
    // Apply the buffer operation
    BufferOp bop = new BufferOp(geometry, (BufferParameters) buffer.get(GEOBUFFER.KEY_PARAMS));
    geometry = bop.getResultGeometry(((Double) buffer.get(GEOBUFFER.KEY_DIST)).doubleValue());
  }
  
  //
  // Convert Geometry to a GeoXPShape
  //

  int maxcells = ((Number) stack.getAttribute(WarpScriptStack.ATTRIBUTE_MAX_GEOCELLS)).intValue();
  Object shape = null;

  if (!this.uniform) {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  } else {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  }

  if (null == shape) {
    throw new WarpScriptException("Maximum number of cells exceeded in a geographic shape (warpscript.maxgeocells=" + maxcells + ")");
  }

  stack.push(shape);
  return stack;
}
 
Example #12
Source File: GeoWKB.java    From warp10-platform with Apache License 2.0 4 votes vote down vote up
@Override
public Object apply(WarpScriptStack stack) throws WarpScriptException {
  Object inside = stack.pop();
  Object pcterror = stack.pop();
  Object wkb = stack.pop();

  if (!(wkb instanceof byte[]) || !(inside instanceof Boolean) || (!(pcterror instanceof Double) && !(pcterror instanceof Long))) {
    throw new WarpScriptException(getName() + " expects a WKB byte array, an error percentage or resolution (even number between 2 and 30) and a boolean as the top 3 elements of the stack.");
  }

  // Check the resolution is even and in 2..30, if relevant
  if (pcterror instanceof Long) {
    long res = ((Number) pcterror).longValue();
    if (1 == (res % 2) || res > 30 || res < 2) {
      throw new WarpScriptException(getName() + " expects the resolution to be an even number between 2 and 30");
    }
  }

  //
  // Read WKB
  //

  WKBReader reader = new WKBReader();

  Geometry geometry = null;

  try {
    byte[] bytes = (byte[]) wkb;
    geometry = reader.read(bytes);
  } catch (ParseException pe) {
    throw new WarpScriptException(pe);
  }

  //
  // Apply buffer if defined
  //
  Map<Object,Object> buffer = (Map<Object,Object>) stack.getAttribute(GEOBUFFER.ATTR_GEOBUFFER);
  
  if (null != buffer) {
    // Clear the buffer
    stack.setAttribute(GEOBUFFER.ATTR_GEOBUFFER, null);
    // Apply the buffer operation
    BufferOp bop = new BufferOp(geometry, (BufferParameters) buffer.get(GEOBUFFER.KEY_PARAMS));
    geometry = bop.getResultGeometry(((Double) buffer.get(GEOBUFFER.KEY_DIST)).doubleValue());
  }
  
  //
  // Convert Geometry to a GeoXPShape
  //

  int maxcells = ((Number) stack.getAttribute(WarpScriptStack.ATTRIBUTE_MAX_GEOCELLS)).intValue();
  Object shape = null;

  if (!this.uniform) {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  } else {
    if (pcterror instanceof Double) {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).doubleValue(), Boolean.TRUE.equals(inside), maxcells);
    } else {
      shape = GeoXPLib.toUniformGeoXPShape(geometry, ((Number) pcterror).intValue(), Boolean.TRUE.equals(inside), maxcells);
    }
  }

  if (null == shape) {
    throw new WarpScriptException("Maximum number of cells exceeded in a geographic shape (warpscript.maxgeocells=" + maxcells + ")");
  }

  stack.push(shape);
  return stack;
}
 
Example #13
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Geometry bufferCurve(Geometry g, double distance)	
{		
   return buildCurveSet(g, distance, new BufferParameters());
}
 
Example #14
Source File: BufferFunctions.java    From jts with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static Geometry singleSidedBuffer(Geometry geom, double distance) {
  BufferParameters bufParams = new BufferParameters();
  bufParams.setSingleSided(true);
  return BufferOp.bufferOp(geom, distance, bufParams);
}