Class: Jetel::Modules::Gadm

Inherits:
Module
  • Object
show all
Defined in:
lib/jetel/modules/gadm/gadm.rb

Instance Attribute Summary

Attributes inherited from Module

#downloader

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#download_dir, download_dir, #download_source, downloaded_file, #downloaded_file, extract_dir, #extract_dir, extracted_file, #extracted_file, #initialize, #load, #sources, target_dir, #target_dir, #transform_dir, transform_dir, transformed_file, #transformed_file, #unzip

Constructor Details

This class inherits a constructor from Jetel::Modules::Module

Class Method Details

.sourcesObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jetel/modules/gadm/gadm.rb', line 17

def sources
  page = Nokogiri::HTML(open('http://www.gadm.org/country'))

  options = page.css('select[name="cnt"] > option')

  res = options.map do |option|
    full_name = option['value']
    name = "#{full_name.split('_').first}"
    filename = "#{name}_adm_shp.zip"
    {
      name: name,
      url: "http://biogeo.ucdavis.edu/data/gadm2.8/shp/#{filename}",
      filename_downloaded: filename,
      flat: true,
      filename_transformed: "#{name}_adm?.topo.json"
    }
  end

  res
end

Instance Method Details

#download(global_options, options, args) ⇒ Object



39
40
41
42
43
# File 'lib/jetel/modules/gadm/gadm.rb', line 39

def download(global_options, options, args)
  self.class.sources.pmap do |source|
    download_source(source, global_options.merge(options))
  end
end

#extract(global_options, options, args) ⇒ Object



45
46
47
48
49
# File 'lib/jetel/modules/gadm/gadm.rb', line 45

def extract(global_options, options, args)
  self.class.sources.pmap do |source|
    unzip(source, global_options.merge(options))
  end
end

#transform(global_options, options, args) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jetel/modules/gadm/gadm.rb', line 51

def transform(global_options, options, args)
  self.class.sources.pmap(8) do |source|
    extracted_file = extracted_file(source, global_options.merge(options))
    transformed_file = transformed_file(source, global_options.merge(options))
    dest_dir = transform_dir(source, global_options.merge(options))
    FileUtils.mkdir_p(dest_dir)

    extracted_dir = extract_dir(source, global_options.merge(options))
    Dir.glob("#{extracted_dir}/*.shp") do |shapefile|
      puts "Transforming #{shapefile}"

      # "topojson data/Gadm/AFG/extracted/AFG_adm0.shp -o data/Gadm/AFG/transformed/AFG_adm0.topo.json"
      destfile = shapefile.gsub(extracted_dir, dest_dir).gsub('.shp', '.topo.json')
      cmd = "topojson --no-stitch-poles --no-force-clockwise #{shapefile} -o #{destfile} -p"
      puts cmd
      PTY.spawn(cmd) do |stdout, stdin, pid|
        begin
          # Do stuff with the output here. Just printing to show it works
          stdout.each { |line| print line }
        end
      end
    end
  end
end