Skip to content

Commit

Permalink
Geoencoder plugin: Correct out-of-date instructions for getting a Goo…
Browse files Browse the repository at this point in the history
…gle Maps API key

closes #1271
  • Loading branch information
ginatrapani committed Mar 22, 2012
1 parent a632620 commit 07ecf63
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 54 deletions.
43 changes: 26 additions & 17 deletions webapp/plugins/geoencoder/model/class.GeoEncoderCrawler.php
Expand Up @@ -45,7 +45,7 @@ class GeoEncoderCrawler {
* Perform Geoencoding using the data available in fields place or location
* @var PostDAO $post_dao
* @var array $post
* @return null
* @return bool true if successfully retrieved geo data from API; false if from DB
*/
public function performGeoencoding($post_dao, $post) {
if (self::$is_api_available) {
Expand All @@ -68,7 +68,7 @@ public function performGeoencoding($post_dao, $post) {
if (is_string($find_geodata[0]) && is_numeric($check_geodata[0]) && is_numeric($check_geodata[1])){
$post['geo'] = $check_geodata[0].' '.$check_geodata[1];
$is_reverse_geoencoded = 1;
self::performReverseGeoencoding($post_dao, $post);
return self::performReverseGeoencoding($post_dao, $post);
}
}
}
Expand All @@ -78,13 +78,13 @@ public function performGeoencoding($post_dao, $post) {
if ($post['in_reply_to_post_id']!=null || $post['in_retweet_of_post_id']!=null) {
$reply_retweet_distance = $this->getDistance($post_dao, $post, $data['latlng']);
if (!$reply_retweet_distance) {
return;
return false;
}
}
$post_dao->setGeoencodedPost($post_id, $post['network'], self::SUCCESS, $data['full_name'],
$data['latlng'], $reply_retweet_distance);
$logger->logSuccess('Lat/long coordinates found in DB', __METHOD__.','.__LINE__);
return;
return false;
}
$string = self::getDataForGeoencoding($location);
$obj=json_decode($string);
Expand All @@ -95,7 +95,7 @@ public function performGeoencoding($post_dao, $post) {
if ($post['in_reply_to_post_id']!=null || $post['in_retweet_of_post_id']!=null) {
$reply_retweet_distance = $this->getDistance($post_dao, $post, $geodata);
if (!$reply_retweet_distance) {
return;
return false;
}
}
$post_dao->setGeoencodedPost($post_id, $post['network'], self::SUCCESS, $location, $geodata,
Expand All @@ -107,18 +107,20 @@ public function performGeoencoding($post_dao, $post) {
'latlng'=>$geodata
);
$location_dao->addLocation($vals);
return true;
} else {
self::failedToGeoencode($post_dao, $post_id, $post['network'], $obj->status);
}
}
}
return false;
}

/**
* Perform Reverse Geoencoding using the data available in field geo
* @var PostDAO $post_dao
* @var array $post
* @return null
* @return bool true if successfully retrieved geo data from API; false if from DB
*/
public function performReverseGeoencoding($post_dao, $post) {
if (self::$is_api_available) {
Expand All @@ -132,17 +134,19 @@ public function performReverseGeoencoding($post_dao, $post) {
if ($post['in_reply_to_post_id']!=null || $post['in_retweet_of_post_id']!=null) {
$reply_retweet_distance = $this->getDistance($post_dao, $post, $data['latlng']);
if (!$reply_retweet_distance) {
return;
return false;
}
}
$post_dao->setGeoencodedPost($post_id, $post['network'], self::SUCCESS, $data['full_name'],
$data['latlng'], $reply_retweet_distance);
$logger->logSuccess('Lat/long coordinates found in DB', __METHOD__.','.__LINE__);
return;
return false;
}
$string = self::getDataForReverseGeoencoding($geodata);
$geodata = explode(' ', $geodata, 2);
$geodata = $geodata[0].','.$geodata[1];
if (isset($geodata[0]) && isset($geodata[1])) {
$geodata = $geodata[0].','.$geodata[1];
}
$obj=json_decode($string);
if ($obj->status == 'OK') {
foreach ($obj->results as $p) {
Expand All @@ -158,26 +162,27 @@ public function performReverseGeoencoding($post_dao, $post) {
if ($post['in_reply_to_post_id']!=null || $post['in_retweet_of_post_id']!=null) {
$reply_retweet_distance = $this->getDistance($post_dao, $post, $geodata);
if (!$reply_retweet_distance) {
return;
return false;
}
}
$post_dao->setGeoencodedPost($post_id, $post['network'], self::SUCCESS, $location, $geodata,
$reply_retweet_distance);
$post_dao->setGeoencodedPost($post_id, $post['network'], self::SUCCESS, $location,
$geodata, $reply_retweet_distance);
$logger->logSuccess('Lat/long coordinates retrieved via API', __METHOD__.','.__LINE__);
$vals = array (
'short_name'=>$post['geo'],
'full_name'=>$location,
'latlng'=>$geodata
);
$location_dao->addLocation($vals);
return;
return true;
}
}
}
} else {
self::failedToGeoencode($post_dao, $post_id, $post['network'], $obj->status);
}
}
return false;
}

