GeoJSON.jl

Introduction

Build Status Build Status Coverage Status Latest Documentation

This library is developed independently of, but is heavily influenced in design by the python-geojson package. It contains:

  • Functions for encoding and decoding GeoJSON formatted data
  • a type hierarchy (according to the GeoJSON specification)
  • An implementation of the __geo_interface__, a GeoJSON-like protocol for geo-spatial (GIS) vector data.

Contents

Installation

The package is registered and can be added using the package manager:

pkg> add GeoJSON

To test if it is installed correctly run:

pkg> test GeoJSON

Basic Usage

Although we use GeoInterface types for representing GeoJSON objects, it works in tandem with the JSON3.jl package, for parsing and some printing of objects. Here are some examples of its functionality:

Reads a GeoJSON String or IO stream into a GeoInterface object

using GeoJSON
osm_buildings = """
{
    "type": "FeatureCollection",
    "features": [{
        "type": "Feature",
        "geometry": {
            "type": "Polygon",
            "coordinates": [
                [
                    [13.42634, 52.49533],
                    [13.42630, 52.49529],
                    [13.42640, 52.49525],
                    [13.42611, 52.49494],
                    [13.42590, 52.49501],
                    [13.42583, 52.49495],
                    [13.42619, 52.49483],
                    [13.42660, 52.49524],
                    [13.42634, 52.49533]
                ]
            ]
        },
        "properties": {
            "color": "rgb(255,200,150)",
            "height": 150
        }
    }]
}"""
buildings = GeoJSON.read(osm_buildings)
buildings
GeoInterface.FeatureCollection{GeoInterface.Feature}(GeoInterface.Feature[GeoInterface.Feature(GeoInterface.Polygon(Array{Array{Float64,1},1}[[[13.42634, 52.49533], [13.4263, 52.49529], [13.4264, 52.49525], [13.42611, 52.49494], [13.4259, 52.49501], [13.42583, 52.49495], [13.42619, 52.49483], [13.4266, 52.49524], [13.42634, 52.49533]]]), Dict{String,Any}("height" => 150,"color" => "rgb(255,200,150)"))], nothing, nothing)

Use GeoJSON.read(read("tech_square.geojson")) to read GeoJSON files from disk.

Transforms a GeoInterface object into a nested Array or Dict

dict = geo2dict(buildings) # geo2dict -- GeoInterface object to Dict/Array-representation
dict
Dict{String,Any} with 2 entries:
  "features" => Dict{String,Any}[Dict("geometry"=>Dict{String,Any}("coordinates…
  "type"     => "FeatureCollection"
using JSON3
JSON3.read(osm_buildings) # should be comparable (if not the same)
JSON3.Object{Base.CodeUnits{UInt8,String},Array{UInt64,1}} with 2 entries:
  :type     => "FeatureCollection"
  :features => JSON3.Object[{…

Transforms from a nested Array/Dict to a GeoInterface object

dict2geo(dict)
GeoInterface.FeatureCollection{GeoInterface.Feature}(GeoInterface.Feature[GeoInterface.Feature(GeoInterface.Polygon(Array{Array{Float64,1},1}[[[13.42634, 52.49533], [13.4263, 52.49529], [13.4264, 52.49525], [13.42611, 52.49494], [13.4259, 52.49501], [13.42583, 52.49495], [13.42619, 52.49483], [13.4266, 52.49524], [13.42634, 52.49533]]]), Dict{String,Any}("height" => 150,"color" => "rgb(255,200,150)"))], nothing, nothing)
GeoJSON.read(osm_buildings) # the original object (for comparison)
GeoInterface.FeatureCollection{GeoInterface.Feature}(GeoInterface.Feature[GeoInterface.Feature(GeoInterface.Polygon(Array{Array{Float64,1},1}[[[13.42634, 52.49533], [13.4263, 52.49529], [13.4264, 52.49525], [13.42611, 52.49494], [13.4259, 52.49501], [13.42583, 52.49495], [13.42619, 52.49483], [13.4266, 52.49524], [13.42634, 52.49533]]]), Dict{String,Any}("height" => 150,"color" => "rgb(255,200,150)"))], nothing, nothing)

Writing back GeoJSON strings is not yet implemented.

GeoInterface

This library implements the GeoInterface. For more information on the types that are returned by this package, and the methods that can be used on them, refer to the documentation of the GeoInterface package.

Functions

Input

To read in GeoJSON data, use GeoJSON.read.

GeoJSON.readFunction
read(input::Union{AbstractString, IO, AbstractVector{UInt8}})

Read a GeoJSON string or IO stream into a GeoInterface object.

To read a file, use GeoJSON.read(read(path)).

Examples

julia> GeoJSON.read("{"type": "Point", "coordinates": [30, 10]}")
GeoInterface.Point([30.0, 10.0])
source

Output

GeoJSON.writeFunction
write(obj)

Create a GeoJSON string from an object that implements the GeoInterface, either AbstractGeometry, AbstractFeature or AbstractFeatureCollection.

Examples

julia> GeoJSON.write(Point([30.0, 10.0]))
"{"coordinates":[30.0,10.0],"type":"Point"}"
source

Conversion

For more fine grained control, to construct or deconstruct parts of a GeoJSON, use geo2dict or dict2geo.

GeoJSON.geo2dictFunction
geo2dict(obj)

Transform a GeoInterface object to a JSON dictionary.

See also: dict2geo

Examples

julia> geo2dict(Point([30.0, 10.0]))
Dict{String,Any} with 2 entries:
  "coordinates" => [30.0, 10.0]
  "type"        => "Point"
source
GeoJSON.dict2geoFunction
dict2geo(obj::AbstractDict{<:Union{Symbol, String}, Any})

Transform a JSON dictionary to a GeoInterface object.

See also: geo2dict

Examples

julia> dict2geo(Dict("type" => "Point", "coordinates" => [30.0, 10.0]))
Point([30.0, 10.0])
source

Index