From c494bc538e3612f79890251ecd6add55e12a0ed4 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 13 Nov 2024 16:16:22 -0500 Subject: [PATCH 1/5] half finished JLD2 extension to serialize ArchGDAL geometries --- Project.toml | 13 +++++++------ ext/ArchGDALJLD2Ext.jl | 23 +++++++++++++++++++++++ test/Project.toml | 1 + test/test_geometry.jl | 14 ++++++++++++++ 4 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 ext/ArchGDALJLD2Ext.jl diff --git a/Project.toml b/Project.toml index 783b2bf8..5a9ae43a 100644 --- a/Project.toml +++ b/Project.toml @@ -19,6 +19,13 @@ 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" @@ -35,11 +42,5 @@ Makie = "0.20, 0.21" Tables = "1" julia = "1.6" -[extensions] -ArchGDALMakieExt = "Makie" - [extras] Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" - -[weakdeps] -Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" diff --git a/ext/ArchGDALJLD2Ext.jl b/ext/ArchGDALJLD2Ext.jl new file mode 100644 index 00000000..270c750e --- /dev/null +++ b/ext/ArchGDALJLD2Ext.jl @@ -0,0 +1,23 @@ +module ArchGDALJLD2Ext + +import ArchGDAL as AG +import GeoInterface as GI +import JLD2 + +struct ArchGDALSerializedGeometry + wkbtype::AG.OGRwkbGeometryType + coords::Vector +end + + +JLD2.writeas(::Type{<: AG.AbstractGeometry{WKBType}}) where WKBType = ArchGDALSerializedGeometry{WKBType} + +function JLD2.wconvert(::Type{<: ArchGDALSerializedGeometry{WKBType}}, x::AG.AbstractGeometry{WKBType}) where WKBType + return ArchGDALSerializedGeometry{WKBType}(GI.coordinates(x)) +end + +function JLD2.rconvert(::Type{<: AG.AbstractGeometry{WKBType}}, x::ArchGDALSerializedGeometry{WKBType}) where WKBType + return AG.lookup_method[typeof(x.trait)](x.coords) +end + +end \ No newline at end of file diff --git a/test/Project.toml b/test/Project.toml index aeef832e..b23afeb8 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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" diff --git a/test/test_geometry.jl b/test/test_geometry.jl index 1544c4ba..6561677e 100644 --- a/test/test_geometry.jl +++ b/test/test_geometry.jl @@ -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 @@ -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 From 75aa60d0354b745f93d58d52e0b8e44948b6faa8 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 13 Nov 2024 16:22:31 -0500 Subject: [PATCH 2/5] use WellKnownBinary instead Co-authored-by: Maarten Pronk --- ext/ArchGDALJLD2Ext.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/ext/ArchGDALJLD2Ext.jl b/ext/ArchGDALJLD2Ext.jl index 270c750e..0ddf2b75 100644 --- a/ext/ArchGDALJLD2Ext.jl +++ b/ext/ArchGDALJLD2Ext.jl @@ -5,19 +5,19 @@ import GeoInterface as GI import JLD2 struct ArchGDALSerializedGeometry - wkbtype::AG.OGRwkbGeometryType - coords::Vector + # TODO: add spatial reference + wkb::Vector{UInt8} end -JLD2.writeas(::Type{<: AG.AbstractGeometry{WKBType}}) where WKBType = ArchGDALSerializedGeometry{WKBType} +JLD2.writeas(::Type{<: AG.AbstractGeometry}) = ArchGDALSerializedGeometry -function JLD2.wconvert(::Type{<: ArchGDALSerializedGeometry{WKBType}}, x::AG.AbstractGeometry{WKBType}) where WKBType - return ArchGDALSerializedGeometry{WKBType}(GI.coordinates(x)) +function JLD2.wconvert(::Type{<: ArchGDALSerializedGeometry}, x::AG.AbstractGeometry) + return ArchGDALSerializedGeometry(AG.toWKB(x)) end -function JLD2.rconvert(::Type{<: AG.AbstractGeometry{WKBType}}, x::ArchGDALSerializedGeometry{WKBType}) where WKBType - return AG.lookup_method[typeof(x.trait)](x.coords) +function JLD2.rconvert(::Type{<: AG.AbstractGeometry}, x::ArchGDALSerializedGeometry) + return AG.fromWKB(x.wkb) end end \ No newline at end of file From 9fd7dda0c56ff0e8d5615cf9d9af7a6641a1fa9f Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 13 Nov 2024 16:29:55 -0500 Subject: [PATCH 3/5] add JLD2 to extras for Julia <1.9 support --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 5a9ae43a..2b0e5632 100644 --- a/Project.toml +++ b/Project.toml @@ -44,3 +44,4 @@ julia = "1.6" [extras] Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" +JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819" From 33c2f11abb0bfa20c99bc86095bce8c3bed659fa Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 13 Nov 2024 16:30:17 -0500 Subject: [PATCH 4/5] add back space --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 2b0e5632..befd453f 100644 --- a/Project.toml +++ b/Project.toml @@ -26,6 +26,7 @@ Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" [extensions] ArchGDALMakieExt = "Makie" ArchGDALJLD2Ext = "JLD2" + [compat] CEnum = "0.4, 0.5" ColorTypes = "0.10, 0.11, 0.12" From af88cdd59db73a641bc696c00719af0b876a60fa Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 13 Nov 2024 16:39:14 -0500 Subject: [PATCH 5/5] add compat bounds --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index befd453f..3529392d 100644 --- a/Project.toml +++ b/Project.toml @@ -41,6 +41,7 @@ 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" [extras]