Skip to content

Commit d5568db

Browse files
authored
Merge pull request #60 from geoscript/GSG-57.VariableBuffer
Add variable buffer method to Geometry
2 parents de7b426 + eaa1166 commit d5568db

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/main/groovy/geoscript/geom/Geometry.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.locationtech.jts.geom.util.AffineTransformation
1010
import org.locationtech.jts.operation.buffer.BufferParameters
1111
import org.locationtech.jts.operation.buffer.BufferOp
1212
import org.locationtech.jts.awt.FontGlyphReader
13+
import org.locationtech.jts.operation.buffer.VariableBuffer
1314
import org.locationtech.jts.operation.overlay.snap.GeometrySnapper
1415
import geoscript.geom.io.Reader
1516
import geoscript.geom.io.Readers
@@ -88,6 +89,21 @@ class Geometry {
8889
wrap(BufferOp.bufferOp(g, distance, params))
8990
}
9091

92+
/**
93+
* Buffer the Geometry with variable sizes
94+
* @param distances A List of buffer distances
95+
* @return A variable width buffer
96+
*/
97+
Geometry variableBuffer(List<Double> distances) {
98+
if (distances.size() == 2) {
99+
Geometry.wrap(VariableBuffer.buffer(g, distances[0], distances[1]))
100+
} else if (distances.size() == 3) {
101+
Geometry.wrap(VariableBuffer.buffer(g, distances[0], distances[1], distances[2]))
102+
} else {
103+
Geometry.wrap(VariableBuffer.buffer(g, distances as double[]))
104+
}
105+
}
106+
91107
/**
92108
* Whether this Geometry contains the other Geometry.
93109
* @param other The other Geometry

src/test/groovy/geoscript/geom/GeometryTestCase.groovy

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,84 @@ class GeometryTestCase {
4242
"0.693359375 46.591796875))", buffer.wkt
4343
}
4444

45+
@Test void variableBuffer() {
46+
String wkt = "LINESTRING (0.693359375 46.591796875, 5.703125 51.337890625, 9.306640625 48.0419921875, " +
47+
"12.03125 53.0078125, 19.2822265625 45.80078125)"
48+
Geometry g = Geometry.fromWKT(wkt)
49+
Geometry buffer = g.variableBuffer([5,10])
50+
assertEquals "POLYGON ((16.639165921339224 59.73347194942151, 24.934201825506648 54.05033733662435, " +
51+
"26.353294374365476 52.871849061865476, 27.596922685525453 51.35648358019602, " +
52+
"28.521021887612868 49.6276155736509, 29.090079366532304 47.75168447016128, 29.2822265625 45.80078125, " +
53+
"29.090079366532304 43.84987802983872, 28.521021887612868 41.9739469263491, " +
54+
"27.596922685525453 40.24507891980398, 26.353294374365476 38.729713438134524, " +
55+
"24.837928892696024 37.486085126974544, 23.1090608861509 36.56198592488713, " +
56+
"21.233129782661283 35.99292844596769, 19.2822265625 35.80078125, 17.331323342338717 35.99292844596769, " +
57+
"15.455392238849104 36.56198592488713, 13.72652423230398 37.486085126974544, " +
58+
"11.06718109869518 40.09876160538111, 10.425735356792835 41.02290888313816, " +
59+
"9.306640625 40.91268767251427, 7.915782311420394 41.04967525968971, " +
60+
"6.578373902829325 41.45537366506438, 5.345811254341676 42.11419212643803, " +
61+
"4.299583605720616 42.97280896803987, 3.4196278586248363 42.400441467116536, " +
62+
"2.60677653682545 41.97239921244357, 1.6688109850806416 41.687870472983846, " +
63+
"0.693359375 41.591796875, -0.2820922350806433 41.687870472983846, " +
64+
"-1.2200577868254516 41.97239921244357, -2.084491790098011 42.43444881348727, " +
65+
"-2.8421745309327386 43.05626296906726, -3.463988686512727 43.813945709901986, " +
66+
"-3.926038287556434 44.67837971317455, -4.210567027016152 45.61634526491936, " +
67+
"-4.306640625 46.591796875, -4.210567027016152 47.56724848508064, " +
68+
"-3.926038287556434 48.50521403682545, -3.4639886865127263 49.369648040098014, " +
69+
"-3.344618968209007 49.54047978433004, 0.6581395348429799 55.02192782246175, " +
70+
"1.2858855670550637 55.75513005794494, 2.232522247546463 56.53201470424467, " +
71+
"3.312532245478202 57.10929207507738, 4.484411382306353 57.46477772482284, " +
72+
"5.255328767328899 57.54070645994614, 6.266382951761842 58.77267954823816, " +
73+
"7.501823024427345 59.786579013214556, 8.91132643563219 60.539974282664176, " +
74+
"10.4407267597424 61.003912798891, 12.03125 61.160565664896154, 13.6217732402576 61.003912798891, " +
75+
"15.15117356436781 60.539974282664176, 16.560676975572658 59.786579013214556, " +
76+
"16.639165921339224 59.73347194942151))", buffer.wkt
77+
78+
buffer = g.variableBuffer([5,10,20])
79+
assertEquals "POLYGON ((6.559380488488829 61.37791668387934, 8.338487539477658 62.54098961775868, " +
80+
"11.628557915198202 64.27837190022574, 15.380420122177455 65.41648685806462, 19.2822265625 65.80078125, " +
81+
"23.184033002822574 65.4164868580646, 26.93589520980181 64.27837190022572, " +
82+
"30.393631222892047 62.430173496050905, 33.42436218623095 59.94291687373095, " +
83+
"35.91161880855091 56.91218591039204, 37.759817212725736 53.4544498973018, " +
84+
"38.89793217056461 49.70258769032257, 39.2822265625 45.80078125, 38.8979321705646 41.898974809677426, " +
85+
"37.75981721272573 38.14711260269819, 35.911618808550905 34.68937658960796, " +
86+
"33.42436218623095 31.658645626269045, 30.393631222892036 29.17138900394909, " +
87+
"26.9358952098018 27.323190599774268, 23.184033002822567 26.18507564193539, " +
88+
"19.2822265625 25.80078125, 15.380420122177426 26.185075641935395, 11.628557915198193 27.32319059977427, " +
89+
"8.170821902107956 29.171389003949095, 5.140090938769045 31.65864562626905, 2.6528343164490913 34.68937658960796, " +
90+
"0.8046359122742643 38.14711260269821, -0.2690516304601481 41.686586085534145, " +
91+
"-0.2820922350806433 41.687870472983846, -1.2200577868254516 41.97239921244357, " +
92+
"-2.084491790098011 42.43444881348727, -2.8421745309327386 43.05626296906726, " +
93+
"-3.463988686512727 43.813945709901986, -3.926038287556434 44.67837971317455, " +
94+
"-4.210567027016152 45.61634526491936, -4.306640625 46.591796875, -4.210567027016152 47.56724848508064, " +
95+
"-3.926038287556434 48.50521403682545, -3.6412863060734937 49.08395393657355, " +
96+
"-0.3458813488962189 54.81570057697087, -0.0984621812696602 55.214387245550824, " +
97+
"0.7692806636323439 56.271734961367656, 1.8266283794491764 57.13947780626966, " +
98+
"3.0329476864294236 57.78426890936304, 3.2559574100103457 57.851918169423506, " +
99+
"3.628037134368597 58.42869928491691, 3.7165538769745474 58.56351483019602, " +
100+
"4.960182188134525 60.078880311865476, 6.47554766980398 61.322508623025456, " +
101+
"6.559380488488829 61.37791668387934))", buffer.wkt
102+
103+
buffer = g.variableBuffer([1,3,5,10,20])
104+
assertEquals "POLYGON ((6.559380488488829 61.37791668387934, 8.338487539477658 62.54098961775868, " +
105+
"11.628557915198202 64.27837190022574, 15.380420122177455 65.41648685806462, 19.2822265625 65.80078125, " +
106+
"23.184033002822574 65.4164868580646, 26.93589520980181 64.27837190022572, " +
107+
"30.393631222892047 62.430173496050905, 33.42436218623095 59.94291687373095, " +
108+
"35.91161880855091 56.91218591039204, 37.759817212725736 53.4544498973018, " +
109+
"38.89793217056461 49.70258769032257, 39.2822265625 45.80078125, 38.8979321705646 41.898974809677426, " +
110+
"37.75981721272573 38.14711260269819, 35.911618808550905 34.68937658960796, " +
111+
"33.42436218623095 31.658645626269045, 30.393631222892036 29.17138900394909, " +
112+
"26.9358952098018 27.323190599774268, 23.184033002822567 26.18507564193539, 19.2822265625 25.80078125, " +
113+
"15.380420122177426 26.185075641935395, 11.628557915198193 27.32319059977427, " +
114+
"8.170821902107956 29.171389003949095, 5.140090938769045 31.65864562626905, " +
115+
"2.6528343164490913 34.68937658960796, 0.8046359122742643 38.14711260269821, " +
116+
"-0.3334790455646086 41.89897480967743, -0.7177734375 45.80078125, " +
117+
"-0.3334790455646086 49.702587690322574, 0.8046359122742643 53.4544498973018, " +
118+
"2.475800831237194 56.64255481983383, 3.628037134368597 58.42869928491691, " +
119+
"3.7165538769745474 58.56351483019602, 4.960182188134525 60.078880311865476, " +
120+
"6.47554766980398 61.322508623025456, 6.559380488488829 61.37791668387934))", buffer.wkt
121+
}
122+
45123
@Test void equals() {
46124
Geometry g1 = new Point(111, -47)
47125
Geometry g2 = new Point(111, -47)

0 commit comments

Comments
 (0)