User:Amake/gmcoords.php

gmcoords.php is a PHP script that fetches coordinates from Google Maps and formats them for inclusion on Wikipedia articles. It is meant to be run locally from the command line. A variant of this script is used by my bot JMuniBot to semi-automatically tag Japanese municipality articles with geodata.

Example usage
user@mybox:~$ ./gmcoords.php 由仁町役場

Querying google for 由仁町役場 ... done Copying to X11 pasteboard:

I typically query for city halls (市役所) or town/village offices (町役場 or 村役場). Google usually has coordinates for these, except in the cases of (relatively) recently merged or renamed towns. In those cases I query the address listed on the ja: page; if that still doesn't work, I figure out which of the merged towns' offices became the new town office, and query that one.

Code

 * 1) !/usr/bin/php -f

<?php

$usage = "Usage: $argv[0] [d|dm|dms] query\n"; $debug = true;

$key = 'GOOGLE MAPS API KEY GOES HERE';

// Handle arguments if (count($argv) == 3) { $accuracy = $argv[1]; switch ($accuracy) { case 'd': case 'dm': case 'dms':	break; default: 	die($usage); }	$query = $argv[2]; } elseif (count($argv) == 2) { $query = $argv[1]; // Default to degrees+minutes $accuracy = 'dm'; } else { die($usage); }

// Perform query (requires cURL module for PHP) $query = str_replace(' ', '+', $query); $ch = curl_init("http://maps.google.com/maps/geo?q=$query&output=csv&key=$key"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); echo 'Querying google for '. $query. ' ... '; $reply = curl_exec($ch); echo "done\n";

if ($debug) echo "Google returned: $reply\n";

$coords = explode(',', $reply);

// Handle errors from Google switch ($coords[0]) { case 602:	die("Query not found\n"); case 610:	die("Bad API key. Get one at http://www.google.com/apis/maps/signup.html\n"); }

// Google returns (blah),(blah),(N coord),(E coord) so remove first 2 items array_shift($coords); array_shift($coords);

// Convert decimal coords to dms switch ($accuracy) { case 'd':	for ($i = 0; $i < 2; $i++) { $degrees[$i] = round($coords[$i]); }			$wiki = ""; break; case 'dm':	for ($i = 0; $i < 2; $i++) { $degrees[$i] = (int) $coords[$i]; // regular modulus (%) only works on ints $decimal1 = fmod($coords[$i], 1); $minutes[$i] = round($decimal1 * 60); if ($minutes[$i] == 60) { $minutes[$i] = '0'; $degrees[$i]++; }			}			$wiki = ""; break; case 'dms':	for ($i = 0; $i < 2; $i++) { $degrees[$i] = (int) $coords[$i]; $decimal1 = fmod($coords[$i], 1); $minutes[$i] = (int) ($decimal1 * 60); $decimal2 = fmod($decimal1 * 60, 1); $seconds[$i] = round($decimal2 * 60); if ($seconds[$i] == 60) { $seconds[$i] = '0'; $minutes[$i]++; }				if ($minutes[$i] == 60) { $minutes[$i] = '0'; $degrees[$i]++; }			}			$wiki = "°N, °W"; break; default:	die("Unknown error\n"); }

// Copy to pasteboard if available if (exec("which xclip")) { echo "Copying to X11 pasteboard: $wiki\n"; exec("echo -n \"$wiki\" | xclip -selection clipboard"); } elseif (exec("which pbcopy")) { echo "Copying to OS X pasteboard: $wiki\n"; exec("echo -n \"$wiki\" | pbcopy"); } else { echo $wiki. "\n"; }

?>