Skip to content

Commit

Permalink
fix: NCDD parser
Browse files Browse the repository at this point in the history
  • Loading branch information
dwilkie committed Aug 8, 2023
1 parent 2fddc65 commit fa41129
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions lib/pumi/data_source/ncdd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ def administrative_unit
"ភូមិ" => AdministrativeUnit.new(en: "Village", km: "ភូមិ", latin: "Phum", code_length: 8, group: "villages")
}.freeze

attr_accessor :existing_data

def initialize(data_files: default_data_files)
@existing_data = data_files.each_with_object({}) do |data_file, result|
result[data_file.type] = data_file.read
end
end

def load_data!(source_dir: "tmp", output_dir: "data")
source_files(source_dir).each do |file|
parse_source_file(file)
Expand All @@ -37,21 +45,21 @@ def load_data!(source_dir: "tmp", output_dir: "data")

private

def data
@data ||= {}
end

def parse_source_file(file)
CSV.read(file, headers: CSV_HEADERS).each do |csv_row|
row = build_row(csv_row)

next unless row.code
next if row.administrative_unit.code_length != row.code.length

write_location(row)
add_data(row)
end
end

def data
@data ||= {}
end

def build_row(row)
Row.new(
code: parse_location_code(row),
Expand All @@ -69,9 +77,10 @@ def parse_location_code(row)
code
end

def write_location(row)
def add_data(row)
data[row.administrative_unit.group] ||= {}
data[row.administrative_unit.group][row.code] = {
data[row.administrative_unit.group][row.code] = existing_data.dig(row.administrative_unit.group, row.code) || {}
data[row.administrative_unit.group][row.code].merge!(
"name" => {
"km" => row.name_km,
"latin" => row.name_latin
Expand All @@ -81,7 +90,7 @@ def write_location(row)
"latin" => row.administrative_unit.latin,
"en" => row.administrative_unit.en
}
}
)
end

def source_files(source_dir)
Expand All @@ -95,6 +104,14 @@ def write_data!(output_dir)
DataFile.new(data_group).write(data.fetch(data_group), data_directory: output_dir)
end
end

def default_data_files
[
DataFile.new(:districts),
DataFile.new(:communes),
DataFile.new(:villages)
]
end
end
end
end

0 comments on commit fa41129

Please sign in to comment.