Skip to content

Commit

Permalink
Merge b79fc3b into 63f85f5
Browse files Browse the repository at this point in the history
  • Loading branch information
yeesian committed May 16, 2017
2 parents 63f85f5 + b79fc3b commit 818d596
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 497 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,7 +2,6 @@ language: julia
os:
- linux
julia:
- 0.4
- 0.5
- nightly
notifications:
Expand Down
4 changes: 2 additions & 2 deletions REQUIRE
@@ -1,3 +1,3 @@
julia 0.4
julia 0.5
JSON
Compat 0.8
GeoInterface 0.2
130 changes: 114 additions & 16 deletions src/GeoJSON.jl
Expand Up @@ -2,20 +2,118 @@ __precompile__()

module GeoJSON

using JSON, Compat

export
Point,
MultiPoint,
LineString,
MultiLineString,
Polygon,
MultiPolygon,
GeometryCollection,
Feature,
FeatureCollection,
geojson

include("types.jl")
include("parser.jl")
import GeoInterface
import JSON

export dict2geo,
geojson

# String/File -> GeoJSON
parse(input; kwargs...) = dict2geo(JSON.parse(input; kwargs...))
parsefile(filename; kwargs...) = dict2geo(JSON.parsefile(filename; kwargs...))

# GeoJSON -> String/IO
for geom in (:AbstractFeatureCollection, :AbstractGeometryCollection, :AbstractFeature,
:AbstractMultiPolygon, :AbstractPolygon, :AbstractMultiLineString,
:AbstractLineString, :AbstractMultiPoint, :AbstractPoint)
@eval JSON.json(obj::GeoInterface.$geom) = JSON.json(geojson(obj))
end

dict2geo(obj::Void) = nothing

function dict2geo(obj::Dict{String,Any})
t = Symbol(obj["type"])
if t == :FeatureCollection
return parseFeatureCollection(obj)
elseif t == :Feature
return parseFeature(obj)
elseif t == :GeometryCollection
return parseGeometryCollection(obj)
elseif t == :MultiPolygon
return GeoInterface.MultiPolygon(obj["coordinates"])
elseif t == :Polygon
return GeoInterface.Polygon(obj["coordinates"])
elseif t == :MultiLineString
return GeoInterface.MultiLineString(obj["coordinates"])
elseif t == :LineString
return GeoInterface.LineString(obj["coordinates"])
elseif t == :MultiPoint
return GeoInterface.MultiPoint(obj["coordinates"])
elseif t == :Point
return GeoInterface.Point(obj["coordinates"])
end
end

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

function parseFeature(obj::Dict{String,Any})
feature = GeoInterface.Feature(dict2geo(obj["geometry"]), obj["properties"])
if haskey(obj, "id")
feature.properties["featureid"] = obj["id"]
end
if haskey(obj, "bbox")
feature.properties["bbox"] = GeoInterface.BBox(obj["bbox"])
end
if haskey(obj, "crs")
feature.properties["crs"] = obj["crs"]
end
feature
end

function parseFeatureCollection(obj::Dict{String,Any})
features = GeoInterface.Feature[map(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"]
end
featurecollection
end

geojson(obj::Void) = nothing

function geojson(obj::GeoInterface.AbstractGeometry)
Dict("type" => string(GeoInterface.geotype(obj)),
"coordinates" => GeoInterface.coordinates(obj))
end

function geojson(obj::GeoInterface.AbstractGeometryCollection)
Dict("type" => string(GeoInterface.geotype(obj)),
"geometries" => map(geojson, GeoInterface.geometries(obj)))
end

function geojson(obj::GeoInterface.AbstractFeature)
result = Dict("type" => string(GeoInterface.geotype(obj)),
"geometry" => geojson(GeoInterface.geometry(obj)),
"properties" => copy(GeoInterface.properties(obj)))
if haskey(result["properties"], "bbox")
result["bbox"] = result["properties"]["bbox"]
delete!(result["properties"], "bbox")
end
if haskey(result["properties"], "crs")
result["crs"] = result["properties"]["crs"]
delete!(result["properties"], "crs")
end
if haskey(result["properties"], "featureid")
result["id"] = result["properties"]["featureid"]
delete!(result["properties"], "featureid")
end
result
end

function geojson(obj::GeoInterface.AbstractFeatureCollection)
result = Dict("type" => string(GeoInterface.geotype(obj)),
"features" => map(geojson, GeoInterface.features(obj)))
if GeoInterface.bbox(obj) != nothing
result["bbox"] = GeoInterface.bbox(obj)
end
if GeoInterface.crs(obj) != nothing
result["crs"] = GeoInterface.crs(obj)
end
result
end

end
77 changes: 0 additions & 77 deletions src/parser.jl

This file was deleted.

0 comments on commit 818d596

Please sign in to comment.