Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionize max vertice for OGCGeometry#buffer #190

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/main/java/com/esri/core/geometry/GeometryEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

package com.esri.core.geometry;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.ArrayList;
Expand Down Expand Up @@ -658,6 +657,13 @@ public static Geometry[] cut(Geometry cuttee, Polyline cutter,

return cutsList.toArray(new Geometry[0]);
}

public static Polygon[] buffer(Geometry[] geometries,
SpatialReference spatialReference, double[] distances,
boolean toUnionResults) {
return buffer(geometries, spatialReference, distances, 0, toUnionResults);
}

/**
* Calculates a buffer polygon for each geometry at each of the
* corresponding specified distances. It is assumed that all geometries have
Expand All @@ -669,11 +675,12 @@ public static Geometry[] cut(Geometry cuttee, Polyline cutter,
* @param geometries An array of geometries to be buffered.
* @param spatialReference The spatial reference of the geometries.
* @param distances The corresponding distances for the input geometries to be buffered.
* @param max_vertices_in_full_circle Max vertices of circle for buffer edge.
* @param toUnionResults TRUE if all geometries buffered at a given distance are to be unioned into a single polygon.
* @return The buffer of the geometries.
*/
public static Polygon[] buffer(Geometry[] geometries,
SpatialReference spatialReference, double[] distances,
SpatialReference spatialReference, double[] distances, int max_vertices_in_full_circle,
boolean toUnionResults) {
// initially assume distances are in unit of spatial reference
double[] bufferDistances = distances;
Expand All @@ -685,7 +692,7 @@ public static Polygon[] buffer(Geometry[] geometries,
SimpleGeometryCursor inputGeometriesCursor = new SimpleGeometryCursor(
geometries);
GeometryCursor result = op.execute(inputGeometriesCursor,
spatialReference, bufferDistances, toUnionResults, null);
spatialReference, bufferDistances, max_vertices_in_full_circle, toUnionResults, null);

ArrayList<Polygon> resultGeoms = new ArrayList<Polygon>();
Geometry g;
Expand All @@ -698,12 +705,17 @@ public static Polygon[] buffer(Geometry[] geometries,
Polygon[] buffers = new Polygon[geometries.length];
for (int i = 0; i < geometries.length; i++) {
buffers[i] = (Polygon) op.execute(geometries[i],
spatialReference, bufferDistances[i], null);
spatialReference, bufferDistances[i], max_vertices_in_full_circle, null);
}
return buffers;
}
}

public static Polygon buffer(Geometry geometry,
SpatialReference spatialReference, double distance) {
return buffer(geometry, spatialReference, 0, distance);
}

/**
* Calculates a buffer polygon of the geometry as specified by the
* distance input. The buffer is implemented in the xy-plane.
Expand All @@ -713,16 +725,18 @@ public static Polygon[] buffer(Geometry[] geometries,
* @param geometry Geometry to be buffered.
* @param spatialReference The spatial reference of the geometry.
* @param distance The specified distance for buffer. Same units as the spatial reference.
* @param max_vertices_in_full_circle Max vertices of circle for buffer edge.
* @return The buffer polygon at the specified distances.
*/
public static Polygon buffer(Geometry geometry,
SpatialReference spatialReference, double distance) {
SpatialReference spatialReference, int max_vertices_in_full_circle, double distance) {
double bufferDistance = distance;

OperatorBuffer op = (OperatorBuffer) factory
.getOperator(Operator.Type.Buffer);
Geometry result = op.execute(geometry, spatialReference,
bufferDistance, null);
bufferDistance, max_vertices_in_full_circle, null
);
return (Polygon) result;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/esri/core/geometry/OperatorBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

package com.esri.core.geometry;

import com.esri.core.geometry.Operator.Type;

