Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require "#{File.dirname(__FILE__)}/../../../lib/xml_processor/trip_xml_processor"
- require 'geokit'
- class RouteTrip
- include Nanite::Actor
- expose :geocode, :process_uploaded_trip, :generate_static_maps
- def process_uploaded_trip(args)
- xp = TripXmlProcessor.new
- klass, ids = xp.start_processing(args)
- if klass.nil?
- #something bum happened while processing, handle it
- else
- for id in ids
- Nanite.push('/routes_trips/geocode', [klass.to_s, id])
- Nanite.push('/routes_trips/generate_static_maps', [klass.to_s, id] )
- end
- end
- ensure
- ActiveRecord::Base.clear_active_connections!
- end
- def geocode(payload)
- klass_name, id = payload
- klass = klass_name.constantize
- asset = klass.find(id)
- asset = asset.instance_of?(Trip) ? asset.route : asset
- reverse_geocode_asset_helper(asset)
- ensure
- ActiveRecord::Base.clear_active_connections!
- end
- # payload, json: ['trip|route', id]
- def generate_static_maps(payload)
- klass_name, id = payload
- klass = klass_name.constantize
- ar_obj = klass.find(id)
- response = []
- data = ar_obj.data_full
- if data.nil? or data.size < 10
- puts "#{klass_name} ##{ar_obj.id} has bum data_full: #{data}"
- else
- mapper = StaticMapper.new(JSON.parse(data))
- Setting.routes.sizes.each do |preset, obj|
- width, height = obj.geometry.split('x').collect{|str| str.to_i}
- path = klass.static_image_path(ar_obj.id, preset)
- if File.exists?(path)
- puts "#{path} already exists"
- next
- end
- begin
- puts "#{klass.to_s} generating #{ar_obj.id} #{preset}"
- img = mapper.draw_map(width, height)
- img.write(path)
- rescue Exception => error
- puts error
- end
- end
- end
- ensure
- ActiveRecord::Base.clear_active_connections!
- end
- ## put this somewhere else
- include Geokit::Geocoders
- def reverse_geocode_asset_helper(asset)
- puts "Reverse geocoding ##{asset.class} ##{asset.id}"
- lat, lng = nil, nil
- if asset.instance_of?(Route)
- lat, lng = asset.first_lat, asset.first_lng
- elsif asset.instance_of?(User)
- lat, lng = asset.lat, asset.lng
- else
- puts "\tIt's not a route or a user...skipping"
- return #it aint a route or a user
- end
- unless lat and lng
- puts "\tno lat and lng found...wtf?"
- return
- end
- res = GoogleGeocoder.reverse_geocode([lat, lng])
- puts "\tGoogle returned #{res.all.size} results..."
- if res.all.size > 0
- best_accuracy = -10
- best = nil
- res.all.each do |r|
- if r.accuracy and r.accuracy > best_accuracy
- best = r
- best_accuracy = r.accuracy
- end
- end
- if best == nil
- puts "\tWTF, no decent result even with #{res.all.size} results??"
- return
- end
- asset.locality = best.city if best.city
- asset.administrative_area = best.state if best.state
- asset.postal_code = best.zip if best.zip
- asset.country_code = best.country_code ? best.country_code : ''
- puts "\tRad, got #{asset.locality}, #{asset.administrative_area}, #{asset.postal_code} in #{asset.country_code}"
- if asset.save
- puts "\tSuccess! Saved it."
- else
- puts "\tFAILED to save, errors: #{asset.errors.inspect}"
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment