Skip to content

Commit

Permalink
Add generic setfield fallbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion committed Dec 16, 2024
1 parent 2e0a4a7 commit 71ebcbd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
keywords = ["GDAL", "IO"]
license = "MIT"
desc = "A high level API for GDAL - Geospatial Data Abstraction Library"
version = "0.10.5"
version = "0.10.6"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
16 changes: 16 additions & 0 deletions src/ogr/feature.jl
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,22 @@ function setfield!(
return feature
end

function setfield!(
feature::AbstractFeature,
i::Integer,
value::Integer,
)::AbstractFeature
return setfield!(feature, i, convert(Int64, value))
end

function setfield!(
feature::AbstractFeature,
i::Integer,
value::Real,
)::AbstractFeature
return setfield!(feature, i, convert(Float64, value))
end

function setfield!(
feature::AbstractFeature,
i::Integer,
Expand Down
12 changes: 12 additions & 0 deletions src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ function Base.convert(og::Type{OGRFieldType}, ::Type{<:Enum{T}}) where {T}
return Base.convert(og, T)
end

function Base.convert(::Type{OGRFieldType}, ::Type{<:Real})
return OFTReal
end

function Base.convert(::Type{OGRFieldType}, ::Type{<:Integer})
return OFTInteger64
end

@convert(
OGRFieldSubType::GDAL.OGRFieldSubType,
OFSTNone::GDAL.OFSTNone,
Expand Down Expand Up @@ -420,6 +428,10 @@ function Base.convert(og::Type{OGRFieldSubType}, ::Type{<:Enum{T}}) where {T}
return Base.convert(og, T)
end

function Base.convert(::Type{OGRFieldSubType}, ::Type{<:Real})
return OFSTNone
end

@convert(
OGRJustification::GDAL.OGRJustification,
OJUndefined::GDAL.OJUndefined,
Expand Down
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
Expand Down
8 changes: 8 additions & 0 deletions test/test_convert.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
using Test
import ArchGDAL as AG
import GeoFormatTypes as GFT
using FixedPointNumbers

struct CustomInt <: Integer
value::Int64
end
Base.convert(::Type{Int64}, x::CustomInt) = x.value

@testset "test_convert.jl" begin

Expand Down Expand Up @@ -67,10 +73,12 @@ import GeoFormatTypes as GFT
@test convert(AG.OGRFieldType, UInt32) == AG.OFTInteger64
@test convert(AG.OGRFieldType, Int32) == AG.OFTInteger
@test convert(AG.OGRFieldType, Int64) == AG.OFTInteger64
@test convert(AG.OGRFieldType, CustomInt) == AG.OFTInteger64

@test convert(AG.OGRFieldType, Float16) == AG.OFTReal
@test convert(AG.OGRFieldType, Float32) == AG.OFTReal
@test convert(AG.OGRFieldType, Float64) == AG.OFTReal
@test convert(AG.OGRFieldType, N0f16) == AG.OFTReal

# Reverse conversion should result in default type, not subtype
@test convert(DataType, AG.OFSTBoolean) == Bool
Expand Down
11 changes: 11 additions & 0 deletions test/test_feature.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test
import ArchGDAL as AG
import GeoInterface as GI
using FixedPointNumbers

@enum MyEnum::Bool begin
MyEnumValue = false
Expand Down Expand Up @@ -264,6 +265,12 @@ end
AG.setsubtype!(fielddefn, AG.OFSTBoolean)
return AG.addfielddefn!(layer, fielddefn)
end
AG.createfielddefn("fixedpointfield", AG.OFTReal) do fielddefn
return AG.addfielddefn!(layer, fielddefn)
end
AG.createfielddefn("customintfield", AG.OFTInteger64) do fielddefn
return AG.addfielddefn!(layer, fielddefn)
end
AG.createfeature(layer) do feature
geojsonstring = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 4, 44 ], [ 5, 44 ], [ 5, 45 ], [ 4, 45 ], [ 4, 44 ] ] ] }"
AG.setfield!(feature, 0, Int64(1))
Expand All @@ -285,6 +292,8 @@ end
AG.setfield!(feature, 16, UInt16(1.0))
AG.setfield!(feature, 17, UInt32(1.0))
AG.setfield!(feature, 18, MyEnumValue)
AG.setfield!(feature, 19, N0f16(1.0))
AG.setfield!(feature, 20, CustomInt(1))
for i in 1:AG.nfield(feature)
@test !AG.isfieldnull(feature, i - 1)
@test AG.isfieldsetandnotnull(feature, i - 1)
Expand All @@ -305,6 +314,8 @@ end
@test AG.getfield(feature, 16) === Int32(1) # Widened from UInt16
@test AG.getfield(feature, 17) === Int64(1) # Widened from UInt32
@test AG.getfield(feature, 18) === false # Enum is lost
@test AG.getfield(feature, 19) === 1.0 # FixedPointNumber is lost
@test AG.getfield(feature, 20) === 1 # CustomInt is lost

AG.addfeature(layer) do newfeature
AG.setfrom!(newfeature, feature)
Expand Down

0 comments on commit 71ebcbd

Please sign in to comment.