Skip to content

Commit c7aa0d6

Browse files
asinghvi17evetion
andauthored
Add a JLD2 extension to allow serializing geometries (#448)
* half finished JLD2 extension to serialize ArchGDAL geometries * use WellKnownBinary instead Co-authored-by: Maarten Pronk <git@evetion.nl> * add JLD2 to extras for Julia <1.9 support * add back space * add compat bounds --------- Co-authored-by: Maarten Pronk <git@evetion.nl>
1 parent b6c95c1 commit c7aa0d6

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

Project.toml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ GeoInterfaceRecipes = "0329782f-3d07-4b52-b9f6-d3137cf03c7a"
1919
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
2020
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
2121

22+
[weakdeps]
23+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
24+
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
25+
26+
[extensions]
27+
ArchGDALMakieExt = "Makie"
28+
ArchGDALJLD2Ext = "JLD2"
29+
2230
[compat]
2331
CEnum = "0.4, 0.5"
2432
ColorTypes = "0.10, 0.11, 0.12"
@@ -33,13 +41,9 @@ GeoInterfaceRecipes = "1.0"
3341
ImageCore = "0.8, 0.9, 0.10"
3442
Makie = "0.20, 0.21"
3543
Tables = "1"
44+
JLD2 = "0.4, 0.5"
3645
julia = "1.6"
3746

38-
[extensions]
39-
ArchGDALMakieExt = "Makie"
40-
4147
[extras]
4248
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
43-
44-
[weakdeps]
45-
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
49+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"

ext/ArchGDALJLD2Ext.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module ArchGDALJLD2Ext
2+
3+
import ArchGDAL as AG
4+
import GeoInterface as GI
5+
import JLD2
6+
7+
struct ArchGDALSerializedGeometry
8+
# TODO: add spatial reference
9+
wkb::Vector{UInt8}
10+
end
11+
12+
13+
JLD2.writeas(::Type{<: AG.AbstractGeometry}) = ArchGDALSerializedGeometry
14+
15+
function JLD2.wconvert(::Type{<: ArchGDALSerializedGeometry}, x::AG.AbstractGeometry)
16+
return ArchGDALSerializedGeometry(AG.toWKB(x))
17+
end
18+
19+
function JLD2.rconvert(::Type{<: AG.AbstractGeometry}, x::ArchGDALSerializedGeometry)
20+
return AG.fromWKB(x.wkb)
21+
end
22+
23+
end

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
1010
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
1111
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
1212
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
13+
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
1314
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1415
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
1516
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

test/test_geometry.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Test
22
import GeoInterface as GI
33
import ArchGDAL as AG
44
import GeoFormatTypes as GFT
5+
import JLD2
56

67
@testset "test_geometry.jl" begin
78
@testset "GeoInterface" begin
@@ -1028,4 +1029,17 @@ import GeoFormatTypes as GFT
10281029
GI.coordinates(ag_geom) ==
10291030
[[1, 2], [1, 2]]
10301031
end
1032+
1033+
@testset "JLD2 serialization" begin
1034+
filepath = joinpath(tempdir(), "test_geometry.jld2")
1035+
geom = AG.fromWKT("MULTIPOLYGON (" *
1036+
"((0 4 8,4 4 8,4 0 8,0 0 8,0 4 8)," *
1037+
"(3 1 8,3 3 8,1 3 8,1 1 8,3 1 8))," *
1038+
"((10 4 8,14 4 8,14 0 8,10 0 8,10 4 8)," *
1039+
"(13 1 8,13 3 8,11 3 8,11 1 8,13 1 8)))")
1040+
1041+
JLD2.save_object(filepath, geom)
1042+
geom2 = JLD2.load_object(filepath)
1043+
@test AG.toWKT(geom2) == AG.toWKT(geom)
1044+
end
10311045
end

0 commit comments

Comments
 (0)