Skip to content

Commit

Permalink
Create the GeoIO package
Browse files Browse the repository at this point in the history
  • Loading branch information
eliascarv committed Aug 28, 2023
0 parents commit 4bcba34
Show file tree
Hide file tree
Showing 60 changed files with 3,927 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
indent = 2
margin = 120
always_for_in = true
always_use_return = false
whitespace_typedefs = false
whitespace_in_kwargs = false
whitespace_ops_in_indices = true
remove_extra_newlines = true
trailing_comma = false
normalize_line_endings = "unix"
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# LaTeX Stuff
*.bib linguist-generated
*.tex linguist-generated
*.html linguist-generated
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "github-actions" # See documentation for possible values
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
49 changes: 49 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: CI
on:
pull_request:
branches:
- master
push:
branches:
- master
tags: '*'
env:
JULIA_NUM_THREADS: 2
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
version:
- '1.9'
- '1'
os:
- ubuntu-latest
- macos-latest
- windows-latest
arch:
- x64
steps:
- uses: actions/checkout@v3
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v3
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v3
with:
file: lcov.info
16 changes: 16 additions & 0 deletions .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CompatHelper
on:
schedule:
- cron: 0 0 * * *
workflow_dispatch:
jobs:
CompatHelper:
runs-on: ubuntu-latest
steps:
- name: Pkg.add("CompatHelper")
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
- name: CompatHelper.main()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
run: julia -e 'using CompatHelper; CompatHelper.main()'
28 changes: 28 additions & 0 deletions .github/workflows/FormatPR.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: FormatPR
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install JuliaFormatter and format
run: |
julia -e 'import Pkg; Pkg.add("JuliaFormatter")'
julia -e 'using JuliaFormatter; format(".")'
- name: Create Pull Request
id: cpr
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: ":robot: Format .jl files"
title: '[AUTO] JuliaFormatter.jl run'
branch: auto-juliaformatter-pr
delete-branch: true
labels: formatting, automated pr, no changelog
- name: Check outputs
run: |
echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}"
echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}"
20 changes: 20 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: TagBot
on:
issue_comment:
types:
- created
workflow_dispatch:
inputs:
lookback:
default: 3
permissions:
contents: write
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
ssh: ${{ secrets.DOCUMENTER_KEY }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Manifest.toml
test/data/path.geojson
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Júlio Hoffimann <[email protected]> and contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name = "GeoIO"
uuid = "f5a160d5-e41d-4189-8b61-d57781c419e3"
authors = ["Elias Carvalho <[email protected]> and contributors"]
version = "0.1.0"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
GADM = "a8dd9ffe-31dc-4cf5-a379-ea69100a8233"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
Meshes = "eacbb407-ea5a-433e-ab97-5258b1ca43fa"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

[compat]
ArchGDAL = "0.10"
GADM = "1.0"
GeoInterface = "1.0"
GeoJSON = "0.7"
Meshes = "0.33, 0.34"
PrecompileTools = "1.2"
Shapefile = "0.10"
Tables = "1.7"
julia = "1.9"
62 changes: 62 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# GeoTables.jl

[![][build-img]][build-url] [![][codecov-img]][codecov-url]

Load geospatial tables from known file formats and convert the
geometries to [Meshes.jl](https://github.com/JuliaGeometry/Meshes.jl)
geometries that are compatible with the
[GeoStats.jl](https://github.com/JuliaEarth/GeoStats.jl) ecosystem.

Geometries are loaded from disk in pure Julia whenever possible
using packages such as [Shapefile.jl](https://github.com/JuliaGeo/Shapefile.jl)
and [GeoJSON.jl](https://github.com/JuliaGeo/GeoJSON.jl), or
(down)loaded from the internet using the
[GADM.jl](https://github.com/JuliaGeo/GADM.jl) package.

## Usage

### Loading/saving data from/to disk

The `load` and `save` functions are self-explanatory:

```julia
using GeoTables

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)
```

Please read the docstrings for more details.

### Loading data from GADM

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

```julia
julia> GeoTables.gadm("BRA", depth = 1)
```

Please read the docstring for more details.

## Asking for help

If you have any questions, please [contact our community](https://juliaearth.github.io/GeoStats.jl/stable/about/community.html).

[build-img]: https://img.shields.io/github/actions/workflow/status/JuliaEarth/GeoTables.jl/CI.yml?branch=master&style=flat-square
[build-url]: https://github.com/JuliaEarth/GeoTables.jl/actions

[codecov-img]: https://img.shields.io/codecov/c/github/JuliaEarth/GeoTables.jl?style=flat-square
[codecov-url]: https://codecov.io/gh/JuliaEarth/GeoTables.jl
5 changes: 5 additions & 0 deletions src/GeoIO.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module GeoIO

greet() = print("Hello World!")

end # module GeoIO
105 changes: 105 additions & 0 deletions src/GeoTables.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# ------------------------------------------------------------------
# Licensed under the MIT License. See LICENSE in the project root.
# ------------------------------------------------------------------

module GeoTables

using Meshes

import Tables
import Meshes

import GADM
import Shapefile as SHP
import GeoJSON as GJS
import ArchGDAL as AG
import GeoInterface as GI

include("conversion.jl")
include("geotable.jl")
include("agwrite.jl")

"""
load(fname, layer=0, lazy=false, kwargs...)
Load geospatial table from file `fname` and convert the
`geometry` column to Meshes.jl geometries.
Optionally, specify the `layer` of geometries to read
within the file and keyword arguments `kwargs` accepted
by `Shapefile.Table`, `GeoJSON.read` `GeoParquet.read` and
`ArchGDAL.read`.
The option `lazy` can be used to convert geometries on
the fly instead of converting them immediately.
## Supported formats
- `*.shp` via Shapefile.jl
- `*.geojson` via GeoJSON.jl
- Other formats via ArchGDAL.jl
"""
function load(fname; layer=0, lazy=false, kwargs...)
if endswith(fname, ".shp")
table = SHP.Table(fname; kwargs...)
elseif endswith(fname, ".geojson")
data = Base.read(fname)
table = GJS.read(data; kwargs...)
else # fallback to GDAL
data = AG.read(fname; kwargs...)
table = AG.getlayer(data, layer)
end
gtable = GeoTable(table)
lazy ? gtable : Meshes.MeshData(gtable)
end

"""
save(fname, geotable; kwargs...)
Save geospatial table to file `fname` using the
appropriate format based on the file extension.
Optionally, specify keyword arguments accepted by
`Shapefile.write` and `GeoJSON.write`. For example, use
`force = true` to force writing on existing `.shp` file.
## Supported formats
- `*.shp` via Shapefile.jl
- `*.geojson` via GeoJSON.jl
- Other formats via ArchGDAL.jl
"""
function save(fname, geotable; kwargs...)
if endswith(fname, ".shp")
SHP.write(fname, geotable; kwargs...)
elseif endswith(fname, ".geojson")
GJS.write(fname, geotable; kwargs...)
else # fallback to GDAL
agwrite(fname, geotable; kwargs...)
end
end

"""
gadm(country, subregions...; depth=0, ϵ=nothing,
min=3, max=typemax(Int), maxiter=10)
(Down)load GADM table using `GADM.get` and convert
the `geometry` column to Meshes.jl geometries.
The `depth` option can be used to return tables for subregions
at a given depth starting from the given region specification.
The options `ϵ`, `min`, `max` and `maxiter` are forwarded to the
`decimate` function from Meshes.jl to reduce the number of vertices.
"""
function gadm(country, subregions...; depth=0, ϵ=nothing, min=3, max=typemax(Int), maxiter=10, kwargs...)
table = GADM.get(country, subregions...; depth=depth, kwargs...)
gtable = GeoTable(table)
𝒯 = values(gtable)
𝒟 = domain(gtable)
𝒩 = decimate(𝒟, ϵ, min=min, max=max, maxiter=maxiter)
meshdata(𝒩, etable=𝒯)
end

include("precompile.jl")

end
Loading

0 comments on commit 4bcba34

Please sign in to comment.