/**
Expand Down Expand Up @@ -291,15 +296,19 @@ public function getDataForGeoencoding ($location) {
}

/**
* Retrieve data for Reverse Geoencoding
* Retrieve data for reverse geoencoding
* @var float $latitude
* @var float $longitude
* @return string $filecontents
*/
public function getDataForReverseGeoencoding($latlng) {
$latlng = explode(' ', $latlng, 2);
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$latlng[0],$latlng[1]&sensor=true";
$filecontents=Utils::getURLContents($url);
return $filecontents;
if (isset($latlng[0]) && isset($latlng[1])) {
$url = "http://maps.google.com/maps/api/geocode/json?latlng=$latlng[0],$latlng[1]&sensor=true";
$filecontents=Utils::getURLContents($url);
return $filecontents;
} else {
return '';
}
}
}
15 changes: 11 additions & 4 deletions webapp/plugins/geoencoder/model/class.GeoEncoderPlugin.php
Expand Up @@ -53,16 +53,23 @@ public function crawl() {
$crawler = new GeoEncoderCrawler;

$posts_to_geoencode = $post_dao->getPostsToGeoencode(2000);
$logger->logUserInfo("There are ".count($posts_to_geoencode)." posts to geoencode.", __METHOD__.','.__LINE__);
$logger->logUserSuccess("Starting to collect lat/log points for ".count($posts_to_geoencode)." posts.",
__METHOD__.','.__LINE__);

$total_api_requests_fulfilled = 0;
foreach ($posts_to_geoencode as $post_data) {
if ($post_data['geo']!='') {
$crawler->performReverseGeoencoding($post_dao, $post_data);
if ($crawler->performReverseGeoencoding($post_dao, $post_data)) {
$total_api_requests_fulfilled++;
}
} else {
$crawler->performGeoencoding($post_dao, $post_data);
if ($crawler->performGeoencoding($post_dao, $post_data)) {
$total_api_requests_fulfilled++;
}
}
}
$logger->logUserSuccess("Post geoencoding complete.", __METHOD__.','.__LINE__);
$logger->logUserSuccess("Post geoencoding complete. ".$total_api_requests_fulfilled.
" API requests fulfilled successfully.", __METHOD__.','.__LINE__);
}

public function renderConfiguration($owner) {
Expand Down
36 changes: 5 additions & 31 deletions webapp/plugins/geoencoder/view/geoencoder.account.index.tpl
Expand Up @@ -22,37 +22,11 @@ The GeoEncoder plugin plots a post's responses on a Google Map and can lists the

{include file="_usermessage.tpl" field="setup"}
<p style="padding:5px">To set up the GeoEncoder plugin:</p>
<ol style="margin-left:40px"><li><a href="http://code.google.com/apis/maps/signup.html" target="_blank" style="text-decoration : underline;">Sign up for a Google Maps API key</a>.</li>
<li>Set the web site URL to <br>
<code style="font-family:Courier;" id="clippy_2989">{$thinkup_site_url}</code>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="100"
height="14"
class="clippy"
id="clippy" >
<param name="movie" value="{$site_root_path}assets/flash/clippy.swf"/>
<param name="allowScriptAccess" value="always" />
<param name="quality" value="high" />
<param name="scale" value="noscale" />
<param NAME="FlashVars" value="id=clippy_2989&amp;copied=copied!&amp;copyto=copy to clipboard">
<param name="bgcolor" value="#FFFFFF">
<param name="wmode" value="opaque">
<embed src="{$site_root_path}assets/flash/clippy.swf"
width="100"
height="14"
name="clippy"
quality="high"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
FlashVars="id=clippy_2989&amp;copied=copied!&amp;copyto=copy to clipboard"
bgcolor="#FFFFFF"
wmode="opaque"
/>
</object>

</li>
<li>Enter the Google-provided API key here.</li></ol>
<ol style="margin-left:40px">
<li><a href="http://code.google.com/apis/console#access" target="_blank" style="text-decoration : underline;">Create a project in the Google APIs Console.</a></li>
<li>Click "Services" and switch Google Maps API v2 to "On." </li>
<li>Click "API Access." Under "Simple API Access", copy and paste the Google-provided API key here.</li>
</ol>
{/if}
<p>
{$options_markup}
Expand Down
Expand Up @@ -175,10 +175,10 @@ public function apiRequest($url, $args = array(), $auth = true, $suppress_404_er

if ($this->function_api_call_limits && isset($this->function_api_call_limits[$calling_function])) {
if ($this->function_api_call_limits[$calling_function]['remaining'] == 0) {
$message = 'We are over the API call limit ' .
$message = 'Exceeded the API call limit ' .
$this->function_api_call_limits[$calling_function]['count'] .
" for the function call $calling_function on line $calling_line";
$logger->logUserInfo($message,__METHOD__.','.__LINE__);
$logger->logInfo($message,__METHOD__.','.__LINE__);
throw new APICallLimitExceededException($message);
}
}
Expand Down

0 comments on commit 07ecf63

Please sign in to comment.