Skip to content
Open
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 src/geojson_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function Base.show(io::IO, G::AbstractGeometry{D,T}) where {D,T}
end

Base.length(g::AbstractGeometry) = length(coordinates(g))
Base.firstindex(g::AbstractGeometry) = 1
Base.lastindex(g::AbstractGeometry) = length(coordinates(g))
Base.size(g::AbstractGeometry) = size(coordinates(g))
Base.axes(g::AbstractGeometry) = axes(coordinates(g))
Expand Down Expand Up @@ -144,6 +145,7 @@ geometry(g::GeometryCollection) = getfield(g, :geometries)

Base.show(io::IO, x::GeometryCollection{D,T}) where {D,T} = print(io, "GeometryCollection with $(length(x.geometries)) $(D)D geometries")
Base.length(g::GeometryCollection) = length(geometry(g))
Base.firstindex(g::GeometryCollection) = 1
Base.lastindex(g::GeometryCollection) = length(geometry(g))
Base.size(g::GeometryCollection) = size(geometry(g))
Base.axes(g::GeometryCollection) = axes(geometry(g))
Expand Down Expand Up @@ -262,6 +264,7 @@ Base.show(io::IO, fc::FeatureCollection) = print(io, "FeatureCollection with $(l
Base.eltype(::Type{<:AbstractFeatureCollection{D,T}}) where {D,T} = Feature{D,T}
Base.IteratorEltype(::Type{<:AbstractFeatureCollection}) = Base.HasEltype()
Base.length(fc::AbstractFeatureCollection) = length(features(fc))
Base.firstindex(fc::AbstractFeatureCollection) = 1
Base.lastindex(fc::AbstractFeatureCollection) = length(features(fc))
Base.IteratorSize(::Type{<:AbstractFeatureCollection}) = Base.HasLength()
Base.size(fc::AbstractFeatureCollection) = size(features(fc))
Expand Down
62 changes: 62 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,68 @@ include("geojson_samples.jl")
"{\"type\":\"Point\",\"coordinates\":[1.0,2.0,3]}"
end

@testset "firstindex and begin indexing" begin
# Test Point geometry
p = GeoJSON.read("""{"type":"Point","coordinates":[30,10]}""")
@test firstindex(p) == 1
@test p[begin] == p[1]
@test p[begin] == 30.0f0

# Test LineString geometry
ls = GeoJSON.read("""{"type":"LineString","coordinates":[[100.0,0.0],[101.0,1.0]]}""")
@test firstindex(ls) == 1
@test ls[begin] == ls[1]
@test ls[begin] == (100.0f0, 0.0f0)

# Test Polygon geometry
poly = GeoJSON.read("""{"type":"Polygon","coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}""")
@test firstindex(poly) == 1
@test poly[begin] == poly[1]

# Test GeometryCollection
gc = GeoJSON.read("""{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}]}""")
@test firstindex(gc) == 1
@test gc[begin] == gc[1]
@test gc[begin] isa GeoJSON.AbstractGeometry

# Test FeatureCollection
fc = GeoJSON.read("""{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102.0,0.5]},"properties":{"prop0":"value0"}}]}""")
@test firstindex(fc) == 1
@test fc[begin] == fc[1]
@test fc[begin] isa GeoJSON.Feature
end

@testset "lastindex and end indexing" begin
# Test Point geometry
p = GeoJSON.read("""{"type":"Point","coordinates":[30,10]}""")
@test lastindex(p) == length(p)
@test p[end] == p[lastindex(p)]
@test p[end] == 10.0f0

# Test LineString geometry
ls = GeoJSON.read("""{"type":"LineString","coordinates":[[100.0,0.0],[101.0,1.0]]}""")
@test lastindex(ls) == length(ls)
@test ls[end] == ls[lastindex(ls)]
@test ls[end] == (101.0f0, 1.0f0)

# Test Polygon geometry
poly = GeoJSON.read("""{"type":"Polygon","coordinates":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}""")
@test lastindex(poly) == length(poly)
@test poly[end] == poly[lastindex(poly)]

# Test GeometryCollection
gc = GeoJSON.read("""{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}]}""")
@test lastindex(gc) == length(gc)
@test gc[end] == gc[lastindex(gc)]
@test gc[end] isa GeoJSON.AbstractGeometry

# Test FeatureCollection
fc = GeoJSON.read("""{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Point","coordinates":[102.0,0.5]},"properties":{"prop0":"value0"}}]}""")
@test lastindex(fc) == length(fc)
@test fc[end] == fc[lastindex(fc)]
@test fc[end] isa GeoJSON.Feature
end

@testset "Tables" begin
# try a namedtuple table
t1 = map(tuple.(1:10, 1:10), rand(10), ["abc" for i in 1:10]) do geometry, prop1, prop2
Expand Down