Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ table = GeoTables.load("file.shp")
GeoTables.save("file.geojson", table)
```

Additional keyword arguments can be passed to `load` and `save` functions. Valid
arguments are those accepted by `GeoJSON.read`, `GeoJSON.write`, `Shapefile.Table`,
`Shapefile.write` and `ArchGDAL.read`. See below some examples:

```julia
# Read `.geojson` geometries with Float64 precision.
table = GeoTables.load("file.geojson", numbertype = Float64)

# Force writing on existing `.shp` file.
GeoTables.save("file.shp", table, force = true)
```

### Loading data from GADM

The `gadm` function (down)loads data from the GADM dataset:
Expand Down
21 changes: 14 additions & 7 deletions src/GeoTables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ specify the layer of geometries to read within the file.
- `*.geojson` via GeoJSON.jl
- Other formats via ArchGDAL.jl
"""
function load(fname, layer=0)
function load(fname; layer=0, kwargs...)
if endswith(fname, ".shp")
table = SHP.Table(fname)
table = SHP.Table(fname; kwargs...)
elseif endswith(fname, ".geojson")
data = Base.read(fname)
table = GJS.read(data)
table = GJS.read(data; kwargs...)
else # fallback to GDAL
data = AG.read(fname)
data = AG.read(fname; kwargs...)
table = AG.getlayer(data, layer)
end
GeoTable(table)
Expand All @@ -54,9 +54,16 @@ appropriate format based on the file extension.

- `*.geojson` via GeoJSON.jl
"""
function save(fname, geotable)
if endswith(fname, ".geojson")
GJS.write(fname, geotable)
function save(fname, geotable; kwargs...)
if endswith(fname, ".shp")
geoms = domain(geotable)
if !(geoms[1] isa Multi) && !(geoms[1] isa Point)
newgeoms = GeometrySet([Multi([geom]) for geom in geoms])
geotable = meshdata(newgeoms, etable = values(geotable))
end
SHP.write(fname, geotable; kwargs...)
elseif endswith(fname, ".geojson")
GJS.write(fname, geotable; kwargs...)
else
throw(ErrorException("file format not supported"))
end
Expand Down
16 changes: 10 additions & 6 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,19 @@ GI.ncoord(::GI.PointTrait, p::Point) = embeddim(p)
GI.getcoord(::GI.PointTrait, p::Point) = coordinates(p)
GI.getcoord(::GI.PointTrait, p::Point, i) = coordinates(p)[i]

GI.ngeom(::Any, s::Segment) = nvertices(s)
GI.getgeom(::Any, s::Segment, i) = vertices(s)[i]
GI.ncoord(::GI.LineTrait, s::Segment) = embeddim(s)
GI.ngeom(::GI.LineTrait, s::Segment) = nvertices(s)
GI.getgeom(::GI.LineTrait, s::Segment, i) = vertices(s)[i]

GI.ngeom(::Any, c::Chain) = nvertices(c)
GI.getgeom(::Any, c::Chain, i) = vertices(c)[i]
GI.ncoord(::GI.LineStringTrait, c::Chain) = embeddim(c)
GI.ngeom(::GI.LineStringTrait, c::Chain) = nvertices(c)
GI.getgeom(::GI.LineStringTrait, c::Chain, i) = vertices(c)[i]

GI.ngeom(::Any, p::Polygon) = length(chains(p))
GI.getgeom(::Any, p::Polygon, i) = chains(p)[i]
GI.ncoord(::GI.PolygonTrait, p::Polygon) = embeddim(p)
GI.ngeom(::GI.PolygonTrait, p::Polygon) = length(chains(p))
GI.getgeom(::GI.PolygonTrait, p::Polygon, i) = chains(p)[i]

GI.ncoord(::Any, m::Multi) = embeddim(m)
GI.ngeom(::Any, m::Multi) = length(collect(m))
GI.getgeom(::Any, m::Multi, i) = collect(m)[i]

Expand Down