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 wrapper types for all geometries #78

Merged
merged 26 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ julia = "1"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GeoFormatTypes = "68eda718-8dee-11e9-39e7-89f7f65f511f"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test", "Documenter"]
test = ["Test", "Documenter", "GeoFormatTypes"]
47 changes: 47 additions & 0 deletions docs/src/guides/defaults.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,50 @@ GeoInterface.y(b) == 2
c = (;X=1, Y=2, Z=3)
GeoInterface.z(c) == 3
```

# Wrapper types

It is common that a package does not implement all objects supported by
GeoInterface.jl, and may lack the ability to define features. It is useful
to define generic objects that can be used in testing and for scripting where
geometries need to be constructed from components. Using generic wrappers
means this is backend agnostic: the same code will work if geometries come from
GeoJSON.jl, Shapefile.jl, LibGEOS.jl, or other packages defining GeoInterface.jl
traits.

Wrapper types are provided for constructing geometries out of any lower-level
components that implement GeoInterface.jl traits. These wrappers can wrap
objects of the same trait (possibly to add extent data), vectors of child
objects, or nested vectors of lower level children, such as points.

As an example, we can construct a polygon from any GeoInterface.jl compatible
geometries that return `LinearRingTrait` from `GeoInterface.geomtrait`:

```julia
poly = Polygon([interior, hole1, hole2])
```

See the API documentation for each wrapper for more details.

- [`Point`](@ref)
- [`Line`](@ref)
- [`LineString`](@ref)
- [`LinearRing`](@ref)
- [`Polygon`](@ref)
- [`MultiLineString`](@ref)
- [`MultiPolygon`](@ref)
- [`MultiPoint`](@ref)
- [`PolyhedralSurface`](@ref)
- [`GeometryCollection`](@ref)

Wrappers for Triangle, Hexagon and some other geometries are yet to be implemented.
Please make a GitHub issue if you need them.

Feature and FeatureCollection wrappers are also provided, to add properties,
crs and extents to any GeoInterface.jl compatible geometries.

- [`Feature`](@ref)
- [`FeatureCollection`](@ref)

Wrappers are not exported by default because they are very common names used by
other packages. To use them directly, run `using GeoInterface.Wrappers`.
4 changes: 4 additions & 0 deletions src/GeoInterface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,9 @@ include("interface.jl")
include("fallbacks.jl")
include("utils.jl")
include("base.jl")
include("wrappers.jl")

using .Wrappers
using .Wrappers: geointerface_geomtype

end # module
3 changes: 2 additions & 1 deletion src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ getpoint(t::AbstractMultiPolygonTrait, geom) = flatten((p for p in getpoint(r))
npatch(t::AbstractPolyhedralSurfaceTrait, geom)::Integer = ngeom(t, geom)
getpatch(t::AbstractPolyhedralSurfaceTrait, geom) = getgeom(t, geom)
getpatch(t::AbstractPolyhedralSurfaceTrait, geom, i::Integer) = getgeom(t, geom, i)
getpoint(t::AbstractPolyhedralSurfaceTrait, geom) = flatten((p for p in getpoint(g)) for g in getgeom(t, geom))

## Default iterator
getgeom(t::AbstractGeometryTrait, geom) = (getgeom(t, geom, i) for i in 1:ngeom(t, geom))
Expand Down Expand Up @@ -94,7 +95,7 @@ issimple(t::AbstractMultiPointTrait, geom) = allunique((getgeom(t, geom)))
issimple(t::AbstractMultiCurveTrait, geom) = all(issimple.(getgeom(t, geom)))
isclosed(t::AbstractMultiCurveTrait, geom) = all(isclosed.(getgeom(t, geom)))

crs(::AbstractGeometryTrait, geom) = nothing
crs(::AbstractTrait, geom) = nothing

# FeatureCollection
getfeature(t::AbstractFeatureCollectionTrait, fc) = (getfeature(t, fc, i) for i in 1:nfeature(t, fc))
Expand Down
2 changes: 1 addition & 1 deletion src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ getpolygon(geom) = getpolygon(geomtrait(geom), geom)
Retrieve Coordinate Reference System for given geom.
In SF this is defined as `SRID`.
"""
crs(geom) = crs(geomtrait(geom), geom)
crs(geom) = crs(trait(geom), geom)

"""
extent(obj; fallback=true) -> T <: Extents.Extent
Expand Down
Loading