google_timeline 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dcf2f77b9a4958774ff1d94d72eaadd44ca5e3f7
4
+ data.tar.gz: 0d32603fe3584c0ca60f6c66064fcccdfdc34047
5
+ SHA512:
6
+ metadata.gz: f4fd78060ec9f043a21a486b7b19f558e7383e00269e314eee2718a5bfd01b87f68c1058fec4c47e996916b7d08e24f8396a12b39820dd970edc1ca0556bfc80
7
+ data.tar.gz: 0db42350db3a2090ebf768b6ac1d8a62563196de192a3d4dd522a72fb86d6fdec80aba001e9f21d2143f3ad9f8a528c397f668af174bdd30ab62320a7f6db40f
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "google_timeline"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module GoogleTimeline
2
+ VERSION = "1.0"
3
+ end
@@ -0,0 +1,120 @@
1
+ require "google_timeline/version"
2
+ require 'date'
3
+ require 'nokogiri'
4
+ require 'polylines'
5
+
6
+ module GoogleTimeline
7
+ def self.get_timeline(file_path,*args)
8
+ home_coordinate = [[22.6520393, 88.407874]]
9
+ if (args.empty?)==false and (args.select{|a| a.is_a?(Hash) and a[:home_coordinate]}.first).nil? == false
10
+ home_coordinate = [args.select{|a| a.is_a?(Hash) and a[:home_coordinate]}.first[:home_coordinate]]
11
+ end
12
+ google_timeline = GoogleMapTimeline.new(file_path,home_coordinate)
13
+ google_timeline.get_timeline_url(args)
14
+ end
15
+ end
16
+ class GoogleMapTimeline
17
+ attr_accessor :home_coordinate
18
+ attr_accessor :file_path
19
+
20
+ def initialize(file_path, home_coordinate)
21
+ self.file_path = file_path
22
+ self.home_coordinate = home_coordinate
23
+ end
24
+
25
+ def get_timeline_url(args)
26
+ op_hash = {}
27
+ url=default_map
28
+ coordinates = []
29
+ value = self.file_path
30
+ key = Date.parse(value.split("/").last.split(".").first.gsub("history-", "")) rescue nil
31
+
32
+ if key.nil? == false and File.exists?(value)
33
+ kml = File.read(value)
34
+ doc = Nokogiri::XML(kml)
35
+ coordinates = doc.search('//gx:coord').map{|c| c.content.split(" ")[0..1].map{|co| co.to_f}.reverse} rescue []
36
+ location_str = polylines_encoding(coordinates)
37
+
38
+ if location_str.length < 1900
39
+ url = location_url(location_str)
40
+ else
41
+ times = doc.search('when').map{|c| DateTime.parse(c.content)} rescue []
42
+
43
+ coordinate_time_hash = {}
44
+ times.each_with_index do |t, i|
45
+ coordinate_time_hash[t] = coordinates[i]
46
+ end
47
+
48
+ interval = 10*60 #10min
49
+ selected_coordinates = []
50
+ prev_time = nil
51
+ coordinate_time_hash.each do |k,v|
52
+ if prev_time.nil?
53
+ selected_coordinates << v
54
+ prev_time = k
55
+ else
56
+ if (((k - prev_time) * 24 * 60 * 60).to_i >= interval)
57
+ selected_coordinates << v
58
+ prev_time = k
59
+ end
60
+ end
61
+ end
62
+ location_str = polylines_encoding(selected_coordinates)
63
+ url=location_url(location_str)
64
+ end
65
+ end
66
+ op_hash[:url] = url
67
+
68
+ if (args.empty?)==false and (args.select{|a| a.is_a?(Hash) and a[:distance]==true}.first).nil? == false
69
+ distance = {:value => overall_distance(coordinates), :unit=>"meter"}
70
+ op_hash[:distance] = distance
71
+ end
72
+ return op_hash
73
+ end
74
+
75
+ private
76
+ def location_url(location_str)
77
+ "http://maps.googleapis.com/maps/api/staticmap?scale=2&path=enc:#{location_str}&size=600x600&sensor=false"
78
+ end
79
+
80
+ def polylines_encoding(str)
81
+ Polylines::Encoder.encode_points(str)
82
+ end
83
+
84
+ def default_map
85
+ location_url(polylines_encoding(self.home_coordinate))
86
+ end
87
+
88
+ def overall_distance(coordinates)
89
+ total_distance = 0
90
+ for i in 0..(coordinates.count - 1)
91
+ coo = coordinates[i]
92
+ if i==0
93
+ loc1 = coo
94
+ else
95
+ loc2 = coo
96
+ total_distance += distance(loc1,loc2)
97
+ loc1 = loc2
98
+ end
99
+ end
100
+ return total_distance
101
+ end
102
+
103
+ def distance(loc1, loc2)
104
+ rad_per_deg = Math::PI/180 # PI / 180
105
+ rkm = 6371 # Earth radius in kilometers
106
+ rm = rkm * 1000 # Radius in meters
107
+
108
+ dlat_rad = (loc2[0]-loc1[0]) * rad_per_deg # Delta, converted to rad
109
+ dlon_rad = (loc2[1]-loc1[1]) * rad_per_deg
110
+
111
+ lat1_rad, lon1_rad = loc1.map {|i| i * rad_per_deg }
112
+ lat2_rad, lon2_rad = loc2.map {|i| i * rad_per_deg }
113
+
114
+ a = Math.sin(dlat_rad/2)**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * Math.sin(dlon_rad/2)**2
115
+ c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
116
+
117
+ rm * c # Delta in meters
118
+ end
119
+
120
+ end
@@ -0,0 +1,68 @@
1
+ require "google_timeline/version"
2
+ require 'date'
3
+ require 'nokogiri'
4
+ require 'polylines'
5
+
6
+ module GoogleTimeline
7
+ def self.get_timeline_url(file_path,*args)
8
+ url = nil
9
+ home_location = [[12.899599, 77.650928]]
10
+
11
+ if (args.empty?)==false and (args.select{|a| a.is_a?(Hash) and a[:home]}.first).nil? == false
12
+ home_location = [args.select{|a| a.is_a?(Hash) and a[:home]}.first[:home]]
13
+ end
14
+
15
+ url=location_url(Polylines::Encoder.encode_points(home_location))
16
+
17
+ value = file_path
18
+ key = Date.parse(file_path.split("/").last.split(".").first.gsub("history-", "")) rescue nil
19
+
20
+ if key.nil? == false and File.exists?(value)
21
+ kml = File.read(value)
22
+ doc = Nokogiri::XML(kml)
23
+ coordinates = doc.search('//gx:coord').map{|c| c.content.split(" ")[0..1].map{|co| co.to_f}.reverse} rescue []
24
+ location_str = Polylines::Encoder.encode_points(coordinates)
25
+
26
+ if location_str.length < 1900
27
+ url = location_url(location_str)
28
+ else
29
+ times = doc.search('when').map{|c| DateTime.parse(c.content)} rescue []
30
+
31
+ coordinate_time_hash = {}
32
+ times.each_with_index do |t, i|
33
+ coordinate_time_hash[t] = coordinates[i]
34
+ end
35
+
36
+ interval = 10*60 #10min
37
+ selected_coordinates = []
38
+ prev_time = nil
39
+ coordinate_time_hash.each do |k,v|
40
+ if prev_time.nil?
41
+ selected_coordinates << v
42
+ prev_time = k
43
+ else
44
+ if (((k - prev_time) * 24 * 60 * 60).to_i >= interval)
45
+ selected_coordinates << v
46
+ prev_time = k
47
+ end
48
+ end
49
+ end
50
+ location_str = Polylines::Encoder.encode_points(selected_coordinates)
51
+ url=location_url(location_str)
52
+ end
53
+ end
54
+ return url
55
+ end
56
+
57
+ private_class_method :location_url
58
+ private_class_method :test
59
+ class << self
60
+ def location_url(location_str)
61
+ "http://maps.googleapis.com/maps/api/staticmap?scale=2&path=enc:#{location_str}&size=600x600&sensor=false"
62
+ end
63
+
64
+ def test
65
+
66
+ end
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_timeline
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Surajit Khan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: polylines
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Integrate Map Timeline (Google)
84
+ email:
85
+ - surajit16@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - bin/console
91
+ - bin/setup
92
+ - lib/google_timeline.rb
93
+ - lib/google_timeline.rb~
94
+ - lib/google_timeline/version.rb
95
+ homepage: http://www.surajitkhan.in
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Integrate Map-Timeline (Google)
119
+ test_files: []