Skip to content

Commit

Permalink
Implement GeoInterface.crs. (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
evetion authored Dec 21, 2023
1 parent 78ca7ea commit 696d94a
Show file tree
Hide file tree
Showing 7 changed files with 41 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.1"
version = "0.10.2"

[deps]
CEnum = "fa961155-64e5-5f13-b03f-caf6b980ea82"
Expand Down
4 changes: 4 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ function Base.convert(
return unsafe_convertcrs(target, importCRS(source))
end

function Base.convert(target::Type{<:GFT.GeoFormat}, source::AbstractSpatialRef)
return unsafe_convertcrs(target, source)
end

function unsafe_convertcrs(::Type{<:GFT.CoordSys}, crsref)
return GFT.CoordSys(toMICoordSys(crsref))
end
Expand Down
15 changes: 15 additions & 0 deletions src/geointerface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,23 @@ let pointtypes = (wkbPoint, wkbPoint25D, wkbPointM, wkbPointZM),
GeoInterface.TriangleTrait,
}

# Raster
function GeoInterface.crs(ds::AbstractDataset)
sr = getproj(ds)
return Base.isempty(sr) ? nothing : GFT.WellKnownText(GFT.CRS(), sr)
end

# Feature
GeoInterface.isfeature(feat::AbstractFeature) = true
function GeoInterface.crs(layer::AbstractFeatureLayer)
sr = getspatialref(layer)
return sr.ptr == C_NULL ? nothing : convert(GFT.WellKnownText, sr)
end
function GeoInterface.crs(geom::AbstractGeometry)
sr = getspatialref(geom)
return sr.ptr == C_NULL ? nothing : convert(GFT.WellKnownText, sr)
end

function GeoInterface.properties(feat::AbstractFeature)
return (; (zip(Symbol.(keys(feat)), values(feat)))...)
end
Expand Down
4 changes: 4 additions & 0 deletions test/test_cookbook_projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import GeoFormatTypes as GFT
AG.importEPSG(4326) do target
AG.createcoordtrans(source, target) do transform
AG.fromWKT("POINT (1120351.57 741921.42)") do point
@test GeoInterface.crs(point) === nothing
@test AG.toWKT(point) ==
"POINT (1120351.57 741921.42)"
AG.transform!(point, transform)
@test GeoInterface.coordinates(point)
[47.3488070138318, -122.5981499431438]
@test GeoInterface.crs(point) isa
GFT.WellKnownText{GFT.CRS}
end
end
end
Expand Down Expand Up @@ -116,6 +119,7 @@ import GeoFormatTypes as GFT
@test AG.toXML(spatialref)[1:17] == "<gml:ProjectedCRS"
@test AG.toMICoordSys(spatialref) ==
"Earth Projection 8, 104, \"m\", -111, 0, 0.9996, 500000, 0"
@test GeoInterface.crs(layer) isa GFT.WellKnownText{GFT.CRS}
AG.nextfeature(layer) do feature
@test AG.toPROJ4(AG.getspatialref(AG.getgeom(feature))) ==
"+proj=utm +zone=12 +datum=WGS84 +units=m +no_defs"
Expand Down
5 changes: 5 additions & 0 deletions test/test_rasterband.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Test
import GDAL
import ArchGDAL as AG
import GeoInterface
import GeoFormatTypes as GFT

@testset "test_rasterband.jl" begin
@testset "Test methods for rasterband" begin
Expand All @@ -15,6 +17,9 @@ import ArchGDAL as AG
[GA_ReadOnly] Band 1 (Gray): 100 x 100 (UInt8)
"""
@test sprint(print, dataset) == ds_result

@test GeoInterface.crs(dataset) isa GFT.WellKnownText{GFT.CRS}

rb = AG.getband(dataset, 1)
sprint(print, rb) == """
[GA_ReadOnly] Band 1 (Gray): 100 x 100 (UInt8)
Expand Down
1 change: 1 addition & 0 deletions test/test_rasterio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ end
nbands = 1,
dtype = ComplexF32,
) do ds
@test isnothing(GeoInterface.crs(ds))
return AG.write!(ds, a, 1)
end

Expand Down
11 changes: 11 additions & 0 deletions test/test_spatialref.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Test
import ArchGDAL as AG
import GeoFormatTypes as GFT
import GeoInterface

@testset "test_spatialref.jl" begin
@testset "Test Formats for Spatial Reference Systems" begin
Expand Down Expand Up @@ -185,6 +186,8 @@ import GeoFormatTypes as GFT
@test AG.toPROJ4(spatialref2) == proj26912
AG.importPROJ4!(spatialref2, AG.toPROJ4(spatialref))
@test AG.toPROJ4(spatialref2) == proj4326
@test convert(GFT.WellKnownText, spatialref) isa
GFT.WellKnownText{GFT.CRS}
end
end

Expand All @@ -194,6 +197,8 @@ import GeoFormatTypes as GFT
@test AG.toWKT(spatialref2) == wkt26912
AG.importWKT!(spatialref2, AG.toWKT(spatialref))
@test AG.toWKT(spatialref2) == wkt4326
@test convert(GFT.WellKnownText, spatialref) isa
GFT.WellKnownText{GFT.CRS}
end
end

Expand All @@ -203,6 +208,8 @@ import GeoFormatTypes as GFT
@test AG.toWKT(spatialref2) == esri26912
AG.importESRI!(spatialref2, AG.toWKT(spatialref))
@test AG.toWKT(spatialref2) == esri4326
@test convert(GFT.WellKnownText, spatialref) isa
GFT.WellKnownText{GFT.CRS}
end
end

Expand All @@ -212,6 +219,8 @@ import GeoFormatTypes as GFT
@test startswith(AG.toXML(spatialref2), "<gml:ProjectedCRS")
AG.importXML!(spatialref2, xml4326)
@test startswith(AG.toXML(spatialref2), "<gml:GeographicCRS")
@test convert(GFT.WellKnownText, spatialref) isa
GFT.WellKnownText{GFT.CRS}
end
end

Expand All @@ -221,6 +230,8 @@ import GeoFormatTypes as GFT
@test contains(AG.toPROJ4(spatialref2), "+geoidgrids=")
AG.importUserInput!(spatialref2, cepsg)
@test contains(AG.toPROJ4(spatialref2), "+geoidgrids=")
@test convert(GFT.WellKnownText, spatialref) isa
GFT.WellKnownText{GFT.CRS}
end
end

Expand Down

4 comments on commit 696d94a

@evetion
Copy link
Collaborator Author

@evetion evetion commented on 696d94a Dec 22, 2023

Choose a reason for hiding this comment

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

@yeesian Are you ok with making a release? Also let me know if I don't need to ask you ;)

@yeesian
Copy link
Owner

@yeesian yeesian commented on 696d94a Dec 22, 2023

Choose a reason for hiding this comment

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

Sure! And yeah @evetion and @visr and @rafaqz don't need to ask me at this point :)

@yeesian
Copy link
Owner

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/97636

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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.10.2 -m "<description of version>" 696d94a83ba199813626514afb3c759c68b427a3
git push origin v0.10.2

Please sign in to comment.