-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check the dimensions of child geoms when rebuilding + add forcexy, fo…
…rcexyz (#239)
- Loading branch information
1 parent
ae6c92c
commit f526f54
Showing
6 changed files
with
93 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,36 @@ | ||
#= | ||
# Force dimensions (xy, xyz) | ||
These functions force the geometry to be 2D or 3D. They work on any geometry, vector of geometries, feature collection, or table! | ||
They're implemented by `apply` pretty simply. | ||
=# | ||
|
||
export forcexy, forcexyz | ||
|
||
""" | ||
forcexy(geom) | ||
Force the geometry to be 2D. Works on any geometry, vector of geometries, feature collection, or table! | ||
""" | ||
function forcexy(geom) | ||
return apply(GI.PointTrait(), geom) do point | ||
(GI.x(point), GI.y(point)) | ||
end | ||
end | ||
|
||
""" | ||
forcexyz(geom, z = 0) | ||
Force the geometry to be 3D. Works on any geometry, vector of geometries, feature collection, or table! | ||
The `z` parameter is the default z value - if a point has no z value, it will be set to this value. | ||
If it does, then the z value will be kept. | ||
""" | ||
function forcexyz(geom, z = 0) | ||
return apply(GI.PointTrait(), geom) do point | ||
x, y = GI.x(point), GI.y(point) | ||
z = GI.is3d(geom) ? GI.z(point) : z | ||
(x, y, z) | ||
end | ||
end |
This file contains 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 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,35 @@ | ||
import GeometryOps as GO | ||
import GeoInterface as GI | ||
using ..TestHelpers | ||
|
||
using Test | ||
|
||
geom = GI.Polygon([GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)]), | ||
GI.LinearRing([(3, 4), (5, 6), (6, 7), (3, 4)])]) | ||
|
||
@testset_implementations "force dimensions" begin | ||
|
||
# Test forcexy on 3D geometry | ||
geom3d = GO.transform($geom) do p | ||
(GI.x(p), GI.y(p), 1.0) | ||
end | ||
@test GI.is3d(geom3d) | ||
geom2d = GO.forcexy(geom3d) | ||
@test !GI.is3d(geom2d) | ||
@test GO.equals(geom2d, geom) | ||
|
||
# Test forcexyz with default z | ||
geom3d_default = GO.forcexyz($geom) | ||
@test GI.is3d(geom3d_default) | ||
points3d = collect(GO.flatten(GI.PointTrait, geom3d_default)) | ||
@test all(p -> GI.z(p) == 0, points3d) | ||
|
||
# Test forcexyz with custom z | ||
geom3d_custom = GO.forcexyz($geom, 5.0) | ||
@test GI.is3d(geom3d_custom) | ||
points3d_custom = collect(GO.flatten(GI.PointTrait, geom3d_custom)) | ||
@test all(p -> GI.z(p) == 5.0, points3d_custom) | ||
|
||
# Test forcexyz preserves existing z values | ||
@test GO.equals(GO.forcexyz(geom3d), geom3d) | ||
end |
This file contains 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