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

use shx for indexing into file if available #52

Merged
merged 4 commits into from
Apr 23, 2021
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: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@ jobs:
- uses: julia-actions/julia-buildpkg@latest
- uses: julia-actions/julia-runtest@latest
- uses: julia-actions/julia-processcoverage@v1
- uses: codecov/codecov-action@v1
with:
file: lcov.info
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Shapefile"
uuid = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4"
license = "MIT"
version = "0.7.0"
version = "0.7.1"

[deps]
DBFTables = "75c7ada1-017a-5fb6-b8c7-2125ff2d6c93"
Expand Down
33 changes: 25 additions & 8 deletions src/Shapefile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,14 @@ mutable struct Handle{T<:Union{<:GeoInterface.AbstractGeometry,Missing}}
shapes::Vector{T}
end

function Handle(path::AbstractString)
function Handle(path::AbstractString, index=nothing)
open(path) do io
read(io, Handle)
read(io, Handle, index)
end
end



Base.length(shp::Handle) = length(shp.shapes)

function Base.read(io::IO, ::Type{Rect})
Expand Down Expand Up @@ -318,7 +320,7 @@ function Base.read(io::IO, ::Type{MultiPatch})
MultiPatch(box, parts, parttypes, points, zvalues) #,measures)
end

function Base.read(io::IO, ::Type{Handle})
function Base.read(io::IO, ::Type{Handle}, index = nothing)
code = bswap(read(io, Int32))
read!(io, Vector{Int32}(undef, 5))
fileSize = bswap(read(io, Int32))
Expand All @@ -341,7 +343,9 @@ function Base.read(io::IO, ::Type{Handle})
Interval(mmin, mmax),
shapes,
)
num = Int32(0)
while (!eof(io))
seeknext(io, num, index)
num = bswap(read(io, Int32))
rlength = bswap(read(io, Int32))
shapeType = read(io, Int32)
Expand All @@ -354,14 +358,27 @@ function Base.read(io::IO, ::Type{Handle})
file
end

include("shx.jl")
include("table.jl")
include("geo_interface.jl")
include("plotrecipes.jl")

seeknext(io, num, ::Nothing) = nothing

function seeknext(io, num, index::IndexHandle)
seek(io, index.indices[num+1].offset * 2)
end

function Handle(path::AbstractString, indexpath::AbstractString)
index = open(indexpath) do io
read(io, IndexHandle)
end
Handle(path, index)
end

function Base.:(==)(a::Rect, b::Rect)
a.left == b.left &&
a.bottom == b.bottom && a.right == b.right && a.top == b.top
end

include("table.jl")
include("geo_interface.jl")
include("shx.jl")
include("plotrecipes.jl")

end # module
8 changes: 7 additions & 1 deletion src/table.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ function Table(path::AbstractString)
stempath, ext = splitext(path)
if lowercase(ext) == ".shp"
shp_path = path
shx_path = string(stempath, ".shx")
dbf_path = string(stempath, ".dbf")
elseif ext == ""
shp_path = string(stempath, ".shp")
shx_path = string(stempath, ".shx")
dbf_path = string(stempath, ".dbf")
else
throw(ArgumentError("Provide the shapefile with either .shp or no extension"))
end
isfile(shp_path) || throw(ArgumentError("File not found: $shp_path"))
isfile(dbf_path) || throw(ArgumentError("File not found: $dbf_path"))

shp = Shapefile.Handle(shp_path)
shp = if isfile(shx_path)
Shapefile.Handle(shp_path,shx_path)
else
Shapefile.Handle(shp_path)
end
dbf = DBFTables.Table(dbf_path)
return Shapefile.Table(shp, dbf)
end
Expand Down
40 changes: 25 additions & 15 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,32 @@ test_tuples = [
@testset "Shapefile" begin

for test in test_tuples
shp = open(joinpath(@__DIR__, test.path)) do fd
read(fd, Shapefile.Handle)
end
shapes = unique(map(typeof, shp.shapes))
@test length(shapes) == 1
@test shapes[1] == test.geomtype
@test eltype(shp.shapes) == Union{test.geomtype,Missing}
# missing and MultiPatch are not covered by the GeoInterface
if !(test.geomtype <: Union{Missing,Shapefile.MultiPatch})
@test GeoInterface.coordinates.(shp.shapes) == test.coordinates
end
@test shp.MBR == test.bbox
for use_shx in (false, true)
shp = if use_shx
# this accesses .shp based on offsets in .shx
stempath, ext = splitext(test.path)
shxname = string(stempath, ".shx")
Shapefile.Handle(joinpath(@__DIR__, test.path), joinpath(@__DIR__, shxname))
else
# use .shp only
open(joinpath(@__DIR__, test.path)) do fd
read(fd, Shapefile.Handle)
end
end
shapes = unique(map(typeof, shp.shapes))
@test length(shapes) == 1
@test shapes[1] == test.geomtype
@test eltype(shp.shapes) == Union{test.geomtype,Missing}
# missing and MultiPatch are not covered by the GeoInterface
if !(test.geomtype <: Union{Missing,Shapefile.MultiPatch})
@test GeoInterface.coordinates.(shp.shapes) == test.coordinates
end
@test shp.MBR == test.bbox

# Multipatch can't be plotted, but it's obscure anyway
if !(test.geomtype == Shapefile.MultiPatch)
plot(shp) # Just test that it actually plots
# Multipatch can't be plotted, but it's obscure anyway
if !(test.geomtype == Shapefile.MultiPatch)
plot(shp) # Just test that it actually plots
end
end
end

Expand Down