-
Couldn't load subscription status.
- Fork 10
add geointerface reprojection #79
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
abee4e3
add geointerface reprojection
rafaqz 6966a3f
add kwargs reproject
rafaqz cf5d164
bugfix
rafaqz 2aea0b2
better tests
rafaqz 0e5f267
Coord 2 and 3
rafaqz 1c8bf0f
dont change proj
rafaqz c65bdc9
use Point rather than Coord
rafaqz be9849b
refactor
rafaqz 9b4e3c8
test properly
rafaqz e1d8de9
allow passing in transform
rafaqz 020c54d
actually run the tests
rafaqz c1ed76c
fix docs
rafaqz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| using GeoInterface | ||
|
|
||
| const GI = GeoInterface | ||
|
|
||
| # Coord is a PointTrait | ||
| GI.isgeometry(::Type{Coord}) = true | ||
| GI.geomtrait(::Coord) = GI.PointTrait() | ||
| GI.x(::PointTrait, c::Coord) = c.x | ||
| GI.y(::PointTrait, c::Coord) = c.y | ||
| GI.z(::PointTrait, c::Coord) = c.z | ||
| GI.getcoord(::PointTrait, c::Coord) = (c.x, c.y, c.z) | ||
| GI.getcoord(::PointTrait, c::Coord, i::Int) = c[i] | ||
| coordnames(::PointTrait, ::Coord) = (:X, :Y, :Z) | ||
|
|
||
| """ | ||
| reproject(geometry, source_crs, target_crs) | ||
|
|
||
| Reproject any GeoInterface.jl compatible `geometry` from `source_crs` to `target_crs`. | ||
|
|
||
| The returned object will be constructed from `GeoInterface.WrapperGeometry` | ||
| geometries, wrapping views of `Proj.Coord`. | ||
| """ | ||
| function reproject(geom, source_crs, target_crs; time=Inf) | ||
| source_crs1 = convert(Proj.CRS, source_crs) | ||
| target_crs1 = convert(Proj.CRS, target_crs) | ||
| trans = Proj.Transformation(source_crs1, target_crs1, always_xy = true) | ||
| coords = if GI.is3d(geom) | ||
| [Proj.Coord(GI.x(p), GI.y(p), GI.z(p), time) for p in GI.getpoint(geom)] | ||
| else | ||
| [Proj.Coord(GI.x(p), GI.y(p), 0.0, time) for p in GI.getpoint(geom)] | ||
| end | ||
| err = Proj.proj_trans_array(trans.pj, Proj.PJ_FWD, length(coords), coords) | ||
| err == 0 || error("Proj error $err") | ||
|
|
||
| crs = target_crs isa GFT.GeoFormat ? target_crs : convert(WellKnownText, crs) | ||
| return reconstruct(geom, coords; crs) | ||
| end | ||
|
|
||
|
|
||
| """ | ||
| reconstruct(geometry, points::AbstractVector) | ||
|
|
||
| Reconstruct GeoInterface compatible `geometry` from the `PointTrait` geometries in `points`. | ||
|
|
||
| The returned object will be constructed from `GeoInterface.WrapperGeometry` | ||
| geometries, wrapping views into the `points` vector. Otherwise, the structure and | ||
| GeoInterface traits will be the same as for `geometry`. | ||
|
|
||
| This is a lazy operation, only allocating for outer wrappers of nested geometries. | ||
| Later changes to the `points` vector *will* affect the returned geometry. | ||
|
|
||
| `GeoInterface.npoint(geometry) == length(points)` must be `true`. | ||
| """ | ||
| function reconstruct(geom, points::AbstractVector; crs=nothing) | ||
| T = nonmissingtype(eltype(points)) | ||
| isgeometry(T) || throw(ArgumentError("points of type $(T) are not GeoInterface.jl compatible objects")) | ||
| trait = GI.trait(first(skipmissing(points))) | ||
| trait isa PointTrait || throw(ArgumentError("can only reconstruct from an array of points, got $trait")) | ||
| n, geom = _reconstruct(geom, points, 0, crs) | ||
| @assert n == GI.npoint(geom) | ||
| return geom | ||
| end | ||
|
|
||
| _reconstruct(geom, points, n::Int, crs) = _reconstruct(GI.trait(geom), geom, points, n::Int, crs) | ||
| # Nested geometries. We need to reconstruct their child geoms. | ||
| function _reconstruct(trait::AbstractGeometryTrait, geom, points, n, crs) | ||
| T = GI.geointerface_geomtype(trait) | ||
| geoms = map(GI.getgeom(geom)) do childgeom | ||
| n, reconstructed_childgeom = _reconstruct(GI.trait(childgeom), childgeom, points, n, crs) | ||
| reconstructed_childgeom | ||
| end | ||
| return n, T(geoms; crs) | ||
| end | ||
| # Bottom-level geometries that can wrap a vector of points. | ||
| function _reconstruct( | ||
| trait::Union{GI.LineTrait,GI.LineStringTrait,GI.LinearRingTrait,GI.MultiPointTrait}, | ||
| geom, points, n, crs | ||
| ) | ||
| T = GI.geointerface_geomtype(trait) | ||
| npoints = GI.npoint(geom) | ||
| v = view(points, n + 1:n + npoints) | ||
| geom = if GI.is3d(geom) | ||
| GI.ismeasured(geom) ? T{true,true}(v; crs) : T{true,false}(v; crs) | ||
| else | ||
| GI.ismeasured(geom) ? T{false,true}(v; crs) : T{false,false}(v; crs) | ||
| end | ||
| return n + npoints, geom | ||
| end | ||
| # Points | ||
| function _reconstruct(::GI.PointTrait, geom, points, n, crs) | ||
| n += 1 | ||
| point = if GI.is3d(geom) | ||
| GI.ismeasured(geom) ? GI.Point{true,true}(points[n]) : GI.Point{true,false}(points[n]) | ||
| else | ||
| GI.ismeasured(geom) ? GI.Point{false,true}(points[n]) : GI.Point{false,false}(points[n]) | ||
| end | ||
| return n, point | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Proj | ||
| using GeoInterface | ||
| using GeoFormatTypes | ||
| const GI = GeoInterface | ||
|
|
||
| linearring1 = GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]) | ||
| linearring2 = GI.LinearRing([(11, 2), (13, 4), (15, 6), (11, 2)]) | ||
|
|
||
| polygon1 = GI.Polygon([linearring1]) | ||
| polygon2 = GI.Polygon([linearring2]) | ||
| multipolygon = GI.MultiPolygon([polygon1, polygon2]) | ||
|
|
||
| Proj.reproject(multipolygon, EPSG(4326), EPSG(3857)) | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add this as a method stub to GeoInterface so that other packages (ArchGDAL?) can specialize on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that make sense though? It wouldn't really give us anything more than we have.
The point of this PR is that it converts any geointerface object using Proj. There is no way to dispatch on that, so a stub would necessitate type piracy. I already wrote a
ArchGDAL.reprojectmethod years ago. It will also handle any GeoInterface geometries when this is merged:yeesian/ArchGDAL.jl#366
But you have to use
ArchGDAL.reprojectfor that to work. If we use a stub method in GeoInterface.jl then the packages can only handle types they own, soArchGDAL.AbstractGeometryfor ArchGDAL.jl, and nothing for Proj.jl.I'm trying to shift everything to package methods because the methods in GeoInterface (like
areaetc) don't really make sense, there is no way to add useful methods to them without piracy or a backend system. Mostly I use Shapefile.jl and GeoJSON.jl directly, and need a way to work with those objects.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you're saying. Packages could implement functions which take arbitrary GeoInterface geometries and return their own internal structs then?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. That's what my PRs in LibGEOS, ArchGDAL and this one are doing.
I'm trying to make them at least be the same name and syntax in all packages.
For example
reprojectshould be the essentially the same in ArchGDAL, Rasters and Proj. (Rasters will need some changes)