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)
144 gc = Magick::Draw.new
145 gc.stroke_linejoin('miter')
148 gc.rectangle(0,0,width,height)
161 px = proj.x(p['longitude'])
162 py = proj.y(p['latitude'])
164 images[n].stroke_width(1)
165 images[n].stroke('#BBBBBB')
166 images[n].fill('#BBBBBB')
167 # puts "A #{px},#{py} - #{oldpx},#{oldpy}"
168 images[n].line(px, py, oldpx, oldpy ) unless first
170 images[mm].stroke_width(3)
171 images[mm].stroke('#000000')
172 images[mm].fill('#000000')
173 images[mm].line(px, py, oldpx, oldpy ) unless first
174 # puts "B #{px},#{py} - #{oldpx},#{oldpy}"
176 if m > num_points.to_f / frames.to_f * (mm+1)
184 il = Magick::ImageList.new
187 canvas = Magick::Image.new(width, height) {
188 self.background_color = 'white'
191 images[n].draw(canvas)
194 canvas.format = 'GIF'
203 def get_icon(min_lat, min_lon, max_lat, max_lon)
204 #puts "getting icon for bbox #{min_lat},#{min_lon} - #{max_lat},#{max_lon}"
207 rat= Math.cos( ((max_lat + min_lat)/2.0) / 180.0 * 3.141592)
208 proj = OSM::Mercator.new((min_lat + max_lat) / 2, (max_lon + min_lon) / 2, (max_lat - min_lat) / width / rat, width, height)
212 gc = Magick::Draw.new
213 gc.stroke_linejoin('miter')
225 px = proj.x(p['longitude'])
226 py = proj.y(p['latitude'])
227 gc.line(px, py, oldpx, oldpy ) unless first
228 # puts "C #{px},#{py} - #{oldpx},#{oldpy}"
234 canvas = Magick::Image.new(width, height) {
235 self.background_color = 'white'
241 canvas.format = 'GIF'
242 return canvas.to_blob
250 # initialise with a base position
251 def initialize(lat, lon)
252 @lat = lat * PI / 180
253 @lon = lon * PI / 180
256 # get the distance from the base position to a given position
257 def distance(lat, lon)
260 return 6372.795 * 2 * asin(sqrt(sin((lat - @lat) / 2) ** 2 + cos(@lat) * cos(lat) * sin((lon - @lon)/2) ** 2))
263 # get the worst case bounds for a given radius from the base position
265 latradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2))
266 lonradius = 2 * asin(sqrt(sin(radius / 6372.795 / 2) ** 2 / cos(@lat) ** 2))
267 minlat = (@lat - latradius) * 180 / PI
268 maxlat = (@lat + latradius) * 180 / PI
269 minlon = (@lon - lonradius) * 180 / PI
270 maxlon = (@lon + lonradius) * 180 / PI
271 return { :minlat => minlat, :maxlat => maxlat, :minlon => minlon, :maxlon => maxlon }
276 def initialize(feed_title='OpenStreetMap GPS Traces', feed_description='OpenStreetMap GPS Traces', feed_url='http://www.openstreetmap.org/traces/')
277 @doc = XML::Document.new
278 @doc.encoding = 'UTF-8'
280 rss = XML::Node.new 'rss'
282 rss['version'] = "2.0"
283 rss['xmlns:geo'] = "http://www.w3.org/2003/01/geo/wgs84_pos#"
284 @channel = XML::Node.new 'channel'
286 title = XML::Node.new 'title'
289 description_el = XML::Node.new 'description'
290 @channel << description_el
292 description_el << feed_description
293 link = XML::Node.new 'link'
296 image = XML::Node.new 'image'
298 url = XML::Node.new 'url'
299 url << 'http://www.openstreetmap.org/images/mag_map-rss2.0.png'
301 title = XML::Node.new 'title'
302 title << "OpenStreetMap"
304 width = XML::Node.new 'width'
307 height = XML::Node.new 'height'
310 link = XML::Node.new 'link'
315 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)
316 item = XML::Node.new 'item'
318 title = XML::Node.new 'title'
321 link = XML::Node.new 'link'
325 guid = XML::Node.new 'guid'
329 description = XML::Node.new 'description'
330 description << description_text
333 author = XML::Node.new 'author'
334 author << author_text
337 pubDate = XML::Node.new 'pubDate'
338 pubDate << timestamp.to_s(:rfc822)
342 lat_el = XML::Node.new 'geo:lat'
343 lat_el << latitude.to_s
348 lon_el = XML::Node.new 'geo:long'
349 lon_el << longitude.to_s
363 doc = XML::Document.new
364 doc.encoding = 'UTF-8'
365 root = XML::Node.new 'osm'
366 root['version'] = API_VERSION
367 root['generator'] = 'OpenStreetMap server'
373 def self.IPLocation(ip_address)
374 Timeout::timeout(4) do
375 Net::HTTP.start('api.hostip.info') do |http|
376 country = http.get("/country.php?ip=#{ip_address}").body
377 country = "GB" if country = "UK"
378 Net::HTTP.start('ws.geonames.org') do |http|
379 xml = REXML::Document.new(http.get("/countryInfo?country=#{country}").body)
380 xml.elements.each("geonames/country") do |ele|
381 minlon = ele.get_text("bBoxWest").to_s
382 minlat = ele.get_text("bBoxSouth").to_s
383 maxlon = ele.get_text("bBoxEast").to_s
384 maxlat = ele.get_text("bBoxNorth").to_s
385 return { :minlon => minlon, :minlat => minlat, :maxlon => maxlon, :maxlat => maxlat }
396 # Construct a random token of a given length
397 def self.make_token(length = 30)
398 chars = 'abcdefghijklmnopqrtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
402 token += chars[(rand * chars.length).to_i].chr
408 # Return an encrypted version of a password
409 def self.encrypt_password(password, salt)
410 return Digest::MD5.hexdigest(password) if salt.nil?
411 return Digest::MD5.hexdigest(salt + password)
414 # Return an SQL fragment to select a given area of the globe
415 def self.sql_for_area(minlat, minlon, maxlat, maxlon, prefix = nil)
416 tilesql = QuadTile.sql_for_area(minlat, minlon, maxlat, maxlon, prefix)
417 minlat = (minlat * 1000000).round
418 minlon = (minlon * 1000000).round
419 maxlat = (maxlat * 1000000).round
420 maxlon = (maxlon * 1000000).round
422 return "#{tilesql} AND #{prefix}latitude BETWEEN #{minlat} AND #{maxlat} AND #{prefix}longitude BETWEEN #{minlon} AND #{maxlon}"