New plugin to add location data for posts that only contain latitude and longitude.

pull/269/head
Michael Vogel 2015-06-20 18:40:05 +02:00
parent 708c670ee4
commit 47329fb2e7
3 changed files with 161 additions and 0 deletions

View File

@ -0,0 +1,89 @@
<?php
/**
* Name: Geocoordinates
* Description: Use the OpenCage Geocoder http://geocoder.opencagedata.com to resolve nearest populated location for given latitude, longitude. Derived from "geonames"
* Version: 0.1
* Author: Michael Vogel <https://pirati.ca/profile/heluecht>
*/
function geocoordinates_install() {
register_hook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
register_hook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
}
function geocoordinates_uninstall() {
unregister_hook('post_local', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
unregister_hook('post_remote', 'addon/geocoordinates/geocoordinates.php', 'geocoordinates_post_hook');
}
function geocoordinates_resolve_item(&$item) {
if((!$item["coord"]) || ($item["location"]))
return;
$key = get_config("geocoordinates", "api_key");
if ($key == "")
return;
$language = get_config("geocoordinates", "language");
if ($language == "")
$language = "de";
$result = Cache::get("geocoordinates:".$language.":".$item["coord"]);
if (!is_null($result)) {
$item["location"] = $result;
return;
}
$coords = explode(' ',$item["coord"]);
$s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
if (!$s) {
logger("API could not be queried", LOGGER_DEBUG);
return;
}
$data = json_decode($s);
if ($data->status->code != "200") {
logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG);
return;
}
if (($data->total_results == 0) OR (count($data->results) == 0)) {
logger("No results found", LOGGER_DEBUG);
return;
}
$item["location"] = $data->results[0]->formatted;
logger("Got location for coordinates ".$item["coord"].": ".$item["location"], LOGGER_DEBUG);
if ($item["location"] != "")
Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]);
}
function geocoordinates_post_hook($a, &$item) {
geocoordinates_resolve_item($item);
}
function geocoordinates_plugin_admin(&$a,&$o) {
$t = get_markup_template("admin.tpl", "addon/geocoordinates/");
$o = replace_macros($t, array(
'$submit' => t('Save Settings'),
'$api_key' => array('api_key', t('API Key'), get_config('geocoordinates', 'api_key' ), ''),
'$language' => array('language', t('Language code (IETF format)'), get_config('geocoordinates', 'language' ), ''),
));
}
function geocoordinates_plugin_admin_post(&$a) {
$api_key = ((x($_POST,'api_key')) ? notags(trim($_POST['api_key'])) : '');
set_config('geocoordinates','api_key',$api_key);
$language = ((x($_POST,'language')) ? notags(trim($_POST['language'])) : '');
set_config('geocoordinates','language',$language);
info(t('Settings updated.'). EOL);
}

View File

@ -0,0 +1,3 @@
{{include file="field_input.tpl" field=$api_key}}
{{include file="field_input.tpl" field=$language}}
<div class="submit"><input type="submit" name="page_site" value="{{$submit}}" /></div>

69
geocoordinates/test.php Normal file
View File

@ -0,0 +1,69 @@
<?php
require_once("boot.php");
if(@is_null($a)) {
$a = new App;
}
if(is_null($db)) {
@include(".htconfig.php");
require_once("dba.php");
$db = new dba($db_host, $db_user, $db_pass, $db_data);
unset($db_host, $db_user, $db_pass, $db_data);
};
$a->set_baseurl(get_config('system','url'));
require_once("addon/geocoordinates/geocoordinates.php");
function geocoordinates_resolve_item2(&$item) {
if((!$item["coord"]) || ($item["location"]))
return;
$key = get_config("geocoordinates", "api_key");
if ($key == "")
return;
$language = get_config("geocoordinates", "language");
if ($language == "")
$language = "de";
$result = Cache::get("geocoordinates:".$language.":".$item["coord"]);
if (!is_null($result)) {
$item["location"] = $result;
return;
}
$coords = explode(' ',$item["coord"]);
$s = fetch_url("https://api.opencagedata.com/geocode/v1/json?q=".$coords[0].",".$coords[1]."&key=".$key."&language=".$language);
if (!$s) {
logger("API could not be queried", LOGGER_DEBUG);
return;
}
$data = json_decode($s);
if ($data->status->code != "200") {
logger("API returned error ".$data->status->code." ".$data->status->message, LOGGER_DEBUG);
return;
}
if (($data->total_results == 0) OR (count($data->results) == 0)) {
logger("No results found", LOGGER_DEBUG);
return;
}
$item["location"] = $data->results[0]->formatted;
Cache::set("geocoordinates:".$language.":".$item["coord"], $item["location"]);
}
$r = q("SELECT coord, location FROM item WHERE guid='stat1721635584fdaf31b19541063667' LIMIT 1");
$item = $r[0];
geocoordinates_resolve_item2($item);
print_r($item);
?>