Skip to content

Commit 52959f4

Browse files
authored
Compatibility with latest ArchGDAL. (#33)
1 parent 56c87a8 commit 52959f4

3 files changed

Lines changed: 48 additions & 99 deletions

File tree

Project.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "GeoDataFrames"
22
uuid = "62cb38b5-d8d2-4862-a48e-6a340996859f"
33
authors = ["Maarten Pronk <git@evetion.nl> and contributors"]
4-
version = "0.2.0"
4+
version = "0.2.1"
55

66
[deps]
77
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
@@ -11,11 +11,11 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
1111
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1212

1313
[compat]
14-
ArchGDAL = "0.7.1"
15-
DataFrames = "0.22, 1.0"
14+
ArchGDAL = "0.8.3"
15+
DataFrames = "1.0"
1616
GeoFormatTypes = "0.3"
17-
Tables = "1.2, 1.3, 1.4"
18-
julia = "1.4"
17+
Tables = "1"
18+
julia = "1.6"
1919

2020
[extras]
2121
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/io.jl

Lines changed: 7 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -10,68 +10,6 @@ const drivermapping = Dict(
1010
".nc" => "netCDF",
1111
)
1212

13-
# Support "exotic" types, but this needs some piracy
14-
Base.convert(::Type{AG.OGRFieldType}, ft::Type{Bool}) = AG.OFTInteger
15-
Base.convert(::Type{AG.OGRFieldType}, ft::Type{Int16}) = AG.OFTInteger
16-
Base.convert(::Type{AG.OGRFieldType}, ft::Type{Float32}) = AG.OFTReal
17-
18-
function Base.convert(::Type{AG.OGRFieldType}, ft::Type{Int8})
19-
@warn "Int8 fields will become Int16"
20-
return AG.OFTInteger
21-
end
22-
function Base.convert(::Type{AG.OGRFieldType}, ft::Type{UInt8})
23-
@warn "UInt8 fields will become Int16"
24-
return AG.OFTInteger
25-
end
26-
function Base.convert(::Type{AG.OGRFieldType}, ft::Type{UInt16})
27-
@warn "UInt16 fields will become Int32"
28-
return AG.OFTInteger
29-
end
30-
function Base.convert(::Type{AG.OGRFieldType}, ft::Type{UInt32})
31-
@warn "UInt32 fields will become Int64"
32-
return AG.OFTInteger64
33-
end
34-
35-
subtypes = Dict(
36-
Bool => AG.OFSTBoolean,
37-
Int16 => AG.OFSTInt16,
38-
Float32 => AG.OFSTFloat32,
39-
Int8 => AG.OFSTInt16,
40-
UInt8 => AG.OFSTInt16,
41-
)
42-
43-
44-
function AG.setfield!(feature::AG.Feature, i::Integer, value::Bool)
45-
AG.GDAL.ogr_f_setfieldinteger(feature.ptr, i, value)
46-
return feature
47-
end
48-
49-
function AG.setfield!(feature::AG.Feature, i::Integer, value::Int16)
50-
AG.GDAL.ogr_f_setfieldinteger(feature.ptr, i, value)
51-
return feature
52-
end
53-
54-
function AG.setfield!(feature::AG.Feature, i::Integer, value::Float32)
55-
AG.GDAL.ogr_f_setfielddouble(feature.ptr, i, value)
56-
return feature
57-
end
58-
59-
function AG.setfield!(feature::AG.Feature, i::Integer, value::Union{UInt8,Int8})
60-
AG.GDAL.ogr_f_setfielddouble(feature.ptr, i, Int16(value))
61-
return feature
62-
end
63-
64-
function AG.setfield!(feature::AG.Feature, i::Integer, value::UInt16)
65-
AG.GDAL.ogr_f_setfielddouble(feature.ptr, i, Int32(value))
66-
return feature
67-
end
68-
69-
function AG.setfield!(feature::AG.Feature, i::Integer, value::UInt32)
70-
AG.GDAL.ogr_f_setfielddouble(feature.ptr, i, Int64(value))
71-
return feature
72-
end
73-
74-
7513
function read(fn::AbstractString; kwargs...)
7614
ds = AG.read(fn; kwargs...)
7715
if AG.nlayer(ds) > 1
@@ -95,7 +33,7 @@ function read(ds, layer)
9533
df
9634
end
9735

98-
function write(fn::AbstractString, table; layer_name::AbstractString = "data", geom_column::Symbol = :geom, crs::Union{GFT.GeoFormat,Nothing} = nothing, driver::Union{Nothing,AbstractString} = nothing)
36+
function write(fn::AbstractString, table; layer_name::AbstractString="data", geom_column::Symbol=:geom, crs::Union{GFT.GeoFormat,Nothing}=nothing, driver::Union{Nothing,AbstractString}=nothing)
9937
rows = Tables.rows(table)
10038
sch = Tables.schema(rows)
10139

@@ -128,17 +66,17 @@ function write(fn::AbstractString, table; layer_name::AbstractString = "data", g
12866
end
12967
AG.create(
13068
fn,
131-
driver = driver
69+
driver=driver
13270
) do ds
13371
spatialref = crs === nothing ? AG.SpatialRef() : AG.importCRS(crs)
13472
AG.createlayer(
135-
name = layer_name,
136-
geom = geom_type,
137-
spatialref = spatialref
73+
name=layer_name,
74+
geom=geom_type,
75+
spatialref=spatialref
13876
) do layer
13977
for (name, type) in fields
14078
AG.createfielddefn(String(name), convert(AG.OGRFieldType, type)) do fd
141-
AG.setsubtype!(fd, get(subtypes, type, AG.OFSTNone))
79+
AG.setsubtype!(fd, convert(AG.OGRFieldSubType, type))
14280
AG.addfielddefn!(layer, fd)
14381
end
14482
end
@@ -155,7 +93,7 @@ function write(fn::AbstractString, table; layer_name::AbstractString = "data", g
15593
end
15694
end
15795
end
158-
AG.copy(layer, dataset = ds, name = layer_name)
96+
AG.copy(layer, dataset=ds, name=layer_name)
15997
end
16098
end
16199
fn

test/runtests.jl

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import GeoDataFrames as GDF
77
import GeoFormatTypes as GFT
88

99
# Use ArchGDAL datasets to test with
10-
const testdatadir = @__DIR__
11-
10+
const testdatadir = joinpath(@__DIR__, "data")
11+
isdir(testdatadir) || mkdir(testdatadir)
1212
REPO_URL = "https://github.com/yeesian/ArchGDALDatasets/blob/master/"
1313

1414
remotefiles = [
@@ -23,7 +23,7 @@ remotefiles = [
2323
for (f, sha) in remotefiles
2424
localfn = joinpath(testdatadir, basename(f))
2525
url = REPO_URL * f * "?raw=true"
26-
PlatformEngines.download_verify(url, sha, localfn; force = true)
26+
PlatformEngines.download_verify(url, sha, localfn; force=true)
2727
end
2828

2929

@@ -63,10 +63,10 @@ end
6363

6464
@testset "Read self written file" begin
6565
# Save table with a few random points
66-
table = DataFrame(geom = AG.createpoint.(coords), name = "test")
66+
table = DataFrame(geom=AG.createpoint.(coords), name="test")
6767
GDF.write("test_points.shp", table)
68-
GDF.write("test_points.gpkg", table, layer_name = "test_points")
69-
GDF.write("test_points.geojson", table, layer_name = "test_points")
68+
GDF.write("test_points.gpkg", table, layer_name="test_points")
69+
GDF.write("test_points.geojson", table, layer_name="test_points")
7070

7171
ntable = GDF.read("test_points.shp")
7272
@test nrow(ntable) == 10
@@ -81,40 +81,51 @@ end
8181
t = GDF.read(fn)
8282

8383
# Save table from reading
84-
GDF.write("test_read.shp", t, layer_name = "test_coastline")
85-
GDF.write("test_read.gpkg", t, layer_name = "test_coastline")
86-
GDF.write("test_read.geojson", t, layer_name = "test_coastline")
84+
GDF.write("test_read.shp", t, layer_name="test_coastline")
85+
GDF.write("test_read.gpkg", t, layer_name="test_coastline")
86+
GDF.write("test_read.geojson", t, layer_name="test_coastline")
8787

8888
end
8989

9090
@testset "Write shapefile with non-GDAL types" begin
91-
coords = zip(rand(Float32, 2), rand(Float32, 2))
91+
coords = collect(zip(rand(Float32, 2), rand(Float32, 2)))
9292
t = DataFrame(
93-
geom = AG.createpoint.(coords),
94-
name = ["test", "test2"],
95-
flag = [typemax(UInt8), typemax(UInt8)],
96-
ex1 = [typemax(Int8), typemax(Int8)],
97-
ex2 = [typemax(UInt16), typemax(UInt16)],
98-
ex3 = [typemax(UInt32), typemax(UInt32)],
99-
check = [false, true],
100-
z = [Float32(8), Float32(-1)],
101-
odd = [1, missing],
102-
date = [now(), now()]
93+
geom=AG.createpoint.(coords),
94+
name=["test", "test2"],
95+
flag=UInt8[typemin(UInt8), typemax(UInt8)],
96+
ex1=Int16[typemin(Int8), typemax(Int8)],
97+
ex2=Int32[typemin(UInt16), typemax(UInt16)],
98+
ex3=Int64[typemin(UInt32), typemax(UInt32)],
99+
check=[false, true],
100+
z=Float32[Float32(8), Float32(-1)],
101+
y=Float16[Float16(8), Float16(-1)],
102+
odd=[1, missing],
103+
date=[DateTime("2022-03-31T15:38:41"), DateTime("2022-03-31T15:38:41")]
103104
)
104-
105105
GDF.write("test_exotic.shp", t)
106106
GDF.write("test_exotic.gpkg", t)
107107
GDF.write("test_exotic.geojson", t)
108+
tt = GDF.read("test_exotic.gpkg")
109+
@test AG.getx.(tt.geom, 0) == AG.getx.(t.geom, 0)
110+
@test tt.flag == t.flag
111+
@test tt.ex1 == t.ex1
112+
@test tt.ex2 == t.ex2
113+
@test tt.ex3 == t.ex3
114+
@test tt.check == t.check
115+
@test tt.z == t.z
116+
@test tt.y == t.y
117+
@test ismissing.(tt.odd) == ismissing.(t.odd)
118+
@test tt.date == t.date
108119
end
109120

110121
@testset "Read shapefile with non-GDAL types" begin
111-
t = GDF.read("test_exotic.shp")
122+
GDF.read("test_exotic.shp")
112123
GDF.read("test_exotic.gpkg")
113124
GDF.read("test_exotic.geojson")
114125
end
115126

116127
@testset "Spatial operations" begin
117-
table = DataFrame(geom = AG.createpoint.(coords), name = "test")
128+
table = DataFrame(geom=AG.createpoint.(coords), name="test")
118129

119130
# Buffer to also write polygons
120131
table.geom = AG.buffer(table.geom, 10)
@@ -124,9 +135,9 @@ end
124135
end
125136

126137
@testset "Reproject" begin
127-
table = DataFrame(geom = AG.createpoint.([[0, 0, 0]]), name = "test")
138+
table = DataFrame(geom=AG.createpoint.([[0, 0, 0]]), name="test")
128139
AG.reproject(table.geom, GFT.EPSG(4326), GFT.EPSG(28992))
129140
@test GDF.AG.getpoint(table.geom[1], 0)[1] -587791.596556932
130-
GDF.write("test_reprojection.gpkg", table, crs = GFT.EPSG(28992))
141+
GDF.write("test_reprojection.gpkg", table, crs=GFT.EPSG(28992))
131142
end
132143
end

0 commit comments

Comments
 (0)