-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathgeos_types.jl
568 lines (531 loc) · 18.2 KB
/
geos_types.jl
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
abstract type AbstractGeometry end
function Base.show(io::IO, geo::AbstractGeometry)
compact = get(io, :compact, false)
if compact
print(io, typeof(geo), "(...)")
else
s = writegeom(geo)
print(io, s)
end
end
function open_issue_if_conversion_makes_sense(T::Type, id)
msg = """LibGEOS: Can't convert a pointer to an element with a GeomType ID id = $id (GEOSGeomType = $(GEOSGeomTypes(id)))
to a geometry of type $T (yet).
Please open an issue if you think this conversion makes sense.")
"""
error(msg)
end
mutable struct Point <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a point from a pointer - only makese sense if it is a pointer to a point, otherwise error
function Point(obj::Union{Point,GEOSGeom}, context::GEOSContext = get_global_context())
id = LibGEOS.geomTypeId(obj, context)
point = if id == GEOS_POINT
point = new(cloneGeom(obj, context), context)
else
open_issue_if_conversion_makes_sense(Point, id)
end
finalizer(destroyGeom, point)
point
end
# create a point from a vector of floats
Point(coords::Vector{Float64}, context::GEOSContext = get_global_context()) =
Point(createPoint(coords, context), context)
Point(x::Real, y::Real, context::GEOSContext = get_global_context()) =
Point(createPoint(x, y, context), context)
Point(x::Real, y::Real, z::Real, context::GEOSContext = get_global_context()) =
Point(createPoint(x, y, z, context), context)
end
mutable struct MultiPoint <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a multipoint from a pointer - only makes sense if it is a pointer to a multipoint
# or to a point, otherwise error
function MultiPoint(
obj::Union{Point,MultiPoint,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
multipoint = if id == GEOS_MULTIPOINT
new(cloneGeom(obj, context), context)
elseif id == GEOS_POINT
new(createCollection(GEOS_MULTIPOINT, [cloneGeom(obj, context)], context), context)
else
open_issue_if_conversion_makes_sense(MultiPoint, id)
end
finalizer(destroyGeom, multipoint)
multipoint
end
# create a multipoint frome a vector of vector coordinates
MultiPoint(
multipoint::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
) = MultiPoint(
createCollection(
GEOS_MULTIPOINT,
GEOSGeom[createPoint(coords, context) for coords in multipoint],
context,
),
context,
)
# create a multipoint from a list of points
MultiPoint(points::Vector{LibGEOS.Point}, context::GEOSContext = get_context(points)) =
MultiPoint(createCollection(GEOS_MULTIPOINT, points, context), context)
end
mutable struct LineString <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a linestring from a linestring pointer, otherwise error
function LineString(
obj::Union{LineString,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
line = if id == GEOS_LINESTRING
new(cloneGeom(obj, context), context)
else
open_issue_if_conversion_makes_sense(LineString, id)
end
finalizer(destroyGeom, line)
line
end
# create a linestring from a vector of points
function LineString(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
)
line = new(createLineString(coords, context), context)
finalizer(destroyGeom, line)
line
end
function LineString(coords::Vector{Point}, context::GEOSContext = get_global_context())
line = new(createLineString(coords, context), context)
finalizer(destroyGeom, line)
line
end
end
mutable struct MultiLineString <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a multiline string from a multilinestring or a linestring pointer, else error
function MultiLineString(
obj::Union{LineString,MultiLineString,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
multiline = if id == GEOS_MULTILINESTRING
new(cloneGeom(obj, context), context)
elseif id == GEOS_LINESTRING
new(
createCollection(GEOS_MULTILINESTRING, [cloneGeom(obj, context)], context),
context,
)
else
open_issue_if_conversion_makes_sense(MultiLineString, id)
end
finalizer(destroyGeom, multiline)
multiline
end
# create a multilinestring from a list of linestring coordinates
MultiLineString(
multiline::Vector{Vector{Vector{Float64}}},
context::GEOSContext = get_global_context(),
) = MultiLineString(
createCollection(
GEOS_MULTILINESTRING,
GEOSGeom[createLineString(coords, context) for coords in multiline],
context,
),
context,
)
MultiLineString(
multiline::Vector{LineString},
context::GEOSContext = get_global_context(),
) = MultiLineString(
createCollection(
GEOS_MULTILINESTRING,
GEOSGeom[ls.ptr for ls in multiline],
context,
),
context,
)
end
mutable struct LinearRing <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a linear ring from a linear ring pointer, otherwise error
function LinearRing(
obj::Union{LinearRing,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
ring = if id == GEOS_LINEARRING
new(cloneGeom(obj, context), context)
else
open_issue_if_conversion_makes_sense(LinearRing, id)
end
finalizer(destroyGeom, ring)
ring
end
# create linear ring from a list of coordinates -
# first and last coordinates must be the same
function LinearRing(
coords::Vector{Vector{Float64}},
context::GEOSContext = get_global_context(),
)
ring = new(createLinearRing(coords, context), context)
finalizer(destroyGeom, ring)
ring
end
end
mutable struct Polygon <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create polygon using GEOSGeom pointer - only makes sense if pointer points to a polygon or a linear ring to start with.
function Polygon(
obj::Union{Polygon,LinearRing,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
polygon = if id == GEOS_POLYGON
new(cloneGeom(obj, context), context)
elseif id == GEOS_LINEARRING
new(cloneGeom(createPolygon(obj, context), context), context)
else
open_issue_if_conversion_makes_sense(Polygon, id)
end
finalizer(destroyGeom, polygon)
polygon
end
# using vector of coordinates in following form:
# [[exterior], [hole1], [hole2], ...] where exterior and holeN are coordinates where the first and last point are the same
function Polygon(
coords::Vector{Vector{Vector{Float64}}},
context::GEOSContext = get_global_context(),
)
exterior = createLinearRing(coords[1], context)
interiors = GEOSGeom[createLinearRing(lr, context) for lr in coords[2:end]]
polygon = new(createPolygon(exterior, interiors, context), context)
finalizer(destroyGeom, polygon)
polygon
end
# using multiple linear rings to form polygon with holes - exterior linear ring will be polygon boundary and list of interior linear rings will form holes
Polygon(
exterior::LinearRing,
holes::Vector{LinearRing},
context::GEOSContext = get_context(exterior),
) = Polygon(createPolygon(exterior, holes, context), context)
end
mutable struct MultiPolygon <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create multipolygon using a multipolygon or polygon pointer, else error
function MultiPolygon(
obj::Union{Polygon,MultiPolygon,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
multipolygon = if id == GEOS_MULTIPOLYGON
new(cloneGeom(obj, context), context)
elseif id == GEOS_POLYGON
new(
createCollection(GEOS_MULTIPOLYGON, [cloneGeom(obj, context)], context),
context,
)
else
open_issue_if_conversion_makes_sense(MultiPolygon, id)
end
finalizer(destroyGeom, multipolygon)
multipolygon
end
# create multipolygon from list of Polygon objects
MultiPolygon(polygons::Vector{Polygon}, context::GEOSContext = get_context(polygons)) =
MultiPolygon(createCollection(GEOS_MULTIPOLYGON, polygons, context), context)
# create multipolygon using list of polygon coordinates - note that each polygon can have holes as explained above in Polygon comments
MultiPolygon(
multipolygon::Vector{Vector{Vector{Vector{Float64}}}},
context::GEOSContext = get_global_context(),
) = MultiPolygon(
createCollection(
GEOS_MULTIPOLYGON,
GEOSGeom[
createPolygon(
createLinearRing(coords[1], context),
GEOSGeom[createLinearRing(c, context) for c in coords[2:end]],
context,
) for coords in multipolygon
],
context,
),
context,
)
end
mutable struct GeometryCollection <: AbstractGeometry
ptr::GEOSGeom
context::GEOSContext
# create a geometric collection from a pointer to a geometric collection, else error
function GeometryCollection(
obj::Union{GeometryCollection,GEOSGeom},
context::GEOSContext = get_global_context(),
)
id = LibGEOS.geomTypeId(obj, context)
geometrycollection = if id == GEOS_GEOMETRYCOLLECTION
new(cloneGeom(obj, context), context)
else
open_issue_if_conversion_makes_sense(GeometryCollection, id)
end
finalizer(destroyGeom, geometrycollection)
geometrycollection
end
# create a geometric collection from a list of pointers to geometric objects
GeometryCollection(
collection::AbstractVector,
context::GEOSContext = get_global_context(),
) = GeometryCollection(
createCollection(GEOS_GEOMETRYCOLLECTION, collection, context),
context,
)
GeometryCollection(
collection::Vector{<:AbstractGeometry},
context::GEOSContext = get_global_context(),
) = GeometryCollection(
createCollection(
GEOS_GEOMETRYCOLLECTION,
GEOSGeom[geom.ptr for geom in collection],
context,
),
context,
)
end
const Geometry = Union{
Point,
MultiPoint,
LineString,
MultiLineString,
LinearRing,
Polygon,
MultiPolygon,
GeometryCollection,
}
"""
clone(obj::Geometry, context=get_context(obj))
Create a deep copy of obj, optionally also moving it to a new context.
"""
function clone(obj::Geometry, context = get_context(obj))
G = typeof(obj)
# Note that all Geometry constructors
# implicitly clone the pointer, in the following line
G(obj, context)::G
end
"""
get_context(obj::AbstractGeometry)::GEOSContext
Return the `GEOSContext` that `obj` belongs to. If obj is not a GEOS object with
a context, return the global context.
"""
get_context(obj::Geometry) = obj.context
get_context(obj::Any) = get_global_context()
mutable struct PreparedGeometry{G<:AbstractGeometry} <: AbstractGeometry
ptr::Ptr{GEOSPreparedGeometry}
ownedby::G
end
get_context(obj::PreparedGeometry) = get_context(obj.ownedby)
const geomtypes = [
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
GeometryCollection,
]
const HasCoordSeq = Union{LineString,LinearRing,Point}
"""
coordinates!(out::Vector{Float64}, geo::$HasCoordSeq, i::Integer)
coordinates!(out::Vector{Float64}, geo::Point)
Copy the coordinates of the ith point of geo into `out`.
"""
function coordinates!(
out,
geo::HasCoordSeq,
i::Integer,
ctx::GEOSContext = get_context(geo),
)
GC.@preserve out geo begin
seq = GEOSGeom_getCoordSeq_r(ctx, geo)::Ptr
getCoordinates!(out, seq, i, ctx)
end
end
function coordinates!(out, geo::Point, ctx::GEOSContext = get_context(geo))
coordinates!(out, geo, 1, ctx)
end
function has_coord_seq(geo::AbstractGeometry)
false
end
function has_coord_seq(::HasCoordSeq)
true
end
Base.@kwdef struct IsApprox
atol::Float64 = 0.0
rtol::Float64 = sqrt(eps(Float64))
end
function Base.:(==)(geo1::AbstractGeometry, geo2::AbstractGeometry)::Bool
compare(==, geo1, geo2)
end
function Base.isequal(geo1::AbstractGeometry, geo2::AbstractGeometry)::Bool
compare(isequal, geo1, geo2)
end
function Base.isapprox(geo1::AbstractGeometry, geo2::AbstractGeometry; kw...)::Bool
compare(IsApprox(; kw...), geo1, geo2)
end
function compare(
cmp,
geo1::AbstractGeometry,
geo2::AbstractGeometry,
ctx = get_context(geo1),
)::Bool
(typeof(geo1) === typeof(geo2)) || return false
if (geo1 === geo2) && (cmp === isequal)
return true
end
if has_coord_seq(geo1)
return compare_coord_seqs(cmp, geo1, geo2, ctx)
else
ng1 = ngeom(geo1)
ng2 = ngeom(geo2)
ng1 == ng2 || return false
for i = 1:ng1
compare(cmp, getgeom(geo1, i), getgeom(geo2, i), ctx) || return false
end
end
return true
end
npoints(pt::Point) = 1
npoints(geo::HasCoordSeq) = GeoInterface.ngeom(geo)
function compare_coord_seqs(cmp, geo1, geo2, ctx)
ncoords1 = GeoInterface.ncoord(geo1)
ncoords2 = GeoInterface.ncoord(geo2)
ncoords1 == ncoords2 || return false
ncoords1 == 0 && return true
np1 = npoints(geo1)
np2 = npoints(geo2)
np1 == np2 || return false
coords1 = Vector{Float64}(undef, ncoords1)
coords2 = Vector{Float64}(undef, ncoords1)
for i = 1:np1
coordinates!(coords1, geo1, i, ctx)
coordinates!(coords2, geo2, i, ctx)
cmp(coords1, coords2) || return false
end
return true
end
function compare_coord_seqs(cmp::IsApprox, geo1, geo2, ctx)
ncoords1 = GeoInterface.ncoord(geo1)
ncoords2 = GeoInterface.ncoord(geo2)
ncoords1 == ncoords2 || return false
ncoords1 == 0 && return true
@assert ncoords1 in 2:3
ncoords1 == ncoords2 || return false
np1 = npoints(geo1)
np2 = npoints(geo2)
np1 == np2 || return false
coords1 = Vector{Float64}(undef, ncoords1)
coords2 = Vector{Float64}(undef, ncoords1)
# isapprox of two vectors is calculated using their euclidean norms and the norm of their difference
# we compute (the squares) of these norms in an allocation free way
s1 = 0.0
s2 = 0.0
s12 = 0.0
for i = 1:np1
coordinates!(coords1, geo1, i, ctx)
coordinates!(coords2, geo2, i, ctx)
if ncoords1 == 2
x1, y1 = coords1
x2, y2 = coords2
s12 += (x1 - x2)^2 + (y1 - y2)^2
else
x1, y1, z1 = coords1
x2, y2, z2 = coords2
s12 += (x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2
end
s1 += sum(abs2, coords1)
s2 += sum(abs2, coords2)
end
return sqrt(s12) <= cmp.atol + cmp.rtol * sqrt(max(s1, s2))
end
typesalt(::Type{GeometryCollection}) = 0xd1fd7c6403c36e5b
typesalt(::Type{PreparedGeometry}) = 0xbc1a26fe2f5b7537
typesalt(::Type{LineString}) = 0x712352fe219fca15
typesalt(::Type{LinearRing}) = 0xac7644fd36955ef1
typesalt(::Type{MultiLineString}) = 0x85aff0a53a2f2a32
typesalt(::Type{MultiPoint}) = 0x6213e67dbfd3b570
typesalt(::Type{MultiPolygon}) = 0xff2f957b4cdb5832
typesalt(::Type{Point}) = 0x4b5c101d3843160e
typesalt(::Type{Polygon}) = 0xa5c895d62ef56723
function Base.hash(geo::AbstractGeometry, h::UInt)::UInt
h = hash(typesalt(typeof(geo)), h)
if has_coord_seq(geo)
return hash_coord_seq(geo, h)
else
for i = 1:ngeom(geo)
h = hash(getgeom(geo, i), h)
end
end
return h
end
function hash_coord_seq(geo::HasCoordSeq, h::UInt)::UInt
nc = GeoInterface.ncoord(geo)
if nc == 0
return h
end
buf = Vector{Float64}(undef, nc)
ctx = get_context(geo)
for i = 1:npoints(geo)
coordinates!(buf, geo, i, ctx)
h = hash(buf, h)
end
return h
end
# teach ccall how to get the pointer to pass to libgeos
# this way the Julia compiler will track the lifetimes for us
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::AbstractGeometry) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::GEOSContext) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::WKTReader) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::WKTWriter) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::WKBReader) = x.ptr
Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::WKBWriter) = x.ptr
const GEOMTYPE = Dict{GEOSGeomTypes,Symbol}(
GEOS_POINT => :Point,
GEOS_LINESTRING => :LineString,
GEOS_LINEARRING => :LinearRing,
GEOS_POLYGON => :Polygon,
GEOS_MULTIPOINT => :MultiPoint,
GEOS_MULTILINESTRING => :MultiLineString,
GEOS_MULTIPOLYGON => :MultiPolygon,
GEOS_GEOMETRYCOLLECTION => :GeometryCollection,
)
function geomFromGEOS(
ptr::Union{Geometry,Ptr{Cvoid}},
context::GEOSContext = get_global_context(),
)
id = geomTypeId(ptr, context)
if id == GEOS_POINT
return Point(ptr, context)
elseif id == GEOS_LINESTRING
return LineString(ptr, context)
elseif id == GEOS_LINEARRING
return LinearRing(ptr, context)
elseif id == GEOS_POLYGON
return Polygon(ptr, context)
elseif id == GEOS_MULTIPOINT
return MultiPoint(ptr, context)
elseif id == GEOS_MULTILINESTRING
return MultiLineString(ptr, context)
elseif id == GEOS_MULTIPOLYGON
return MultiPolygon(ptr, context)
else
@assert id == GEOS_GEOMETRYCOLLECTION
return GeometryCollection(ptr, context)
end
end