Skip to content
Merged
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
8 changes: 8 additions & 0 deletions doc/api/geom/geometry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ Common Geometry Methods

Construct a geometry that buffers this geometry by the given width.

.. function:: Geometry.variableBuffer

:arg distances: ``Array`` An array of distances.

:returns: :class:`geom.Geometry`

Construct a geometry that buffers this geometry with an array of distances.

.. function:: Geometry.clone

:returns: :class:`geom.Geometry`
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/org/geoscript/js/geom/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Envelope;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.operation.buffer.VariableBuffer;
import org.locationtech.jts.operation.buffer.BufferOp;
import org.locationtech.jts.operation.buffer.BufferParameters;
import org.locationtech.jts.triangulate.VoronoiDiagramBuilder;
Expand Down Expand Up @@ -257,6 +258,21 @@ public Geometry buffer(double distance, NativeObject options) {
return wrapped;
}

@JSFunction
public Geometry variableBuffer(NativeArray distances) {
if (distances.size() == 2) {
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), getDouble(distances.get(0)), getDouble(distances.get(1))));
} else if (distances.size() == 3) {
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), getDouble(distances.get(0)), getDouble(distances.get(1)), getDouble(distances.get(2))));
} else {
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), distances.stream().mapToDouble(d -> getDouble(d)).toArray()));
}
}

private double getDouble(Object obj) {
return ((Number) obj).doubleValue();
}

@JSGetter
public Projection getProjection() {
return projection;
Expand Down
15 changes: 14 additions & 1 deletion src/test/resources/org/geoscript/js/tests/geoscript/test_geom.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports["test: create(point)"] = function() {

exports["test: create(linestring)"] = function() {
var type = "LineString";
var coordinates = [[0, 1], [1, 2]];
var coordinates = [[0, 1], [1, 2]];
var o, g;

// create a linestring
Expand Down Expand Up @@ -196,6 +196,19 @@ exports["test: create non-conforming delaunay triangles"] = function() {

}

exports["test: variable buffer"] = function() {

var geom = new GEOM.LineString([[1,2], [10,20], [30,50], [100, 150]]);
var buffer = geom.variableBuffer([10,50])
ASSERT.ok(buffer instanceof GEOM.Polygon)

buffer = geom.variableBuffer([10, 20, 50])
ASSERT.ok(buffer instanceof GEOM.Polygon)

buffer = geom.variableBuffer([10, 20, 50, 75])
ASSERT.ok(buffer instanceof GEOM.Polygon)

}

exports["test: Point"] = require("./geom/test_point");
exports["test: LineString"] = require("./geom/test_linestring");
Expand Down