JuliaGeo / GeoJSON.jl

Utilities for working with GeoJSON data in Julia
https://juliageo.org/GeoJSON.jl/stable/
MIT License
68 stars 10 forks source link

Issues trying to read a geojson file #41

Closed mroavi closed 2 years ago

mroavi commented 2 years ago

Hi there. I'm new to GeoJSON. I'm having issues trying to load the following json file: https://github.com/finiterank/mapa-colombia-js/blob/master/colombia-municipios.json

I downloaded it to my computer. This is how I'm trying to read the file:

using GeoJSON, GeoMakie, GLMakie

# Use local geojson file
# Source: https://github.com/finiterank/mapa-colombia-js/blob/master/colombia-municipios.json
filepath = joinpath(@__DIR__, "colombia-municipios.json")
geo = GeoJSON.read(read(filepath)) # TODO: does not work

The issue is that nothing get's loaded (Nothing literally gets stored in geo).

I've investigated a little to see if I could discover what is going wrong.

I actually managed to read and plot the following geojson file: https://observablehq.com/@john-guerra/spike-map-of-colombian-cities-by-population

I compared the content of both files and I found differences. For example, the one that fails to be read does not have a FeatureCollection or Features in it. However, I find it strange that GitHub is able to visualize it correctly.

Could it be that the first geojson file has a different format that is not supported by GeoJSON.jl?

rafaqz commented 2 years ago

As GeoJSON is a subset of JSON its completely possible to have valid JSON that github can visualise, that is not valid GeoJSON.

This may or may not be the case with this file... a better test is if another json package un a different language can read it.

You can also try GeoJSONTables.jl, which will soon replace the code here.

mroavi commented 2 years ago

I see. Thanks for clarifying. I'll give GeoJSONTables.jl a go.

mroavi commented 2 years ago

I noticed that GeoJSONTables is not registered yet. I tried installing the master branch the github's URL but I'm having issues during the precompilation:

Click to expand! ```julia julia> using GeoJSONTables [ Info: Precompiling GeoJSONTables [b51d8a2e-9f51-4bbc-a3e6-443ab3727fd0] ERROR: LoadError: UndefVarError: geomtype not defined Stacktrace: [1] getproperty(x::Module, f::Symbol) @ Base ./Base.jl:35 [2] top-level scope @ ~/.julia/packages/GeoJSONTables/8WCFW/src/geointerface.jl:5 [3] include(mod::Module, _path::String) @ Base ./Base.jl:418 [4] include(x::String) @ GeoJSONTables ~/.julia/packages/GeoJSONTables/8WCFW/src/GeoJSONTables.jl:1 [5] top-level scope @ ~/.julia/packages/GeoJSONTables/8WCFW/src/GeoJSONTables.jl:153 [6] include @ ./Base.jl:418 [inlined] [7] include_package_for_output(pkg::Base.PkgId, input::String, depot_path::Vector{String}, dl_load_path::Vector{String}, load_path::Vector{String}, concrete_deps::Vector{Pair{Base.PkgId, UInt64}}, source::Nothing) @ Base ./loading.jl:1318 [8] top-level scope @ none:1 [9] eval @ ./boot.jl:373 [inlined] [10] eval(x::Expr) @ Base.MainInclude ./client.jl:453 [11] top-level scope @ none:1 in expression starting at /home/mroavi/.julia/packages/GeoJSONTables/8WCFW/src/geointerface.jl:5 in expression starting at /home/mroavi/.julia/packages/GeoJSONTables/8WCFW/src/GeoJSONTables.jl:1 ERROR: LoadError: Failed to precompile GeoJSONTables [b51d8a2e-9f51-4bbc-a3e6-443ab3727fd0] to /home/mroavi/.julia/compiled/v1.7/GeoJSONTables/jl_ED4a78. Stacktrace: [1] error(s::String) @ Base ./error.jl:33 [2] compilecache(pkg::Base.PkgId, path::String, internal_stderr::IO, internal_stdout::IO, ignore_loaded_modules::Bool) @ Base ./loading.jl:1466 [3] compilecache(pkg::Base.PkgId, path::String) @ Base ./loading.jl:1410 [4] _require(pkg::Base.PkgId) @ Base ./loading.jl:1120 [5] require(uuidkey::Base.PkgId) @ Base ./loading.jl:1013 [6] require(into::Module, mod::Symbol) @ Base ./loading.jl:997 [7] eval @ ./boot.jl:373 [inlined] [8] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String) @ Base ./loading.jl:1196 [9] include_string(m::Module, txt::String, fname::String) @ Base ./loading.jl:1206 [10] top-level scope @ REPL[13]:1 in expression starting at /home/mroavi/Dropbox/tue/phd/software/julia/Makie.jl/plots/maps/plebiscito-por-la-paz/plebiscito-por-la-paz.jl:1 ```

I created a clean environment and added this package only.

If you want I can create an issue in GeoJSONTables instead.

visr commented 2 years ago

The colombia-municipios.json file is not a GeoJSON, but TopoJSON, which is an extension of GeoJSON that encodes topology. Find out more here: https://github.com/topojson/topojson.

This package, nor GeoJSONTables, support TopoJSON. Perhaps one day it will, or maybe it's better suited for a separate package, I don't know. If you want to read it now however your best best is to read it through GDAL, for instance with GeoDataFrames.jl

mroavi commented 2 years ago

Ahhh that makes it clear now! Later, when the GeoJSONTables functionality gets integrated, it would be nice if an error is thrown whenever the read fails. For now, I will just work with the second file I posted (which doesn't contain the municipalities but should be ok). Thanks again for your time.

visr commented 2 years ago

Yeah returning nothing isn't great. In GeoJSONTables the error will be better:

julia> GeoJSONTables.read(read("colombia-municipios.json"))
ERROR: ArgumentError: Unknown geometry type Topology