0
|
1 <?php
|
|
2 include_once('../interface/Lib_mb_utf8.php');
|
|
3 include_once('../interface/config.php');
|
|
4
|
|
5 set_time_limit(0);
|
|
6 ini_set('memory_limit', '-1');
|
|
7
|
|
8 $link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
|
|
9 mysql_query("SET NAMES utf8");
|
|
10
|
|
11 if (!$link_mysql) {
|
|
12 die('Could not connect: ' . mysql_error());
|
|
13 }
|
|
14
|
|
15 $db_selected = mysql_select_db($mysql_database, $link_mysql);
|
|
16 if (!$db_selected) {
|
|
17 die ('Can\'t use foo : ' . mysql_error());
|
|
18 }
|
|
19
|
|
20 function readCsvFile($fileName){
|
|
21 $fp=fopen("../search/csv_files/".$fileName,"r");
|
|
22 $data=fgetcsv($fp);
|
|
23 $columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2',
|
|
24 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT',
|
|
25 'Description']; //columns from input array/file
|
|
26 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
|
|
27 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
|
|
28 'PAGE','SECTION','CONTENT',
|
|
29 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
|
|
30 $columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge
|
|
31 $bookArray=array();
|
|
32 $count=0;
|
|
33 $line=0;
|
|
34 while(!feof($fp)){
|
|
35 $data=fgetcsv($fp);
|
|
36 if(sizeof($data)<sizeof($columnNameMappingArray)){
|
|
37 continue;
|
|
38 }
|
|
39 ///$bookId=$data[0];
|
|
40 foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data)
|
|
41 $bookInfo[$newName]=$data[$idx];
|
|
42 }
|
|
43 $bookId=$bookInfo['BOOK_ID'];
|
|
44 $coordinateArray=getCoordinateFromDatabase($bookId);
|
|
45 foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray
|
|
46 $bookArray[$count]=array_merge($bookInfo,$coordinate);
|
|
47 $bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]);
|
|
48 $count++;
|
|
49 }
|
|
50 }
|
|
51 fclose($fp);
|
|
52 return $bookArray;
|
|
53 }
|
|
54 function writeCsvFile($fileName,$bookArray){
|
|
55 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
|
|
56 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
|
|
57 'PAGE','SECTION','CONTENT',
|
|
58 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];
|
|
59 $fp=fopen("./datasets/".$fileName,"w");
|
|
60 fputcsv($fp,$columnNameArray);
|
|
61
|
|
62 foreach($bookArray as $book){
|
|
63 fputcsv($fp,$book);
|
|
64 }
|
|
65 fclose($fp);
|
|
66 }
|
|
67 function getCoordinateFromDatabase($bookId){
|
|
68 $query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude,
|
|
69 chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID
|
|
70 FROM coordinates_books
|
|
71 WHERE books_id='".$bookId."'";
|
|
72 $result = mysql_query($query);
|
|
73 $coordinateArray=array();
|
|
74 while ($row = mysql_fetch_assoc($result)) {
|
|
75 $coordinateArray[]=$row;
|
|
76 }
|
|
77 return $coordinateArray;
|
|
78 }
|
|
79
|
|
80 $fileName=$_GET['file'];
|
|
81 $name=$_GET['name'];
|
|
82 $bookArray=readCsvFile($fileName);
|
|
83 writeCsvFile($fileName,$bookArray);
|
|
84 header("Location: map.php?mode=1&file=".$fileName."&name=".$name);
|
|
85 ?>
|