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
27 @dlon = width / 2 * degrees_per_pixel
28 @dlat = height / 2 * degrees_per_pixel * cos(@clat * PI / 180)
30 @tx = xsheet(@clon - @dlon)
31 @ty = ysheet(@clat - @dlat)
33 @bx = xsheet(@clon + @dlon)
34 @by = ysheet(@clat + @dlat)
38 #the following two functions will give you the x/y on the entire sheet
41 return 40008.0 / 360.0 * @degrees_per_pixel
45 log(tan(PI / 4 + (lat * PI / 180 / 2)))
52 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
55 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
59 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
65 # FIXME swap REXML for libXML
66 attr_reader :possible_points
67 attr_reader :actual_points
68 attr_reader :tracksegs
70 def initialize(filename)
76 file = File.new(filename)
77 parser = REXML::Parsers::SAX2Parser.new( file )
87 parser.listen( :start_element, %w{ trkpt }) do |uri,localname,qname,attributes|
88 lat = attributes['lat'].to_f
89 lon = attributes['lon'].to_f
94 parser.listen( :characters, %w{ ele } ) do |text|
99 parser.listen( :characters, %w{ time } ) do |text|
100 if text && text != ''
101 date = Time.parse(text)
106 parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
110 parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
111 if gotlatlon && gotdate
112 ele = '0' unless gotele
113 if lat < 90 && lat > -90 && lon > -180 && lon < 180
115 @points.push(Hash['latitude' => lat,'longitude' => lon,'timestamp' => date,'altitude' => ele,'segment' => @tracksegs])
126 @points.each { |p| yield p }
129 def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
130 #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
134 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
135 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
140 gc = Magick::Draw.new
141 gc.stroke_linejoin('miter')
144 gc.rectangle(0,0,width,height)
157 px = proj.x(p['longitude'])
158 py = proj.y(p['latitude'])
160 images[n].stroke_width(1)
161 images[n].stroke('#BBBBBB')
162 images[n].fill('#BBBBBB')
163 # puts "A #{px},#{py} - #{oldpx},#{oldpy}"
164 images[n].line(px, py, oldpx, oldpy ) unless first
166 images[mm].stroke_width(3)
167 images[mm].stroke('#000000')
168 images[mm].fill('#000000')
169 images[mm].line(px, py, oldpx, oldpy ) unless first
170 # puts "B #{px},#{py} - #{oldpx},#{oldpy}"
172 if m > num_points.to_f / frames.to_f * (mm+1)
180 il = Magick::ImageList.new
183 canvas = Magick::Image.new(width, height) {
184 self.background_color = 'white'
187 images[n].draw(canvas)
190 canvas.format = 'GIF'
199 def get_icon(min_lat, min_lon, max_lat, max_lon)
200 puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
203 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
204 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
208 gc = Magick::Draw.new
209 gc.stroke_linejoin('miter')
221 px = proj.x(p['longitude'])
222 py = proj.y(p['latitude'])
223 gc.line(px, py, oldpx, oldpy ) unless first
224 # puts "C #{px},#{py} - #{oldpx},#{oldpy}"
230 canvas = Magick::Image.new(width, height) {
231 self.background_color = 'white'
237 canvas.format = 'GIF'
238 return canvas.to_blob
244 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
245 @doc = XML::Document.new
246 @doc.encoding = 'UTF-8'
248 rss = XML::Node.new 'rss'
250 rss['version'] = "2.0"
251 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
252 @channel = XML::Node.new 'channel'
254 title = XML::Node.new 'title'
257 description_el = XML::Node.new 'description'
258 @channel << description_el
260 description_el << feed_description
261 link = XML::Node.new 'link'
264 image = XML::Node.new 'image'
266 url = XML::Node.new 'url'
267 url << 'http://www.openstreetmap.org/feeds/mag_map-rss2.0.png'
269 title = XML::Node.new 'title'
270 title << "OpenStreetMap"
272 width = XML::Node.new 'width'
275 height = XML::Node.new 'height'
278 link = XML::Node.new 'link'
283 def add(latitude=0, longitude=0, title_text='dummy title', url='http://www.example.com/', description_text='dummy description', timestamp=Time.now)
284 item = XML::Node.new 'item'
286 title = XML::Node.new 'title'
289 link = XML::Node.new 'link'
293 guid = XML::Node.new 'guid'
297 description = XML::Node.new 'description'
298 description << description_text
301 pubDate = XML::Node.new 'pubDate'
302 pubDate << timestamp.to_s(:rfc822)
306 lat_el = XML::Node.new 'geo:lat'
307 lat_el << latitude.to_s
312 lon_el = XML::Node.new 'geo:long'
313 lon_el << longitude.to_s
327 doc = XML::Document.new
328 doc.encoding = 'UTF-8'
329 root = XML::Node.new 'osm'
330 root['version'] = API_VERSION
331 root['generator'] = 'OpenStreetMap server'