Module: Barton::Setup

Defined in:
lib/barton/core.rb

Class Method Summary collapse

Class Method Details

.create_geobox(boundaries) ⇒ Object

Creates a minimum bounded box encapsulating the boundary polygons



266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/barton/core.rb', line 266

def self.create_geobox( boundaries )
  return nil unless boundaries.instance_of? Array
  box = Hash.new
  boundaries.each do |b|
    b.split( ' ' ).each do |c|
      coords = c.split( ',' )
      box['north'] = coords[1].to_f if box['north'].nil? or coords[1].to_f > box['north']
      box['south'] = coords[1].to_f if box['south'].nil? or coords[1].to_f < box['south']
      box['east'] = coords[0].to_f if box['east'].nil? or coords[0].to_f > box['east']
      box['west'] = coords[0].to_f if box['west'].nil? or coords[0].to_f < box['west']
    end
  end
  box
end

.load_file(filename) ⇒ Object

Load data from yaml source file



282
283
284
285
286
287
288
# File 'lib/barton/core.rb', line 282

def self.load_file( filename )
  puts "Loading data from #{filename} to #{Barton.environment}....."
  electorates = self.parse_yaml( filename )
  puts "Failed to load #{filename}" if electorates.nil?
  connected = Data.update_es( electorates )
  abort "Connection to Elasticsearch failed" if connected.nil?
end

.parse_yaml(filename) ⇒ Object

Parses a electorate yaml file, merges it with a geo yaml file,

loads them to the datastore and resaves the yaml files


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/barton/core.rb', line 208

def self.parse_yaml( filename )
  data = Array.new
  electorates = YAML::load_file( filename ) if File.exist?( filename )
  geofile = "#{File.dirname( filename )}/geo/#{File.basename( filename )}"
  geo = YAML::load_file( geofile ) if File.exist?( geofile )
  if electorates.respond_to?( 'each' )
    electorates.each do |e|
      # id is the first 6 digits of a hash of object name with file name EG BulimbaQLD-State.yaml
      e['id'] = Digest::SHA1.hexdigest( [e['name'], filename].join )[0..5].force_encoding('utf-8') unless e['id'] or not e['name']
      # make geobox and update geo.yaml if boundaries present
      if e.has_key?( 'boundaries')
        e['geobox'] = self.create_geobox( e['boundaries'] )
      # else merge geo if no boundaries are present
      elsif geo and geo.has_key?( e['id'] ) and geo[e['id']].has_key?( 'boundaries' )
        e['boundaries'] = geo[e['id']]['boundaries']
        e['geobox'] = geo[e['id']]['geobox']
      end

      ei = e.clone   # this will be the electorate that is indexed
      ei['type'] = 'electorate'

      if e.has_key?( 'members' )
        # create seperate types for member details
        members = Array.new
        e['members'].each do |m|
          m['role'] = "Member for #{e['name']}" unless m['role']
          m['id'] = Digest::SHA1.hexdigest( [m['role'], filename].join )[0..5].force_encoding('utf-8') unless m['id'] or not m['role']
          # only add this for indexing
          mi = m.clone
          mi['type'] = 'member'
          mi['tags'] = [] unless m.has_key?( 'tags' )
          mi['tags'] |= e['tags'] if e.has_key?( 'tags' )
          mi['electorate'] = {'name' => e['name'], 'id' => e['id']}
          data.push( mi )
          members.push( {'role' => m['role'], 'name' => m['name'], 'id' => m['id']})
        end
        # only index limited member data
        ei['members'] = members
      end
      data.push( ei )
    end
    # save parsed yaml
    data_yaml = Array.new
    geo_yaml = Hash.new
    electorates.each do |e|
      # remove geo from data
      geo_yaml[e['id']] = Hash['boundaries' => e['boundaries'], 'geobox' => e['geobox']] if e.has_key?( 'boundaries' )
      data_yaml.push( e.select { |k,v| not ['boundaries', 'geobox', 'type'].include? k } )
    end
    File.open( filename, 'w+' ) { |f| f.puts( YAML.dump( data_yaml ) ) }
    File.open( geofile, 'w+' ) { |f| f.puts( YAML.dump( geo_yaml ) ) }
    data
  else
    nil
  end
end