Skip to content

Commit 348adf6

Browse files
committed
parse all numbers as Float64 when reading JSON
This will avoid problems with coordinates like 1 or 1.0 being converted to Int. This also affects the properties, so integer columns will now be Float64, and will need to be manually converted to integer types.
1 parent 19dfb76 commit 348adf6

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Extents = "0.1"
1717
GeoFormatTypes = "0.4"
1818
GeoInterface = "1"
1919
GeoInterfaceRecipes = "1"
20-
JSON3 = "1"
20+
JSON3 = "1.12"
2121
Tables = "1"
2222
julia = "1.6"
2323

src/json.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Read a GeoJSON string to a GeoInterface.jl compatible feature or geometry object.
66
"""
77
function read(source)
8-
object = JSON3.read(source)
8+
object = JSON3.read(source; numbertype = Float64)
99
if object === nothing
1010
error("JSON string is empty")
1111
end

test/geojson_samples.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,8 @@ geom_collection = """{
487487
}]
488488
}"""
489489

490-
geometries = [multi, bbox, bbox_z, bermuda_triangle, geom_collection]
490+
point_int = """{"type":"Point","coordinates":[1,2]}"""
491+
492+
geometries = [multi, bbox, bbox_z, bermuda_triangle, geom_collection, point_int]
491493

492494
end # module test

test/runtests.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ include("geojson_samples.jl")
234234
@test t[1] isa GeoJSON.Feature
235235
@test t.geometry isa Vector{Union{T,Missing}} where {T<:GeoJSON.Point}
236236
@test ismissing(t.geometry[3])
237-
@test t.a isa Vector{Union{Int,Missing}}
237+
@test t.a isa Vector{Union{Float64,Missing}}
238238
@test isequal(t.a, [1, missing, 3])
239239
@test t.b isa Vector{Missing}
240240
@test Tables.columntable(t) isa NamedTuple
@@ -280,6 +280,17 @@ include("geojson_samples.jl")
280280
@test p isa GeoJSON.Point
281281
end
282282

283+
@testset "numbertype" begin
284+
# all numbers are Float64 since we use numbertype=Float64
285+
p = GeoJSON.read(T.point_int)
286+
@test p isa GeoJSON.Point
287+
coords = GeoJSON.coordinates(p)
288+
@test coords isa JSON3.Array
289+
@test eltype(coords) == Float64
290+
@test coords == [1, 2]
291+
@test copy(coords) isa Vector{Float64}
292+
end
293+
283294
Aqua.test_all(GeoJSON)
284295

285296
end # testset "GeoJSON"

0 commit comments

Comments
 (0)