3 # This piece of magic reads a GPX with SAX and spits out
6 # This would print every latitude value:
8 # gpx = OSM:GPXImporter.new('somefile.gpx')
9 # gpx.points {|p| puts p['latitude']}
12 require 'rexml/parsers/sax2parser'
20 def initialize(lat, lon, degrees_per_pixel, width, height)
21 #init me with your centre lat/lon, the number of degrees per pixel and the size of your image
24 @degrees_per_pixel = degrees_per_pixel
25 @degrees_per_pixel = 0.0000000001 if @degrees_per_pixel < 0.0000000001
28 @dlon = width / 2 * @degrees_per_pixel
29 @dlat = height / 2 * @degrees_per_pixel * cos(@clat * PI / 180)
31 @tx = xsheet(@clon - @dlon)
32 @ty = ysheet(@clat - @dlat)
34 @bx = xsheet(@clon + @dlon)
35 @by = ysheet(@clat + @dlat)
39 #the following two functions will give you the x/y on the entire sheet
42 return 40008.0 / 360.0 * @degrees_per_pixel
46 log(tan(PI / 4 + (lat * PI / 180 / 2)))
53 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
56 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
60 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
66 # FIXME swap REXML for libXML
67 attr_reader :possible_points
68 attr_reader :actual_points
69 attr_reader :tracksegs
71 def initialize(filename)
77 file = File.new(filename)
78 parser = REXML::Parsers::SAX2Parser.new( file )
83 date = DateTime.now();
88 parser.listen( :start_element, %w{ trkpt }) do |uri,localname,qname,attributes|
89 lat = attributes['lat'].to_f
90 lon = attributes['lon'].to_f
95 parser.listen( :characters, %w{ ele } ) do |text|
100 parser.listen( :characters, %w{ time } ) do |text|
101 if text && text != ''
102 date = DateTime.parse(text)
107 parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
111 parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
112 if gotlatlon && gotdate
113 ele = '0' unless gotele
114 if lat < 90 && lat > -90 && lon > -180 && lon < 180
116 @points.push(Hash['latitude' => lat,'longitude' => lon,'timestamp' => date,'altitude' => ele,'segment' => @tracksegs])
127 @points.each { |p| yield p }
130 def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
131 #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
135 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
136 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
141 gc = Magick::Draw.new
142 gc.stroke_linejoin('miter')
145 gc.rectangle(0,0,width,height)
158 px = proj.x(p['longitude'])
159 py = proj.y(p['latitude'])
161 images[n].stroke_width(1)
162 images[n].stroke('#BBBBBB')
163 images[n].fill('#BBBBBB')
164 # puts "A #{px},#{py} - #{oldpx},#{oldpy}"
165 images[n].line(px, py, oldpx, oldpy ) unless first
167 images[mm].stroke_width(3)
168 images[mm].stroke('#000000')
169 images[mm].fill('#000000')
170 images[mm].line(px, py, oldpx, oldpy ) unless first
171 # puts "B #{px},#{py} - #{oldpx},#{oldpy}"
173 if m > num_points.to_f / frames.to_f * (mm+1)
181 il = Magick::ImageList.new
184 canvas = Magick::Image.new(width, height) {
185 self.background_color = 'white'
188 images[n].draw(canvas)
191 canvas.format = 'GIF'
200 def get_icon(min_lat, min_lon, max_lat, max_lon)
201 #puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
204 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
205 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
209 gc = Magick::Draw.new
210 gc.stroke_linejoin('miter')
222 px = proj.x(p['longitude'])
223 py = proj.y(p['latitude'])
224 gc.line(px, py, oldpx, oldpy ) unless first
225 # puts "C #{px},#{py} - #{oldpx},#{oldpy}"
231 canvas = Magick::Image.new(width, height) {
232 self.background_color = 'white'
238 canvas.format = 'GIF'
239 return canvas.to_blob
245 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
246 @doc = XML::Document.new
247 @doc.encoding = 'UTF-8'
249 rss = XML::Node.new 'rss'
251 rss['version'] = "2.0"
252 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
253 @channel = XML::Node.new 'channel'
255 title = XML::Node.new 'title'
258 description_el = XML::Node.new 'description'
259 @channel << description_el
261 description_el << feed_description
262 link = XML::Node.new 'link'
265 image = XML::Node.new 'image'
267 url = XML::Node.new 'url'
268 url << 'http://www.openstreetmap.org/feeds/mag_map-rss2.0.png'
270 title = XML::Node.new 'title'
271 title << "OpenStreetMap"
273 width = XML::Node.new 'width'
276 height = XML::Node.new 'height'
279 link = XML::Node.new 'link'
284 def add(latitude=0, longitude=0, title_text='dummy title', url='http://www.example.com/', description_text='dummy description', timestamp=DateTime.now)
285 item = XML::Node.new 'item'
287 title = XML::Node.new 'title'
290 link = XML::Node.new 'link'
294 guid = XML::Node.new 'guid'
298 description = XML::Node.new 'description'
299 description << description_text
302 pubDate = XML::Node.new 'pubDate'
303 pubDate << timestamp.to_s(:rfc822)
307 lat_el = XML::Node.new 'geo:lat'
308 lat_el << latitude.to_s
313 lon_el = XML::Node.new 'geo:long'
314 lon_el << longitude.to_s
328 doc = XML::Document.new
329 doc.encoding = 'UTF-8'
330 root = XML::Node.new 'osm'
331 root['version'] = API_VERSION
332 root['generator'] = 'OpenStreetMap server'