Skip to content

Commit

Permalink
add geointerface methods for Row
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaqz committed May 23, 2022
1 parent eafd274 commit 2fff2ab
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions src/table.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import GeoInterface, DBFTables, Tables

"""
Row
Row(geometry, record::DBFTables.Row)
A struct representing a single record in a shapefile.
Property names accessable by `row.x` are `geometry` for the
geometry object, and the names of the columns in `record`.
"""
struct Row{T}
geometry::T
record::DBFTables.Row
end


Base.getproperty(row::Row, name::Symbol) = getproperty(getfield(row, :record), name)
Base.propertynames(row::Row) = propertynames(getfield(row, :record))

GeoInterface.isfeature(t::Row) = true
GeoInterface.geometry(t::Row) = getfield(t, :geometry)
GeoInterface.properties(t::Row) = getfield(t, :record)

"""
Table
Expand Down Expand Up @@ -55,21 +78,6 @@ function Table(path::AbstractString)
return Shapefile.Table(shp, dbf)
end

"""
Row
Row(geometry, record::DBFTables.Row)
A struct representing a single record in a shapefile.
Property names accessable by `row.x` are `geometry` for the
geometry object, and the names of the columns in `record`.
"""
struct Row{T}
geometry::T
record::DBFTables.Row
end

getshp(t::Table) = getfield(t, :shp)
getdbf(t::Table) = getfield(t, :dbf)

Expand All @@ -88,13 +96,11 @@ Iterate over the rows of a Shapefile.Table, yielding a Shapefile.Row for each ro
"""
function Base.iterate(t::Table, st = 1)
st > length(t) && return nothing
geom = @inbounds shapes(t)[st]
geom = shapes(t)[st]
record = DBFTables.Row(getdbf(t), st)
return Row(geom, record), st + 1
end

Base.getproperty(row::Row, name::Symbol) = getproperty(getfield(row, :record), name)

function Base.getproperty(t::Table, name::Symbol)
if name === :geometry
shapes(t)
Expand All @@ -103,8 +109,6 @@ function Base.getproperty(t::Table, name::Symbol)
end
end

Base.propertynames(row::Row) = propertynames(getfield(row, :record))

function Base.propertynames(t::Table)
names = propertynames(getdbf(t))
pushfirst!(names, :geometry)
Expand All @@ -131,13 +135,6 @@ end
# TODO generalize these with a future GeoInterface/GeoTables
# should probably be geometry/geometries but don't want to claim these names yet

"""
shapes(row::Row)
Get the geometry associated with a `Row` from a shapefile `Table`.
"""
shape(row::Row) = getfield(row, :geometry)

"""
shapes(t::Table)
Expand Down

0 comments on commit 2fff2ab

Please sign in to comment.