1 class AmfController < ApplicationController
5 # RAILS_DEFAULT_LOGGER.error("Args: #{args[0]}, #{args[1]}, #{args[2]}, #{args[3]}")
7 # ====================================================================
10 # ---- talk process AMF request
13 req=StringIO.new(request.raw_post) # Get POST data as request
14 req.read(2) # Skip version indicator and client ID
15 results={} # Results of each body
20 headers=getint(req) # Read number of headers
22 headers.times do # Read each header
23 name=getstring(req) # |
24 req.getc # | skip boolean
25 value=getvalue(req) # |
26 header["name"]=value # |
29 bodies=getint(req) # Read number of bodies
30 bodies.times do # Read each body
31 message=getstring(req) # | get message name
32 index=getstring(req) # | get index in response sequence
33 bytes=getlong(req) # | get total size in bytes
34 args=getvalue(req) # | get response (probably an array)
37 when 'getpresets'; results[index]=putdata(index,getpresets)
38 when 'whichways'; results[index]=putdata(index,whichways(args))
39 when 'getway'; results[index]=putdata(index,getway(args))
40 when 'putway'; results[index]=putdata(index,putway(args))
41 when 'deleteway'; results[index]=putdata(index,deleteway(args))
48 RAILS_DEFAULT_LOGGER.info(" Response: start")
49 a,b=results.length.divmod(256)
50 render :content_type => "application/x-amf", :text => proc { |response, output|
51 output.write 0.chr+0.chr+0.chr+0.chr+a.chr+b.chr
56 RAILS_DEFAULT_LOGGER.info(" Response: end")
62 # ====================================================================
66 # return presets,presetmenus and presetnames arrays
70 presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]
71 presetnames={}; presetnames['point']={}; presetnames['way']={}
75 RAILS_DEFAULT_LOGGER.info(" Message: getpresets")
77 # File.open("config/potlatch/presets.txt") do |file|
79 # Temporary patch to get around filepath problem
80 # To remove this patch and make the code nice again:
81 # 1. uncomment above line
82 # 2. fix the path in the above line
83 # 3. delete this here document, and the following line (StringIO....)
87 motorway: highway=motorway,ref=(type road number)
88 trunk road: highway=trunk,ref=(type road number),name=(type road name)
89 primary road: highway=primary,ref=(type road number),name=(type road name)
90 secondary road: highway=secondary,ref=(type road number),name=(type road name)
91 residential road: highway=residential,name=(type road name)
92 unclassified road: highway=unclassified,name=(type road name)
95 footpath: highway=footway,foot=yes
96 bridleway: highway=bridleway,foot=yes,horse=yes,bicycle=yes
97 byway: highway=byway,foot=yes,horse=yes,bicycle=yes,motorcar=yes
98 permissive path: highway=footway,foot=permissive
101 cycle lane: highway=cycleway,cycleway=lane,ncn_ref=
102 cycle track: highway=cycleway,cycleway=track,ncn_ref=
103 cycle lane (NCN): highway=cycleway,cycleway=lane,name=(type name here),ncn_ref=(type route number)
104 cycle track (NCN): highway=cycleway,cycleway=track,name=(type name here),ncn_ref=(type route number)
107 canal: waterway=canal,name=(type name here)
108 navigable river: waterway=river,boat=yes,name=(type name here)
109 navigable drain: waterway=drain,boat=yes,name=(type name here)
110 derelict canal: waterway=derelict_canal,name=(type name here)
111 unnavigable river: waterway=river,boat=no,name=(type name here)
112 unnavigable drain: waterway=drain,boat=no,name=(type name here)
115 railway: railway=rail
116 tramway: railway=tram
117 light railway: railway=light_rail
118 preserved railway: railway=preserved
119 disused railway tracks: railway=disused
120 course of old railway: railway=abandoned
123 mini roundabout: highway=mini_roundabout
124 traffic lights: highway=traffic_signals
127 bridge: highway=bridge
130 cattle grid: highway=cattle_grid
136 lock gate: waterway=lock_gate
138 aqueduct: waterway=aqueduct
139 winding hole: waterway=turning_point
140 mooring: waterway=mooring
143 station: railway=station
144 viaduct: railway=viaduct
145 level crossing: railway=crossing
148 StringIO.open(txt) do |file|
149 file.each_line {|line|
151 if (t=~/(\w+)\/(\w+)/) then
154 presetmenus[presettype].push(presetcategory)
155 presetnames[presettype][presetcategory]=["(no preset)"]
156 elsif (t=~/^(.+):\s?(.+)$/) then
158 presetnames[presettype][presetcategory].push(pre)
160 kv.split(',').each {|a|
161 if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
166 return [presets,presetmenus,presetnames]
169 # ----- whichways(left,bottom,right,top)
170 # return array of ways in current bounding box
171 # at present, instead of using correct (=more complex) SQL to find
172 # corner-crossing ways, it simply enlarges the bounding box by +/- 0.01
175 xmin = args[0].to_f-0.01
176 ymin = args[1].to_f-0.01
177 xmax = args[2].to_f+0.01
178 ymax = args[3].to_f+0.01
180 RAILS_DEFAULT_LOGGER.info(" Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
182 waylist=WaySegment.find_by_sql("SELECT DISTINCT current_way_segments.id AS wayid"+
183 " FROM current_way_segments,current_segments,current_nodes,current_ways "+
184 " WHERE segment_id=current_segments.id "+
185 " AND current_segments.visible=1 "+
186 " AND node_a=current_nodes.id "+
187 " AND current_ways.id=current_way_segments.id "+
188 " AND current_ways.visible=1 "+
189 " AND (latitude BETWEEN "+ymin.to_s+" AND "+ymax.to_s+") "+
190 " AND (longitude BETWEEN "+xmin.to_s+" AND "+xmax.to_s+")")
192 ways = waylist.collect {|a| a.wayid.to_i } # get an array of way id's
194 pointlist =ActiveRecord::Base.connection.select_all("SELECT current_nodes.id,current_nodes.tags "+
195 " FROM current_nodes "+
196 " LEFT OUTER JOIN current_segments cs1 ON cs1.node_a=current_nodes.id "+
197 " LEFT OUTER JOIN current_segments cs2 ON cs2.node_b=current_nodes.id "+
198 " WHERE (latitude BETWEEN "+ymin.to_s+" AND "+ymax.to_s+") "+
199 " AND (longitude BETWEEN "+xmin.to_s+" AND "+xmax.to_s+") "+
200 " AND cs1.id IS NULL AND cs2.id IS NULL "+
201 " AND current_nodes.visible=1")
203 points = pointlist.collect {|a| [a['id'],tag2array(a['tags'])] } # get a list of node ids and their tags
208 # ----- getway (objectname, way, baselong, basey, masterscale)
209 # returns objectname, array of co-ordinates, attributes,
210 # xmin,xmax,ymin,ymax
213 objname,wayid,baselong,basey,masterscale=args
218 xmax = ymax = -999999
220 RAILS_DEFAULT_LOGGER.info(" Message: getway, id=#{wayid}")
222 readwayquery(wayid).each {|row|
223 xs1=long2coord(row['long1'].to_f,baselong,masterscale); ys1=lat2coord(row['lat1'].to_f,basey,masterscale)
224 xs2=long2coord(row['long2'].to_f,baselong,masterscale); ys2=lat2coord(row['lat2'].to_f,basey,masterscale)
225 points << [xs1,ys1,row['id1'].to_i,0,tag2array(row['tags1']),0] if (row['id1'].to_i!=lastid)
226 lastid = row['id2'].to_i
227 points << [xs2,ys2,row['id2'].to_i,1,tag2array(row['tags2']),row['segment_id'].to_i]
228 xmin = [xmin,row['long1'].to_f,row['long2'].to_f].min
229 xmax = [xmax,row['long1'].to_f,row['long2'].to_f].max
230 ymin = [ymin,row['lat1'].to_f,row['lat2'].to_f].min
231 ymax = [ymax,row['lat1'].to_f,row['lat2'].to_f].max
235 attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
236 attrlist.each {|a| attributes[a['k']]=a['v'] }
238 [objname,points,attributes,xmin,xmax,ymin,ymax]
241 # ----- putway (user token, way, array of co-ordinates, array of attributes,
242 # baselong, basey, masterscale)
243 # returns current way ID, new way ID, hash of renumbered nodes,
244 # xmin,xmax,ymin,ymax
247 usertoken,originalway,points,attributes,baselong,basey,masterscale=args
248 uid=getuserid(usertoken)
250 db_uqs='uniq'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquesegments table name, typically 51 chars
251 db_uqn='unin'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
252 db_now='@now'+uid.to_s+originalway.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
253 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
254 originalway=originalway.to_i
256 RAILS_DEFAULT_LOGGER.info(" Message: putway, id=#{originalway}")
258 # -- 3. read original way into memory
260 xc={}; yc={}; tagc={}; seg={}
263 readwayquery(way).each { |row|
264 id1=row['id1'].to_i; xc[id1]=row['long1'].to_f; yc[id1]=row['lat1'].to_f; tagc[id1]=row['tags1']
265 id2=row['id2'].to_i; xc[id2]=row['long2'].to_f; yc[id2]=row['lat2'].to_f; tagc[id2]=row['tags2']
266 seg[row['segment_id'].to_i]=id1.to_s+'-'+id2.to_s
268 ActiveRecord::Base.connection.update("UPDATE current_ways SET timestamp=#{db_now},user_id=#{uid},visible=1 WHERE id=#{way}")
270 way=ActiveRecord::Base.connection.insert("INSERT INTO current_ways (user_id,timestamp,visible) VALUES (#{uid},#{db_now},1)")
273 # -- 4. get version by inserting new row into ways
275 version=ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},1)")
277 # -- 5. compare nodes and update xmin,xmax,ymin,ymax
280 xmax = ymax = -999999
284 points.each_index do |i|
285 xs=coord2long(points[i][0],masterscale,baselong)
286 ys=coord2lat(points[i][1],masterscale,basey)
287 xmin=[xs,xmin].min; xmax=[xs,xmax].max
288 ymin=[ys,ymin].min; ymax=[ys,ymax].max
289 node=points[i][2].to_i
290 tagstr=array2tag(points[i][4])
291 tagsql="'"+sqlescape(tagstr)+"'"
296 newnode=ActiveRecord::Base.connection.insert("INSERT INTO current_nodes ( latitude,longitude,timestamp,user_id,visible,tags) VALUES ( #{ys},#{xs},#{db_now},#{uid},1,#{tagsql})")
297 ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags) VALUES (#{newnode},#{ys},#{xs},#{db_now},#{uid},1,#{tagsql})")
299 renumberednodes[node.to_s]=newnode.to_s
301 elsif xc.has_key?(node)
302 # old node from original way - update
303 if (xs!=xc[node] or (ys/0.0000001).round!=(yc[node]/0.0000001).round or tagstr!=tagc[node])
304 ActiveRecord::Base.connection.insert("INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible,tags) VALUES (#{node},#{ys},#{xs},#{db_now},#{uid},1,#{tagsql})")
305 ActiveRecord::Base.connection.update("UPDATE current_nodes SET latitude=#{ys},longitude=#{xs},timestamp=#{db_now},user_id=#{uid},tags=#{tagsql},visible=1 WHERE id=#{node}")
308 # old node, created in another way and now added to this way
314 # -- 6.i compare segments
317 seglist='' # list of existing segments that we want to keep
318 for i in (0..(points.length-2))
319 if (points[i+1][3].to_i==0) then next end
320 segid=points[i+1][5].to_i
321 from =points[i ][2].to_i
322 to =points[i+1][2].to_i
323 if seg.has_key?(segid)
324 if seg[segid]=="#{from}-#{to}" then
325 if (seglist!='') then seglist+=',' end; seglist+=segid.to_s
329 segid=ActiveRecord::Base.connection.insert("INSERT INTO current_segments ( node_a,node_b,timestamp,user_id,visible,tags) VALUES ( #{from},#{to},#{db_now},#{uid},1,'')")
330 ActiveRecord::Base.connection.insert("INSERT INTO segments (id,node_a,node_b,timestamp,user_id,visible,tags) VALUES (#{segid},#{from},#{to},#{db_now},#{uid},1,'')")
332 numberedsegments[(i+1).to_s]=segid.to_s
334 # numberedsegments.each{|a,b| RAILS_DEFAULT_LOGGER.error("Sending back: seg no. #{a} -> id #{b}") }
337 # -- 6.ii insert new way segments
339 createuniquesegments(way,db_uqs,seglist) # segments which appear in this way but no other
341 # delete segments from uniquesegments (and not in modified way)
344 INSERT INTO segments (id,node_a,node_b,timestamp,user_id,visible)
345 SELECT DISTINCT segment_id,node_a,node_b,#{db_now},#{uid},0
346 FROM current_segments AS cs, #{db_uqs} AS us
347 WHERE cs.id=us.segment_id AND cs.visible=1
349 ActiveRecord::Base.connection.insert(sql)
352 UPDATE current_segments AS cs, #{db_uqs} AS us
353 SET cs.timestamp=#{db_now},cs.visible=0,cs.user_id=#{uid}
354 WHERE cs.id=us.segment_id AND cs.visible=1
356 ActiveRecord::Base.connection.update(sql)
358 # delete nodes not in modified way or any other segments
360 createuniquenodes(db_uqs,db_uqn) # nodes which appear in this way but no other
363 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible)
364 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0
365 FROM current_nodes AS cn,#{db_uqn}
368 ActiveRecord::Base.connection.insert(sql)
371 UPDATE current_nodes AS cn, #{db_uqn}
372 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
375 ActiveRecord::Base.connection.update(sql)
377 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqs}")
378 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
380 # insert new version of route into way_segments
385 for i in (0..(points.length-2))
386 if (points[i+1][3].to_i==0) then next end
387 if insertsql !='' then insertsql +=',' end
388 if currentsql!='' then currentsql+=',' end
389 insertsql +="(#{way},#{points[i+1][5]},#{version})"
390 currentsql+="(#{way},#{points[i+1][5]},#{sequence})"
394 ActiveRecord::Base.connection.execute("DELETE FROM current_way_segments WHERE id=#{way}");
395 ActiveRecord::Base.connection.insert("INSERT INTO way_segments (id,segment_id,version ) VALUES #{insertsql}");
396 ActiveRecord::Base.connection.insert("INSERT INTO current_way_segments (id,segment_id,sequence_id) VALUES #{currentsql}");
398 # -- 7. insert new way tags
402 attributes.each do |k,v|
403 if v=='' then next end
404 if v[0,6]=='(type ' then next end
405 if insertsql !='' then insertsql +=',' end
406 if currentsql!='' then currentsql+=',' end
407 insertsql +="(#{way},'"+sqlescape(k)+"','"+sqlescape(v)+"',#{version})"
408 currentsql+="(#{way},'"+sqlescape(k)+"','"+sqlescape(v)+"')"
411 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
412 if (insertsql !='') then ActiveRecord::Base.connection.insert("INSERT INTO way_tags (id,k,v,version) VALUES #{insertsql}" ) end
413 if (currentsql!='') then ActiveRecord::Base.connection.insert("INSERT INTO current_way_tags (id,k,v) VALUES #{currentsql}") end
415 [originalway,way,renumberednodes,numberedsegments,xmin,xmax,ymin,ymax]
418 # ----- deleteway (user token, way)
419 # returns way ID only
424 RAILS_DEFAULT_LOGGER.info(" Message: deleteway, id=#{way}")
426 uid=getuserid(usertoken); if !uid then return end
429 db_uqs='uniq'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquesegments table name, typically 51 chars
430 db_uqn='unin'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # temp uniquenodes table name, typically 51 chars
431 db_now='@now'+uid.to_s+way.to_i.abs.to_s+Time.new.to_i.to_s # 'now' variable name, typically 51 chars
432 ActiveRecord::Base.connection.execute("SET #{db_now}=NOW()")
433 createuniquesegments(way,db_uqs,'')
435 # - delete any otherwise unused segments
438 INSERT INTO segments (id,node_a,node_b,timestamp,user_id,visible)
439 SELECT DISTINCT segment_id,node_a,node_b,#{db_now},#{uid},0
440 FROM current_segments AS cs, #{db_uqs} AS us
441 WHERE cs.id=us.segment_id
443 ActiveRecord::Base.connection.insert(sql)
446 UPDATE current_segments AS cs, #{db_uqs} AS us
447 SET cs.timestamp=#{db_now},cs.visible=0,cs.user_id=#{uid}
448 WHERE cs.id=us.segment_id
450 ActiveRecord::Base.connection.update(sql)
452 # - delete any unused nodes
454 createuniquenodes(db_uqs,db_uqn)
457 INSERT INTO nodes (id,latitude,longitude,timestamp,user_id,visible)
458 SELECT DISTINCT cn.id,cn.latitude,cn.longitude,#{db_now},#{uid},0
459 FROM current_nodes AS cn,#{db_uqn}
462 ActiveRecord::Base.connection.insert(sql)
465 UPDATE current_nodes AS cn, #{db_uqn}
466 SET cn.timestamp=#{db_now},cn.visible=0,cn.user_id=#{uid}
469 ActiveRecord::Base.connection.update(sql)
471 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqs}")
472 ActiveRecord::Base.connection.execute("DROP TABLE #{db_uqn}")
476 ActiveRecord::Base.connection.insert("INSERT INTO ways (id,user_id,timestamp,visible) VALUES (#{way},#{uid},#{db_now},0)")
477 ActiveRecord::Base.connection.update("UPDATE current_ways SET user_id=#{uid},timestamp=#{db_now},visible=0 WHERE id=#{way}")
478 ActiveRecord::Base.connection.execute("DELETE FROM current_way_segments WHERE id=#{way}")
479 ActiveRecord::Base.connection.execute("DELETE FROM current_way_tags WHERE id=#{way}")
484 # ====================================================================
485 # Support functions for remote calls
488 ActiveRecord::Base.connection.select_all "SELECT n1.latitude AS lat1,n1.longitude AS long1,n1.id AS id1,n1.tags as tags1, "+
489 " n2.latitude AS lat2,n2.longitude AS long2,n2.id AS id2,n2.tags as tags2,segment_id "+
490 " FROM current_way_segments,current_segments,current_nodes AS n1,current_nodes AS n2 "+
491 " WHERE current_way_segments.id=#{id} "+
492 " AND segment_id=current_segments.id "+
493 " AND current_segments.visible=1 "+
494 " AND n1.id=node_a and n2.id=node_b "+
495 " AND n1.visible=1 AND n2.visible=1 "+
496 " ORDER BY sequence_id"
499 def createuniquesegments(way,uqs_name,seglist)
500 # Finds segments which appear in (previous version of) this way and no other
502 CREATE TEMPORARY TABLE #{uqs_name}
504 FROM (SELECT DISTINCT segment_id FROM current_way_segments
506 LEFT JOIN current_way_segments b
507 ON b.segment_id = a.segment_id
509 WHERE b.segment_id IS NULL
511 if (seglist!='') then sql+=" AND a.segment_id NOT IN (#{seglist})" end
512 ActiveRecord::Base.connection.execute(sql)
515 def createuniquenodes(uqs_name,uqn_name)
516 # Finds nodes which appear in uniquesegments but no other segments
518 CREATE TEMPORARY TABLE #{uqn_name}
519 SELECT DISTINCT node_id
520 FROM (SELECT cn.id AS node_id
521 FROM current_nodes AS cn,
522 current_segments AS cs,
524 WHERE cs.id=us.segment_id
525 AND (cn.id=cs.node_a OR cn.id=cs.node_b)) AS n
526 LEFT JOIN current_segments AS cs2 ON node_id=cs2.node_a AND cs2.visible=1
527 LEFT JOIN current_segments AS cs3 ON node_id=cs3.node_b AND cs3.visible=1
528 WHERE cs2.node_a IS NULL
529 AND cs3.node_b IS NULL
531 ActiveRecord::Base.connection.execute(sql)
535 a.gsub("'","''").gsub(92.chr,92.chr+92.chr)
540 a.gsub(';;;','#%').split(';').each do |b|
544 if k.nil? then k='' end
545 if v.nil? then v='' end
546 tags[k.gsub('#%','=')]=v.gsub('#%','=')
554 if v=='' then next end
555 if v[0,6]=='(type ' then next end
556 if str!='' then str+=';' end
557 str+=k.gsub(';',';;;').gsub('=','===')+'='+v.gsub(';',';;;').gsub('=','===')
563 token=sqlescape(token)
564 if (token=~/^(.+)\+(.+)$/) then
565 return ActiveRecord::Base.connection.select_value("SELECT id FROM users WHERE active=1 AND email='#{$1}' AND pass_crypt=MD5('#{$2}')")
567 return ActiveRecord::Base.connection.select_value("SELECT id FROM users WHERE active=1 AND token='#{token}'")
573 # ====================================================================
574 # AMF read subroutines
576 # ----- getint return two-byte integer
577 # ----- getlong return four-byte long
578 # ----- getstring return string with two-byte length
579 # ----- getdouble return eight-byte double-precision float
580 # ----- getobject return object/hash
581 # ----- getarray return numeric array
588 ((s.getc*256+s.getc)*256+s.getc)*256+s.getc
592 len=s.getc*256+s.getc
597 a=s.read(8).unpack('G') # G big-endian, E little-endian
612 while (key=getstring(s))
613 if (key=='') then break end
616 s.getc # skip the 9 'end of object' value
620 # ----- getvalue parse and get value
624 when 0; return getdouble(s) # number
625 when 1; return s.getc # boolean
626 when 2; return getstring(s) # string
627 when 3; return getobject(s) # object/hash
628 when 5; return nil # null
629 when 6; return nil # undefined
630 when 8; s.read(4) # mixedArray
631 return getobject(s) # |
632 when 10;return getarray(s) # array
633 else; return nil # error
637 # ====================================================================
638 # AMF write subroutines
640 # ----- putdata envelope data into AMF writeable form
641 # ----- encodevalue pack variables as AMF
644 d =encodestring(index+"/onResult")
645 d+=encodestring("null")
653 a=10.chr+encodelong(n.length)
661 a+=encodestring(k)+encodevalue(v)
665 2.chr+encodestring(n)
666 when 'Bignum','Fixnum','Float'
667 0.chr+encodedouble(n)
671 RAILS_DEFAULT_LOGGER.error("Unexpected Ruby type for AMF conversion: "+n.class.to_s)
675 # ----- encodestring encode string with two-byte length
676 # ----- encodedouble encode number as eight-byte double precision float
677 # ----- encodelong encode number as four-byte long
680 a,b=n.size.divmod(256)
692 # ====================================================================
693 # Co-ordinate conversion
695 def lat2coord(a,basey,masterscale)
696 -(lat2y(a)-basey)*masterscale+250
699 def long2coord(a,baselong,masterscale)
700 (a-baselong)*masterscale+350
704 180/Math::PI * Math.log(Math.tan(Math::PI/4+a*(Math::PI/180)/2))
707 def coord2lat(a,masterscale,basey)
708 y2lat((a-250)/-masterscale+basey)
711 def coord2long(a,masterscale,baselong)
712 (a-350)/masterscale+baselong
716 180/Math::PI * (2*Math.atan(Math.exp(a*Math::PI/180))-Math::PI/2)