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'
21 def initialize(lat, lon, degrees_per_pixel, width, height)
22 #init me with your centre lat/lon, the number of degrees per pixel and the size of your image
25 @degrees_per_pixel = degrees_per_pixel
26 @degrees_per_pixel = 0.0000000001 if @degrees_per_pixel < 0.0000000001
29 @dlon = width / 2 * @degrees_per_pixel
30 @dlat = height / 2 * @degrees_per_pixel * cos(@clat * PI / 180)
32 @tx = xsheet(@clon - @dlon)
33 @ty = ysheet(@clat - @dlat)
35 @bx = xsheet(@clon + @dlon)
36 @by = ysheet(@clat + @dlat)
40 #the following two functions will give you the x/y on the entire sheet
43 return 40008.0 / 360.0 * @degrees_per_pixel
47 log(tan(PI / 4 + (lat * PI / 180 / 2)))
54 #and these two will give you the right points on your image. all the constants can be reduced to speed things up. FIXME
57 return @height - ((ysheet(lat) - @ty) / (@by - @ty) * @height)
61 return ((xsheet(lon) - @tx) / (@bx - @tx) * @width)
67 # FIXME swap REXML for libXML
68 attr_reader :possible_points
69 attr_reader :actual_points
70 attr_reader :tracksegs
72 def initialize(filename)
84 date = DateTime.now();
89 parser = REXML::Parsers::SAX2Parser.new(File.new(@filename))
91 parser.listen( :start_element, %w{ trkpt }) do |uri,localname,qname,attributes|
92 lat = attributes['lat'].to_f
93 lon = attributes['lon'].to_f
100 parser.listen( :characters, %w{ ele } ) do |text|
105 parser.listen( :characters, %w{ time } ) do |text|
106 if text && text != ''
108 date = DateTime.parse(text)
115 parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
119 parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
120 if gotlatlon && gotdate
121 ele = '0' unless gotele
122 if lat < 90 && lat > -90 && lon > -180 && lon < 180
124 yield Hash['latitude' => lat, 'longitude' => lon, 'timestamp' => date, 'altitude' => ele, 'segment' => @tracksegs]
135 def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
136 #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
140 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
141 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
143 linegc = Magick::Draw.new
144 linegc.stroke_linejoin('miter')
145 linegc.stroke_width(1)
146 linegc.stroke('#BBBBBB')
147 linegc.fill('#BBBBBB')
149 highlightgc = Magick::Draw.new
150 highlightgc.stroke_linejoin('miter')
151 highlightgc.stroke_width(3)
152 highlightgc.stroke('#000000')
153 highlightgc.fill('#000000')
158 image = Magick::Image.new(width, height) do |image|
159 image.background_color = 'white'
174 px = proj.x(p['longitude'])
175 py = proj.y(p['latitude'])
185 gc.line(px, py, oldpx, oldpy)
192 if m > num_points.to_f / frames.to_f * (mm+1)
200 il = Magick::ImageList.new
212 def get_icon(min_lat, min_lon, max_lat, max_lon)
213 #puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
216 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
217 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
219 gc = Magick::Draw.new
220 gc.stroke_linejoin('miter')
225 image = Magick::Image.new(width, height) do |image|
226 image.background_color = 'white'
236 px = proj.x(p['longitude'])
237 py = proj.y(p['latitude'])
239 gc.dup.line(px, py, oldpx, oldpy).draw(image) unless first
254 # initialise with a base position
255 def initialize(lat, lon)
256 @lat = lat * PI / 180
257 @lon = lon * PI / 180
260 # get the distance from the base position to a given position
261 def distance(lat, lon)
264 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
267 # get the worst case bounds for a given radius from the base position
269 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
270 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
271 minlat = (@lat - latradius) * 180 / PI
272 maxlat = (@lat + latradius) * 180 / PI
273 minlon = (@lon - lonradius) * 180 / PI
274 maxlon = (@lon + lonradius) * 180 / PI
275 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
280 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
281 @doc = XML::Document.new
282 @doc.encoding = 'UTF-8'
284 rss = XML::Node.new 'rss'
286 rss['version'] = "2.0"
287 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
288 @channel = XML::Node.new 'channel'
290 title = XML::Node.new 'title'
293 description_el = XML::Node.new 'description'
294 @channel << description_el
296 description_el << feed_description
297 link = XML::Node.new 'link'
300 image = XML::Node.new 'image'
302 url = XML::Node.new 'url'
303 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
305 title = XML::Node.new 'title'
306 title << "OpenStreetMap"
308 width = XML::Node.new 'width'
311 height = XML::Node.new 'height'
314 link = XML::Node.new 'link'
319 def add(latitude=0, longitude=0, title_text='dummy title', author_text='anonymous', url='http://www.example.com/', description_text='dummy description', timestamp=DateTime.now)
320 item = XML::Node.new 'item'
322 title = XML::Node.new 'title'
325 link = XML::Node.new 'link'
329 guid = XML::Node.new 'guid'
333 description = XML::Node.new 'description'
334 description << description_text
337 author = XML::Node.new 'author'
338 author << author_text
341 pubDate = XML::Node.new 'pubDate'
342 pubDate << timestamp.to_s(:rfc822)
346 lat_el = XML::Node.new 'geo:lat'
347 lat_el << latitude.to_s
352 lon_el = XML::Node.new 'geo:long'
353 lon_el << longitude.to_s
367 doc = XML::Document.new
368 doc.encoding = 'UTF-8'
369 root = XML::Node.new 'osm'
370 root['version'] = API_VERSION
371 root['generator'] = 'OpenStreetMap server'
377 def self.IPLocation(ip_address)
378 Timeout::timeout(4) do
379 Net::HTTP.start('api.hostip.info') do |http|
380 country = http.get("/country.php?ip=#{ip_address}").body
381 country = "GB" if country == "UK"
382 Net::HTTP.start('ws.geonames.org') do |http|
383 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
384 xml.elements.each("geonames/country") do |ele|
385 minlon = ele.get_text("bBoxWest").to_s
386 minlat = ele.get_text("bBoxSouth").to_s
387 maxlon = ele.get_text("bBoxEast").to_s
388 maxlat = ele.get_text("bBoxNorth").to_s
389 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
400 # Construct a random token of a given length
401 def self.make_token(length = 30)
402 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
406 token += chars[(rand * chars.length).to_i].chr
412 # Return an encrypted version of a password
413 def self.encrypt_password(password, salt)
414 return Digest::MD5.hexdigest(password) if salt.nil?
415 return Digest::MD5.hexdigest(salt + password)
418 # Return an SQL fragment to select a given area of the globe
419 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
420 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
421 minlat = (minlat * 10000000).round
422 minlon = (minlon * 10000000).round
423 maxlat = (maxlat * 10000000).round
424 maxlon = (maxlon * 10000000).round
426 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"