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
98 parser.listen( :characters, %w{ ele } ) do |text|
103 parser.listen( :characters, %w{ time } ) do |text|
104 if text && text != ''
106 date = DateTime.parse(text)
113 parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
117 parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
118 if gotlatlon && gotdate
119 ele = '0' unless gotele
120 if lat < 90 && lat > -90 && lon > -180 && lon < 180
122 yield Hash['latitude' => lat, 'longitude' => lon, 'timestamp' => date, 'altitude' => ele, 'segment' => @tracksegs]
133 def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
134 #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
138 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
139 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
141 linegc = Magick::Draw.new
142 linegc.stroke_linejoin('miter')
143 linegc.stroke_width(1)
144 linegc.stroke('#BBBBBB')
145 linegc.fill('#BBBBBB')
147 highlightgc = Magick::Draw.new
148 highlightgc.stroke_linejoin('miter')
149 highlightgc.stroke_width(3)
150 highlightgc.stroke('#000000')
151 highlightgc.fill('#000000')
156 image = Magick::Image.new(width, height) do |image|
157 image.background_color = 'white'
172 px = proj.x(p['longitude'])
173 py = proj.y(p['latitude'])
183 gc.line(px, py, oldpx, oldpy)
190 if m > num_points.to_f / frames.to_f * (mm+1)
198 il = Magick::ImageList.new
210 def get_icon(min_lat, min_lon, max_lat, max_lon)
211 #puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
214 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
215 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
217 gc = Magick::Draw.new
218 gc.stroke_linejoin('miter')
223 image = Magick::Image.new(width, height) do |image|
224 image.background_color = 'white'
234 px = proj.x(p['longitude'])
235 py = proj.y(p['latitude'])
237 gc.dup.line(px, py, oldpx, oldpy).draw(image) unless first
252 # initialise with a base position
253 def initialize(lat, lon)
254 @lat = lat * PI / 180
255 @lon = lon * PI / 180
258 # get the distance from the base position to a given position
259 def distance(lat, lon)
262 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
265 # get the worst case bounds for a given radius from the base position
267 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
268 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
269 minlat = (@lat - latradius) * 180 / PI
270 maxlat = (@lat + latradius) * 180 / PI
271 minlon = (@lon - lonradius) * 180 / PI
272 maxlon = (@lon + lonradius) * 180 / PI
273 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
278 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
279 @doc = XML::Document.new
280 @doc.encoding = 'UTF-8'
282 rss = XML::Node.new 'rss'
284 rss['version'] = "2.0"
285 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
286 @channel = XML::Node.new 'channel'
288 title = XML::Node.new 'title'
291 description_el = XML::Node.new 'description'
292 @channel << description_el
294 description_el << feed_description
295 link = XML::Node.new 'link'
298 image = XML::Node.new 'image'
300 url = XML::Node.new 'url'
301 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
303 title = XML::Node.new 'title'
304 title << "OpenStreetMap"
306 width = XML::Node.new 'width'
309 height = XML::Node.new 'height'
312 link = XML::Node.new 'link'
317 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)
318 item = XML::Node.new 'item'
320 title = XML::Node.new 'title'
323 link = XML::Node.new 'link'
327 guid = XML::Node.new 'guid'
331 description = XML::Node.new 'description'
332 description << description_text
335 author = XML::Node.new 'author'
336 author << author_text
339 pubDate = XML::Node.new 'pubDate'
340 pubDate << timestamp.to_s(:rfc822)
344 lat_el = XML::Node.new 'geo:lat'
345 lat_el << latitude.to_s
350 lon_el = XML::Node.new 'geo:long'
351 lon_el << longitude.to_s
365 doc = XML::Document.new
366 doc.encoding = 'UTF-8'
367 root = XML::Node.new 'osm'
368 root['version'] = API_VERSION
369 root['generator'] = 'OpenStreetMap server'
375 def self.IPLocation(ip_address)
376 Timeout::timeout(4) do
377 Net::HTTP.start('api.hostip.info') do |http|
378 country = http.get("/country.php?ip=#{ip_address}").body
379 country = "GB" if country == "UK"
380 Net::HTTP.start('ws.geonames.org') do |http|
381 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
382 xml.elements.each("geonames/country") do |ele|
383 minlon = ele.get_text("bBoxWest").to_s
384 minlat = ele.get_text("bBoxSouth").to_s
385 maxlon = ele.get_text("bBoxEast").to_s
386 maxlat = ele.get_text("bBoxNorth").to_s
387 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
398 # Construct a random token of a given length
399 def self.make_token(length = 30)
400 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
404 token += chars[(rand * chars.length).to_i].chr
410 # Return an encrypted version of a password
411 def self.encrypt_password(password, salt)
412 return Digest::MD5.hexdigest(password) if salt.nil?
413 return Digest::MD5.hexdigest(salt + password)
416 # Return an SQL fragment to select a given area of the globe
417 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
418 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
419 minlat = (minlat * 10000000).round
420 minlon = (minlon * 10000000).round
421 maxlat = (maxlat * 10000000).round
422 maxlon = (maxlon * 10000000).round
424 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"