diff get_coordinates_for_listed_books.php @ 0:57bde4830927

first commit
author Zoe Hong <zhong@mpiwg-berlin.mpg.de>
date Tue, 24 Mar 2015 11:37:17 +0100
parents
children de2c442b6cbb
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/get_coordinates_for_listed_books.php	Tue Mar 24 11:37:17 2015 +0100
@@ -0,0 +1,85 @@
+<?php
+include_once('../interface/Lib_mb_utf8.php');
+include_once('../interface/config.php');
+
+set_time_limit(0);
+ini_set('memory_limit', '-1');
+
+$link_mysql = mysql_connect($mysql_server, $mysql_user, $mysql_password);
+mysql_query("SET NAMES utf8");
+
+if (!$link_mysql) {
+        die('Could not connect: ' . mysql_error());
+}
+
+$db_selected = mysql_select_db($mysql_database, $link_mysql);
+if (!$db_selected) {
+        die ('Can\'t use foo : ' . mysql_error());
+}
+
+function readCsvFile($fileName){
+	$fp=fopen("../search/csv_files/".$fileName,"r");
+	$data=fgetcsv($fp);
+	$columnNameMappingArray=['BOOK_ID','LEVEL1','LEVEL2',
+                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','PAGE','SECTION','CONTENT',
+                'Description']; //columns from input array/file
+	$columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
+                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
+		'PAGE','SECTION','CONTENT',
+                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description']; //columns for output array/file (for map.php)
+	$columnNameArray=array_flip($columnNameArray); //key, value swap in order to keep the order of the columns after merge
+	$bookArray=array();
+	$count=0;
+	$line=0;
+	while(!feof($fp)){
+		$data=fgetcsv($fp);
+		if(sizeof($data)<sizeof($columnNameMappingArray)){
+			continue;
+		}
+		///$bookId=$data[0];
+                foreach($columnNameMappingArray as $idx=>$newName){ //use column names as the array indices (instead of 0,1,2,... as from $data)
+                        $bookInfo[$newName]=$data[$idx];
+                }
+                $bookId=$bookInfo['BOOK_ID'];
+		$coordinateArray=getCoordinateFromDatabase($bookId);
+		foreach($coordinateArray as $coordinate){ //merge a record from $booArray and ots cooresponding coordinateArray
+			$bookArray[$count]=array_merge($bookInfo,$coordinate);
+			$bookArray[$count]=array_merge($columnNameArray,$bookArray[$count]);	
+			$count++;
+		}
+	}
+	fclose($fp);
+	return $bookArray;
+}
+function writeCsvFile($fileName,$bookArray){
+	$columnNameArray=['Address','ADMIN_TYPE','LEVEL1','LEVEL2',
+                'Name','PERIOD','TimeSpan:begin','TimeSpan:end','Longitude','Latitude',
+		'PAGE','SECTION','CONTENT',
+                'BOOK_ID','CHGIS_ID','1820_ID','1911_ID','CBDB_ID','Description'];
+	$fp=fopen("./datasets/".$fileName,"w");
+	fputcsv($fp,$columnNameArray);
+	
+	foreach($bookArray as $book){
+		fputcsv($fp,$book);	
+	}
+	fclose($fp);
+}
+function getCoordinateFromDatabase($bookId){
+	$query="SELECT place_name AS Address, admin_type AS ADMIN_TYPE, x AS Longitude, y AS Latitude,
+			chgis_id AS CHGIS_ID, 1820_id AS 1820_ID, 1911_id AS 1911_ID, cbdb_id AS CBDB_ID 
+			FROM coordinates_books 
+			WHERE books_id='".$bookId."'";
+	$result = mysql_query($query);
+	$coordinateArray=array();
+	while ($row = mysql_fetch_assoc($result)) {
+		$coordinateArray[]=$row;
+	}
+	return $coordinateArray;
+}
+
+$fileName=$_GET['file'];
+$name=$_GET['name'];
+$bookArray=readCsvFile($fileName);
+writeCsvFile($fileName,$bookArray);
+header("Location: map.php?mode=1&file=".$fileName."&name=".$name);
+?>