Skip to content

Commit 12d067f

Browse files
authored
Add variable width buffer method to Geometry. (#77)
1 parent eed2491 commit 12d067f

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

doc/api/geom/geometry.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ Common Geometry Methods
9999

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

102+
.. function:: Geometry.variableBuffer
103+
104+
:arg distances: ``Array`` An array of distances.
105+
106+
:returns: :class:`geom.Geometry`
107+
108+
Construct a geometry that buffers this geometry with an array of distances.
109+
102110
.. function:: Geometry.clone
103111

104112
:returns: :class:`geom.Geometry`

src/main/java/org/geoscript/js/geom/Geometry.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.locationtech.jts.geom.Coordinate;
3535
import org.locationtech.jts.geom.Envelope;
3636
import org.locationtech.jts.geom.GeometryFactory;
37+
import org.locationtech.jts.operation.buffer.VariableBuffer;
3738
import org.locationtech.jts.operation.buffer.BufferOp;
3839
import org.locationtech.jts.operation.buffer.BufferParameters;
3940
import org.locationtech.jts.triangulate.VoronoiDiagramBuilder;
@@ -257,6 +258,21 @@ public Geometry buffer(double distance, NativeObject options) {
257258
return wrapped;
258259
}
259260

261+
@JSFunction
262+
public Geometry variableBuffer(NativeArray distances) {
263+
if (distances.size() == 2) {
264+
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), getDouble(distances.get(0)), getDouble(distances.get(1))));
265+
} else if (distances.size() == 3) {
266+
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), getDouble(distances.get(0)), getDouble(distances.get(1)), getDouble(distances.get(2))));
267+
} else {
268+
return (Geometry) GeometryWrapper.wrap(getParentScope(), VariableBuffer.buffer(getGeometry(), distances.stream().mapToDouble(d -> getDouble(d)).toArray()));
269+
}
270+
}
271+
272+
private double getDouble(Object obj) {
273+
return ((Number) obj).doubleValue();
274+
}
275+
260276
@JSGetter
261277
public Projection getProjection() {
262278
return projection;

src/test/resources/org/geoscript/js/tests/geoscript/test_geom.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ exports["test: create(point)"] = function() {
2222

2323
exports["test: create(linestring)"] = function() {
2424
var type = "LineString";
25-
var coordinates = [[0, 1], [1, 2]];
25+
var coordinates = [[0, 1], [1, 2]];
2626
var o, g;
2727

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

197197
}
198198

199+
exports["test: variable buffer"] = function() {
200+
201+
var geom = new GEOM.LineString([[1,2], [10,20], [30,50], [100, 150]]);
202+
var buffer = geom.variableBuffer([10,50])
203+
ASSERT.ok(buffer instanceof GEOM.Polygon)
204+
205+
buffer = geom.variableBuffer([10, 20, 50])
206+
ASSERT.ok(buffer instanceof GEOM.Polygon)
207+
208+
buffer = geom.variableBuffer([10, 20, 50, 75])
209+
ASSERT.ok(buffer instanceof GEOM.Polygon)
210+
211+
}
199212

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

0 commit comments

Comments
 (0)