1 | #!/usr/bin/ruby |
---|
2 | |
---|
3 | # ---------------------------------------------------------------- |
---|
4 | # getgps.cgi |
---|
5 | # GPX responder for Potlatch Openstreetmap editor |
---|
6 | # Ruby/Ming version |
---|
7 | # === now superseded by pure Ruby version === |
---|
8 | # |
---|
9 | # editions Systeme D / Richard Fairhurst 2006 |
---|
10 | # public domain |
---|
11 | # ---------------------------------------------------------------- |
---|
12 | |
---|
13 | require 'rubygems' |
---|
14 | require 'web' |
---|
15 | require 'mysql' |
---|
16 | |
---|
17 | require 'ming/ming' |
---|
18 | include Ming |
---|
19 | |
---|
20 | def lat2coord(a) ; -(lat2y(a)-$basey)*$masterscale+250; end |
---|
21 | def long2coord(a); (a-$baselong)*$masterscale+350; end |
---|
22 | def lat2y(a) ; 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2)); end |
---|
23 | |
---|
24 | # ==================================================================== |
---|
25 | # Main code |
---|
26 | |
---|
27 | # ----- Initialise |
---|
28 | |
---|
29 | Ming.set_scale(20.0) |
---|
30 | Ming.use_SWF_version(6) |
---|
31 | dbh=Mysql.real_connect("localhost","db_user","db_pass","osm") |
---|
32 | |
---|
33 | m=SWFMovie.new |
---|
34 | |
---|
35 | $baselong =Web['baselong'].to_f |
---|
36 | $basey =Web['basey'].to_f |
---|
37 | $masterscale=Web['masterscale'].to_f |
---|
38 | |
---|
39 | if (Web::key?('user')) |
---|
40 | user=Web['user'].to_i |
---|
41 | else |
---|
42 | user=-1 |
---|
43 | end |
---|
44 | |
---|
45 | # ----- Start Flash line |
---|
46 | |
---|
47 | s=SWFShape.new |
---|
48 | s.set_line(1,0,255,255,255) |
---|
49 | |
---|
50 | # ----- Send SQL and draw line |
---|
51 | |
---|
52 | lasttime=0 |
---|
53 | lastfile=-1 |
---|
54 | xmin=Web['xmin'].to_f/0.0000001; xmax=Web['xmax'].to_f/0.0000001 |
---|
55 | ymin=Web['ymin'].to_f/0.0000001; ymax=Web['ymax'].to_f/0.0000001 |
---|
56 | if Web::key?('token') |
---|
57 | token=dbh.escape_string(Web['token']) |
---|
58 | sql="SELECT gps_points.latitude,gps_points.longitude,gpx_files.id AS fileid,UNIX_TIMESTAMP(gps_points.timestamp) AS ts "+ |
---|
59 | " FROM gpx_files,gps_points,users "+ |
---|
60 | "WHERE gpx_files.id=gpx_id "+ |
---|
61 | " AND gpx_files.user_id=users.id "+ |
---|
62 | " AND token='#{token}' "+ |
---|
63 | " AND (gps_points.longitude BETWEEN #{xmin} AND #{xmax}) "+ |
---|
64 | " AND (gps_points.latitude BETWEEN #{ymin} AND #{ymax}) "+ |
---|
65 | "ORDER BY fileid,ts" |
---|
66 | else |
---|
67 | sql="SELECT latitude,longitude,gpx_id,UNIX_TIMESTAMP(timestamp) AS ts "+ |
---|
68 | " FROM gps_points "+ |
---|
69 | "WHERE (longitude BETWEEN #{xmin} AND #{xmax}) "+ |
---|
70 | " AND (latitude BETWEEN #{ymin} AND #{ymax}) "+ |
---|
71 | "ORDER BY gpx_id,ts" |
---|
72 | end |
---|
73 | |
---|
74 | dbr=dbh.query(sql) |
---|
75 | while row=dbr.fetch_row do |
---|
76 | xs=long2coord(row[1].to_f*0.0000001) |
---|
77 | ys=lat2coord(row[0].to_f*0.0000001) |
---|
78 | if (row[3].to_i-lasttime>180 or row[2].to_i!=lastfile) |
---|
79 | s.move_pen_to(xs,ys) |
---|
80 | else |
---|
81 | s.draw_line_to(xs,ys) |
---|
82 | end |
---|
83 | lasttime=row[3].to_i |
---|
84 | lastfile=row[2].to_i |
---|
85 | end |
---|
86 | dbr.free |
---|
87 | |
---|
88 | m.add(s) |
---|
89 | |
---|
90 | |
---|
91 | #Ê================================================================= |
---|
92 | # Output file |
---|
93 | |
---|
94 | m.next_frame |
---|
95 | filename='/tmp/swf_'+rand(65535).to_s+'.swf' |
---|
96 | m.save(filename) |
---|
97 | |
---|
98 | Web::open do |connection| |
---|
99 | Web::content_type="application/x-shockwave-flash" |
---|
100 | Web::send_file(filename) |
---|
101 | end |
---|
102 | |
---|
103 | File.delete(filename) |
---|
104 | dbh.close |
---|