Guest User

Untitled

a guest
Jul 17th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. require "#{File.dirname(__FILE__)}/../../../lib/xml_processor/trip_xml_processor"
  2. require 'geokit'
  3.  
  4. class RouteTrip
  5. include Nanite::Actor
  6. expose :geocode, :process_uploaded_trip, :generate_static_maps
  7.  
  8. def process_uploaded_trip(args)
  9. xp = TripXmlProcessor.new
  10. klass, ids = xp.start_processing(args)
  11.  
  12. if klass.nil?
  13. #something bum happened while processing, handle it
  14. else
  15. for id in ids
  16. Nanite.push('/routes_trips/geocode', [klass.to_s, id])
  17. Nanite.push('/routes_trips/generate_static_maps', [klass.to_s, id] )
  18. end
  19. end
  20. ensure
  21. ActiveRecord::Base.clear_active_connections!
  22. end
  23.  
  24. def geocode(payload)
  25. klass_name, id = payload
  26. klass = klass_name.constantize
  27. asset = klass.find(id)
  28. asset = asset.instance_of?(Trip) ? asset.route : asset
  29. reverse_geocode_asset_helper(asset)
  30. ensure
  31. ActiveRecord::Base.clear_active_connections!
  32. end
  33.  
  34. # payload, json: ['trip|route', id]
  35. def generate_static_maps(payload)
  36. klass_name, id = payload
  37. klass = klass_name.constantize
  38. ar_obj = klass.find(id)
  39.  
  40. response = []
  41.  
  42. data = ar_obj.data_full
  43. if data.nil? or data.size < 10
  44. puts "#{klass_name} ##{ar_obj.id} has bum data_full: #{data}"
  45. else
  46. mapper = StaticMapper.new(JSON.parse(data))
  47.  
  48. Setting.routes.sizes.each do |preset, obj|
  49. width, height = obj.geometry.split('x').collect{|str| str.to_i}
  50.  
  51. path = klass.static_image_path(ar_obj.id, preset)
  52.  
  53. if File.exists?(path)
  54. puts "#{path} already exists"
  55. next
  56. end
  57.  
  58. begin
  59. puts "#{klass.to_s} generating #{ar_obj.id} #{preset}"
  60. img = mapper.draw_map(width, height)
  61. img.write(path)
  62. rescue Exception => error
  63. puts error
  64. end
  65. end
  66. end
  67. ensure
  68. ActiveRecord::Base.clear_active_connections!
  69. end
  70.  
  71. ## put this somewhere else
  72. include Geokit::Geocoders
  73. def reverse_geocode_asset_helper(asset)
  74. puts "Reverse geocoding ##{asset.class} ##{asset.id}"
  75. lat, lng = nil, nil
  76. if asset.instance_of?(Route)
  77. lat, lng = asset.first_lat, asset.first_lng
  78. elsif asset.instance_of?(User)
  79. lat, lng = asset.lat, asset.lng
  80. else
  81. puts "\tIt's not a route or a user...skipping"
  82. return #it aint a route or a user
  83. end
  84. unless lat and lng
  85. puts "\tno lat and lng found...wtf?"
  86. return
  87. end
  88.  
  89. res = GoogleGeocoder.reverse_geocode([lat, lng])
  90. puts "\tGoogle returned #{res.all.size} results..."
  91. if res.all.size > 0
  92. best_accuracy = -10
  93. best = nil
  94. res.all.each do |r|
  95. if r.accuracy and r.accuracy > best_accuracy
  96. best = r
  97. best_accuracy = r.accuracy
  98. end
  99. end
  100. if best == nil
  101. puts "\tWTF, no decent result even with #{res.all.size} results??"
  102. return
  103. end
  104.  
  105. asset.locality = best.city if best.city
  106. asset.administrative_area = best.state if best.state
  107. asset.postal_code = best.zip if best.zip
  108. asset.country_code = best.country_code ? best.country_code : ''
  109. puts "\tRad, got #{asset.locality}, #{asset.administrative_area}, #{asset.postal_code} in #{asset.country_code}"
  110.  
  111. if asset.save
  112. puts "\tSuccess! Saved it."
  113. else
  114. puts "\tFAILED to save, errors: #{asset.errors.inspect}"
  115. end
  116. end
  117. end
  118. end
Add Comment
Please, Sign In to add comment