Skip to content

Commit

Permalink
Merge b1670db into ec11e4d
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Aug 11, 2019
2 parents ec11e4d + b1670db commit ac4bbea
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 140 deletions.
12 changes: 4 additions & 8 deletions .travis.yml
Expand Up @@ -5,20 +5,16 @@ os:
- osx
julia:
- 1.0
- 1.1
- 1.2
- nightly
notifications:
email: false
# uncomment the following lines to override the default test script
#script:
# - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi
# - julia -e 'using Pkg; Pkg.clone(pwd()); Pkg.build("GeoJSON"); Pkg.test("GeoJSON"; coverage=true)'
after_success:
# push coverage results to Coveralls
- julia -e 'using Pkg; cd(Pkg.dir("GeoJSON")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'
coveralls: true
jobs:
include:
- stage: "Documentation"
julia: 1.0
julia: 1.2
os: linux
script:
- julia --project=docs/ -e 'using Pkg; Pkg.instantiate();
Expand Down
17 changes: 17 additions & 0 deletions Project.toml
@@ -0,0 +1,17 @@
name = "GeoJSON"
uuid = "61d90e0f-e114-555e-ac52-39dfb47a3ef9"

[deps]
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"

[compat]
GeoInterface = "≥ 0.4.0"
JSON3 = "≥ 0.1.4"
julia = "≥ 1.0.0"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
3 changes: 0 additions & 3 deletions REQUIRE

This file was deleted.

100 changes: 0 additions & 100 deletions docs/Manifest.toml

This file was deleted.

4 changes: 2 additions & 2 deletions docs/Project.toml
@@ -1,7 +1,7 @@
[deps]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
GeoInterface = "cf35fbd7-0cd7-5166-be24-54bfbe79505f"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
JSON3 = "0f8b85d8-7281-11e9-16c2-39a750bddbf1"

[compat]
Documenter = "~0.20"
Documenter = "~0.23"
1 change: 0 additions & 1 deletion docs/make.jl
Expand Up @@ -3,7 +3,6 @@ using GeoJSON

makedocs(
sitename = "GeoJSON",
format = :html,
modules = [GeoJSON],
)

Expand Down
6 changes: 3 additions & 3 deletions docs/src/index.md
Expand Up @@ -29,7 +29,7 @@ pkg> test GeoJSON

