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 #init me with your bounding box and the size of your image
23 def initialize(min_lat, min_lon, max_lat, max_lon, width, height)
24 xsize = xsheet(max_lon) - xsheet(min_lon)
25 ysize = ysheet(max_lat) - ysheet(min_lat)
26 xscale = xsize / width
27 yscale = ysize / height
28 scale = [xscale, yscale].max
30 xpad = width * scale - xsize
31 ypad = height * scale - ysize
36 @tx = xsheet(min_lon) - xpad / 2
37 @ty = ysheet(min_lat) - ypad / 2
39 @bx = xsheet(max_lon) + xpad / 2
40 @by = ysheet(max_lat) + ypad / 2
43 #the following two functions will give you the x/y on the entire sheet
46 log(tan(PI / 4 + (lat * PI / 180 / 2))) / (PI / 180)
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)
83 date = DateTime.now();
88 parser = REXML::Parsers::SAX2Parser.new(File.new(@filename))
90 parser.listen( :start_element, %w{ trkpt }) do |uri,localname,qname,attributes|
91 lat = attributes['lat'].to_f
92 lon = attributes['lon'].to_f
99 parser.listen( :characters, %w{ ele } ) do |text|
104 parser.listen( :characters, %w{ time } ) do |text|
105 if text && text != ''
107 date = DateTime.parse(text)
114 parser.listen( :end_element, %w{ trkseg } ) do |uri, localname, qname|
118 parser.listen( :end_element, %w{ trkpt } ) do |uri,localname,qname|
119 if gotlatlon && gotdate
120 ele = '0' unless gotele
121 if lat < 90 && lat > -90 && lon > -180 && lon < 180
123 yield Hash['latitude' => lat, 'longitude' => lon, 'timestamp' => date, 'altitude' => ele, 'segment' => @tracksegs]
134 def get_picture(min_lat, min_lon, max_lat, max_lon, num_points)
135 #puts "getting picfor bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
139 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, 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 proj = OSM::Mercator.new(min_lat, min_lon, max_lat, max_lon, width, height)
216 gc = Magick::Draw.new
217 gc.stroke_linejoin('miter')
222 image = Magick::Image.new(width, height) do |image|
223 image.background_color = 'white'
233 px = proj.x(p['longitude'])
234 py = proj.y(p['latitude'])
236 gc.dup.line(px, py, oldpx, oldpy).draw(image) unless first
251 # initialise with a base position
252 def initialize(lat, lon)
253 @lat = lat * PI / 180
254 @lon = lon * PI / 180
257 # get the distance from the base position to a given position
258 def distance(lat, lon)
261 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
264 # get the worst case bounds for a given radius from the base position
266 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
267 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
268 minlat = (@lat - latradius) * 180 / PI
269 maxlat = (@lat + latradius) * 180 / PI
270 minlon = (@lon - lonradius) * 180 / PI
271 maxlon = (@lon + lonradius) * 180 / PI
272 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
277 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
278 @doc = XML::Document.new
279 @doc.encoding = 'UTF-8'
281 rss = XML::Node.new 'rss'
283 rss['version'] = "2.0"
284 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
285 @channel = XML::Node.new 'channel'
287 title = XML::Node.new 'title'
290 description_el = XML::Node.new 'description'
291 @channel << description_el
293 description_el << feed_description
294 link = XML::Node.new 'link'
297 image = XML::Node.new 'image'
299 url = XML::Node.new 'url'
300 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
302 title = XML::Node.new 'title'
303 title << "OpenStreetMap"
305 width = XML::Node.new 'width'
308 height = XML::Node.new 'height'
311 link = XML::Node.new 'link'
316 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)
317 item = XML::Node.new 'item'
319 title = XML::Node.new 'title'
322 link = XML::Node.new 'link'
326 guid = XML::Node.new 'guid'
330 description = XML::Node.new 'description'
331 description << description_text
334 author = XML::Node.new 'author'
335 author << author_text
338 pubDate = XML::Node.new 'pubDate'
339 pubDate << timestamp.to_s(:rfc822)
343 lat_el = XML::Node.new 'geo:lat'
344 lat_el << latitude.to_s
349 lon_el = XML::Node.new 'geo:long'
350 lon_el << longitude.to_s
364 doc = XML::Document.new
365 doc.encoding = 'UTF-8'
366 root = XML::Node.new 'osm'
367 root['version'] = API_VERSION
368 root['generator'] = 'OpenStreetMap server'
374 def self.IPLocation(ip_address)
375 Timeout::timeout(4) do
376 Net::HTTP.start('api.hostip.info') do |http|
377 country = http.get("/country.php?ip=#{ip_address}").body
378 country = "GB" if country == "UK"
379 Net::HTTP.start('ws.geonames.org') do |http|
380 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
381 xml.elements.each("geonames/country") do |ele|
382 minlon = ele.get_text("bBoxWest").to_s
383 minlat = ele.get_text("bBoxSouth").to_s
384 maxlon = ele.get_text("bBoxEast").to_s
385 maxlat = ele.get_text("bBoxNorth").to_s
386 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
397 # Construct a random token of a given length
398 def self.make_token(length = 30)
399 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
403 token += chars[(rand * chars.length).to_i].chr
409 # Return an encrypted version of a password
410 def self.encrypt_password(password, salt)
411 return Digest::MD5.hexdigest(password) if salt.nil?
412 return Digest::MD5.hexdigest(salt + password)
415 # Return an SQL fragment to select a given area of the globe
416 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
417 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
418 minlat = (minlat * 10000000).round
419 minlon = (minlon * 10000000).round
420 maxlat = (maxlat * 10000000).round
421 maxlon = (maxlon * 10000000).round
423 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"