earth-ruby19 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/Gemfile +5 -0
  2. data/Gemfile.lock +138 -0
  3. data/LICENSE +20 -0
  4. data/README.markdown +38 -0
  5. data/lib/earth.rb +146 -0
  6. data/lib/earth/active_record_ext.rb +9 -0
  7. data/lib/earth/air.rb +13 -0
  8. data/lib/earth/air/aircraft.rb +21 -0
  9. data/lib/earth/air/aircraft/data_miner.rb +184 -0
  10. data/lib/earth/air/aircraft_class.rb +10 -0
  11. data/lib/earth/air/aircraft_class/data_miner.rb +42 -0
  12. data/lib/earth/air/aircraft_manufacturer.rb +9 -0
  13. data/lib/earth/air/aircraft_manufacturer/data_miner.rb +20 -0
  14. data/lib/earth/air/airline.rb +16 -0
  15. data/lib/earth/air/airline/data_miner.rb +57 -0
  16. data/lib/earth/air/airport.rb +44 -0
  17. data/lib/earth/air/airport/data_miner.rb +80 -0
  18. data/lib/earth/air/data_miner.rb +15 -0
  19. data/lib/earth/air/flight_configuration.rb +7 -0
  20. data/lib/earth/air/flight_configuration/data_miner.rb +16 -0
  21. data/lib/earth/air/flight_distance_class.rb +7 -0
  22. data/lib/earth/air/flight_distance_class/data_miner.rb +16 -0
  23. data/lib/earth/air/flight_domesticity.rb +6 -0
  24. data/lib/earth/air/flight_domesticity/data_miner.rb +57 -0
  25. data/lib/earth/air/flight_fuel_type.rb +12 -0
  26. data/lib/earth/air/flight_fuel_type/data_miner.rb +12 -0
  27. data/lib/earth/air/flight_propulsion.rb +7 -0
  28. data/lib/earth/air/flight_propulsion/data_miner.rb +16 -0
  29. data/lib/earth/air/flight_seat_class.rb +12 -0
  30. data/lib/earth/air/flight_seat_class/data_miner.rb +36 -0
  31. data/lib/earth/air/flight_segment.rb +29 -0
  32. data/lib/earth/air/flight_segment/data_miner.rb +330 -0
  33. data/lib/earth/air/flight_service.rb +7 -0
  34. data/lib/earth/air/flight_service/data_miner.rb +16 -0
  35. data/lib/earth/all.rb +11 -0
  36. data/lib/earth/automobile.rb +8 -0
  37. data/lib/earth/automobile/automobile_fuel_type.rb +18 -0
  38. data/lib/earth/automobile/automobile_fuel_type/data_miner.rb +45 -0
  39. data/lib/earth/automobile/automobile_make.rb +14 -0
  40. data/lib/earth/automobile/automobile_make/data_miner.rb +68 -0
  41. data/lib/earth/automobile/automobile_make_fleet_year.rb +15 -0
  42. data/lib/earth/automobile/automobile_make_fleet_year/data_miner.rb +29 -0
  43. data/lib/earth/automobile/automobile_make_year.rb +14 -0
  44. data/lib/earth/automobile/automobile_make_year/data_miner.rb +45 -0
  45. data/lib/earth/automobile/automobile_model.rb +14 -0
  46. data/lib/earth/automobile/automobile_model/data_miner.rb +38 -0
  47. data/lib/earth/automobile/automobile_model_year.rb +15 -0
  48. data/lib/earth/automobile/automobile_model_year/data_miner.rb +51 -0
  49. data/lib/earth/automobile/automobile_size_class.rb +14 -0
  50. data/lib/earth/automobile/automobile_size_class/data_miner.rb +43 -0
  51. data/lib/earth/automobile/automobile_variant.rb +17 -0
  52. data/lib/earth/automobile/automobile_variant/data_miner.rb +460 -0
  53. data/lib/earth/automobile/data_miner.rb +8 -0
  54. data/lib/earth/base.rb +7 -0
  55. data/lib/earth/bus.rb +1 -0
  56. data/lib/earth/bus/bus_class.rb +19 -0
  57. data/lib/earth/bus/bus_class/data_miner.rb +41 -0
  58. data/lib/earth/bus/data_miner.rb +1 -0
  59. data/lib/earth/conversions_ext.rb +45 -0
  60. data/lib/earth/data_miner.rb +10 -0
  61. data/lib/earth/diet.rb +2 -0
  62. data/lib/earth/diet/data_miner.rb +2 -0
  63. data/lib/earth/diet/diet_class.rb +15 -0
  64. data/lib/earth/diet/diet_class/data_miner.rb +36 -0
  65. data/lib/earth/diet/food_group.rb +17 -0
  66. data/lib/earth/diet/food_group/data_miner.rb +26 -0
  67. data/lib/earth/fuel.rb +2 -0
  68. data/lib/earth/fuel/data_miner.rb +2 -0
  69. data/lib/earth/fuel/fuel_price.rb +13 -0
  70. data/lib/earth/fuel/fuel_price/data_miner.rb +20 -0
  71. data/lib/earth/fuel/fuel_type.rb +18 -0
  72. data/lib/earth/fuel/fuel_type/data_miner.rb +37 -0
  73. data/lib/earth/hospitality.rb +1 -0
  74. data/lib/earth/hospitality/data_miner.rb +1 -0
  75. data/lib/earth/hospitality/lodging_class.rb +9 -0
  76. data/lib/earth/hospitality/lodging_class/data_miner.rb +30 -0
  77. data/lib/earth/industry.rb +10 -0
  78. data/lib/earth/industry/industry.rb +23 -0
  79. data/lib/earth/industry/industry_product.rb +22 -0
  80. data/lib/earth/industry/industry_product_line.rb +20 -0
  81. data/lib/earth/industry/industry_sector.rb +20 -0
  82. data/lib/earth/industry/merchant.rb +18 -0
  83. data/lib/earth/industry/merchant_category.rb +22 -0
  84. data/lib/earth/industry/merchant_category_industry.rb +20 -0
  85. data/lib/earth/industry/product_line.rb +22 -0
  86. data/lib/earth/industry/product_line_industry_product.rb +20 -0
  87. data/lib/earth/industry/sector.rb +19 -0
  88. data/lib/earth/inflectors.rb +9 -0
  89. data/lib/earth/locality.rb +10 -0
  90. data/lib/earth/locality/census_division.rb +22 -0
  91. data/lib/earth/locality/census_division/data_miner.rb +64 -0
  92. data/lib/earth/locality/census_region.rb +13 -0
  93. data/lib/earth/locality/census_region/data_miner.rb +17 -0
  94. data/lib/earth/locality/climate_division.rb +17 -0
  95. data/lib/earth/locality/climate_division/data_miner.rb +20 -0
  96. data/lib/earth/locality/country.rb +13 -0
  97. data/lib/earth/locality/country/data_miner.rb +19 -0
  98. data/lib/earth/locality/data_miner.rb +10 -0
  99. data/lib/earth/locality/egrid_region.rb +15 -0
  100. data/lib/earth/locality/egrid_region/data_miner.rb +35 -0
  101. data/lib/earth/locality/egrid_subregion.rb +16 -0
  102. data/lib/earth/locality/egrid_subregion/data_miner.rb +65 -0
  103. data/lib/earth/locality/petroleum_administration_for_defense_district.rb +13 -0
  104. data/lib/earth/locality/petroleum_administration_for_defense_district/data_miner.rb +21 -0
  105. data/lib/earth/locality/state.rb +22 -0
  106. data/lib/earth/locality/state/data_miner.rb +37 -0
  107. data/lib/earth/locality/urbanity.rb +10 -0
  108. data/lib/earth/locality/urbanity/data_miner.rb +15 -0
  109. data/lib/earth/locality/zip_code.rb +23 -0
  110. data/lib/earth/locality/zip_code/data_miner.rb +43 -0
  111. data/lib/earth/pet.rb +4 -0
  112. data/lib/earth/pet/breed.rb +15 -0
  113. data/lib/earth/pet/breed/data_miner.rb +25 -0
  114. data/lib/earth/pet/breed_gender.rb +14 -0
  115. data/lib/earth/pet/breed_gender/data_miner.rb +21 -0
  116. data/lib/earth/pet/data_miner.rb +4 -0
  117. data/lib/earth/pet/gender.rb +10 -0
  118. data/lib/earth/pet/gender/data_miner.rb +13 -0
  119. data/lib/earth/pet/species.rb +40 -0
  120. data/lib/earth/pet/species/data_miner.rb +42 -0
  121. data/lib/earth/rail.rb +1 -0
  122. data/lib/earth/rail/data_miner.rb +1 -0
  123. data/lib/earth/rail/rail_class.rb +16 -0
  124. data/lib/earth/rail/rail_class/data_miner.rb +36 -0
  125. data/lib/earth/residence.rb +8 -0
  126. data/lib/earth/residence/air_conditioner_use.rb +13 -0
  127. data/lib/earth/residence/air_conditioner_use/data_miner.rb +22 -0
  128. data/lib/earth/residence/clothes_machine_use.rb +10 -0
  129. data/lib/earth/residence/clothes_machine_use/data_miner.rb +28 -0
  130. data/lib/earth/residence/data_miner.rb +8 -0
  131. data/lib/earth/residence/dishwasher_use.rb +10 -0
  132. data/lib/earth/residence/dishwasher_use/data_miner.rb +28 -0
  133. data/lib/earth/residence/residence_appliance.rb +16 -0
  134. data/lib/earth/residence/residence_appliance/data_miner.rb +20 -0
  135. data/lib/earth/residence/residence_class.rb +16 -0
  136. data/lib/earth/residence/residence_class/data_miner.rb +15 -0
  137. data/lib/earth/residence/residence_fuel_price.rb +18 -0
  138. data/lib/earth/residence/residence_fuel_price/data_miner.rb +200 -0
  139. data/lib/earth/residence/residence_fuel_type.rb +32 -0
  140. data/lib/earth/residence/residence_fuel_type/data_miner.rb +18 -0
  141. data/lib/earth/residence/residential_energy_consumption_survey_response.rb +39 -0
  142. data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +283 -0
  143. data/spec/lib/earth_spec.rb +25 -0
  144. data/spec/spec_helper.rb +11 -0
  145. data/vendor/geokit-rails/CHANGELOG.rdoc +46 -0
  146. data/vendor/geokit-rails/MIT-LICENSE +20 -0
  147. data/vendor/geokit-rails/README.markdown +561 -0
  148. data/vendor/geokit-rails/Rakefile +18 -0
  149. data/vendor/geokit-rails/about.yml +9 -0
  150. data/vendor/geokit-rails/assets/api_keys_template +61 -0
  151. data/vendor/geokit-rails/init.rb +1 -0
  152. data/vendor/geokit-rails/install.rb +14 -0
  153. data/vendor/geokit-rails/lib/geokit-rails.rb +24 -0
  154. data/vendor/geokit-rails/lib/geokit-rails/acts_as_mappable.rb +456 -0
  155. data/vendor/geokit-rails/lib/geokit-rails/adapters/abstract.rb +31 -0
  156. data/vendor/geokit-rails/lib/geokit-rails/adapters/mysql.rb +22 -0
  157. data/vendor/geokit-rails/lib/geokit-rails/adapters/postgresql.rb +22 -0
  158. data/vendor/geokit-rails/lib/geokit-rails/adapters/sqlserver.rb +43 -0
  159. data/vendor/geokit-rails/lib/geokit-rails/defaults.rb +22 -0
  160. data/vendor/geokit-rails/lib/geokit-rails/geocoder_control.rb +16 -0
  161. data/vendor/geokit-rails/lib/geokit-rails/ip_geocode_lookup.rb +46 -0
  162. data/vendor/geokit-rails/test/acts_as_mappable_test.rb +474 -0
  163. data/vendor/geokit-rails/test/boot.rb +25 -0
  164. data/vendor/geokit-rails/test/database.yml +20 -0
  165. data/vendor/geokit-rails/test/fixtures/companies.yml +7 -0
  166. data/vendor/geokit-rails/test/fixtures/custom_locations.yml +54 -0
  167. data/vendor/geokit-rails/test/fixtures/locations.yml +54 -0
  168. data/vendor/geokit-rails/test/fixtures/mock_addresses.yml +17 -0
  169. data/vendor/geokit-rails/test/fixtures/mock_families.yml +2 -0
  170. data/vendor/geokit-rails/test/fixtures/mock_houses.yml +9 -0
  171. data/vendor/geokit-rails/test/fixtures/mock_organizations.yml +5 -0
  172. data/vendor/geokit-rails/test/fixtures/mock_people.yml +5 -0
  173. data/vendor/geokit-rails/test/fixtures/stores.yml +0 -0
  174. data/vendor/geokit-rails/test/ip_geocode_lookup_test.rb +77 -0
  175. data/vendor/geokit-rails/test/models/company.rb +3 -0
  176. data/vendor/geokit-rails/test/models/custom_location.rb +12 -0
  177. data/vendor/geokit-rails/test/models/location.rb +4 -0
  178. data/vendor/geokit-rails/test/models/mock_address.rb +4 -0
  179. data/vendor/geokit-rails/test/models/mock_family.rb +3 -0
  180. data/vendor/geokit-rails/test/models/mock_house.rb +3 -0
  181. data/vendor/geokit-rails/test/models/mock_organization.rb +4 -0
  182. data/vendor/geokit-rails/test/models/mock_person.rb +4 -0
  183. data/vendor/geokit-rails/test/models/store.rb +3 -0
  184. data/vendor/geokit-rails/test/schema.rb +60 -0
  185. data/vendor/geokit-rails/test/tasks.rake +31 -0
  186. data/vendor/geokit-rails/test/test_helper.rb +23 -0
  187. metadata +476 -0
