Skip to content

Commit

Permalink
Add ncoord for WrapperGeometry (#124)
Browse files Browse the repository at this point in the history
This was omitted earlier.

* Add `ncoord` for arbitrary `WrapperGeometry`

This does actually compile down, see:
```julia
julia> x = GI.Point(1, 2, 3, 4)
GeoInterface.Wrappers.Point{true, true, NTuple{4, Int64}, Nothing}((1, 2, 3, 4), nothing)

julia> @code_typed GI.ncoord(x)
CodeInfo(
1 ─     return 4
) => Int64
```

* Add tests
  • Loading branch information
asinghvi17 authored Apr 11, 2024
1 parent 7adf735 commit d42b960
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ abstract type WrapperGeometry{Z,M,T,C} end
isgeometry(::Type{<:WrapperGeometry}) = true
is3d(::WrapperGeometry{Z}) where Z = Z
ismeasured(::WrapperGeometry{<:Any,M}) where M = M
ncoord(::WrapperGeometry{Z, M}) where {Z, M} = 2 + Z + M

Base.parent(geom::WrapperGeometry) = geom.geom

Expand Down
19 changes: 19 additions & 0 deletions test/test_wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ point = GI.Point(1, 2)
GI.getcoord(point, 1)
@test !GI.ismeasured(point)
@test !GI.is3d(point)
@test GI.ncoord(point) == 2
@test GI.extent(point) == Extent(X=(1, 1), Y=(2, 2))
@test point == GI.Point(point)
@test (GI.x(point), GI.y(point)) == (1, 2)
Expand All @@ -23,6 +24,7 @@ point_crs = GI.Point(point; crs=EPSG(4326))
pointz = GI.Point(1, 2, 3)
@test !GI.ismeasured(pointz)
@test GI.is3d(pointz)
@test GI.ncoord(pointz) == 3
@test (GI.x(pointz), GI.y(pointz), GI.z(pointz)) == (1, 2, 3)
@test GI.testgeometry(pointz)
@test GI.convert(GI, pointz) === pointz
Expand All @@ -34,6 +36,7 @@ pointzm = GI.Point(; X=1, Y=2, Z=3, M=4)
@test pointzm != GI.Point(1, 2, 3)
@test GI.ismeasured(pointzm)
@test GI.is3d(pointzm)
@test GI.ncoord(pointzm) == 4
@test pointzm == GI.Point(pointzm)
@test point != GI.Point(pointzm)
@test (GI.x(pointzm), GI.y(pointzm), GI.z(pointzm), GI.m(pointzm)) == (1, 2, 3, 4)
Expand All @@ -48,6 +51,7 @@ pointm = GI.Point((X=1, Y=2, M=3))
@test_throws MethodError GI.Point(; X=1, Y=2, T=3)
@test GI.ismeasured(pointm)
@test !GI.is3d(pointm)
@test GI.ncoord(pointm) == 3
@test pointm == GI.Point(pointm)
@test point != GI.Point(pointm)
@test (GI.x(pointm), GI.y(pointm), GI.m(pointm)) == (1, 2, 3)
Expand All @@ -62,6 +66,7 @@ pointtm = GI.Point{false,true}(1, 2, 3)
@test_throws ArgumentError GI.Point{false,true}(1, 2, 3, 4)
@test GI.ismeasured(pointtm)
@test !GI.is3d(pointtm)
@test GI.ncoord(pointtm) == 3
@test (GI.x(pointtm), GI.y(pointtm), GI.m(pointtm)) == (1, 2, 3)
@test_throws ArgumentError GI.z(pointtm)
@test GI.testgeometry(pointtm)
Expand All @@ -73,25 +78,29 @@ pointtm_crs = GI.Point{false,true}(1, 2, 3; crs=EPSG(4326))
pointa = GI.Point([1, 2])
@test !GI.ismeasured(pointa)
@test !GI.is3d(pointa)
@test GI.ncoord(pointa) == 2
@test (GI.x(pointa), GI.y(pointa)) == (1, 2)
@test GI.testgeometry(pointa)

pointaz = GI.Point([1, 2, 3])
@test !GI.ismeasured(pointaz)
@test GI.is3d(pointaz)
@test GI.ncoord(pointaz) == 3
@test (GI.x(pointaz), GI.y(pointaz), GI.z(pointaz)) == (1, 2, 3)
@test GI.testgeometry(pointaz)

pointazm = GI.Point([1, 2, 3, 4])
@test GI.ismeasured(pointazm)
@test GI.is3d(pointazm)
@test GI.ncoord(pointazm) == 4
@test (GI.x(pointazm), GI.y(pointazm), GI.z(pointazm), GI.m(pointazm)) == (1, 2, 3, 4)
@test GI.testgeometry(pointazm)

# We can force a vector point to be measured
pointam = GI.Point{false,true}([1, 2, 3])
@test GI.ismeasured(pointam)
@test !GI.is3d(pointam)
@test GI.ncoord(pointam) == 3
@test (GI.x(pointam), GI.y(pointam), GI.m(pointam)) == (1, 2, 3)
@test_throws ArgumentError GI.z(pointam)
@test GI.testgeometry(pointam)
Expand All @@ -104,6 +113,7 @@ line = GI.Line([(1, 2), (3, 4)])
@test GI.getgeom(line) == [(1, 2), (3, 4)]
@test GI.testgeometry(line)
@test !GI.is3d(line)
@test GI.ncoord(line) == 2
@test GI.extent(line) == Extent(X=(1, 3), Y=(2, 4))
@test_throws ArgumentError GI.Line(point)
@test_throws ArgumentError GI.Line([(1, 2)])
Expand All @@ -119,6 +129,7 @@ linestring = GI.LineString([(1, 2), (3, 4)])
@test GI.getgeom(linestring) == [(1, 2), (3, 4)]
@test GI.testgeometry(linestring)
@test !GI.is3d(linestring)
@test GI.ncoord(linestring) == 2
@test @inferred(GI.extent(linestring)) == Extent(X=(1, 3), Y=(2, 4))
@test_throws ArgumentError GI.LineString([(1, 2)])
linestring_crs = GI.LineString(linestring; crs=EPSG(4326))
Expand All @@ -132,6 +143,7 @@ linearring = GI.LinearRing([(1, 2), (3, 4), (5, 6), (1, 2)])
@test GI.getgeom(linearring) == [(1, 2), (3, 4), (5, 6), (1, 2)]
@test GI.testgeometry(linearring)
@test !GI.is3d(linearring)
@test GI.ncoord(linearring) == 2
@test @inferred(GI.extent(linearring)) == Extent(X=(1, 5), Y=(2, 6))
@test_throws ArgumentError GI.LinearRing([(1, 2)])
linearring_crs = GI.LinearRing(linearring; crs=EPSG(4326))
Expand All @@ -146,6 +158,7 @@ polygon = GI.Polygon([linearring, linearring])
@test collect(GI.getpoint(polygon)) == vcat(collect(GI.getpoint(linearring)), collect(GI.getpoint(linearring)))
@test GI.testgeometry(polygon)
@test !GI.is3d(polygon)
@test GI.ncoord(polygon) == 2
@test @inferred(GI.extent(polygon)) == Extent(X=(1, 5), Y=(2, 6))
@test GI.convert(GI, MyPolygon()) isa GI.Polygon
@test GI.convert(GI, polygon) === polygon
Expand All @@ -160,13 +173,15 @@ polygon = GI.Polygon([linestring, linestring])
linearring3d = GI.LinearRing([(1, 2, 3), (3, 4, 5), (5, 6, 7), (1, 2, 3)])
polygon3d = GI.Polygon([linearring3d, linearring3d])
@test GI.is3d(polygon3d)
@test GI.ncoord(polygon3d) == 3
@test GI.extent(polygon3d) == Extents.Extent(X=(1, 5), Y=(2, 6), Z=(3, 7))

# MultiPoint
multipoint = GI.MultiPoint([(1, 2), (3, 4), (3, 2), (1, 4), (7, 8), (9, 10)])
@test multipoint == GI.MultiPoint(multipoint)
@test GI.getgeom(multipoint, 1) === (1, 2)
@test !GI.is3d(multipoint)
@test GI.ncoord(multipoint) == 2
@test @inferred(GI.extent(multipoint)) == Extent(X=(1, 9), Y=(2, 10))
@test_throws ArgumentError GI.MultiPoint([[(1, 2), (3, 4), (3, 2), (1, 4), (7, 8), (9, 10)]])
@test GI.testgeometry(multipoint)
Expand All @@ -181,6 +196,7 @@ collection = GI.GeometryCollection(geoms)
@test GI.getgeom(collection) == geoms
@test GI.testgeometry(collection)
@test !GI.is3d(collection)
@test GI.ncoord(collection) == 2
@test GI.extent(collection) == reduce(Extents.union, map(GI.extent, geoms))
collection_crs = GI.GeometryCollection(collection; crs=EPSG(4326))
@test parent(collection_crs) == parent(collection)
Expand All @@ -192,6 +208,7 @@ multicurve = GI.MultiCurve([linestring, linearring])
@test multicurve == GI.MultiCurve(multicurve)
@test GI.getgeom(multicurve, 1) === linestring
@test !GI.is3d(multicurve)
@test GI.ncoord(multicurve) == 2
@test GI.extent(multicurve) == Extent(X=(1, 5), Y=(2, 6))
@test_throws ArgumentError GI.MultiCurve([pointz, polygon])
@test GI.testgeometry(multicurve)
Expand All @@ -206,6 +223,7 @@ multipolygon = GI.MultiPolygon([polygon])
@test multipolygon == GI.MultiPolygon(multipolygon)
@test GI.getgeom(multipolygon, 1) === polygon
@test !GI.is3d(multipolygon)
@test GI.ncoord(multipolygon) == 2
@show polygon
@show GI.getgeom(polygon, 1)
# MultiPolygon extent does not infer, maybe due to nesting
Expand All @@ -221,6 +239,7 @@ multipolygon_crs = GI.MultiPolygon(multipolygon; crs=EPSG(4326))
polyhedralsurface = GI.PolyhedralSurface([polygon, polygon])
@test polyhedralsurface == GI.PolyhedralSurface(polyhedralsurface)
@test !GI.is3d(polyhedralsurface)
@test GI.ncoord(polyhedralsurface) == 2
@test @inferred(GI.extent(polyhedralsurface)) == Extent(X=(1, 5), Y=(2, 6))
@test GI.getgeom(polyhedralsurface, 1) === polygon
@test collect(GI.getgeom(polyhedralsurface)) == [polygon, polygon]
Expand Down

0 comments on commit d42b960

Please sign in to comment.