Skip to content

Commit

Permalink
Add support for raw images (#3)
Browse files Browse the repository at this point in the history
* Add support for raw images

* Update image extensions

* Update style and docstrings

* Update docstring
  • Loading branch information
juliohm authored Aug 30, 2023
1 parent 147f8eb commit 81b0b94
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ version = "1.0.0"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
GADM = "a8dd9ffe-31dc-4cf5-a379-ea69100a8233"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
GeoJSON = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
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"
FileIO = "1.16"
GADM = "1.0"
GeoInterface = "1.0"
GeoJSON = "0.7"
ImageIO = "0.6"
Meshes = "0.33, 0.34"
PrecompileTools = "1.2"
Shapefile = "0.10"
Expand Down
31 changes: 23 additions & 8 deletions src/GeoIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ using Meshes
using Tables

import GADM
import FileIO
import Shapefile as SHP
import GeoJSON as GJS
import ArchGDAL as AG
Expand All @@ -17,6 +18,8 @@ include("conversion.jl")
include("geotable.jl")
include("agwrite.jl")

const IMGEXT = (".png", ".jpg", ".jpeg", ".tif", ".tiff")

"""
load(fname, layer=0, lazy=false, kwargs...)
Expand All @@ -33,20 +36,32 @@ the fly instead of converting them immediately.
## Supported formats
- `*.shp` via Shapefile.jl
- `*.geojson` via GeoJSON.jl
- `.shp` via Shapefile.jl
- `.geojson` via GeoJSON.jl
- `.png`, `.jpg`, `.jpeg`, `.tif`, `.tiff` via ImageIO.jl
- Other formats via ArchGDAL.jl
"""
function load(fname; layer=0, lazy=false, kwargs...)
if endswith(fname, ".shp")
table = SHP.Table(fname; kwargs...)
# raw image formats
if any(ext -> endswith(fname, ext), IMGEXT)
data = FileIO.load(fname)
dims = size(data)
etable = (; color=vec(data))
domain = CartesianGrid(dims)
return meshdata(domain; etable)
end

# GIS file formats
table = if endswith(fname, ".shp")
SHP.Table(fname; kwargs...)
elseif endswith(fname, ".geojson")
data = Base.read(fname)
table = GJS.read(data; kwargs...)
GJS.read(data; kwargs...)
else # fallback to GDAL
data = AG.read(fname; kwargs...)
table = AG.getlayer(data, layer)
AG.getlayer(data, layer)
end

gtable = GeoTable(table)
lazy ? gtable : MeshData(gtable)
end
Expand All @@ -62,8 +77,8 @@ Optionally, specify keyword arguments accepted by
## Supported formats
- `*.shp` via Shapefile.jl
- `*.geojson` via GeoJSON.jl
- `.shp` via Shapefile.jl
- `.geojson` via GeoJSON.jl
- Other formats via ArchGDAL.jl
"""
function save(fname, geotable; kwargs...)
Expand Down
Binary file added test/data/image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ end
end

@testset "load" begin
@testset "Images" begin
table = GeoIO.load(joinpath(datadir, "image.jpg"))
@test table.geometry isa CartesianGrid
@test length(table.color) == length(table.geometry)
end

@testset "Shapefile" begin
table = GeoIO.load(joinpath(datadir, "points.shp"))
@test length(table.geometry) == 5
Expand Down

0 comments on commit 81b0b94

Please sign in to comment.