data/lib/earth/all.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'earth/air'
2
+ require 'earth/automobile'
3
+ require 'earth/bus'
4
+ require 'earth/diet'
5
+ require 'earth/fuel'
6
+ require 'earth/hospitality'
7
+ require 'earth/industry'
8
+ require 'earth/locality'
9
+ require 'earth/pet'
10
+ require 'earth/rail'
11
+ require 'earth/residence'
@@ -0,0 +1,8 @@
1
+ require 'earth/automobile/automobile_fuel_type'
2
+ require 'earth/automobile/automobile_make'
3
+ require 'earth/automobile/automobile_make_fleet_year'
4
+ require 'earth/automobile/automobile_make_year'
5
+ require 'earth/automobile/automobile_model'
6
+ require 'earth/automobile/automobile_model_year'
7
+ require 'earth/automobile/automobile_size_class'
8
+ require 'earth/automobile/automobile_variant'
@@ -0,0 +1,18 @@
1
+ class AutomobileFuelType < ActiveRecord::Base
2
+ set_primary_key :code
3
+
4
+ has_many :automobiles, :foreign_key => 'fuel_type_id'
5
+
6
+ scope :ordered, :order => 'name'
7
+
8
+ falls_back_on :emission_factor => 20.781.pounds_per_gallon.to(:kilograms_per_litre) # https://brighterplanet.sifterapp.com/projects/30/issues/428
9
+
10
+ data_miner do
11
+ tap "Brighter Planet's sanitized automobile fuel type data", Earth.taps_server
12
+ end
13
+
14
+ CODES = {
15
+ :electricity => 'El',
16
+ :diesel => 'D'
17
+ }
18
+ end
@@ -0,0 +1,45 @@
1
+ AutomobileFuelType.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'code'
5
+ string 'name'
6
+ float 'emission_factor'
7
+ string 'emission_factor_units'
8
+ float 'annual_distance'
9
+ string 'annual_distance_units'
10
+ end
11
+
12
+ import("the fuels used in the 2000 EPA fuel economy guide",
13
+ :url => 'http://www.fueleconomy.gov/FEG/epadata/00data.zip',
14
+ :filename => 'Gd6-dsc.txt',
15
+ :format => :fixed_width,
16
+ :crop => 21..26, # inclusive
17
+ :cut => '2-',
18
+ :select => lambda { |row| /\A[A-Z]/.match row[:code] },
19
+ :schema => [[ 'code', 2, { :type => :string } ],
20
+ [ 'spacer', 2 ],
21
+ [ 'name', 52, { :type => :string } ]]) do
22
+ key 'code'
23
+ store 'name'
24
+ end
25
+
26
+ import "a pre-calculated emission factor and average annual distance for each fuel",
27
+ :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/models_export/automobile_fuel_type.csv' do
28
+ key 'code', :field_name => 'code'
29
+ store 'name'
30
+ store 'annual_distance', :units => :kilometres
31
+ store 'emission_factor', :units => :kilograms_per_litre
32
+ end
33
+
34
+ # pull electricity emission factor from residential electricity
35
+ import "a pre-calculated emission factor for electricity",
36
+ :url => 'http://spreadsheets.google.com/pub?key=rukxnmuhhsOsrztTrUaFCXQ',
37
+ :select => lambda { |row| row['name'] == 'electricity' } do
38
+ key 'name'
39
+ store 'emission_factor', :units => :kilograms_per_litre
40
+ end
41
+
42
+ # still need distance estimate for electric cars
43
+ end
44
+ end
45
+
@@ -0,0 +1,14 @@
1
+ class AutomobileMake < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ has_many :make_years, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_name'
5
+ has_many :models, :class_name => 'AutomobileModel', :foreign_key => 'make_name'
6
+ has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_name'
7
+ has_many :variants, :class_name => 'AutomobileVariant', :foreign_key => 'make_name'
8
+
9
+ scope :major, :conditions => { :major => true }, :order => :name
10
+
11
+ data_miner do
12
+ tap "Brighter Planet's make year data", Earth.taps_server
13
+ end
14
+ end
@@ -0,0 +1,68 @@
1
+ AutomobileMake.class_eval do
2
+ data_miner do
3
+ process "Start from scratch" do
4
+ connection.drop_table table_name
5
+ end
6
+
7
+ schema Earth.database_options do
8
+ string 'name'
9
+ boolean 'major'
10
+ float 'fuel_efficiency'
11
+ string 'fuel_efficiency_units'
12
+ end
13
+
14
+ process "Derive automobile makes from automobile variants" do
15
+ AutomobileVariant.run_data_miner!
16
+ connection.execute %{
17
+ INSERT IGNORE INTO automobile_makes(name)
18
+ SELECT automobile_variants.make_name FROM automobile_variants WHERE LENGTH(automobile_variants.make_name) > 0
19
+ }
20
+ end
21
+
22
+ import "a Brighter-Planet defined list of major automobile manufacturers",
23
+ :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/makes/make_importance.csv' do
24
+ key 'name'
25
+ store 'major'
26
+ end
27
+
28
+ process "Derive average fuel efficiency from automobile makes and years" do
29
+ AutomobileMakeYear.run_data_miner!
30
+ make_years = AutomobileMakeYear.arel_table
31
+ makes = AutomobileMake.arel_table
32
+ conditional_relation = makes[:name].eq(make_years[:make_name])
33
+ relation = AutomobileMakeYear.weighted_average_relation(:fuel_efficiency, :weighted_by => :volume).where(conditional_relation)
34
+ update_all "fuel_efficiency = (#{relation.to_sql})"
35
+ update_all "fuel_efficiency_units = 'kilometres_per_litre'"
36
+ end
37
+ end
38
+ end
39
+
40
+ # leave this for later if we need it
41
+ # SUBSIDIARIES = {
42
+ # 'Chevrolet' => 'GM',
43
+ # 'Pontiac' => 'GM',
44
+ # 'Audi' => 'Volkswagen',
45
+ # 'Dodge' => 'Chrysler',
46
+ # 'Lincoln' => 'Ford',
47
+ # 'Plymouth' => 'Chrysler',
48
+ # 'Buick' => 'GM',
49
+ # 'Cadillac' => 'GM',
50
+ # 'Merkur' => 'Ford',
51
+ # 'Oldsmobile' => 'GM',
52
+ # 'GMC' => 'GM',
53
+ # 'Bentley' => 'Rolls-Royce', # currently owned by Volkswagen, but a Flying Spur is hardly a rebranded Passat
54
+ # 'Acura' => 'Honda',
55
+ # 'Land Rover' => 'Ford',
56
+ # 'Eagle' => 'Chrysler',
57
+ # 'Geo' => 'GM',
58
+ # 'Laforza' => 'Ford',
59
+ # 'Infiniti' => 'Nissan',
60
+ # 'Lexus' => 'Toyota',
61
+ # 'Saturn' => 'GM',
62
+ # 'Mercury' => 'Ford',
63
+ # 'Alpina' => 'BMW',
64
+ # 'Mini' => 'BMW',
65
+ # 'Maybach' => 'Mercedes',
66
+ # 'Hummer' => 'GM'
67
+ # }
68
+
@@ -0,0 +1,15 @@
1
+ class AutomobileMakeFleetYear < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
+ # WRONG belongs_to :model_year, :class_name => 'AutomobileModelYear', :foreign_key => 'automobile_model_year_name'
6
+ belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
7
+
8
+ data_miner do
9
+ tap "Brighter Planet's sanitized auto make fleet year data", Earth.taps_server
10
+
11
+ process "bring in dependencies" do
12
+ run_data_miner_on_belongs_to_associations
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ AutomobileMakeFleetYear.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'name'
5
+ string 'make_year_name'
6
+ string 'make_name'
7
+ string 'fleet'
8
+ integer 'year'
9
+ float 'fuel_efficiency'
10
+ string 'fuel_efficiency_units'
11
+ integer 'volume'
12
+ end
13
+
14
+ # CAFE data privately emailed to Andy from Terry Anderson at the DOT/NHTSA
15
+ import "annual corporate average fuel economy data for domestic and imported vehicle fleets from the NHTSA",
16
+ :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/make_fleet_years/make_fleet_years.csv',
17
+ :errata => 'http://static.brighterplanet.com/science/data/transport/automobiles/make_fleet_years/errata.csv',
18
+ :select => lambda { |row| row['volume'].to_i > 0 } do
19
+ key 'name', :synthesize => lambda { |row| [ row['manufacturer_name'], row['year_content'], row['fleet'][2,2] ].join ' ' }
20
+ store 'make_year_name', :synthesize => lambda { |row| [ row['manufacturer_name'], row['year_content'] ].join ' ' }
21
+ store 'make_name', :field_name => 'manufacturer_name'
22
+ store 'year', :field_name => 'year_content'
23
+ store 'fleet', :chars => 2..3 # zero-based
24
+ store 'fuel_efficiency', :from_units => :miles_per_gallon, :to_units => :kilometres_per_litre
25
+ store 'volume'
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,14 @@
1
+ class AutomobileMakeYear < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
5
+ has_many :fleet_years, :class_name => 'AutomobileMakeFleetYear', :foreign_key => 'make_year_name'
6
+
7
+ data_miner do
8
+ tap "Brighter Planet's make year data", Earth.taps_server
9
+
10
+ process "bring in dependencies" do
11
+ run_data_miner_on_belongs_to_associations
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,45 @@
1
+ AutomobileMakeYear.class_eval do
2
+ data_miner do
3
+ process "Start from scratch" do
4
+ connection.drop_table table_name
5
+ end
6
+
7
+ schema Earth.database_options do
8
+ string 'name'
9
+ string 'make_name'
10
+ integer 'year'
11
+ float 'fuel_efficiency'
12
+ string 'fuel_efficiency_units'
13
+ integer 'volume'
14
+ end
15
+
16
+ process "Derive manufacturer names and years from automobile make fleet years" do
17
+ AutomobileMakeFleetYear.run_data_miner!
18
+ connection.execute %{
19
+ INSERT IGNORE INTO automobile_make_years(name, make_name, year)
20
+ SELECT
21
+ automobile_make_fleet_years.make_year_name,
22
+ automobile_make_fleet_years.make_name,
23
+ automobile_make_fleet_years.year
24
+ FROM automobile_make_fleet_years
25
+ }
26
+ end
27
+
28
+ process "Derive annual corporate average fuel economy across all vehicles from automobile make fleet years" do
29
+ AutomobileMakeFleetYear.run_data_miner!
30
+ make_fleet_years = AutomobileMakeFleetYear.arel_table
31
+ make_years = AutomobileMakeYear.arel_table
32
+ conditional_relation = make_years[:name].eq(make_fleet_years[:make_year_name])
33
+ relation = AutomobileMakeFleetYear.weighted_average_relation(:fuel_efficiency, :weighted_by => :volume).where(conditional_relation)
34
+ update_all "fuel_efficiency = (#{relation.to_sql})"
35
+ update_all "fuel_efficiency_units = 'kilometres_per_litre'"
36
+ end
37
+
38
+ process "Derive sales volume across all vehicles from automobile make fleet years" do
39
+ connection.execute %{
40
+ UPDATE automobile_make_years SET automobile_make_years.volume = (SELECT SUM(automobile_make_fleet_years.volume) FROM automobile_make_fleet_years WHERE automobile_make_fleet_years.make_year_name = automobile_make_years.name)
41
+ }
42
+ end
43
+ end
44
+ end
45
+
@@ -0,0 +1,14 @@
1
+ class AutomobileModel < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ has_many :variants, :class_name => 'AutomobileVariant', :foreign_key => 'model_name'
5
+ belongs_to :make, :class_name => 'AutomobileMake', :foreign_key => 'make_name'
6
+
7
+ data_miner do
8
+ tap "Brighter Planet's auto model data", Earth.taps_server
9
+
10
+ process "bring in dependencies" do
11
+ run_data_miner_on_belongs_to_associations
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ AutomobileModel.class_eval do
2
+ data_miner do
3
+ process "Start from scratch" do
4
+ connection.drop_table table_name
5
+ end
6
+
7
+ schema Earth.database_options do
8
+ string 'name' # make + model
9
+ string 'make_name'
10
+ float 'fuel_efficiency_city'
11
+ string 'fuel_efficiency_city_units'
12
+ float 'fuel_efficiency_highway'
13
+ string 'fuel_efficiency_highway_units'
14
+ end
15
+
16
+ process "Derive model names from automobile variants" do
17
+ AutomobileVariant.run_data_miner!
18
+ connection.execute %{
19
+ INSERT IGNORE INTO automobile_models(name, make_name)
20
+ SELECT automobile_variants.model_name, automobile_variants.make_name FROM automobile_variants WHERE LENGTH(automobile_variants.model_name) > 0 AND LENGTH(automobile_variants.make_name) > 0
21
+ }
22
+ end
23
+
24
+ # TODO not weighted until we get weightings on auto variants
25
+ process "Derive average fuel economy from automobile variants" do
26
+ AutomobileVariant.run_data_miner!
27
+ automobile_models = AutomobileModel.arel_table
28
+ automobile_variants = AutomobileVariant.arel_table
29
+ conditional_relation = automobile_models[:name].eq(automobile_variants[:model_name])
30
+ %w{ city highway }.each do |i|
31
+ relation = AutomobileVariant.where(conditional_relation).where("`automobile_variants`.`fuel_efficiency_#{i}` IS NOT NULL").project("AVG(`automobile_variants`.`fuel_efficiency_#{i}`)")
32
+ update_all "fuel_efficiency_#{i} = (#{relation.to_sql})"
33
+ update_all "fuel_efficiency_#{i}_units = 'kilometres_per_litre'"
34
+ end
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,15 @@
1
+ class AutomobileModelYear < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ belongs_to :make_year, :class_name => 'AutomobileMakeYear', :foreign_key => 'make_year_name'
5
+ has_many :variants, :class_name => 'AutomobileVariant', :foreign_key => 'model_year_name'
6
+ has_many :automobiles, :foreign_key => 'model_year_id'
7
+
8
+ data_miner do
9
+ tap "Brighter Planet's model year data", Earth.taps_server
10
+
11
+ process "pull dependencies" do
12
+ run_data_miner_on_belongs_to_associations
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,51 @@
1
+ AutomobileModelYear.class_eval do
2
+ data_miner do
3
+ process "Start from scratch" do
4
+ connection.drop_table table_name
5
+ end
6
+
7
+ schema Earth.database_options do
8
+ string 'name'
9
+ string 'make_name'
10
+ string 'model_name'
11
+ integer 'year'
12
+ string 'make_year_name'
13
+ float 'fuel_efficiency'
14
+ string 'fuel_efficiency_units'
15
+ float 'fuel_efficiency_city'
16
+ string 'fuel_efficiency_city_units'
17
+ float 'fuel_efficiency_highway'
18
+ string 'fuel_efficiency_highway_units'
19
+ end
20
+
21
+ process "Derive model year names from automobile variants" do
22
+ AutomobileVariant.run_data_miner!
23
+ connection.execute %{
24
+ INSERT IGNORE INTO automobile_model_years(name, make_name, model_name, year, make_year_name)
25
+ SELECT automobile_variants.model_year_name, automobile_variants.make_name, automobile_variants.model_name, automobile_variants.year, automobile_variants.make_year_name FROM automobile_variants WHERE LENGTH(automobile_variants.make_name) > 0 AND LENGTH(automobile_variants.model_name) > 0
26
+ }
27
+ end
28
+
29
+ # TODO: weight by volume somehow
30
+ # note that we used to derive averages from make years, but here we get it from variants
31
+ # even without volume-weighting, the values are much better.
32
+ # for example, 20km/l for a toyota prius 2006 vs. 13km/l if you use make years
33
+ process "Calculate city and highway fuel efficiency from automobile variants" do
34
+ AutomobileVariant.run_data_miner!
35
+ automobile_model_years = AutomobileModelYear.arel_table
36
+ automobile_variants = AutomobileVariant.arel_table
37
+ conditional_relation = automobile_model_years[:name].eq(automobile_variants[:model_year_name])
38
+ %w{ city highway }.each do |i|
39
+ relation = AutomobileVariant.where(conditional_relation).where("`automobile_variants`.`fuel_efficiency_#{i}` IS NOT NULL").project("AVG(`automobile_variants`.`fuel_efficiency_#{i}`)")
40
+ update_all "fuel_efficiency_#{i} = (#{relation.to_sql})"
41
+ update_all "fuel_efficiency_#{i}_units = 'kilometres_per_litre'"
42
+ end
43
+ end
44
+
45
+ process "Calculate overall fuel efficiency using the latest EPA equation" do
46
+ update_all "fuel_efficiency = 1 / ((0.43 / fuel_efficiency_city) + (0.57 / fuel_efficiency_highway))"
47
+ update_all "fuel_efficiency_units = 'kilometres_per_litre'"
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,14 @@
1
+ class AutomobileSizeClass < ActiveRecord::Base
2
+ set_primary_key :name
3
+
4
+ has_many :automobiles, :foreign_key => 'size_class_id'
5
+
6
+ falls_back_on :hybrid_fuel_efficiency_city_multiplier => 1.651, # https://brighterplanet.sifterapp.com/issue/667
7
+ :hybrid_fuel_efficiency_highway_multiplier => 1.213,
8
+ :conventional_fuel_efficiency_city_multiplier => 0.987,
9
+ :conventional_fuel_efficiency_highway_multiplier => 0.996
10
+
11
+ data_miner do
12
+ tap "Brighter Planet's sanitized automobile size class data", Earth.taps_server
13
+ end
14
+ end
@@ -0,0 +1,43 @@
1
+ AutomobileSizeClass.class_eval do
2
+ data_miner do
3
+ schema Earth.database_options do
4
+ string 'name'
5
+ float 'fuel_efficiency_city'
6
+ string 'fuel_efficiency_city_units'
7
+ float 'fuel_efficiency_highway'
8
+ string 'fuel_efficiency_highway_units'
9
+ float 'annual_distance'
10
+ string 'annual_distance_units'
11
+ string 'emblem'
12
+ float 'hybrid_fuel_efficiency_city_multiplier'
13
+ float 'hybrid_fuel_efficiency_highway_multiplier'
14
+ float 'conventional_fuel_efficiency_city_multiplier'
15
+ float 'conventional_fuel_efficiency_highway_multiplier'
16
+ end
17
+
18
+ import "a list of size classes and pre-calculated annual distances and fuel efficiencies",
19
+ :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/models_export/automobile_size_class.csv' do
20
+ key 'name'
21
+ store 'annual_distance', :units => :kilometres
22
+ store 'fuel_efficiency_city', :units => :kilometres_per_litre
23
+ store 'fuel_efficiency_highway', :units => :kilometres_per_litre
24
+ end
25
+
26
+ # Ian 5/27/2010 I'm pretty sure we don't need emblems in middleware
27
+ # import "",
28
+ # :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/models_export/automobile_size_class_emblems.csv' do
29
+ # key 'name'
30
+ # store 'emblem'
31
+ # end
32
+
33
+ import "pre-calculated hybridity multipliers",
34
+ :url => 'http://static.brighterplanet.com/science/data/transport/automobiles/vehicle_classes/fuel_efficiency_multipliers.csv' do
35
+ key 'name'
36
+ store 'hybrid_fuel_efficiency_city_multiplier'
37
+ store 'hybrid_fuel_efficiency_highway_multiplier'
38
+ store 'conventional_fuel_efficiency_city_multiplier'
39
+ store 'conventional_fuel_efficiency_highway_multiplier'
40
+ end
41
+ end
42
+ end
43
+