-
Notifications
You must be signed in to change notification settings - Fork 3
/
uk-wards-2024.pl
130 lines (105 loc) · 3.03 KB
/
uk-wards-2024.pl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/perl
use utf8;
use JSON::XS;
use Data::Dumper;
# A JSON containing the property "final" which points to where the combined file is
$config = getJSON("config.json");
if($ARGV[0] eq "split"){
splitRegions();
}elsif($ARGV[0] eq "combine"){
combineRegions();
}elsif($ARGV[0] eq "ticklist"){
createTickList();
}
simplify();
#############################
sub getJSON {
my (@files,$str,@lines);
my $file = $_[0];
open(FILE,$file);
@lines = <FILE>;
close(FILE);
$str = join("",@lines);
return JSON::XS->new->decode($str);
}
sub makeJSON {
my $json = shift;
$txt = JSON::XS->new->utf8->canonical(1)->pretty->space_before(0)->encode($json);
$txt =~ s/ /\t/g;
$txt =~ s/(\t{3}.*)\n/$1/g;
$txt =~ s/\,\t{3}/\, /g;
$txt =~ s/\t{2}\}(\,?)\n/ \}$1\n/g;
$txt =~ s/\{\n\t{3}/\{ /g;
$txt =~ s/\"\: /\"\:/g;
$txt =~ s/\, \"/\,\"/g;
$txt =~ s/":\{ "/":\{"/g;
$txt =~ s/\" \},/\"\},/g;
return $txt;
}
sub simplify {
print "Simplifying the content of $config->{'final'}\n";
my ($regions,$id,$region,$fh);
my $hexjson = getJSON($config->{'final'});
foreach $id (keys(%{$hexjson->{'hexes'}})){
foreach $d (keys(%{$hexjson->{'hexes'}{$id}})){
if($d ne "q" && $d ne "r" && $d ne "name" && $d ne "n"){
delete $hexjson->{'hexes'}{$id}{$d};
}
}
}
my $simple = makeJSON($hexjson);
$simple =~ s/\t//gs;
$simple =~ s/ \}/\}/gs;
open($fh,">","uk-wards-2024.hexjson");
print $fh $simple;
close($fh);
}
sub createTickList {
print "Creating tick list from $config->{'final'}\n";
my ($lads,$id,$lad,$fh);
my $hexjson = getJSON($config->{'final'});
foreach $id (keys(%{$hexjson->{'hexes'}})){
$lad = $hexjson->{'hexes'}{$id}{'LAD24CD'};
$lads->{$lad} = {'name'=>$hexjson->{'hexes'}{$id}{'LAD24NM'},'region'=>$hexjson->{'hexes'}{$id}{'RGN24CD'}};
}
foreach $lad (sort(keys(%{$lads}))){
print "- [ ] [$lad](https://open-innovations.org/projects/hexmaps/editor/?https://open-innovations.github.io/uk-wards-2024/$lads->{$lad}{'region'}.hexjson) - $lads->{$lad}{'name'}\n";
}
}
sub splitRegions {
print "Splitting regions in $config->{'final'}\n";
my ($regions,$id,$region,$fh);
my $hexjson = getJSON($config->{'final'});
foreach $id (keys(%{$hexjson->{'hexes'}})){
# print "$id\n";
$region = $hexjson->{'hexes'}{$id}{'RGN24CD'};
if(!$regions->{$region}){
$regions->{$region} = {'layout'=>$hexjson->{'layout'},'hexes'=>{}};
}
$regions->{$region}{'hexes'}{$id} = $hexjson->{'hexes'}{$id};
}
foreach $region (sort(keys(%{$regions}))){
print $region."\n";
open($fh,">",$region.".hexjson");
print $fh makeJSON($regions->{$region});
close($fh);
}
}
sub combineRegions {
my ($tmp,$dh,$filename,$json,$hex,$fh);
$json = {'layout'=>'odd-r','hexes'=>{}};
opendir($dh,"./");
while(($filename = readdir($dh))){
if($filename =~ /[ENSW][0-9]{8}.hexjson$/){
print "Read from $filename\n";
$tmp = getJSON($filename);
foreach $hex (keys(%{$tmp->{'hexes'}})){
$json->{'hexes'}{$hex} = $tmp->{'hexes'}{$hex};
}
}
}
closedir($dh);
open($fh,">",$config->{'final'});
print $fh makeJSON($json);
close($fh);
}