## Basic Usage
Although we use GeoInterface types for representing GeoJSON objects, it works in tandem
with the [JSON.jl](https://github.com/JuliaIO/JSON.jl) package, for parsing and some
with the [JSON3.jl](https://github.com/quinnj/JSON3.jl) package, for parsing and some
printing of objects. Here are some examples of its functionality:

### Parses a GeoJSON String or IO stream into a GeoInterface object
Expand Down Expand Up @@ -77,8 +77,8 @@ dict
```

```@example basic
using JSON
JSON.parse(osm_buildings) # should be comparable (if not the same)
using JSON3
JSON3.read(osm_buildings) # should be comparable (if not the same)
```

### Transforms from a nested Array/Dict to a GeoInterface object
Expand Down
35 changes: 20 additions & 15 deletions src/GeoJSON.jl
@@ -1,14 +1,14 @@
module GeoJSON

import GeoInterface
import JSON
import JSON3

export dict2geo,
geo2dict,
geojson

"""
parse(input::Union{String, IO}, inttype::Type{<:Real}=Int64)
parse(input::Union{String, IO})
Parse a GeoJSON string or IO stream into a GeoInterface object.
Expand All @@ -20,16 +20,20 @@ julia> GeoJSON.parse("{\"type\": \"Point\", \"coordinates\": [30, 10]}")
GeoInterface.Point([30.0, 10.0])
```
"""
parse(input; kwargs...) = dict2geo(JSON.parse(input; kwargs...))
parse(input) = dict2geo(JSON3.read(input))

"""
parsefile(filename::AbstractString, inttype::Type{<:Real}=Int64)
parsefile(filename::AbstractString)
Parse a GeoJSON file into a GeoInterface object.
See also: [`parse`](@ref)
"""
parsefile(filename; kwargs...) = dict2geo(JSON.parsefile(filename; kwargs...))
function parsefile(filename)
open(filename) do io
dict2geo(JSON3.read(io))
end
end

"""
geojson(obj)
Expand All @@ -48,11 +52,11 @@ function geojson end
for geom in (:AbstractFeatureCollection, :AbstractGeometryCollection, :AbstractFeature,
:AbstractMultiPolygon, :AbstractPolygon, :AbstractMultiLineString,
:AbstractLineString, :AbstractMultiPoint, :AbstractPoint)
@eval geojson(obj::GeoInterface.$geom) = JSON.json(geo2dict(obj))
@eval geojson(obj::GeoInterface.$geom) = JSON3.write(geo2dict(obj))
end

"""
dict2geo(obj::Dict{String, Any})
dict2geo(obj::AbstractDict{<:Union{Symbol, String}, Any})
Transform a parsed JSON dictionary to a GeoInterface object.
Expand All @@ -64,7 +68,7 @@ julia> dict2geo(Dict("type" => "Point", "coordinates" => [30.0, 10.0]))
Point([30.0, 10.0])
```
"""
function dict2geo(obj::Dict{String,Any})
function dict2geo(obj::AbstractDict{<:Union{Symbol, String}, Any})
t = Symbol(obj["type"])
if t == :FeatureCollection
return parseFeatureCollection(obj)
Expand All @@ -89,11 +93,12 @@ end

dict2geo(obj::Nothing) = nothing

parseGeometryCollection(obj::Dict{String,Any}) =
GeoInterface.GeometryCollection(map(dict2geo,obj["geometries"]))
parseGeometryCollection(obj::AbstractDict{<:Union{Symbol, String}, Any}) =
GeoInterface.GeometryCollection(dict2geo.(obj["geometries"]))

function parseFeature(obj::Dict{String,Any})
feature = GeoInterface.Feature(dict2geo(obj["geometry"]), obj["properties"])
function parseFeature(obj::AbstractDict{<:Union{Symbol, String}, Any})
properties = Dict{String, Any}(String(k) => v for (k, v) in obj["properties"])
feature = GeoInterface.Feature(dict2geo(obj["geometry"]), properties)
if haskey(obj, "id")
feature.properties["featureid"] = obj["id"]
end
Expand All @@ -106,14 +111,14 @@ function parseFeature(obj::Dict{String,Any})
feature
end

function parseFeatureCollection(obj::Dict{String,Any})
features = GeoInterface.Feature[map(parseFeature,obj["features"])...]
function parseFeatureCollection(obj::AbstractDict{<:Union{Symbol, String}, Any})
features = parseFeature.(obj["features"])
featurecollection = GeoInterface.FeatureCollection(features)
if haskey(obj, "bbox")
featurecollection.bbox = GeoInterface.BBox(obj["bbox"])
end
if haskey(obj, "crs")
featurecollection.crs = obj["crs"]
featurecollection.crs = Dict{String, Any}(String(k) => v for (k, v) in obj["crs"])
end
featurecollection
end
Expand Down
16 changes: 8 additions & 8 deletions test/runtests.jl
@@ -1,4 +1,4 @@
using GeoJSON, GeoInterface
using GeoJSON, GeoInterface, JSON3
using Test

include(joinpath(@__DIR__, "geojson_samples.jl"))
Expand Down Expand Up @@ -49,7 +49,7 @@ end
@test length(feature.properties) == 3
@test feature.properties["type"] == "meow"
@test GeoInterface.crs(feature) == feature.properties["crs"]
@test GeoInterface.crs(feature) isa Dict{String,Any}
@test GeoInterface.crs(feature) isa JSON3.Object
@test GeoInterface.crs(feature)["type"] == "name"
@test GeoInterface.crs(feature)["properties"]["name"] == "urn:ogc:def:crs:EPSG::3785"
dict = geo2dict(feature)
Expand Down Expand Up @@ -108,17 +108,17 @@ end
@testset "G: MultiPolygon" begin
featurecollection = GeoJSON.parse(g)
# printing to GeoJSON not yet implemented
# @test sprint(print,JSON.json(featurecollection)) ==
# @test JSON3.write(featurecollection) ==
# "{\"features\":[{\"geometry\":{\"coordinates\":[[[[-117.913883,33.96657],[-117.907767,33.967747],[-117.912919,33.96445],[-117.913883,33.96657]]]],\"type\":\"MultiPolygon\"},\"properties\":{\"addr2\":\"Rowland Heights\",\"cartodb_id\":46,\"addr1\":\"18150 E. Pathfinder Rd.\",\"park\":\"Pathfinder Park\"},\"type\":\"Feature\"}],\"bbox\":[100.0,0.0,105.0,1.0],\"type\":\"FeatureCollection\",\"crs\":{\"properties\":{\"name\":\"urn:ogc:def:crs:EPSG::3785\"},\"type\":\"name\"}}"
@test featurecollection.bbox [100,0,105,1]
@test featurecollection.crs == Dict("properties" => Dict("name" => "urn:ogc:def:crs:EPSG::3785"),
"type" => "name")
"type" => "name")
end

@testset "H: Print" begin
feature = GeoJSON.parse(h)
# printing to GeoJSON not yet implemented
# @test sprint(print,JSON.json(feature)) ==
# @test JSON3.write(feature) ==
# "{\"geometry\":{\"coordinates\":[[[3.75,9.25],[-130.95,1.52]],[[23.15,-34.25],[-1.35,-4.65],[3.45,77.95]]],\"type\":\"MultiLineString\"},\"properties\":{\"title\":\"Dict 1\"},\"bbox\":[-180.0,-90.0,180.0,90.0],\"type\":\"Feature\"}"
end

Expand Down Expand Up @@ -323,10 +323,10 @@ end
@test building_dict isa Dict{String,Any}

# printing to GeoJSON not yet implemented
# @test GeoJSON.json(buildings) ==
# @test GeoJSON.write(buildings) ==
# """{\"features\":[{\"geometry\":{\"coordinates\":[[[13.42634,52.49533],[13.4266,52.49524],[13.42619,52.49483],[13.42583,52.49495],[13.4259,52.49501],[13.42611,52.49494],[13.4264,52.49525],[13.4263,52.49529],[13.42634,52.49533]]],\"type\":\"Polygon\"},\"properties\":{\"height\":150,\"color\":\"rgb(255,200,150)\"},\"type\":\"Feature\"},{\"geometry\":{\"coordinates\":[[[13.42706,52.49535],[13.42745,52.4952],[13.42745,52.4952],[13.42741,52.49516],[13.42717,52.49525],[13.42692,52.49501],[13.42714,52.49494],[13.42686,52.49466],[13.4265,52.49478],[13.42657,52.49486],[13.42678,52.4948],[13.42694,52.49496],[13.42675,52.49503],[13.42706,52.49535]]],\"type\":\"Polygon\"},\"properties\":{\"height\":130,\"color\":\"rgb(180,240,180)\"},\"type\":\"Feature\"},{\"geometry\":{\"coordinates\":[[[[13.42746,52.4944],[13.42794,52.49494],[13.42799,52.49492],[13.42755,52.49442],[13.42798,52.49428],[13.42846,52.4948],[13.42851,52.49478],[13.428,52.49422],[13.42746,52.4944]]],[[[13.42803,52.49497],[13.428,52.49493],[13.42844,52.49479],[13.42847,52.49483],[13.42803,52.49497]]]],\"type\":\"MultiPolygon\"},\"properties\":{\"height\":120,\"color\":\"rgb(200,200,250)\"},\"type\":\"Feature\"},{\"geometry\":{\"coordinates\":[[[13.42857,52.4948],[13.42918,52.49465],[13.42867,52.49412],[13.4285,52.49419],[13.42896,52.49465],[13.42882,52.49469],[13.42837,52.49423],[13.42821,52.49428],[13.42863,52.49473],[13.42853,52.49476],[13.42857,52.4948]]],\"type\":\"Polygon\"},\"properties\":{\"height\":140,\"color\":\"rgb(150,180,210)\"},\"type\":\"Feature\"}],\"type\":\"FeatureCollection\"}"""
# @test GeoJSON.json(buildings) == JSON.json(JSON.parse(osm_buildings))
# @test building_dict == JSON.parse(osm_buildings)
# @test GeoJSON.write(buildings) == JSON3.write(JSON3.read(osm_buildings))
# @test building_dict == JSON3.read(osm_buildings)
end

@testset "Tech Square: parsefile" begin
Expand Down

0 comments on commit ac4bbea

Please sign in to comment.