Skip to content

Commit 02e6d88

Browse files
ErickChaconjuliohm
andauthored
write to shapefile (#15)
* write to shapefile * remove initial conversion to MeshData * Update src/GeoTables.jl * add information about keyword arguments * better description of examples * docstring for keyargs * Update README.md Co-authored-by: Júlio Hoffimann <julio.hoffimann@gmail.com> * remove temporal fix of Shapefile.jl * tests for writing * use datadir for writing --------- Co-authored-by: Júlio Hoffimann <julio.hoffimann@gmail.com>
1 parent c8114fb commit 02e6d88

File tree

4 files changed

+97
-20
lines changed

4 files changed

+97
-20
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ table = GeoTables.load("file.shp")
2727
GeoTables.save("file.geojson", table)
2828
```
2929

30+
Additional keyword arguments can be passed to `load` and `save` functions. Valid
31+
arguments are those accepted by `GeoJSON.read`, `GeoJSON.write`, `Shapefile.Table`,
32+
`Shapefile.write` and `ArchGDAL.read`. See below some examples:
33+
34+
```julia
35+
# read `.geojson` geometries with Float64 precision
36+
table = GeoTables.load("file.geojson", numbertype = Float64)
37+
38+
# force writing on existing `.shp` file
39+
GeoTables.save("file.shp", table, force = true)
40+
```
41+
3042
### Loading data from GADM
3143

3244
The `gadm` function (down)loads data from the GADM dataset:

src/GeoTables.jl

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,53 @@ include("conversion.jl")
1919
include("geotable.jl")
2020

2121
"""
22-
load(fname, layer=0)
22+
load(fname, layer=0, kwargs...)
2323
2424
Load geospatial table from file `fname` and convert the
2525
`geometry` column to Meshes.jl geometries. Optionally,
26-
specify the layer of geometries to read within the file.
26+
specify the layer of geometries to read within the file and
27+
keyword arguments accepted by `Shapefile.Table`, `GeoJSON.read`
28+
and `ArchGDAL.read`. For example, use `numbertype = Float64` to
29+
read `.geojson` geometries with Float64 precision.
2730
2831
## Supported formats
2932
3033
- `*.shp` via Shapefile.jl
3134
- `*.geojson` via GeoJSON.jl
3235
- Other formats via ArchGDAL.jl
3336
"""
34-
function load(fname, layer=0)
37+
function load(fname; layer=0, kwargs...)
3538
if endswith(fname, ".shp")
36-
table = SHP.Table(fname)
39+
table = SHP.Table(fname; kwargs...)
3740
elseif endswith(fname, ".geojson")
3841
data = Base.read(fname)
39-
table = GJS.read(data)
42+
table = GJS.read(data; kwargs...)
4043
else # fallback to GDAL
41-
data = AG.read(fname)
44+
data = AG.read(fname; kwargs...)
4245
table = AG.getlayer(data, layer)
4346
end
4447
GeoTable(table)
4548
end
4649

4750
"""
48-
save(fname, geotable)
51+
save(fname, geotable; kwargs...)
4952
5053
Save geospatial table to file `fname` using the
5154
appropriate format based on the file extension.
55+
Optionally, specify keyword arguments accepted by
56+
`Shapefile.write` and `GeoJSON.write`. For example, use
57+
`force = true` to force writing on existing `.shp` file.
5258
5359
## Supported formats
5460
61+
- `*.shp` via Shapefile.jl
5562
- `*.geojson` via GeoJSON.jl
5663
"""
57-
function save(fname, geotable)
58-
if endswith(fname, ".geojson")
59-
GJS.write(fname, geotable)
64+
function save(fname, geotable; kwargs...)
65+
if endswith(fname, ".shp")
66+
SHP.write(fname, geotable; kwargs...)
67+
elseif endswith(fname, ".geojson")
68+
GJS.write(fname, geotable; kwargs...)
6069
else
6170
throw(ErrorException("file format not supported"))
6271
end

src/conversion.jl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,19 @@ GI.ncoord(::GI.PointTrait, p::Point) = embeddim(p)
2020
GI.getcoord(::GI.PointTrait, p::Point) = coordinates(p)
2121
GI.getcoord(::GI.PointTrait, p::Point, i) = coordinates(p)[i]
2222

23-
GI.ngeom(::Any, s::Segment) = nvertices(s)
24-
GI.getgeom(::Any, s::Segment, i) = vertices(s)[i]
23+
GI.ncoord(::GI.LineTrait, s::Segment) = embeddim(s)
24+
GI.ngeom(::GI.LineTrait, s::Segment) = nvertices(s)
25+
GI.getgeom(::GI.LineTrait, s::Segment, i) = vertices(s)[i]
2526

26-
GI.ngeom(::Any, c::Chain) = nvertices(c)
27-
GI.getgeom(::Any, c::Chain, i) = vertices(c)[i]
27+
GI.ncoord(::GI.LineStringTrait, c::Chain) = embeddim(c)
28+
GI.ngeom(::GI.LineStringTrait, c::Chain) = nvertices(c)
29+
GI.getgeom(::GI.LineStringTrait, c::Chain, i) = vertices(c)[i]
2830

29-
GI.ngeom(::Any, p::Polygon) = length(chains(p))
30-
GI.getgeom(::Any, p::Polygon, i) = chains(p)[i]
31+
GI.ncoord(::GI.PolygonTrait, p::Polygon) = embeddim(p)
32+
GI.ngeom(::GI.PolygonTrait, p::Polygon) = length(chains(p))
33+
GI.getgeom(::GI.PolygonTrait, p::Polygon, i) = chains(p)[i]
3134

35+
GI.ncoord(::Any, m::Multi) = embeddim(m)
3236
GI.ngeom(::Any, m::Multi) = length(collect(m))
3337
GI.getgeom(::Any, m::Multi, i) = collect(m)[i]
3438

test/runtests.jl

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,62 @@ datadir = joinpath(@__DIR__,"data")
201201
end
202202

203203
@testset "save" begin
204-
table = GeoTables.load(joinpath(datadir,"path.shp"))
205-
GeoTables.save(joinpath(datadir,"path.geojson"), table)
206-
table = GeoTables.load(joinpath(datadir,"zone.shp"))
207-
GeoTables.save(joinpath(datadir,"path.geojson"), table)
204+
205+
@testset "points" begin
206+
table = GeoTables.load(joinpath(datadir, "points.geojson"), )
207+
GeoTables.save(joinpath(datadir, "tpoints.geojson"), table)
208+
GeoTables.save(joinpath(datadir, "tpoints.shp"), table, force = true)
209+
210+
table = GeoTables.load(joinpath(datadir, "points.gpkg"))
211+
GeoTables.save(joinpath(datadir, "tpoints.geojson"), table)
212+
GeoTables.save(joinpath(datadir, "tpoints.shp"), table, force = true)
213+
214+
table = GeoTables.load(joinpath(datadir, "points.shp"))
215+
GeoTables.save(joinpath(datadir, "tpoints.geojson"), table)
216+
GeoTables.save(joinpath(datadir, "tpoints.shp"), table, force = true)
217+
end
218+
219+
@testset "lines" begin
220+
table = GeoTables.load(joinpath(datadir, "lines.geojson"))
221+
GeoTables.save(joinpath(datadir, "tlines.geojson"), table)
222+
# GeoTables.save(joinpath(datadir, "tlines.shp"), table, force = true)
223+
224+
table = GeoTables.load(joinpath(datadir, "lines.gpkg"))
225+
GeoTables.save(joinpath(datadir, "tlines.geojson"), table)
226+
# GeoTables.save(joinpath(datadir, "tlines.shp"), table, force = true)
227+
228+
table = GeoTables.load(joinpath(datadir, "lines.shp"))
229+
GeoTables.save(joinpath(datadir, "tlines.geojson"), table)
230+
GeoTables.save(joinpath(datadir, "tlines.shp"), table, force = true)
231+
end
232+
233+
@testset "polygons" begin
234+
table = GeoTables.load(joinpath(datadir, "polygons.geojson"))
235+
GeoTables.save(joinpath(datadir, "tpolygons.geojson"), table)
236+
# GeoTables.save(joinpath(datadir, "tpolygons.shp"), table, force = true)
237+
238+
table = GeoTables.load(joinpath(datadir, "polygons.gpkg"))
239+
GeoTables.save(joinpath(datadir, "tpolygons.geojson"), table)
240+
# GeoTables.save(joinpath(datadir, "tpolygons.shp"), table, force = true)
241+
242+
table = GeoTables.load(joinpath(datadir, "polygons.shp"))
243+
GeoTables.save(joinpath(datadir, "tpolygons.geojson"), table)
244+
GeoTables.save(joinpath(datadir, "tpolygons.shp"), table, force = true)
245+
end
246+
247+
@testset "multipolygons" begin
248+
table = GeoTables.load(joinpath(datadir,"path.shp"))
249+
GeoTables.save(joinpath(datadir, "tpath.geojson"), table)
250+
GeoTables.save(joinpath(datadir, "tpath.shp"), table, force = true)
251+
252+
table = GeoTables.load(joinpath(datadir,"zone.shp"))
253+
GeoTables.save(joinpath(datadir, "tzone.geojson"), table)
254+
GeoTables.save(joinpath(datadir, "tzone.shp"), table, force = true)
255+
256+
table = GeoTables.load(joinpath(datadir,"ne_110m_land.shp"))
257+
GeoTables.save(joinpath(datadir, "tne_110m_land.geojson"), table)
258+
GeoTables.save(joinpath(datadir, "tne_110m_land.shp"), table, force = true)
259+
end
208260
end
209261

210262
@testset "gadm" begin

0 commit comments

Comments
 (0)