/**
*Creates buffer polygons around geometries.
*/
Expand All @@ -41,10 +39,11 @@ public Type getType() {
*@param inputGeometries The geometries to buffer.
*@param sr The SpatialReference of the Geometries.
*@param distances The buffer distances for the Geometries. If the size of the distances array is less than the number of geometries in the inputGeometries, the last distance value is used for the rest of geometries.
*@param max_vertices_in_full_circle Max vertices of circle for buffer edge.
*@param bUnion If True, the buffered geometries will be unioned, otherwise they wont be unioned.
*/
public abstract GeometryCursor execute(GeometryCursor inputGeometries,
SpatialReference sr, double[] distances, boolean bUnion,
SpatialReference sr, double[] distances, int max_vertices_in_full_circle, boolean bUnion,
ProgressTracker progressTracker);

/**
Expand All @@ -53,9 +52,10 @@ public abstract GeometryCursor execute(GeometryCursor inputGeometries,
*@param inputGeometry The geometry to buffer.
*@param sr The SpatialReference of the Geometry.
*@param distance The buffer distance for the Geometry.
*@param max_vertices_in_full_circle Max vertices of circle for buffer edge.
*/
public abstract Geometry execute(Geometry inputGeometry,
SpatialReference sr, double distance,
SpatialReference sr, double distance, int max_vertices_in_full_circle,
ProgressTracker progressTracker);

/**
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/esri/core/geometry/OperatorBufferLocal.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ class OperatorBufferLocal extends OperatorBuffer {

@Override
public GeometryCursor execute(GeometryCursor inputGeometries,
SpatialReference sr, double[] distances, boolean bUnion,
SpatialReference sr, double[] distances, int max_vertices_in_full_circle, boolean bUnion,
ProgressTracker progressTracker) {
return execute(inputGeometries, sr, distances, NumberUtils.NaN(), 96,
bUnion, progressTracker);
}

@Override
public Geometry execute(Geometry inputGeometry, SpatialReference sr,
double distance, ProgressTracker progressTracker) {
public Geometry execute(
Geometry inputGeometry, SpatialReference sr,
double distance, int max_vertices_in_full_circle, ProgressTracker progressTracker
) {
SimpleGeometryCursor inputCursor = new SimpleGeometryCursor(
inputGeometry);
double[] distances = new double[1];
distances[0] = distance;
GeometryCursor outputCursor = execute(inputCursor, sr, distances,
false, progressTracker);
max_vertices_in_full_circle, false, progressTracker
);

return outputCursor.next();
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/esri/core/geometry/ogc/OGCGeometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,10 @@ private OGCGeometry simplifyBunch_(GeometryCursor gc) {
}

public OGCGeometry buffer(double distance) {
return buffer(distance, 0); // use default which is 96 currently
}

public OGCGeometry buffer(double distance, int max_vertices_in_full_circle) {
OperatorBuffer op = (OperatorBuffer) OperatorFactoryLocal.getInstance()
.getOperator(Operator.Type.Buffer);
if (distance == 0) {// when distance is 0, return self (maybe we should
Expand All @@ -461,7 +465,7 @@ public OGCGeometry buffer(double distance) {

double d[] = { distance };
com.esri.core.geometry.GeometryCursor cursor = op.execute(
getEsriGeometryCursor(), getEsriSpatialReference(), d, true,
getEsriGeometryCursor(), getEsriSpatialReference(), d, max_vertices_in_full_circle, true,
null);
return OGCGeometry.createFromEsriGeometry(cursor.next(), esriSR);
}
Expand Down
48 changes: 24 additions & 24 deletions src/test/java/com/esri/core/geometry/TestBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testBufferPoint() {
.getInstance().getOperator(Operator.Type.Buffer);
OperatorSimplify simplify = (OperatorSimplify) OperatorFactoryLocal
.getInstance().getOperator(Operator.Type.Simplify);
Geometry result = buffer.execute(inputGeom, sr, 40.0, null);
Geometry result = buffer.execute(inputGeom, sr, 40.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) result;
int pathCount = poly.getPathCount();
Expand All @@ -65,13 +65,13 @@ public void testBufferPoint() {
assertTrue(is_simple);

{
result = buffer.execute(inputGeom, sr, 0, null);
result = buffer.execute(inputGeom, sr, 0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -1, null);
result = buffer.execute(inputGeom, sr, -1, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}
Expand All @@ -85,7 +85,7 @@ public void testBufferEnvelope() {
.getInstance().getOperator(Operator.Type.Buffer);
OperatorSimplify simplify = (OperatorSimplify) OperatorFactoryLocal
.getInstance().getOperator(Operator.Type.Simplify);
Geometry result = buffer.execute(inputGeom, sr, 40.0, null);
Geometry result = buffer.execute(inputGeom, sr, 40.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -102,25 +102,25 @@ public void testBufferEnvelope() {
assertTrue(simplify.isSimpleAsFeature(result, sr, true, nsr, null));

{
result = buffer.execute(inputGeom, sr, -200.0, null);
result = buffer.execute(inputGeom, sr, -200.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -200.0, null);
result = buffer.execute(inputGeom, sr, -200.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -199 / 2.0, null);
result = buffer.execute(inputGeom, sr, -199 / 2.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -50.0, null);
result = buffer.execute(inputGeom, sr, -50.0, 0, null);
poly = (Polygon) (result);
result.queryEnvelope2D(env2D);
assertTrue(Math.abs(env2D.getWidth() - (199 - 100)) < 0.001
Expand All @@ -145,7 +145,7 @@ public void testBufferMultiPoint() {
MultiPoint inputGeom = new MultiPoint();
inputGeom.add(12, 120);
inputGeom.add(20, 120);
Geometry result = buffer.execute(inputGeom, sr, 40.0, null);
Geometry result = buffer.execute(inputGeom, sr, 40.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -161,13 +161,13 @@ public void testBufferMultiPoint() {
assertTrue(simplify.isSimpleAsFeature(result, sr, null));

{
result = buffer.execute(inputGeom, sr, 0, null);
result = buffer.execute(inputGeom, sr, 0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -1, null);
result = buffer.execute(inputGeom, sr, -1, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}
Expand All @@ -181,7 +181,7 @@ public void testBufferLine() {
.getInstance().getOperator(Operator.Type.Buffer);
OperatorSimplify simplify = (OperatorSimplify) OperatorFactoryLocal
.getInstance().getOperator(Operator.Type.Simplify);
Geometry result = buffer.execute(inputGeom, sr, 40.0, null);
Geometry result = buffer.execute(inputGeom, sr, 40.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -197,13 +197,13 @@ public void testBufferLine() {
assertTrue(simplify.isSimpleAsFeature(result, sr, null));

{
result = buffer.execute(inputGeom, sr, 0, null);
result = buffer.execute(inputGeom, sr, 0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
result = buffer.execute(inputGeom, sr, -1, null);
result = buffer.execute(inputGeom, sr, -1, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}
Expand All @@ -223,19 +223,19 @@ public void testBufferPolyline() {
inputGeom.lineTo(0, 50);

{
Geometry result = buffer.execute(inputGeom, sr, 0, null);
Geometry result = buffer.execute(inputGeom, sr, 0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
Geometry result = buffer.execute(inputGeom, sr, -1, null);
Geometry result = buffer.execute(inputGeom, sr, -1, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result.isEmpty());
}

{
Geometry result = buffer.execute(inputGeom, sr, 40.0, null);
Geometry result = buffer.execute(inputGeom, sr, 40.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -252,7 +252,7 @@ public void testBufferPolyline() {
}

{
Geometry result = buffer.execute(inputGeom, sr, 4.0, null);
Geometry result = buffer.execute(inputGeom, sr, 4.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -275,7 +275,7 @@ public void testBufferPolyline() {
inputGeom.startPath(50, 0);
inputGeom.lineTo(0, 50);

Geometry result = buffer.execute(inputGeom, sr, 4.0, null);
Geometry result = buffer.execute(inputGeom, sr, 4.0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -298,7 +298,7 @@ public void testBufferPolyline() {
inputGeom.lineTo(1.763006,0.607034);
inputGeom.lineTo(1.762548,0.607135);

Geometry result = buffer.execute(inputGeom, sr, 0.005, null);
Geometry result = buffer.execute(inputGeom, sr, 0.005, 0, null);
assertTrue(simplify.isSimpleAsFeature(result, sr, null));
}
}
Expand All @@ -316,13 +316,13 @@ public void testBufferPolygon() {
inputGeom.lineTo(50, 0);

{
Geometry result = buffer.execute(inputGeom, sr, 0, null);
Geometry result = buffer.execute(inputGeom, sr, 0, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
assertTrue(result == inputGeom);
}

{
Geometry result = buffer.execute(inputGeom, sr, 10, null);
Geometry result = buffer.execute(inputGeom, sr, 10, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand All @@ -345,7 +345,7 @@ public void testBufferPolygon() {
inputGeom.lineTo(50, 50);
inputGeom.lineTo(50, 0);

Geometry result = buffer.execute(inputGeom, sr, -10, null);
Geometry result = buffer.execute(inputGeom, sr, -10, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand Down Expand Up @@ -373,7 +373,7 @@ public void testBufferPolygon() {
inputGeom.lineTo(40, 40);
inputGeom.lineTo(10, 40);

Geometry result = buffer.execute(inputGeom, sr, -2, null);
Geometry result = buffer.execute(inputGeom, sr, -2, 0, null);
assertTrue(result.getType().value() == Geometry.GeometryType.Polygon);
Polygon poly = (Polygon) (result);
Envelope2D env2D = new Envelope2D();
Expand Down