-
Notifications
You must be signed in to change notification settings - Fork 0
/
giessen_stadt_xls_parser_2011_stand.php
373 lines (327 loc) · 9.88 KB
/
giessen_stadt_xls_parser_2011_stand.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
// includes
require_once 'inc/gruppen.inc.php';
require_once 'lib/phpExcelReader/Excel/reader.php';
// excel reader
$data = new Spreadsheet_Excel_Reader();
$data->setUTFEncoder('iconv');
#$data->setOutputEncoding('UTF-8');
$data->setOutputEncoding('ISO-8859-1');
// open file
$data->read('pdfs/GI/Stadt/684_7030_1_open.xls');
// error reporting
error_reporting(E_ALL ^ E_NOTICE);
// print first info
echo sizeof($data->sheets)." Pages\n";
// set filename
$file = basename(__FILE__);
$file = str_replace(".php", "_", $file);
// run
$infos = array();
foreach($data->sheets as $side => $page) {
for ($i=1; $i<=$page['numRows']; $i++) {
$cell = $page["cells"][$i][1];
$cell = trim($cell);
if (strpos($cell, "Kennzahlen f") !== false) {
// extracted info
$info = array(
"page" => ($side+1),
"data" => array(),
);
// find kennzahl
preg_match_all("/(\d)+/", $cell, $regs);
#print_r($regs);
$idx = $regs[0][0];
$info["idx"] = $idx;
// complex parsing errors
if (!empty($idx)) {
$cplx = 0;
$cell = explode($idx, $cell);
if (isset($cell[1])) {
#print_r($cell);
if (strpos($cell[1], "Plan 2009") !== false) {
$cplx++;
if (strpos($cell[1], "Ist 2009") !== false) {
$cplx++;
if (strpos($cell[1], "Plan 2010") !== false) {
$cplx++;
}
}
}
}
#echo $cplx."\n";
}
// get short description
for ($ii=1; $ii<=$page['numRows']; $ii++) {
for ($j=1; $j<=2; $j++) {
$cell = $page["cells"][$ii][$j];
$search = "Gruppe ".$info["idx"];
if (strpos($cell, $search) !== false) {
$cell = explode($search, $cell);
$cell = array_pop($cell);
$cell = trim($cell);
#echo $cell."\n";
$info["short"] = $cell;
}
}
}
// set group and maybe fix idx
if (strlen($info["idx"]) != 8) {
preg_match_all("/(\d)+/", $info["short"], $regs);
#print_r($regs);
if (isset($regs[0][0])) {
$info["idx"] = $regs[0][0];
$info["short"] = str_replace($info["idx"], "", $info["short"]);
$info["short"] = trim($info["short"]);
print_r($info);
}
#die();
}
$info["group"] = substr($info["idx"], 0, 2);
// get long description
$start_row = 0;
$end_row = 0;
for ($ii=1; $ii<=$page['numRows']; $ii++) {
$cell = $page["cells"][$ii][1];
$cell = trim($cell);
#echo $cell."\n";
if (strpos($cell, "Beschreibung") !== false) {
$start_row = $ii;
} else if (strpos($cell, "Ziele") !== false) {
$end_row = $ii;
break;
} else if (strpos($cell, "Kennzahlen") !== false) {
$end_row = $ii;
break;
}
}
#echo $start_row.":".$end_row."\n";
$long = "";
for ($ii=$start_row; $ii<=$end_row; $ii++) {
for ($j=1; $j<=$page['numCols']; $j++) {
$cell = $page["cells"][$ii][$j];
$cell = str_replace("Beschreibung", "", $cell);
$cell = trim($cell);
#echo $cell."\n";
if (strpos($cell, "Ziele") !== false)
break;
else if (strpos($cell, "Kennzahlen") !== false)
break;
else {
$long .= $cell;
$long = trim($long);
$long .= " ";
}
}
}
$search = "Gruppe ".$info["idx"];
if (strpos($long, $search) !== false) {
$long = explode($search, $long);
$long = $long[1];
}
if (strpos($long, "Verantw") !== false && strpos($long, "Org") !== false && strpos($long, "Einheit") !== false) {
$long = explode("Verantw.Org.Einheit", $long);
$long = $long[0];
}
$long = trim($long);
#echo $long."\n";
$info["long"] = $long;
// get organisations einheit
for ($ii=1; $ii<=$page['numRows']; $ii++) {
$cell = $page["cells"][$ii][1];
if (strpos($cell, "Verantw") !== false && strpos($cell, "Org") !== false && strpos($cell, "Einheit") !== false) {
#echo $cell."\n";
$tcell = $page["cells"][$ii][2];
$tcell = trim($tcell);
#echo $tcell."\n";
if (empty($tcell)) {
$tcell = explode("Einheit", $cell);
#print_r($tcell);
$tcell = $tcell[1];
$tcell = trim($tcell);
}
preg_match_all("/\-(\d+)\-/", $tcell, $regs);
#print_r($regs);
$tcell = str_replace("-".$regs[1][0]."-", "", $tcell);
$tcell = trim($tcell);
$info["org"] = array(
"id" => $regs[1][0],
"text" => $tcell,
);
#print_r($info);
}
}
// debug
#print_r($info);
// find col indexes
$idx_2009 = -1;
$idx_2009_sub = -1;
$idx_2010 = -1;
$idx_2010_sub = -1;
$idx_2011 = -1;
echo ($side+1)." ".$i." ".$page["cells"][$i][1]."\n";
for ($j=2; $j<=$page['numCols']; $j++) {
$cell = $page["cells"][$i][$j];
$cell = trim($cell);
switch ($cell) {
case "Plan 2009":
break;
case "Ist 2009":
$idx_2009 = $j;
break;
case "Plan 2009 Ist 2009":
$idx_2009 = $j;
$idx_2009_sub = 1;
break;
case "Plan 2010":
$idx_2010 = $j;
break;
case "Plan 2011":
$idx_2011 = $j;
break;
default:
if (!empty($cell)) {
echo $cell;
die();
}
}
/*
if (!empty($cell)) {
echo "\t".$j." ".$cell."\n";
}
*/
}
#echo $idx_2009." : ".$idx_2010." : ".$idx_2011."\n";
// extract annual numbers
/* ordentliche aufwendungen nehmen oder verwaltungsergebnis */
for ($ii=$i; $ii<=$page['numRows']; $ii++) {
$cell = $page["cells"][$ii][1];
if (strpos($cell, "Ordentliche") !== false && strpos($cell, "Aufwendungen") !== false) {
// 2009
// default
$val_2009 = $page["cells"][$ii][$idx_2009];
$val_2009 = createNumber($val_2009);
// simple parsing error
if ($idx_2009_sub != -1) {
$val_2009 = explode(" ", $val_2009);
$val_2009 = $val_2009[$idx_2009_sub];
$val_2009 = createNumber($val_2009);
}
// set
$info["data"]["2009"] = $val_2009;
// 2010
$val_2010 = $page["cells"][$ii][$idx_2010];
$val_2010 = createNumber($val_2010);
$info["data"]["2010"] = $val_2010;
// 2011
$val_2011 = $page["cells"][$ii][$idx_2011];
$val_2011 = createNumber($val_2011);
$info["data"]["2011"] = $val_2011;
// exit
break;
}
}
// look on next page
if (empty($info["data"])) {
$nextpage = $data->sheets[($side+1)];
for ($ii=1; $ii<=$nextpage['numRows']; $ii++) {
$cell = $nextpage["cells"][$ii][1];
$row = "";
if (strpos($cell, "Ordentliche") !== false && strpos($cell, "Aufwendungen") !== false) {
// aggregate
for ($j=1; $j<=$nextpage['numCols']; $j++) {
$cell = $nextpage["cells"][$ii][$j];
$cell = trim($cell);
$row .= $cell." ";
}
$row = trim($row);
// prepare for fix
$cell = $row;
$cplx = 4;
break;
}
}
}
// fix complex parsing errors
if ($cplx > 0) {
$cell = explode(" ", $cell);
#print_r($cell);
if ($cplx == 4) {
$val_2011 = createNumber(array_pop($cell));
$info["data"]["2011"] = $val_2011;
$cplx--;
}
if ($cplx == 3) {
$val_2010 = createNumber(array_pop($cell));
$info["data"]["2010"] = $val_2010;
$cplx--;
}
if ($cplx == 2) {
array_pop($cell);
$cplx--;
}
if ($cplx == 1) {
$val_2009 = createNumber(array_pop($cell));
$info["data"]["2009"] = $val_2009;
}
}
// debug
print_r($info);
// check for parsing errors
if (!isset($val_2009) || !isset($val_2010) || !isset($val_2011)) {
echo "\n!!! PARSING ERROR NUMVER VALUES !!!\n";
die();
} else if (empty($info["data"])) {
echo "\n!!! PARSING ERROR NUMVER VALUES !!!\n";
die();
}
if (!isset($info["long"]) || !isset($info["short"]) || !isset($info["org"])) {
echo "\n!!! PARSING ERROR TEXT VALUES !!!\n";
die();
} else if (empty($info["long"]) || empty($info["short"]) || empty($info["org"])) {
echo "\n!!! PARSING ERROR TEXT VALUES !!!\n";
die();
}
// debug
#file_put_contents("data/".$file."page_".$side.".txt", var_export($page, true));
#die();
// add data
$gid = $info["group"];
if (!isset($infos[$gid]))
$infos[$gid] = array(
"text" => $gruppen[$gid],
"data" => array(),
);
unset($info["group"]);
$oid = intval($info["org"]["id"]);
if (!isset($infos[$gid]["data"][$oid]))
$infos[$gid]["data"][$oid] = array(
"text" => $info["org"]["text"],
"data" => array(),
);
unset($info["org"]);
$idx = intval($info["idx"]);
unset($info["idx"]);
$infos[$gid]["data"][$oid]["data"][$idx] = $info;
// output
echo "\n";
}
}
}
// sort
ksort($infos);
foreach($infos as $oid => $data) {
ksort($data["data"]);
$infos[$oid]["data"] = $data["data"];
}
// write files
file_put_contents("data/".$file."data.txt", serialize($infos));
file_put_contents("data/".$file."data_raw.txt", var_export($infos, true));
// functions ##########################################################################################################################
function createNumber($text) {
$text = str_replace(".", "", $text);
$text = str_replace(",", ".", $text);
$text = trim($text);
return floatval($text);
}
?>