Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for saving CRS in Shapefile #123

Merged
merged 4 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ArchGDAL = "0.10"
CSV = "0.10"
Colors = "0.12"
CommonDataModel = "0.2, 0.3"
CoordRefSystems = "0.12 - 0.14"
CoordRefSystems = "0.15"
FileIO = "1.16"
Format = "1.3"
GRIBDatasets = "0.3"
Expand Down
1 change: 1 addition & 0 deletions src/GeoIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import ArchGDAL as AG
import GeoParquet as GPQ
import GeoInterface as GI
import GeoFormatTypes as GFT
import ArchGDAL.GDAL

# image extensions
const IMGEXTS = [".png", ".jpg", ".jpeg"]
Expand Down
11 changes: 11 additions & 0 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
GI.geomtrait(::MultiRing) = GI.MultiLineStringTrait()
GI.geomtrait(::MultiPolygon) = GI.MultiPolygonTrait()

GI.crs(geom::Geometry) = gicrs(crs(geom))

Check warning on line 20 in src/conversion.jl

View check run for this annotation

Codecov / codecov/patch

src/conversion.jl#L20

Added line #L20 was not covered by tests

GI.ncoord(::GI.PointTrait, p::Point) = CoordRefSystems.ncoords(crs(p))
GI.getcoord(::GI.PointTrait, p::Point) = ustrip.(raw(coords(p)))
GI.getcoord(trait::GI.PointTrait, p::Point, i) = GI.getcoord(trait, p)[i]
Expand All @@ -42,9 +44,18 @@

GI.isfeaturecollection(::Type{<:AbstractGeoTable}) = true
GI.trait(::AbstractGeoTable) = GI.FeatureCollectionTrait()
GI.crs(gtb::AbstractGeoTable) = gicrs(crs(domain(gtb)))
GI.nfeature(::Any, gtb::AbstractGeoTable) = nrow(gtb)
GI.getfeature(::Any, gtb::AbstractGeoTable, i) = gtb[i, :]

function gicrs(CRS)
try
GFT.WellKnownText2(GFT.CRS(), wktstring(CoordRefSystems.code(CRS)))
catch
nothing
end
end

# --------------------------------------
# Convert geometries to Meshes.jl types
# --------------------------------------
Expand Down
13 changes: 13 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@
uniquename(names, name)
end
end

function wktstring(code; format="WKT2", multiline=false)
spref = spatialref(code)
wktptr = Ref{Cstring}()
options = ["FORMAT=$format", "MULTILINE=$(multiline ? "YES" : "NO")"]
GDAL.osrexporttowktex(spref, wktptr, options)
unsafe_string(wktptr[])
end

spatialref(code) = AG.importUserInput(codestring(code))

codestring(::Type{EPSG{Code}}) where {Code} = "EPSG:$Code"
codestring(::Type{ESRI{Code}}) where {Code} = "ESRI:$Code"

Check warning on line 62 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L62

Added line #L62 was not covered by tests
6 changes: 3 additions & 3 deletions test/gis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@
file = joinpath(savedir, "gis-points.shp")
GeoIO.save(file, gtpoint)
gtb = GeoIO.load(file)
@test_broken gtb.geometry == gtpoint.geometry
@test gtb.geometry == gtpoint.geometry
@test values(gtb) == values(gtpoint)

# note: Shapefile saves Chain as MultiChain
file = joinpath(savedir, "gis-rings.shp")
GeoIO.save(file, gtring)
gtb = GeoIO.load(file)
@test_broken _isequal(gtb.geometry, gtring.geometry)
@test _isequal(gtb.geometry, gtring.geometry)
@test values(gtb) == values(gtring)

# note: Shapefile saves PolyArea as MultiPolyArea
file = joinpath(savedir, "gis-polys.shp")
GeoIO.save(file, gtpoly)
gtb = GeoIO.load(file)
@test_broken _isequal(gtb.geometry, gtpoly.geometry)
@test _isequal(gtb.geometry, gtpoly.geometry)
@test values(gtb) == values(gtpoly)

# GeoJSON
Expand Down
6 changes: 3 additions & 3 deletions test/io/shapefile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
gtb1 = GeoIO.load(file1)
GeoIO.save(file2, gtb1)
gtb2 = GeoIO.load(file2)
@test_broken gtb1.geometry == gtb2.geometry
@test gtb1.geometry == gtb2.geometry
@test values(gtb1) == values(gtb2)
@test values(gtb1, 0) == values(gtb2, 0)

Expand All @@ -79,7 +79,7 @@
gtb1 = GeoIO.load(file1)
GeoIO.save(file2, gtb1)
gtb2 = GeoIO.load(file2)
@test_broken gtb1.geometry == gtb2.geometry
@test gtb1.geometry == gtb2.geometry
@test values(gtb1) == values(gtb2)
@test values(gtb1, 0) == values(gtb2, 0)

Expand All @@ -88,7 +88,7 @@
gtb1 = GeoIO.load(file1)
GeoIO.save(file2, gtb1)
gtb2 = GeoIO.load(file2)
@test_broken gtb1.geometry == gtb2.geometry
@test gtb1.geometry == gtb2.geometry
@test values(gtb1) == values(gtb2)
@test values(gtb1, 0) == values(gtb2, 0)
end
Expand Down