/
parser.jl
77 lines (67 loc) · 2.41 KB
/
parser.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# Dict -> GeoJSON
function dict2geojson(obj::Dict{@compat(AbstractString),Any})
t = symbol(obj["type"])
if t == :FeatureCollection
return FeatureCollection(obj)
elseif t == :Feature
return Feature(obj)
elseif t == :GeometryCollection
return GeometryCollection(obj)
elseif t == :MultiPolygon
return MultiPolygon(obj)
elseif t == :Polygon
return Polygon(obj)
elseif t == :MultiLineString
return MultiLineString(obj)
elseif t == :LineString
return LineString(obj)
elseif t == :MultiPoint
return MultiPoint(obj)
elseif t == :Point
return Point(obj)
end
end
dict2geojson(obj::Nothing) = obj
# GeoJSON -> Dict
for geom in (MultiPolygon, MultiLineString, MultiPoint,
Polygon, LineString, Point)
@eval begin
function geojson2dict(obj::$geom)
dict = @compat Dict("type" => string($geom),
"coordinates" => coordinates(obj))
hasbbox(obj) && (dict["bbox"] = bbox(obj))
hascrs(obj) && (dict["crs"] = crs(obj))
dict
end
end
end
function geojson2dict(obj::GeometryCollection)
dict = @compat Dict("type" => "GeometryCollection",
"geometries" => map(geojson2dict, geometries(obj)))
hasbbox(obj) && (dict["bbox"] = bbox(obj))
hascrs(obj) && (dict["crs"] = crs(obj))
dict
end
function geojson2dict(obj::Feature)
dict = @compat Dict("type" => "Feature",
"properties" => properties(obj),
"geometry" => geojson2dict(geometry(obj)))
hasbbox(obj) && (dict["bbox"] = bbox(obj))
hascrs(obj) && (dict["crs"] = crs(obj))
hasid(obj) && (dict["id"] = id(obj))
dict
end
function geojson2dict(obj::FeatureCollection)
dict = @compat Dict("type" => "FeatureCollection",
"features" => map(geojson2dict, features(obj)))
hasbbox(obj) && (dict["bbox"] = bbox(obj))
hascrs(obj) && (dict["crs"] = crs(obj))
dict
end
geojson2dict(obj::Nothing) = obj
# @compat(AbstractString)/File -> GeoJSON
parse(input; kwargs...) = dict2geojson(JSON.parse(input; kwargs...))
parsefile(filename; kwargs...) = dict2geojson(JSON.parsefile(filename; kwargs...))
# GeoJSON -> @compat(AbstractString)/IO
geojson(obj::AbstractGeoJSON) = JSON.json(geojson2dict(obj))
print(io::IO, obj::AbstractGeoJSON) = JSON.print(io, geojson2dict(obj))