Created
March 6, 2012 18:29
-
-
Save asterite/1987991 to your computer and use it in GitHub Desktop.
Import Rwanda sites for InSTEDD's Resource Map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
APP_PATH = File.expand_path('../../config/application', __FILE__) | |
require File.expand_path('../../config/boot', __FILE__) | |
require File.expand_path('../../config/environment', __FILE__) | |
require 'csv' | |
filename = File.expand_path('../FOSA-Table.csv', __FILE__) | |
rows = CSV.read filename | |
rows = rows[1 .. -1] | |
User.transaction do | |
user = User.first | |
puts "Creating collection" | |
collection = Collection.new :name => "Rwanda" | |
user.create_collection collection | |
layer = collection.layers.new :name => "Rwanda" | |
layer.fields.new :name => "FOSA ID", :code => "fosaid", :kind => :numeric | |
layer.fields.new :name => "Govt FOSA Code", :code => "govtfosacode", :kind => :numeric | |
field_type = layer.fields.new :name => "Type", :code => "type", :kind => :select_one, :config => {'options' => []} | |
field_network = layer.fields.new :name => "Network", :code => "network", :kind => :select_one, :config => {'options' => []} | |
field_ownership = layer.fields.new :name => "Ownership", :code => "ownership", :kind => :select_one, :config => {'options' => []} | |
field_status = layer.fields.new :name => "Status", :code => "status", :kind => :select_one, :config => {'options' => []} | |
layer.fields.new :name => "PBF Code", :code => "pbfcode", :kind => :numeric | |
layer.fields.new :name => "Population", :code => "population", :kind => :numeric | |
layer.fields.new :name => "Population Year", :code => "population_year", :kind => :numeric | |
layer.fields.new :name => "Population Source", :code => "population_source", :kind => :text | |
layer.fields.new :name => "CF Code", :code => "cfcode", :kind => :numeric | |
provinces = [] | |
districts = [] | |
sectors = [] | |
puts "Collecting provinces, districts and sectors" | |
rows.each do |row| | |
provinces.push(row[7].strip) unless row[7].blank? || provinces.include?(row[7]) | |
districts.push([row[8].strip, row[7].strip]) unless row[7].blank? || row[8].blank? || districts.any?{|p| p[0] == row[8]} | |
sectors.push([row[9].strip, row[8].strip]) unless row[8].blank? || row[9].blank? || sectors.any?{|p| p[0] == row[9]} | |
end | |
puts "Creating provinces" | |
provinces.map! do |name| | |
Site.new :name => name, :group => true, :collection_id => collection.id | |
end | |
puts "Creating districts" | |
districts.map! do |name, parent| | |
province = provinces.find{|p| p.name == parent} | |
district = Site.new :name => name, :group => true, :collection_id => collection.id | |
province.sites.push district | |
district | |
end | |
puts "Creating sectors" | |
sectors.map! do |name, parent| | |
district = districts.find{|p| p.name == parent} | |
sector = Site.new :name => name, :group => true, :collection_id => collection.id | |
district.sites.push sector | |
sector | |
end | |
puts "Creating sites" | |
sites = rows.map do |row| | |
[ | |
[field_type, 23, 3], | |
[field_network, 24, 4], | |
[field_ownership, 25, 6], | |
[field_status, 26, 12], | |
].each do |field, code_idx, label_idx| | |
if row[code_idx].present? && row[label_idx].present? | |
unless field.config['options'].any?{|o| o['code'] == row[code_idx].strip} | |
field.config['options'] << {'code' => row[code_idx].strip, 'label' => row[label_idx].strip} | |
end | |
end | |
end | |
site = Site.new({ | |
:name => row[1], | |
:lat => row[10].present? ? row[10].to_f : nil, | |
:lng => row[11].present? ? row[11].to_f : nil, | |
:collection_id => collection.id | |
}) | |
[ | |
['fosaid', 0, :to_i], | |
['govtfosacode', 2, :to_i], | |
['type', 23], | |
['network', 24], | |
['ownership', 25], | |
['status', 26], | |
['pbfcode', 15, :to_i], | |
['population', 16, :to_i], | |
['population_year', 17, :to_i], | |
['population_source', 18], | |
['cfcode', 19, :to_i] | |
].each do |name, row_idx, method| | |
unless row[row_idx].blank? | |
value = row[row_idx].strip | |
value = value.send method if method | |
site.properties[name] = value | |
end | |
end | |
site | |
end | |
[field_type, field_network, field_ownership, field_status].each do |field| | |
field.config['options'].sort_by!{|o| o['code'].to_i} | |
end | |
puts "Assigning hierarchy" | |
rows.each_with_index do |row, i| | |
site = sites[i] or next | |
parent_sector = row[9] | |
if parent_sector.present? | |
sector = sectors.find{|x| x.name == parent_sector.strip} or raise "Sector not found: #{parent_sector}" | |
sector.sites.push site | |
else | |
parent_district = row[8] | |
if parent_district.present? | |
district = districts.find{|x| x.name == parent_district.strip} or raise "District not found: #{parent_district}" | |
district.sites.push site | |
else | |
parent_province = row[7] | |
if parent_province.present? | |
province = provinces.find{|x| x.name == parent_province.strip} or raise "Province not found: #{parent_province}" | |
province.sites.push site | |
end | |
end | |
end | |
end | |
puts "Checking groups" | |
sites.each do |site| | |
if site && site.sites.length > 0 | |
site.group = true | |
site.location_mode = :manual | |
end | |
end | |
puts "Saving all" | |
provinces.each &:save! | |
layer.save! | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment