Skip to content

Commit

Permalink
Add a JLD2 extension to allow serializing geometries (#448)
Browse files Browse the repository at this point in the history
* half finished JLD2 extension to serialize ArchGDAL geometries

* use WellKnownBinary instead

Co-authored-by: Maarten Pronk <[email protected]>

* add JLD2 to extras for Julia <1.9 support

* add back space

* add compat bounds

---------

Co-authored-by: Maarten Pronk <[email protected]>
  • Loading branch information
asinghvi17 and evetion authored Nov 14, 2024
1 parent b6c95c1 commit c7aa0d6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
16 changes: 10 additions & 6 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ GeoInterfaceRecipes = "0329782f-3d07-4b52-b9f6-d3137cf03c7a"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[weakdeps]
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[extensions]
ArchGDALMakieExt = "Makie"
ArchGDALJLD2Ext = "JLD2"

[compat]
CEnum = "0.4, 0.5"
ColorTypes = "0.10, 0.11, 0.12"
Expand All @@ -33,13 +41,9 @@ GeoInterfaceRecipes = "1.0"
ImageCore = "0.8, 0.9, 0.10"
Makie = "0.20, 0.21"
Tables = "1"
JLD2 = "0.4, 0.5"
julia = "1.6"

[extensions]
ArchGDALMakieExt = "Makie"

[extras]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"

[weakdeps]
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
23 changes: 23 additions & 0 deletions ext/ArchGDALJLD2Ext.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module ArchGDALJLD2Ext

import ArchGDAL as AG
import GeoInterface as GI
import JLD2

struct ArchGDALSerializedGeometry
# TODO: add spatial reference
wkb::Vector{UInt8}
end


JLD2.writeas(::Type{<: AG.AbstractGeometry}) = ArchGDALSerializedGeometry

function JLD2.wconvert(::Type{<: ArchGDALSerializedGeometry}, x::AG.AbstractGeometry)
return ArchGDALSerializedGeometry(AG.toWKB(x))
end

function JLD2.rconvert(::Type{<: AG.AbstractGeometry}, x::ArchGDALSerializedGeometry)
return AG.fromWKB(x.wkb)
end

end
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Expand Down
14 changes: 14 additions & 0 deletions test/test_geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using Test
import GeoInterface as GI
import ArchGDAL as AG
import GeoFormatTypes as GFT
import JLD2

@testset "test_geometry.jl" begin
@testset "GeoInterface" begin
Expand Down Expand Up @@ -1028,4 +1029,17 @@ import GeoFormatTypes as GFT
GI.coordinates(ag_geom) ==
[[1, 2], [1, 2]]
end

@testset "JLD2 serialization" begin
filepath = joinpath(tempdir(), "test_geometry.jld2")
geom = AG.fromWKT("MULTIPOLYGON (" *
"((0 4 8,4 4 8,4 0 8,0 0 8,0 4 8)," *
"(3 1 8,3 3 8,1 3 8,1 1 8,3 1 8))," *
"((10 4 8,14 4 8,14 0 8,10 0 8,10 4 8)," *
"(13 1 8,13 3 8,11 3 8,11 1 8,13 1 8)))")

JLD2.save_object(filepath, geom)
geom2 = JLD2.load_object(filepath)
@test AG.toWKT(geom2) == AG.toWKT(geom)
end
end

0 comments on commit c7aa0d6

Please sign in to comment.