earth-ruby19 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +5 -0
- data/Gemfile.lock +138 -0
- data/LICENSE +20 -0
- data/README.markdown +38 -0
- data/lib/earth.rb +146 -0
- data/lib/earth/active_record_ext.rb +9 -0
- data/lib/earth/air.rb +13 -0
- data/lib/earth/air/aircraft.rb +21 -0
- data/lib/earth/air/aircraft/data_miner.rb +184 -0
- data/lib/earth/air/aircraft_class.rb +10 -0
- data/lib/earth/air/aircraft_class/data_miner.rb +42 -0
- data/lib/earth/air/aircraft_manufacturer.rb +9 -0
- data/lib/earth/air/aircraft_manufacturer/data_miner.rb +20 -0
- data/lib/earth/air/airline.rb +16 -0
- data/lib/earth/air/airline/data_miner.rb +57 -0
- data/lib/earth/air/airport.rb +44 -0
- data/lib/earth/air/airport/data_miner.rb +80 -0
- data/lib/earth/air/data_miner.rb +15 -0
- data/lib/earth/air/flight_configuration.rb +7 -0
- data/lib/earth/air/flight_configuration/data_miner.rb +16 -0
- data/lib/earth/air/flight_distance_class.rb +7 -0
- data/lib/earth/air/flight_distance_class/data_miner.rb +16 -0
- data/lib/earth/air/flight_domesticity.rb +6 -0
- data/lib/earth/air/flight_domesticity/data_miner.rb +57 -0
- data/lib/earth/air/flight_fuel_type.rb +12 -0
- data/lib/earth/air/flight_fuel_type/data_miner.rb +12 -0
- data/lib/earth/air/flight_propulsion.rb +7 -0
- data/lib/earth/air/flight_propulsion/data_miner.rb +16 -0
- data/lib/earth/air/flight_seat_class.rb +12 -0
- data/lib/earth/air/flight_seat_class/data_miner.rb +36 -0
- data/lib/earth/air/flight_segment.rb +29 -0
- data/lib/earth/air/flight_segment/data_miner.rb +330 -0
- data/lib/earth/air/flight_service.rb +7 -0
- data/lib/earth/air/flight_service/data_miner.rb +16 -0
- data/lib/earth/all.rb +11 -0
- data/lib/earth/automobile.rb +8 -0
- data/lib/earth/automobile/automobile_fuel_type.rb +18 -0
- data/lib/earth/automobile/automobile_fuel_type/data_miner.rb +45 -0
- data/lib/earth/automobile/automobile_make.rb +14 -0
- data/lib/earth/automobile/automobile_make/data_miner.rb +68 -0
- data/lib/earth/automobile/automobile_make_fleet_year.rb +15 -0
- data/lib/earth/automobile/automobile_make_fleet_year/data_miner.rb +29 -0
- data/lib/earth/automobile/automobile_make_year.rb +14 -0
- data/lib/earth/automobile/automobile_make_year/data_miner.rb +45 -0
- data/lib/earth/automobile/automobile_model.rb +14 -0
- data/lib/earth/automobile/automobile_model/data_miner.rb +38 -0
- data/lib/earth/automobile/automobile_model_year.rb +15 -0
- data/lib/earth/automobile/automobile_model_year/data_miner.rb +51 -0
- data/lib/earth/automobile/automobile_size_class.rb +14 -0
- data/lib/earth/automobile/automobile_size_class/data_miner.rb +43 -0
- data/lib/earth/automobile/automobile_variant.rb +17 -0
- data/lib/earth/automobile/automobile_variant/data_miner.rb +460 -0
- data/lib/earth/automobile/data_miner.rb +8 -0
- data/lib/earth/base.rb +7 -0
- data/lib/earth/bus.rb +1 -0
- data/lib/earth/bus/bus_class.rb +19 -0
- data/lib/earth/bus/bus_class/data_miner.rb +41 -0
- data/lib/earth/bus/data_miner.rb +1 -0
- data/lib/earth/conversions_ext.rb +45 -0
- data/lib/earth/data_miner.rb +10 -0
- data/lib/earth/diet.rb +2 -0
- data/lib/earth/diet/data_miner.rb +2 -0
- data/lib/earth/diet/diet_class.rb +15 -0
- data/lib/earth/diet/diet_class/data_miner.rb +36 -0
- data/lib/earth/diet/food_group.rb +17 -0
- data/lib/earth/diet/food_group/data_miner.rb +26 -0
- data/lib/earth/fuel.rb +2 -0
- data/lib/earth/fuel/data_miner.rb +2 -0
- data/lib/earth/fuel/fuel_price.rb +13 -0
- data/lib/earth/fuel/fuel_price/data_miner.rb +20 -0
- data/lib/earth/fuel/fuel_type.rb +18 -0
- data/lib/earth/fuel/fuel_type/data_miner.rb +37 -0
- data/lib/earth/hospitality.rb +1 -0
- data/lib/earth/hospitality/data_miner.rb +1 -0
- data/lib/earth/hospitality/lodging_class.rb +9 -0
- data/lib/earth/hospitality/lodging_class/data_miner.rb +30 -0
- data/lib/earth/industry.rb +10 -0
- data/lib/earth/industry/industry.rb +23 -0
- data/lib/earth/industry/industry_product.rb +22 -0
- data/lib/earth/industry/industry_product_line.rb +20 -0
- data/lib/earth/industry/industry_sector.rb +20 -0
- data/lib/earth/industry/merchant.rb +18 -0
- data/lib/earth/industry/merchant_category.rb +22 -0
- data/lib/earth/industry/merchant_category_industry.rb +20 -0
- data/lib/earth/industry/product_line.rb +22 -0
- data/lib/earth/industry/product_line_industry_product.rb +20 -0
- data/lib/earth/industry/sector.rb +19 -0
- data/lib/earth/inflectors.rb +9 -0
- data/lib/earth/locality.rb +10 -0
- data/lib/earth/locality/census_division.rb +22 -0
- data/lib/earth/locality/census_division/data_miner.rb +64 -0
- data/lib/earth/locality/census_region.rb +13 -0
- data/lib/earth/locality/census_region/data_miner.rb +17 -0
- data/lib/earth/locality/climate_division.rb +17 -0
- data/lib/earth/locality/climate_division/data_miner.rb +20 -0
- data/lib/earth/locality/country.rb +13 -0
- data/lib/earth/locality/country/data_miner.rb +19 -0
- data/lib/earth/locality/data_miner.rb +10 -0
- data/lib/earth/locality/egrid_region.rb +15 -0
- data/lib/earth/locality/egrid_region/data_miner.rb +35 -0
- data/lib/earth/locality/egrid_subregion.rb +16 -0
- data/lib/earth/locality/egrid_subregion/data_miner.rb +65 -0
- data/lib/earth/locality/petroleum_administration_for_defense_district.rb +13 -0
- data/lib/earth/locality/petroleum_administration_for_defense_district/data_miner.rb +21 -0
- data/lib/earth/locality/state.rb +22 -0
- data/lib/earth/locality/state/data_miner.rb +37 -0
- data/lib/earth/locality/urbanity.rb +10 -0
- data/lib/earth/locality/urbanity/data_miner.rb +15 -0
- data/lib/earth/locality/zip_code.rb +23 -0
- data/lib/earth/locality/zip_code/data_miner.rb +43 -0
- data/lib/earth/pet.rb +4 -0
- data/lib/earth/pet/breed.rb +15 -0
- data/lib/earth/pet/breed/data_miner.rb +25 -0
- data/lib/earth/pet/breed_gender.rb +14 -0
- data/lib/earth/pet/breed_gender/data_miner.rb +21 -0
- data/lib/earth/pet/data_miner.rb +4 -0
- data/lib/earth/pet/gender.rb +10 -0
- data/lib/earth/pet/gender/data_miner.rb +13 -0
- data/lib/earth/pet/species.rb +40 -0
- data/lib/earth/pet/species/data_miner.rb +42 -0
- data/lib/earth/rail.rb +1 -0
- data/lib/earth/rail/data_miner.rb +1 -0
- data/lib/earth/rail/rail_class.rb +16 -0
- data/lib/earth/rail/rail_class/data_miner.rb +36 -0
- data/lib/earth/residence.rb +8 -0
- data/lib/earth/residence/air_conditioner_use.rb +13 -0
- data/lib/earth/residence/air_conditioner_use/data_miner.rb +22 -0
- data/lib/earth/residence/clothes_machine_use.rb +10 -0
- data/lib/earth/residence/clothes_machine_use/data_miner.rb +28 -0
- data/lib/earth/residence/data_miner.rb +8 -0
- data/lib/earth/residence/dishwasher_use.rb +10 -0
- data/lib/earth/residence/dishwasher_use/data_miner.rb +28 -0
- data/lib/earth/residence/residence_appliance.rb +16 -0
- data/lib/earth/residence/residence_appliance/data_miner.rb +20 -0
- data/lib/earth/residence/residence_class.rb +16 -0
- data/lib/earth/residence/residence_class/data_miner.rb +15 -0
- data/lib/earth/residence/residence_fuel_price.rb +18 -0
- data/lib/earth/residence/residence_fuel_price/data_miner.rb +200 -0
- data/lib/earth/residence/residence_fuel_type.rb +32 -0
- data/lib/earth/residence/residence_fuel_type/data_miner.rb +18 -0
- data/lib/earth/residence/residential_energy_consumption_survey_response.rb +39 -0
- data/lib/earth/residence/residential_energy_consumption_survey_response/data_miner.rb +283 -0
- data/spec/lib/earth_spec.rb +25 -0
- data/spec/spec_helper.rb +11 -0
- data/vendor/geokit-rails/CHANGELOG.rdoc +46 -0
- data/vendor/geokit-rails/MIT-LICENSE +20 -0
- data/vendor/geokit-rails/README.markdown +561 -0
- data/vendor/geokit-rails/Rakefile +18 -0
- data/vendor/geokit-rails/about.yml +9 -0
- data/vendor/geokit-rails/assets/api_keys_template +61 -0
- data/vendor/geokit-rails/init.rb +1 -0
- data/vendor/geokit-rails/install.rb +14 -0
- data/vendor/geokit-rails/lib/geokit-rails.rb +24 -0
- data/vendor/geokit-rails/lib/geokit-rails/acts_as_mappable.rb +456 -0
- data/vendor/geokit-rails/lib/geokit-rails/adapters/abstract.rb +31 -0
- data/vendor/geokit-rails/lib/geokit-rails/adapters/mysql.rb +22 -0
- data/vendor/geokit-rails/lib/geokit-rails/adapters/postgresql.rb +22 -0
- data/vendor/geokit-rails/lib/geokit-rails/adapters/sqlserver.rb +43 -0
- data/vendor/geokit-rails/lib/geokit-rails/defaults.rb +22 -0
- data/vendor/geokit-rails/lib/geokit-rails/geocoder_control.rb +16 -0
- data/vendor/geokit-rails/lib/geokit-rails/ip_geocode_lookup.rb +46 -0
- data/vendor/geokit-rails/test/acts_as_mappable_test.rb +474 -0
- data/vendor/geokit-rails/test/boot.rb +25 -0
- data/vendor/geokit-rails/test/database.yml +20 -0
- data/vendor/geokit-rails/test/fixtures/companies.yml +7 -0
- data/vendor/geokit-rails/test/fixtures/custom_locations.yml +54 -0
- data/vendor/geokit-rails/test/fixtures/locations.yml +54 -0
- data/vendor/geokit-rails/test/fixtures/mock_addresses.yml +17 -0
- data/vendor/geokit-rails/test/fixtures/mock_families.yml +2 -0
- data/vendor/geokit-rails/test/fixtures/mock_houses.yml +9 -0
- data/vendor/geokit-rails/test/fixtures/mock_organizations.yml +5 -0
- data/vendor/geokit-rails/test/fixtures/mock_people.yml +5 -0
- data/vendor/geokit-rails/test/fixtures/stores.yml +0 -0
- data/vendor/geokit-rails/test/ip_geocode_lookup_test.rb +77 -0
- data/vendor/geokit-rails/test/models/company.rb +3 -0
- data/vendor/geokit-rails/test/models/custom_location.rb +12 -0
- data/vendor/geokit-rails/test/models/location.rb +4 -0
- data/vendor/geokit-rails/test/models/mock_address.rb +4 -0
- data/vendor/geokit-rails/test/models/mock_family.rb +3 -0
- data/vendor/geokit-rails/test/models/mock_house.rb +3 -0
- data/vendor/geokit-rails/test/models/mock_organization.rb +4 -0
- data/vendor/geokit-rails/test/models/mock_person.rb +4 -0
- data/vendor/geokit-rails/test/models/store.rb +3 -0
- data/vendor/geokit-rails/test/schema.rb +60 -0
- data/vendor/geokit-rails/test/tasks.rake +31 -0
- data/vendor/geokit-rails/test/test_helper.rb +23 -0
- 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
|
+
|