Add geopo to timing points master
[bus.git] / maxious-canberra-transit-feed / 04-generateymlinclude.rb
blob:a/maxious-canberra-transit-feed/04-generateymlinclude.rb -> blob:b/maxious-canberra-transit-feed/04-generateymlinclude.rb
--- a/maxious-canberra-transit-feed/04-generateymlinclude.rb
+++ b/maxious-canberra-transit-feed/04-generateymlinclude.rb
@@ -2,6 +2,29 @@
 
 require 'highline.rb'
 include HighLine
+
+#
+# GeoPo Encode in Ruby
+# @author : Shintaro Inagaki
+# @param location (Hash) [lat (Float), lng (Float), scale(Int)]
+# @return geopo (String)
+#
+def geopoEncode(lat, lng, scale)
+	# 64characters (number + big and small letter + hyphen + underscore)
+	chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
+	
+	geopo = ""
+	
+	# Change a degree measure to a decimal number
+	lat = (lat + 90.0) / 180 * 8 ** 10; # 90.0 is forced FLOAT type when lat is INT
+	lng = (lng + 180.0) / 360 * 8 ** 10; # 180.0 is same
+
+	# Compute a GeoPo code from head and concatenate
+	for i in 0..scale
+		geopo += chars[(lat / 8 ** (9 - i) % 8).floor + (lng / 8 ** (9 - i) % 8).floor * 8, 1];
+	end
+	return geopo;
+end
 
 require 'rubygems'
 require 'postgres'
@@ -31,7 +54,7 @@
 		# 2 = lng*100000
 		# 7 = suburb(s)
 		#pp time_point[0]
-		f2.puts "  - { name: #{time_point[0]},stop_code: #{time_point[0]}, lat: #{Float(time_point[1])/10000000}, lng: #{Float(time_point[2])/10000000}, zone_id: #{time_point[7]} }"
+		f2.puts "  - { name: #{time_point[0]},stop_code: #{time_point[0]}, lat: #{Float(time_point[1])/10000000}, lng: #{Float(time_point[2])/10000000}, zone_id: #{geopoEncode(Float(time_point[1])/10000000,Float(time_point[2])/10000000,7)}@#{time_point[7]} }"
 	end
 	begin
 		stops = connbus.exec("SELECT * from stops")
@@ -47,7 +70,7 @@
 		# 3 = name
 		# 4 = suburb(s)
 		#pp time_point[0]
-		f2.puts "  - { name: #{stop[3]},stop_code: #{stop[0]}, lat: #{Float(stop[1])/10000000}, lng: #{Float(stop[2])/10000000}, zone_id: #{stop[4]} }"
+		f2.puts "  - { name: #{stop[3]},stop_code: #{stop[0]}, lat: #{Float(stop[1])/10000000}, lng: #{Float(stop[2])/10000000}, zone_id: #{stop[0]}@#{stop[4]} }"
 	end
 	f2.puts "routes:\n";
 end