Skip to content

Commit

Permalink
Allow writing 3d geometry. (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion authored Oct 14, 2023
1 parent 42a14af commit 75760d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "GeoDataFrames"
uuid = "62cb38b5-d8d2-4862-a48e-6a340996859f"
authors = ["Maarten Pronk <[email protected]> and contributors"]
version = "0.3.6"
version = "0.3.7"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
32 changes: 22 additions & 10 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ function find_driver(fn::AbstractString)
AG.extensiondriver(fn)
end

const lookup_type = Dict{DataType,AG.OGRwkbGeometryType}(
AG.GeoInterface.PointTrait => AG.wkbPoint,
AG.GeoInterface.MultiPointTrait => AG.wkbMultiPoint,
AG.GeoInterface.LineStringTrait => AG.wkbLineString,
AG.GeoInterface.LinearRingTrait => AG.wkbMultiLineString,
AG.GeoInterface.MultiLineStringTrait => AG.wkbMultiLineString,
AG.GeoInterface.PolygonTrait => AG.wkbPolygon,
AG.GeoInterface.MultiPolygonTrait => AG.wkbMultiPolygon,
const lookup_type = Dict{Tuple{DataType,Int},AG.OGRwkbGeometryType}(
(AG.GeoInterface.PointTrait, 2) => AG.wkbPoint,
(AG.GeoInterface.PointTrait, 3) => AG.wkbPoint25D,
(AG.GeoInterface.PointTrait, 4) => AG.wkbPointZM,
(AG.GeoInterface.MultiPointTrait, 2) => AG.wkbMultiPoint,
(AG.GeoInterface.MultiPointTrait, 3) => AG.wkbMultiPoint25D,
(AG.GeoInterface.MultiPointTrait, 4) => AG.wkbMultiPointZM,
(AG.GeoInterface.LineStringTrait, 2) => AG.wkbLineString,
(AG.GeoInterface.LineStringTrait, 3) => AG.wkbLineString25D,
(AG.GeoInterface.LineStringTrait, 4) => AG.wkbLineStringZM,
(AG.GeoInterface.MultiLineStringTrait, 2) => AG.wkbMultiLineString,
(AG.GeoInterface.MultiLineStringTrait, 3) => AG.wkbMultiLineString25D,
(AG.GeoInterface.MultiLineStringTrait, 4) => AG.wkbMultiLineStringZM,
(AG.GeoInterface.PolygonTrait, 2) => AG.wkbPolygon,
(AG.GeoInterface.PolygonTrait, 3) => AG.wkbPolygon25D,
(AG.GeoInterface.PolygonTrait, 4) => AG.wkbPolygonZM,
(AG.GeoInterface.MultiPolygonTrait, 2) => AG.wkbMultiPolygon,
(AG.GeoInterface.MultiPolygonTrait, 3) => AG.wkbMultiPolygon25D,
(AG.GeoInterface.MultiPolygonTrait, 4) => AG.wkbMultiPolygonZM,
)


Expand Down Expand Up @@ -94,8 +105,9 @@ function write(fn::AbstractString, table; layer_name::AbstractString="data", crs
geom_types = []
for geom_column in geom_columns
trait = AG.GeoInterface.geomtrait(getproperty(first(rows), geom_column))
geom_type = get(lookup_type, typeof(trait), nothing)
isnothing(geom_type) && throw(ArgumentError("Can't convert $trait of column $geom_column to ArchGDAL yet."))
ndim = AG.GeoInterface.ncoord(getproperty(first(rows), geom_column))
geom_type = get(lookup_type, (typeof(trait), ndim), nothing)
isnothing(geom_type) && throw(ArgumentError("Can't convert $trait with $ndim dimensions of column $geom_column to ArchGDAL yet."))
push!(geom_types, geom_type)
end

Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ end
@testset "GeoDataFrames.jl" begin
fn = joinpath(testdatadir, "sites.shp")
coords = zip(rand(10), rand(10))
coords3 = zip(rand(10), rand(10), rand(10))

@testset "Read shapefile" begin
t = GDF.read(fn)
Expand Down Expand Up @@ -88,6 +89,11 @@ end
@test nrow(ntable) == 10
ntable = GDF.read(joinpath(testdatadir, "test_points.geojson"))
@test nrow(ntable) == 10

tablez = DataFrame(geometry=AG.createpoint.(coords3), name="test")
GDF.write(joinpath(testdatadir, "test_pointsz.gpkg"), tablez; layer_name="test_points")
ntable = GDF.read(joinpath(testdatadir, "test_pointsz.gpkg"))
@test GI.ncoord(ntable.geometry[1]) == 3
end

@testset "Write shapefile" begin
Expand Down

2 comments on commit 75760d3

@evetion
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/93392

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.7 -m "<description of version>" 75760d3557ce79640581a2c99795ed1a743baf5c
git push origin v0.3.7

Please sign in to comment.