-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathgis.rb
executable file
·163 lines (142 loc) · 3.09 KB
/
gis.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env ruby
class Track
def initialize(segments, name=nil)
@name = name
segment_objects = []
segments.each do |s|
segment_objects.append(TrackSegment.new(s))
end
# set segments to segment_objects
@segments = segment_objects
end
def get_track_json()
j = '{'
j += '"type": "Feature", '
if @name != nil
j+= '"properties": {'
j += '"title": "' + @name + '"'
j += '},'
end
j += '"geometry": {'
j += '"type": "MultiLineString",'
j +='"coordinates": ['
# Loop through all the segment objects
@segments.each_with_index do |s, index|
if index > 0
j += ","
end
j += '['
# Loop through all the coordinates in the segment
tsj = ''
s.coordinates.each do |c|
if tsj != ''
tsj += ','
end
# Add the coordinate
tsj += '['
tsj += "#{c.lon},#{c.lat}"
if c.ele != nil
tsj += ",#{c.ele}"
end
tsj += ']'
end
j+=tsj
j+=']'
end
j + ']}}'
end
end
class TrackSegment
attr_reader :coordinates
def initialize(coordinates)
@coordinates = coordinates
end
end
class Point
attr_reader :lat, :lon, :ele
def initialize(lon, lat, ele=nil)
@lon = lon
@lat = lat
@ele = ele
end
end
class Waypoint
attr_reader :lat, :lon, :ele, :name, :type
def initialize(lon, lat, ele=nil, name=nil, type=nil)
@lat = lat
@lon = lon
@ele = ele
@name = name
@type = type
end
def get_waypoint_json(indent=0)
j = '{"type": "Feature",'
# if name is not nil or type is not nil
j += '"geometry": {"type": "Point","coordinates": '
j += "[#{@lon},#{@lat}"
if ele != nil
j += ",#{@ele}"
end
j += ']},'
if name != nil or type != nil
j += '"properties": {'
if name != nil
j += '"title": "' + @name + '"'
end
if type != nil # if type is not nil
if name != nil
j += ','
end
j += '"icon": "' + @type + '"' # type is the icon
end
j += '}'
end
j += "}"
return j
end
end
class World
def initialize(name, things)
@name = name
@features = things
end
def add_feature(f)
@features.append(t)
end
def to_geojson(indent=0)
# Write stuff
s = '{"type": "FeatureCollection","features": ['
@features.each_with_index do |f,i|
if i != 0
s +=","
end
if f.class == Track
s += f.get_track_json
elsif f.class == Waypoint
s += f.get_waypoint_json
end
end
s + "]}"
end
end
def main()
w = Waypoint.new(-121.5, 45.5, 30, "home", "flag")
w2 = Waypoint.new(-121.5, 45.6, nil, "store", "dot")
ts1 = [
Point.new(-122, 45),
Point.new(-122, 46),
Point.new(-121, 46),
]
ts2 = [ Point.new(-121, 45), Point.new(-121, 46), ]
ts3 = [
Point.new(-121, 45.5),
Point.new(-122, 45.5),
]
t = Track.new([ts1, ts2], "track 1")
t2 = Track.new([ts3], "track 2")
world = World.new("My Data", [w, w2, t, t2])
puts world.to_geojson()
end
if File.identical?(__FILE__, $0)
main()
end