\n") === FALSE) { echo "Cannot write to file."; exit; } // Open file containing previously geocoded locations and read entire file into a string $cacheFileHandle = fopen($cacheFileName, "r"); $cacheString = fread($cacheFileHandle, filesize($cacheFileName)); fclose($cacheFileHandle); // Feed geocode cache file into XML parser and then read into array of known locations $cache = new SimpleXMLElement($cacheString); foreach ($cache->geocode as $geocode) { $coords = $geocode['latitude'] . "," . $geocode['longitude']; $knownLocs[(string)($geocode['location'])] = $coords; } $rows = 0; $valid = 0; $geo = 0; $requested = false; // Feed exported file into XML parser $workbook = new SimpleXMLElement($entire_XML_File); // Loop thru file and try to extract data from every 'Row' element from within every worksheet and table foreach ($workbook->Worksheet as $worksheet) { foreach ($worksheet->Table as $table) { $firstRow = true; foreach ($table->Row as $row) { // Ignore first row, which contains headings if ($firstRow) { $firstRow = false; } else { $colNum = 1; // used to keep track of current column while stepping thru each row $filename = ""; // unique identifier of each image $title = ""; // title of image $location = ""; // geographic place name $imageNum = ""; // image number used by archive server to identify each image $status = -1; // status returned by geocoder $rows++; // Loop thru every cell in a row extracting needed data // If a cell is empty it is not included in the exported XML file and the next cell // with data has an added attribute ('ssIndex') with the current column number foreach ($row->Cell as $cell) { // If the cell has a column index, set column counter to the given index if ((int) $cell['ssIndex'] > 0) { $colNum = (int) $cell['ssIndex']; } if ($colNum == FILENAME_COLUMN) { // The filename needed to get the URL of the image $filename = $cell->Data; $filename = str_replace(".jpg", "", $filename); // Remove file extension } elseif ($colNum == TITLE_COLUMN) { // The title that will be used for the image $title = $cell->Data; $title = str_replace("\"", """, $title); // Replace any quotation marks $title = str_replace("&", "&", $title); // Replace any ampersand marks $title = str_replace("<", "<", $title); // Replace any less than marks $title = str_replace(">", ">", $title); // Replace any greater than marks } elseif ($colNum == LOCATION_COLUMN) { // The location given for the image $location = $cell->Data; // Remove '()' from around state name and replace with ',' after city name $location = str_replace(" (", ", ", $location); $location = str_replace(")", "", $location); // If more than one location is given (separated by ';'), use only the first $pos = stripos($location, ";"); if ($pos !== false) { $location = substr($location, 0, $pos); } // Check to see if we already have this location, if not then // request coordinates from Google Maps API geocoder and add to $knownLocs array if found if (array_key_exists($location, $knownLocs)) { $response = $knownLocs[$location]; } elseif (array_key_exists($location, $unknownLocs)) { // Do Nothing } else { $response = googleGeocode($location); // add to $knownLocs array if found, add to $unknownLocs array if not found (and not already there) if ($response) { $knownLocs[$location] = $response; } else { if ($filename != "") $unknownLocs[$location] = $filename; } $requested = true; } if ($response) { list($latitude, $longitude) = explode(",", $response); } if ($requested) $geo++; $requested = false; } elseif ($colNum == IMAGENUM_COLUMN) { // The image number needed for its thumbnail URL $imageNum = $cell->Data; // Strip file extension $pos = stripos($imageNum, "."); if ($pos !== false) { $imageNum = substr($imageNum, 0, $pos); } } $colNum++; } // If a filename, title, and image number were found in a row include // it in the output file, otherwise ignore if (($filename !== "") && ($title !== "") && ($imageNum !== "")) { /*** For debugging purposes ********************* printf("Title: %s
", $title); printf("Location: %s
", $location); printf("Status: %s
", $status); printf("Image Number: %s
", $imageNum); *************************************************/ if ($response) { // Construct the XML element // All < > " symbols have to be replaced with < > " respectively $output = " \n"; fwrite($writeHandle, $output); $valid++; } else { // If not a valid location output to the screen //echo "Not Found: " . $filename . " Location: " . $location . "
"; } } } } } } fwrite($writeHandle, ""); fclose($readHandle); fclose($writeHandle); writeGeoCacheFile($knownLocs, $cacheFileName); echo "File Processing Complete
"; echo "" . $rows . " items were in the import file.
"; echo "" . $valid . " items were able to be geocoded.
"; echo "" . $geo . " of these were unique locations.
"; //echo "" . $pre . "
"; //echo "previousLocs length = " . count($knownLocs); //print_r($knownLocs); printUnknownLocs($unknownLocs); /********** BEGIN FUNCTIONS **********/ function googleGeocode($location) { $locNoSpaces = str_replace(" ", "+", $location); // Replace spaces with plus sign for geocoder $geocodeURL = "http://maps.google.com/maps/geo?q=" . $locNoSpaces . "&output=csv&key=" . GOOGLE_API_KEY; $response = file_get_contents($geocodeURL); // Parse return and make sure successful (status == 200) list($status, $accuracy, $latitude, $longitude) = explode(",", $response); if ((int)$status == 200) { return $latitude . "," . $longitude; } else return FALSE; } function writeGeoCacheFile($locArray, $filename) { $writeHandle = fopen($filename, "w"); fwrite($writeHandle, "\n"); ksort($locArray); // put in alphabetic order by location name foreach($locArray as $location=>$geocode) { list($latitude, $longitude) = explode(",", $geocode); $line = " \n"; fwrite($writeHandle, $line); } fwrite($writeHandle, "\n"); fclose($writeHandle); } function printUnknownLocs($locArray) { echo "


"; echo ""; echo ""; echo ""; $counter = 0; ksort($locArray); foreach($locArray as $location=>$filename) { echo ""; echo ""; echo ""; echo ""; echo ""; $counter++; } echo ""; echo ""; echo "
Locations Not Found:
" . $location . "Example ImageLatitude: Longitude: