Mercurial > hg > LGMap
comparison get_coordinates_for_listed_books.php @ 6:30b59e7b88c0
add php error log
author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
---|---|
date | Thu, 25 Jun 2015 17:47:11 +0200 |
parents | b85894a9b2dc |
children |
comparison
equal
deleted
inserted
replaced
5:6095925edcba | 6:30b59e7b88c0 |
---|---|
17 die ('Can\'t use foo : ' . mysql_error()); | 17 die ('Can\'t use foo : ' . mysql_error()); |
18 } | 18 } |
19 | 19 |
20 function readCsvFile($fileName){ | 20 function readCsvFile($fileName){ |
21 global $server_host; | 21 global $server_host; |
22 $fp=fopen($server_host."LGSearch/csv_files/".$fileName,"r"); | 22 |
23 $data=fgetcsv($fp); | 23 if ($fp = fopen($server_host."LGSearch/csv_files/".$fileName,"r") ) { |
24 $columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2', | 24 |
25 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT', | 25 // $fp=fopen($server_host."LGSearch/csv_files/".$fileName,"r"); |
26 'Description']; //columns from input array/file | |
27 $columnNameArray=['Address','LEVEL1','LEVEL2', | |
28 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', | |
29 'PAGE','SECTION','CONTENT', | |
30 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php) | |
31 /* | |
32 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2', | |
33 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', | |
34 'PAGE','SECTION','CONTENT', | |
35 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php) | |
36 */ | |
37 $columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge | |
38 $bookArray=array(); | |
39 $count=0; | |
40 $line=0; | |
41 while(!feof($fp)){ | |
42 $data=fgetcsv($fp); | 26 $data=fgetcsv($fp); |
43 if(sizeof($data)<sizeof($columnNameMappingArray)){ | 27 $columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2', |
44 continue; | 28 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT', |
29 'Description']; //columns from input array/file | |
30 $columnNameArray=['Address','LEVEL1','LEVEL2', | |
31 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', | |
32 'PAGE','SECTION','CONTENT', | |
33 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php) | |
34 /* | |
35 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2', | |
36 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', | |
37 'PAGE','SECTION','CONTENT', | |
38 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php) | |
39 */ | |
40 $columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge | |
41 $bookArray=array(); | |
42 $count=0; | |
43 $line=0; | |
44 while(!feof($fp)){ | |
45 $data=fgetcsv($fp); | |
46 if(sizeof($data)<sizeof($columnNameMappingArray)){ | |
47 continue; | |
48 } | |
49 ///$bookId=$data[0]; | |
50 foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data) | |
51 $bookInfo[$newName]=$data[$idx]; | |
52 } | |
53 $bookId=$bookInfo['BOOK_ID']; | |
54 $coordinateArray=getCoordinateFromDatabase($bookId); | |
55 foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray | |
56 $bookArray[$count]=array_merge($bookInfo,$coordinate); | |
57 $bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]); | |
58 $count++; | |
59 } | |
45 } | 60 } |
46 ///$bookId=$data[0]; | 61 fclose($fp); |
47 foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data) | 62 |
48 $bookInfo[$newName]=$data[$idx]; | 63 } else { |
49 } | 64 // file open failed |
50 $bookId=$bookInfo['BOOK_ID']; | 65 error_log("error when opening file: ".$server_host."LGSearch/csv_files/".$fileName, 0); |
51 $coordinateArray=getCoordinateFromDatabase($bookId); | 66 return array(); |
52 foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray | |
53 $bookArray[$count]=array_merge($bookInfo,$coordinate); | |
54 $bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]); | |
55 $count++; | |
56 } | |
57 } | 67 } |
58 fclose($fp); | 68 |
59 return $bookArray; | 69 return $bookArray; |
60 } | 70 } |
61 function writeCsvFile($fileName,$bookArray){ | 71 function writeCsvFile($fileName,$bookArray){ |
62 /* | 72 /* |
63 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2', | 73 $columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2', |
67 */ | 77 */ |
68 $columnNameArray=['Address','LEVEL1','LEVEL2', | 78 $columnNameArray=['Address','LEVEL1','LEVEL2', |
69 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', | 79 'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude', |
70 'PAGE','SECTION','CONTENT', | 80 'PAGE','SECTION','CONTENT', |
71 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; | 81 'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; |
72 $fp=fopen("./datasets/".$fileName,"w"); | 82 |
73 fputcsv($fp,$columnNameArray); | 83 if ($fp=fopen("./datasets/".$fileName,"w")) { |
84 | |
85 //$fp=fopen("./datasets/".$fileName,"w"); | |
86 fputcsv($fp,$columnNameArray); | |
74 | 87 |
75 foreach($bookArray as $book){ | 88 foreach($bookArray as $book){ |
76 fputcsv($fp,$book); | 89 fputcsv($fp,$book); |
90 } | |
91 fclose($fp); | |
92 } else { | |
93 error_log("error when opening file: "."./datasets/".$fileName, 0); | |
77 } | 94 } |
78 fclose($fp); | 95 |
79 } | 96 } |
80 function getCoordinateFromDatabase($bookId){ | 97 function getCoordinateFromDatabase($bookId){ |
81 /* | 98 /* |
82 $query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude, | 99 $query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude, |
83 chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID | 100 chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID |