Skip to content

Commit d157063

Browse files
authored
Add generic setfield fallbacks. (#454)
1 parent 6ed8bcc commit d157063

File tree

6 files changed

+49
-1
lines changed

6 files changed

+49
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ uuid = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
33
keywords = ["GDAL", "IO"]
44
license = "MIT"
55
desc = "A high level API for GDAL - Geospatial Data Abstraction Library"
6-
version = "0.10.5"
6+
version = "0.10.6"
77

88
[deps]
99
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"

src/ogr/feature.jl

+16
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,22 @@ function setfield!(
625625
return feature
626626
end
627627

628+
function setfield!(
629+
feature::AbstractFeature,
630+
i::Integer,
631+
value::Integer,
632+
)::AbstractFeature
633+
return setfield!(feature, i, convert(Int64, value))
634+
end
635+
636+
function setfield!(
637+
feature::AbstractFeature,
638+
i::Integer,
639+
value::Real,
640+
)::AbstractFeature
641+
return setfield!(feature, i, convert(Float64, value))
642+
end
643+
628644
function setfield!(
629645
feature::AbstractFeature,
630646
i::Integer,

src/types.jl

+12
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ function Base.convert(og::Type{OGRFieldType}, ::Type{<:Enum{T}}) where {T}
374374
return Base.convert(og, T)
375375
end
376376

377+
function Base.convert(::Type{OGRFieldType}, ::Type{<:Real})
378+
return OFTReal
379+
end
380+
381+
function Base.convert(::Type{OGRFieldType}, ::Type{<:Integer})
382+
return OFTInteger64
383+
end
384+
377385
@convert(
378386
OGRFieldSubType::GDAL.OGRFieldSubType,
379387
OFSTNone::GDAL.OFSTNone,
@@ -420,6 +428,10 @@ function Base.convert(og::Type{OGRFieldSubType}, ::Type{<:Enum{T}}) where {T}
420428
return Base.convert(og, T)
421429
end
422430

431+
function Base.convert(::Type{OGRFieldSubType}, ::Type{<:Real})
432+
return OFSTNone
433+
end
434+
423435
@convert(
424436
OGRJustification::GDAL.OGRJustification,
425437
OJUndefined::GDAL.OJUndefined,

test/Project.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ DiskArrays = "3c3547ce-8d99-4f5e-a174-61eb10b00ae3"
66
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
77
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
88
Extents = "411431e0-e8b7-467b-b5e0-f676ba4f2910"
9+
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
910
GDAL = "add2ef01-049f-52c4-9ee2-e494f65e021a"
1011
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
1112
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"

test/test_convert.jl

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
using Test
22
import ArchGDAL as AG
33
import GeoFormatTypes as GFT
4+
using FixedPointNumbers
5+
6+
struct CustomInt <: Integer
7+
value::Int64
8+
end
9+
Base.convert(::Type{Int64}, x::CustomInt) = x.value
410

511
@testset "test_convert.jl" begin
612

@@ -67,10 +73,12 @@ import GeoFormatTypes as GFT
6773
@test convert(AG.OGRFieldType, UInt32) == AG.OFTInteger64
6874
@test convert(AG.OGRFieldType, Int32) == AG.OFTInteger
6975
@test convert(AG.OGRFieldType, Int64) == AG.OFTInteger64
76+
@test convert(AG.OGRFieldType, CustomInt) == AG.OFTInteger64
7077

7178
@test convert(AG.OGRFieldType, Float16) == AG.OFTReal
7279
@test convert(AG.OGRFieldType, Float32) == AG.OFTReal
7380
@test convert(AG.OGRFieldType, Float64) == AG.OFTReal
81+
@test convert(AG.OGRFieldType, N0f16) == AG.OFTReal
7482

7583
# Reverse conversion should result in default type, not subtype
7684
@test convert(DataType, AG.OFSTBoolean) == Bool

test/test_feature.jl

+11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test
22
import ArchGDAL as AG
33
import GeoInterface as GI
4+
using FixedPointNumbers
45

56
@enum MyEnum::Bool begin
67
MyEnumValue = false
@@ -264,6 +265,12 @@ end
264265
AG.setsubtype!(fielddefn, AG.OFSTBoolean)
265266
return AG.addfielddefn!(layer, fielddefn)
266267
end
268+
AG.createfielddefn("fixedpointfield", AG.OFTReal) do fielddefn
269+
return AG.addfielddefn!(layer, fielddefn)
270+
end
271+
AG.createfielddefn("customintfield", AG.OFTInteger64) do fielddefn
272+
return AG.addfielddefn!(layer, fielddefn)
273+
end
267274
AG.createfeature(layer) do feature
268275
geojsonstring = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 4, 44 ], [ 5, 44 ], [ 5, 45 ], [ 4, 45 ], [ 4, 44 ] ] ] }"
269276
AG.setfield!(feature, 0, Int64(1))
@@ -285,6 +292,8 @@ end
285292
AG.setfield!(feature, 16, UInt16(1.0))
286293
AG.setfield!(feature, 17, UInt32(1.0))
287294
AG.setfield!(feature, 18, MyEnumValue)
295+
AG.setfield!(feature, 19, N0f16(1.0))
296+
AG.setfield!(feature, 20, CustomInt(1))
288297
for i in 1:AG.nfield(feature)
289298
@test !AG.isfieldnull(feature, i - 1)
290299
@test AG.isfieldsetandnotnull(feature, i - 1)
@@ -305,6 +314,8 @@ end
305314
@test AG.getfield(feature, 16) === Int32(1) # Widened from UInt16
306315
@test AG.getfield(feature, 17) === Int64(1) # Widened from UInt32
307316
@test AG.getfield(feature, 18) === false # Enum is lost
317+
@test AG.getfield(feature, 19) === 1.0 # FixedPointNumber is lost
318+
@test AG.getfield(feature, 20) === 1 # CustomInt is lost
308319

309320
AG.addfeature(layer) do newfeature
310321
AG.setfrom!(newfeature, feature)

0 commit comments

Comments
 (0)