-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathprocess.rb
executable file
·66 lines (52 loc) · 1.42 KB
/
process.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
require 'stringex'
def format_str(input)
if input.nil?
return nil
end
input.downcase.to_ascii.gsub(' ', '_')
end
def consolidate_all_features(hash, header)
hash.each do |k, v|
p k
group = {}
group["crs"] = header
group["features"] = v
group["type"] = "FeatureCollection"
output_file = File.new(k + "/all.geojson", "w+")
output_file.truncate(0)
output_file << group.to_json
end
end
file = File.read('all.geojson')
data = JSON.parse(file)
header = data["crs"]
features_by_prov = {}
features_by_reg = {}
data["features"].each do |feature|
props = feature["properties"]
region = format_str(props["NOM_REG"])
if region.nil?
next
end
province = format_str(props["NOM_PROV"])
county = format_str(props["NOM_COM"])
dir = [region, province].join('/')
FileUtils::mkdir_p dir
features_by_reg[region] = [] if features_by_reg[region].nil?
features_by_reg[region] << feature
features_by_prov[dir] = [] if features_by_prov[dir].nil?
features_by_prov[dir] << feature
filename = [region, province, county + ".geojson"].join('/')
p "Writing #{filename}"
output_file = File.new(filename, "w+")
output_file.truncate(0)
value = {}
feature["crs"] = header
output_file << feature.to_json
end
p "Generating feature group"
consolidate_all_features(features_by_prov, header)
consolidate_all_features(features_by_reg, header)