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 converting to WKT to EPSG #391

Merged
merged 11 commits into from
Nov 28, 2023
3 changes: 3 additions & 0 deletions src/convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ end
function unsafe_convertcrs(::Type{<:GFT.ProjString}, crsref)
return GFT.ProjString(toPROJ4(crsref))
end
function unsafe_convertcrs(::Type{<:GFT.EPSG}, crsref)
alex-s-gardner marked this conversation as resolved.
Show resolved Hide resolved
return GFT.EPSG(toEPSG(crsref))
end
function unsafe_convertcrs(::Type{<:GFT.WellKnownText}, crsref)
return GFT.WellKnownText(GFT.CRS(), toWKT(crsref))
end
Expand Down
27 changes: 27 additions & 0 deletions src/spatialref.jl
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ Convert this SRS into a nicely formatted WKT string for display to a person.
function toWKT(spref::AbstractSpatialRef, simplify::Bool)::String
wktptr = Ref{Cstring}()
result = GDAL.osrexporttoprettywkt(spref, wktptr, simplify)

@ogrerr result "Failed to convert this SRS into pretty WKT"
return unsafe_string(wktptr[])
end
Expand All @@ -616,6 +617,32 @@ function toPROJ4(spref::AbstractSpatialRef)::String
return unsafe_string(projptr[])
end

"""
toEPSG(spref::AbstractSpatialRef)

Export EPSG code for this coordinate system if available.
"""
function toEPSG(spref::AbstractSpatialRef)::Int64
result = GDAL.osrgetauthorityname(spref.ptr, "PROJCS")

if !isnothing(result)
projcs = "PROJCS"
else
result = GDAL.osrgetauthorityname(spref.ptr, "GEOGCS")
projcs = "GEOGCS"
end

if isnothing(result)
error("No PROJCS or GEOGCS Authority found")
yeesian marked this conversation as resolved.
Show resolved Hide resolved
elseif result == "EPSG"
epsg = GDAL.osrgetauthoritycode(spref.ptr, projcs)
epsg = parse(Int64, epsg)
return epsg
else
error("$result is not an EPSG authority")
end
end

"""
toXML(spref::AbstractSpatialRef)

Expand Down
8 changes: 8 additions & 0 deletions test/test_convert.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ import GeoFormatTypes as GFT
) == proj4326
@test convert(GFT.CoordSys, GFT.CRS(), proj4326) isa GFT.CoordSys
@test convert(GFT.GML, GFT.CRS(), proj4326) isa GFT.GML

epsg = GFT.EPSG(4326)
wkt = convert(GFT.WellKnownText, epsg)
@test convert(GFT.EPSG, wkt) == epsg

epsg = GFT.EPSG(3013)
wkt = convert(GFT.WellKnownText, epsg)
@test convert(GFT.EPSG, wkt) == epsg
end

@testset "geometry conversions" begin
Expand Down