/
Polygon.scala
423 lines (350 loc) · 13.4 KB
/
Polygon.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
* Copyright 2016 Azavea
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package geotrellis.vector
import com.vividsolutions.jts.geom.TopologyException
import com.vividsolutions.jts.{geom => jts}
import com.vividsolutions.jts.operation.union._
import GeomFactory._
import geotrellis.vector._
import spire.syntax.cfor._
import scala.collection.JavaConversions._
object Polygon {
implicit def jtsToPolygon(jtsGeom: jts.Polygon): Polygon =
Polygon(jtsGeom)
def apply(exterior: Point*)(implicit d: DummyImplicit): Polygon =
apply(Line(exterior), Set())
def apply(exterior: Seq[Point]): Polygon =
apply(Line(exterior), Set())
def apply(exterior: Line): Polygon =
apply(exterior, Set())
def apply(exterior: Line, holes:Line*): Polygon =
apply(exterior, holes)
def apply(exterior: Line, holes:Traversable[Line]): Polygon = {
if(!exterior.isClosed) {
sys.error(s"Cannot create a polygon with unclosed exterior: $exterior")
}
if(exterior.vertices.length < 4) {
sys.error(s"Cannot create a polygon with exterior with less that 4 points: $exterior")
}
val extGeom = factory.createLinearRing(exterior.jtsGeom.getCoordinates)
val holeGeoms = (
for (hole <- holes) yield {
if (!hole.isClosed) {
sys.error(s"Cannot create a polygon with an unclosed hole: $hole")
} else {
if (hole.vertices.length < 4)
sys.error(s"Cannot create a polygon with a hole with less that 4 points: $hole")
else
factory.createLinearRing(hole.jtsGeom.getCoordinates)
}
}).toArray
Polygon(factory.createPolygon(extGeom, holeGeoms))
}
}
/** Class representing a polygon */
case class Polygon(jtsGeom: jts.Polygon) extends Geometry
with TwoDimensions {
assert(!jtsGeom.isEmpty, s"Polygon Empty: $jtsGeom")
/** Returns a unique representation of the geometry based on standard coordinate ordering. */
def normalized(): Polygon = {
val geom = jtsGeom.clone.asInstanceOf[jts.Polygon]
geom.normalize
Polygon(geom)
}
/** Tests whether this Polygon is a rectangle. */
lazy val isRectangle: Boolean =
jtsGeom.isRectangle
/** Returns the area of this Polygon. */
lazy val area: Double =
jtsGeom.getArea
/** Returns the exterior ring of this Polygon. */
lazy val exterior: Line =
Line(jtsGeom.getExteriorRing.clone.asInstanceOf[jts.LineString])
/** Returns the hole rings of this Polygon. */
lazy val holes: Array[Line] = {
for (i <- 0 until numberOfHoles) yield
Line(jtsGeom.getInteriorRingN(i).clone.asInstanceOf[jts.LineString])
}.toArray
/** Returns true if this Polygon contains holes */
lazy val hasHoles: Boolean =
numberOfHoles > 0
/** Returns the number of holes in this Polygon */
lazy val numberOfHoles: Int =
jtsGeom.getNumInteriorRing
/**
* Returns the boundary of this Polygon.
* The boundary of a Polygon is the set of closed curves corresponding to its
* exterior and interior boundaries.
*/
lazy val boundary: PolygonBoundaryResult =
jtsGeom.getBoundary
/** Returns this Polygon's vertices. */
lazy val vertices: Array[Point] = {
val coords = jtsGeom.getCoordinates
val arr = Array.ofDim[Point](coords.size)
cfor(0)(_ < arr.size, _ + 1) { i =>
val coord = coords(i)
arr(i) = Point(coord.x, coord.y)
}
arr
}
/** Get the number of vertices in this geometry */
lazy val vertexCount: Int = jtsGeom.getNumPoints
/**
* Returns this Polygon's perimeter.
* A Polygon's perimeter is the length of its exterior and interior
* boundaries.
*/
lazy val perimeter: Double =
jtsGeom.getLength
// -- Intersection
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and p.
*/
def &(p: Point): PointOrNoResult =
intersection(p)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and p.
*/
def intersection(p: Point): PointOrNoResult =
jtsGeom.intersection(p.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of the
* points shared by this Polygon and g. If it fails, it reduces the
* precision to avoid TopologyException.
*/
def intersectionSafe(p: Point): PointOrNoResult =
try intersection(p)
catch {
case _: TopologyException => simplifier.reduce(jtsGeom).intersection(simplifier.reduce(p.jtsGeom))
}
@deprecated("This will be removed in 2.0 - use intersectionSafe instead", "1.2")
def safeIntersection(p: Point): PointOrNoResult = intersectionSafe(p)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and mp.
*/
def &(mp: MultiPoint): MultiPointAtLeastOneDimensionIntersectionResult =
intersection(mp)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and mp.
*/
def intersection(mp: MultiPoint): MultiPointAtLeastOneDimensionIntersectionResult =
jtsGeom.intersection(mp.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of the
* points shared by this Polygon and g. If it fails, it reduces the
* precision to avoid TopologyException.
*/
def intersectionSafe(mp: MultiPoint): MultiPointAtLeastOneDimensionIntersectionResult =
try intersection(mp)
catch {
case _: TopologyException => simplifier.reduce(jtsGeom).intersection(simplifier.reduce(mp.jtsGeom))
}
@deprecated("This will be removed in 2.0 - use intersectionSafe instead", "1.2")
def safeIntersection(mp: MultiPoint): MultiPointAtLeastOneDimensionIntersectionResult =
intersectionSafe(mp)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and g.
*/
def &(g: OneDimension): OneDimensionAtLeastOneDimensionIntersectionResult =
intersection(g)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and g.
*/
def intersection(g: OneDimension): OneDimensionAtLeastOneDimensionIntersectionResult =
jtsGeom.intersection(g.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of the
* points shared by this Polygon and g. If it fails, it reduces the
* precision to avoid TopologyException.
*/
def intersectionSafe(g: OneDimension): OneDimensionAtLeastOneDimensionIntersectionResult =
try intersection(g)
catch {
case _: TopologyException => simplifier.reduce(jtsGeom).intersection(simplifier.reduce(g.jtsGeom))
}
@deprecated("This will be removed in 2.0 - use intersectionSafe instead", "1.2")
def safeIntersection(g: OneDimension): OneDimensionAtLeastOneDimensionIntersectionResult =
intersectionSafe(g)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and g.
*/
def &(g: TwoDimensions): TwoDimensionsTwoDimensionsIntersectionResult =
intersection(g)
/**
* Computes a Result that represents a Geometry made up of the points shared
* by this Polygon and g.
*/
def intersection(g: TwoDimensions): TwoDimensionsTwoDimensionsIntersectionResult =
jtsGeom.intersection(g.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of the
* points shared by this Polygon and g. If it fails, it reduces the
* precision to avoid TopologyException.
*/
def intersectionSafe(g: TwoDimensions): TwoDimensionsTwoDimensionsIntersectionResult =
try intersection(g)
catch {
case _: TopologyException => simplifier.reduce(jtsGeom).intersection(simplifier.reduce(g.jtsGeom))
}
@deprecated("This will be removed in 2.0 - use intersectionSafe instead", "1.2")
def safeIntersection(g: TwoDimensions): TwoDimensionsTwoDimensionsIntersectionResult =
intersectionSafe(g)
// -- Union
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon and g.
*/
def |(g: AtMostOneDimension): AtMostOneDimensionPolygonUnionResult =
union(g)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon and g.
*/
def union(g: AtMostOneDimension): AtMostOneDimensionPolygonUnionResult =
jtsGeom.union(g.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon and g.
*/
def |(g: TwoDimensions): TwoDimensionsTwoDimensionsUnionResult =
union(g)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon and g. Uses cascaded polygon union if g is a (multi)polygon
* else falls back to default jts union method.
*/
def union(g: TwoDimensions): TwoDimensionsTwoDimensionsUnionResult = g match {
case p:Polygon =>
new CascadedPolygonUnion(Seq(this, p).map(_.jtsGeom)).union
case mp:MultiPolygon =>
new CascadedPolygonUnion((this +: mp.polygons).map(_.jtsGeom).toSeq).union
case _ =>
jtsGeom.union(g.jtsGeom)
}
// -- Difference
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g.
*/
def -(g: AtMostOneDimension): PolygonAtMostOneDimensionDifferenceResult =
difference(g)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g.
*/
def difference(g: AtMostOneDimension): PolygonAtMostOneDimensionDifferenceResult =
jtsGeom.difference(g.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g.
*/
def -(g: TwoDimensions): TwoDimensionsTwoDimensionsDifferenceResult =
difference(g)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g.
*/
def difference(g: TwoDimensions): TwoDimensionsTwoDimensionsDifferenceResult =
jtsGeom.difference(g.jtsGeom)
// -- SymDifference
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g and all the points in g that are not in
* this Polygon.
*/
def symDifference(g: AtMostOneDimension): AtMostOneDimensionPolygonSymDifferenceResult =
jtsGeom.symDifference(g.jtsGeom)
/**
* Computes a Result that represents a Geometry made up of all the points in
* this Polygon that are not in g and all the points in g that are not in
* this Polygon.
*/
def symDifference(g: TwoDimensions): TwoDimensionsTwoDimensionsSymDifferenceResult =
jtsGeom.symDifference(g.jtsGeom)
// -- Buffer
/** Computes a buffer area around this Polygon having width d. */
def buffer(d: Double): Polygon =
jtsGeom.buffer(d) match {
case p: jts.Polygon => Polygon(p)
case x =>
sys.error(s"Unexpected result for Polygon buffer: ${x.getGeometryType}")
}
// -- Predicates
/**
* Tests whether this Polygon contains the specified Geometry g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* T*****FF*.
*/
def contains(g: Geometry): Boolean =
jtsGeom.contains(g.jtsGeom)
/**
* Tests whether this Polygon is covered by the specified TwoDimensions g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is T*F**F*** or
* *TF**F*** or **FT*F*** or **F*TF***.
*/
def coveredBy(g: TwoDimensions): Boolean =
jtsGeom.coveredBy(g.jtsGeom)
/**
* Tests whether this Polygon covers the specified Geometry g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* T*****FF* or *T****FF* or ***T**FF* or ****T*FF*.
*/
def covers(g: Geometry): Boolean =
jtsGeom.covers(g.jtsGeom)
/**
* Tests whether this Polygon crosses the specified MultiPoint mp.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* T*****T** (A/P).
*/
def crosses(mp: MultiPoint): Boolean =
jtsGeom.crosses(mp.jtsGeom)
/**
* Tests whether this Polygon crosses the specified OneDimension g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* T*****T** (A/L).
*/
def crosses(g: OneDimension): Boolean =
jtsGeom.crosses(g.jtsGeom)
/**
* Tests whether this Polygon overlaps the specified TwoDimensions g.
* Returns true if The DE-9IM Intersection Matrix for the two geometries is
* T*T***T**.
*/
def overlaps(g: TwoDimensions): Boolean =
jtsGeom.overlaps(g.jtsGeom)
/**
* Tests whether this Polygon touches the specified Geometry g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* FT*******, F**T***** or F***T****.
*/
def touches(g: Geometry): Boolean =
jtsGeom.touches(g.jtsGeom)
/**
* Tests whether this Polygon is within the specified TwoDimensions g.
* Returns true if the DE-9IM Intersection Matrix for the two geometries is
* T*F**F***.
*/
def within(g: TwoDimensions): Boolean =
jtsGeom.within(g.jtsGeom)
}