-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_json_builder.rb
83 lines (66 loc) · 1.41 KB
/
geo_json_builder.rb
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
78
79
80
81
82
# Convert array of positions into a proper Geojson file and save it
class GeoJsonBuilder
def convert_to_line_string( positions, properties = {} )
@geojson = feature( properties ) do
lineString do
positions.map{ |coordinates| position_to_array( coordinates ) }
end
end
self
end
def convert_to_points( positions , properties = {})
@geojson = feature_collection do
positions.map do |p|
feature( p.merge( properties) ) do
point do
position_to_array( p )
end
end
end
end
self
end
def save(path)
File.open( path, 'w') do |f|
f.write( @geojson.to_json )
end
end
private
def position_to_array( position )
[position[:position][:longitude],position[:position][:latitude]]
end
def point
{
type: "Point",
coordinates: yield
}
end
def lineString
{
type: "LineString",
coordinates: yield,
}
end
def feature( options = {} )
properties = default_properties.merge( options )
{
type: "Feature",
geometry: yield,
properties: properties
}
end
def feature_collection( options = {} )
{
type: "FeatureCollection",
id: options[:id],
features: yield
}
end
def default_properties
{
stroke: "#f86767",
title: "Trajectory",
description: 'The road'
}
end
end