Skip to content

Commit e5a3b9a

Browse files
committed
Add Geo reverse address part #62
1 parent a0b898e commit e5a3b9a

3 files changed

Lines changed: 345 additions & 0 deletions

File tree

admin/admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
$tabsheet = new tabsheet();
5151
$tabsheet->add( 'config', '<span class="icon-cog"></span>' . l10n('Configuration'), add_url_params( $my_base_url, array('tab'=>'config') ) );
52+
$tabsheet->add( 'loc', '<span class="osm-location"></span>' . l10n('Location'), add_url_params( $my_base_url, array('tab'=>'loc') ) );
5253
$tabsheet->select($page['tab']);
5354

5455
$tabsheet->assign();

admin/admin_loc.php

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
<?php
2+
/***********************************************
3+
* File : admin_sync.php
4+
* Project : piwigo-openstreetmap
5+
* Descr : Create tags from reverse address
6+
*
7+
* Created : 03.07.2015
8+
*
9+
* Copyright 2015 <xbgmsharp@gmail.com>
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
************************************************/
25+
26+
// Check whether we are indeed included by Piwigo.
27+
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
28+
29+
// Check access and exit when user status is not ok
30+
check_status(ACCESS_ADMINISTRATOR);
31+
32+
// Setup plugin Language
33+
load_language('plugin.lang', OSM_PATH);
34+
35+
// Fetch the template.
36+
global $template, $conf, $lang;
37+
38+
// Generate default value
39+
$sync_options = array(
40+
'overwrite' => true,
41+
'simulate' => true,
42+
'cat_id' => 0,
43+
'subcats_included' => true,
44+
'osm_tag_address_city_district' => false,
45+
'osm_tag_address_city' => true,
46+
'osm_tag_address_county' => false,
47+
'osm_tag_address_state' => false,
48+
'osm_tag_address_country' => false,
49+
'osm_tag_address_postcode' => false,
50+
'osm_tag_address_country_code' => false,
51+
);
52+
53+
if ( isset($_POST['osm_submit']) )
54+
{
55+
// Override default value from the form
56+
$sync_options = array(
57+
'overwrite' => isset($_POST['overwrite']),
58+
'simulate' => isset($_POST['simulate']),
59+
'cat_id' => isset($_POST['cat_id']) ? (int)$_POST['cat_id'] : 0,
60+
'subcats_included' => isset($_POST['subcats_included']),
61+
'osm_tag_address_city_district' => isset($_POST['osm_tag_address_city_district']),
62+
'osm_tag_address_city' => isset($_POST['osm_tag_address_city']),
63+
'osm_tag_address_county' => isset($_POST['osm_tag_address_county']),
64+
'osm_tag_address_state' => isset($_POST['osm_tag_address_state']),
65+
'osm_tag_address_country' => isset($_POST['osm_tag_address_country']),
66+
'osm_tag_address_postcode' => isset($_POST['osm_tag_address_postcode']),
67+
'osm_tag_address_country_code' => isset($_POST['osm_tag_address_country_code'])
68+
);
69+
70+
include_once( dirname(__FILE__).'/../../../include/functions_tag.inc.php' );
71+
// TODO allow to filter on overwrite
72+
// Define files which lat and long avaiable
73+
define('SQL_EXIF', "`latitude` IS NOT NULL AND `longitude` is NOT NULL");
74+
if ( $sync_options['cat_id']!=0 )
75+
{
76+
$query=' SELECT id FROM '.CATEGORIES_TABLE.' WHERE ';
77+
78+
if ( $sync_options['subcats_included'])
79+
$query .= 'uppercats REGEXP \'(^|,)'.$sync_options['cat_id'].'(,|$)\'';
80+
else
81+
$query .= 'id='.$sync_options['cat_id'];
82+
$cat_ids = array_from_query($query, 'id');
83+
84+
$query='SELECT `id`, `latitude`, `longitude` FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id
85+
WHERE '. SQL_EXIF .' AND category_id IN ('.implode(',', $cat_ids).')
86+
GROUP BY id';
87+
}
88+
else
89+
{
90+
$query='SELECT `id`, `latitude`, `longitude` FROM '.IMAGES_TABLE.' WHERE '. SQL_EXIF;
91+
}
92+
93+
$images = hash_from_query( $query, 'id');
94+
$datas = array();
95+
$errors = array();
96+
$warnings = array();
97+
$infos = array();
98+
foreach ($images as $image)
99+
{
100+
// Fech reverse location from API
101+
// https://nominatim.openstreetmap.org/reverse?format=xml&lat=51.082333&lon=10.366229&zoom=12
102+
// https://open.mapquestapi.com/nominatim/v1/reverse.php?format=xml&lat=48.858366666667&lon=2.2942166666667&zoom=12
103+
// http://wiki.openstreetmap.org/wiki/Nominatim
104+
//$osm_url = "https://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1&zoom=12&lat=". $image['latitude'] ."&lon=". $image['longitude'];
105+
$osm_url = "https://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&addressdetails=1&zoom=12&lat=". $image['latitude'] ."&lon=". $image['longitude'];
106+
//print $osm_url ."<br/>";
107+
108+
// Ensure we do have PHP curl install
109+
// Or should fallback to fopen
110+
if (function_exists('curl_init'))
111+
{
112+
// Get cURL resource
113+
$curl = curl_init();
114+
// Set some options http://wiki.openstreetmap.org/wiki/Nominatim_usage_policy
115+
curl_setopt_array($curl, array(
116+
CURLOPT_RETURNTRANSFER => 1,
117+
CURLOPT_URL => $osm_url,
118+
CURLOPT_USERAGENT => 'Piwigo OSM xbgmsharp@gmail.com',
119+
CURLOPT_REFERER => 'http://piwigo.org/'
120+
));
121+
// Send the request & save response to $json
122+
$json = curl_exec($curl);
123+
// Close request to clear up some resources
124+
curl_close($curl);
125+
126+
} else {
127+
// Curl module un available, use fopen
128+
$opts = array(
129+
'http'=>array(
130+
'method'=>"GET",
131+
'header'=>"User-Agent: Piwigo OSM xbgmsharp@gmail.com\r\n"
132+
)
133+
);
134+
$context = stream_context_create($opts);
135+
if (false !== ($json = @file_get_contents($osm_url, flase, $context))) {
136+
// all good
137+
//return $json;
138+
} else {
139+
// error happened
140+
//return false;
141+
$errors[] = "Error fetching reverse data";
142+
}
143+
}
144+
145+
if (!isset($json) or empty($json))
146+
$errors[] = "Error fetching geo reverse address data for ". $image['id'];
147+
else
148+
//var_dump(json_decode($json, true));
149+
$response = json_decode($json, true);
150+
//print_r($response);
151+
152+
// If reponse include [address]
153+
if (isset($response) and isset($response['address']) and is_array($response['address']))
154+
{
155+
//print_r($response['address']);
156+
//print_r($sync_options);
157+
$tag_ids = array();
158+
if (isset($response['address']['city_district']) and $sync_options['osm_tag_address_city_district']) {
159+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['city_district']) );
160+
}
161+
if (isset($response['address']['city']) and $sync_options['osm_tag_address_city']) {
162+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['city']) );
163+
}
164+
if (isset($response['address']['county']) and $sync_options['osm_tag_address_county']) {
165+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['county']) );
166+
}
167+
if (isset($response['address']['state']) and $sync_options['osm_tag_address_state']) {
168+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['state']) );
169+
}
170+
if (isset($response['address']['country']) and $sync_options['osm_tag_address_country']) {
171+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['country']) );
172+
}
173+
if (isset($response['address']['postcode']) and $sync_options['osm_tag_address_postcode']) {
174+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['postcode']) );
175+
}
176+
if (isset($response['address']['country_code']) and $sync_options['osm_tag_address_country_code']) {
177+
array_push( $tag_ids, tag_id_from_tag_name("location:".$response['address']['country_code']) );
178+
}
179+
add_tags($tag_ids, [$image['id']]);
180+
}
181+
182+
//die("Done one image");
183+
} // Images loop
184+
185+
// Send sync result to template
186+
$template->assign('sync_errors', $errors );
187+
$template->assign('sync_warnings', $warnings );
188+
$template->assign('sync_infos', $infos );
189+
190+
// Send result to templates
191+
$template->assign(
192+
'metadata_result',
193+
array(
194+
'NB_ELEMENTS_DONE' => count($datas),
195+
'NB_ELEMENTS_CANDIDATES' => count($images),
196+
'NB_ERRORS' => count($errors),
197+
'NB_WARNINGS' => count($warnings),
198+
)
199+
);
200+
}
201+
202+
$query = 'SELECT COUNT(*) FROM '.IMAGES_TABLE.' WHERE `latitude` IS NOT NULL and `longitude` IS NOT NULL ';
203+
list($nb_geotagged) = pwg_db_fetch_array( pwg_query($query) );
204+
205+
$query = 'SELECT id, CONCAT(name, IF(dir IS NULL, " (V)", "") ) AS name, uppercats, global_rank FROM '.CATEGORIES_TABLE;
206+
display_select_cat_wrapper($query,
207+
array( $sync_options['cat_id'] ),
208+
'categories',
209+
false);
210+
211+
// Send value to templates
212+
$template->assign(
213+
array(
214+
'SUBCATS_INCLUDED_CHECKED' => $sync_options['subcats_included'] ? 'checked="checked"' : '',
215+
'NB_GEOTAGGED' => $nb_geotagged,
216+
'OSM_PATH' => OSM_PATH,
217+
)
218+
);
219+
220+
?>

admin/admin_loc.tpl

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{html_head}
2+
<link rel="stylesheet" href="{$OSM_PATH}fontello/css/osm.css" />
3+
<style>
4+
{literal}
5+
.osm_layout {
6+
text-align: left;
7+
border: 2px solid rgb(221, 221, 221);
8+
padding: 1em;
9+
margin: 1em;
10+
}
11+
{/literal}
12+
</style>
13+
{/html_head}
14+
15+
Find the address base on the GPS (latitude, longitude) metadata information from the database.
16+
<br/><br/>
17+
Refer to the <a href="https://github.com/xbgmsharp/piwigo-openstreetmap/wiki" target="_blanck">plugin documentation</a> for additional information. Create an <a href="https://github.com/xbgmsharp/piwigo-openstreetmap/issues" target="_blanck">issue</a> for support, or feedback, or feature request.
18+
19+
<div class="osm_layout">
20+
<legend>{'Statistics'|@translate}</legend>
21+
<ul>
22+
<li class="update_summary_new">{$NB_GEOTAGGED} geotagged items in your gallery</li>
23+
</ul>
24+
</div>
25+
26+
{if isset($metadata_result)}
27+
<div class="osm_layout">
28+
<legend>Synchronization results</legend>
29+
<ul>
30+
<li>{$metadata_result.NB_ELEMENTS_DONE} {'photos updated in the database'|@translate}</li>
31+
<li>{$metadata_result.NB_ELEMENTS_CANDIDATES} {'photos candidates for metadata synchronization'|@translate}</li>
32+
<li>{$metadata_result.NB_WARNINGS} {'warnings during synchronization'|@translate}</li>
33+
<li>{$metadata_result.NB_ERRORS} {'errors during synchronization'|@translate}</li>
34+
</ul>
35+
36+
{if not empty($sync_errors)}
37+
<h3>{'SYNC_ERRORS'|@translate}</h3>
38+
<div class="errors">
39+
<ul>
40+
{foreach from=$sync_errors item=error}
41+
<li>{$error}</li>
42+
{/foreach}
43+
</ul>
44+
</div>
45+
{/if}
46+
47+
{if not empty($sync_warnings)}
48+
<h3>{'SYNC_WARNINGS'|@translate}</h3>
49+
<div class="warnings">
50+
<ul>
51+
{foreach from=$sync_warnings item=warning}
52+
<li>{$warning}</li>
53+
{/foreach}
54+
</ul>
55+
</div>
56+
{/if}
57+
58+
{if not empty($sync_infos)}
59+
<h3>{'SYNC_INFOS'|@translate}</h3>
60+
<div class="infos">
61+
<ul>
62+
{foreach from=$sync_infos item=info}
63+
<li>{$info}</li>
64+
{/foreach}
65+
</ul>
66+
</div>
67+
{/if}
68+
69+
</div>
70+
{/if}
71+
72+
<form action="" method="post" id="update">
73+
74+
<fieldset id="syncOverwrite">
75+
<legend>{'Manage tags'|@translate}</legend>
76+
<ul>
77+
<li>
78+
<label>{'TAG_ADDRESS'|@translate} : </label><br/>
79+
<div style="padding-left: 25px">
80+
<input type="checkbox" name="osm_tag_address_city_district" value="true" {if $osm_tag_address_city_district}checked="checked"{/if}/> {'city_district'|@translate}<br />
81+
<input type="checkbox" name="osm_tag_address_city" value="true" {if $osm_tag_address_city}checked="checked"{/if}/> {'city'|@translate}<br />
82+
<input type="checkbox" name="osm_tag_address_county" value="true" {if $osm_tag_address_county}checked="checked"{/if}/> {'county'|@translate}<br />
83+
<input type="checkbox" name="osm_tag_address_state" value="true" {if $osm_tag_address_state}checked="checked"{/if}/> {'state'|@translate}<br />
84+
<input type="checkbox" name="osm_tag_address_country" value="true" {if $osm_tag_address_country}checked="checked"{/if}/> {'country'|@translate}<br />
85+
<input type="checkbox" name="osm_tag_address_postcode" value="true" {if $osm_tag_address_postcode}checked="checked"{/if}/> {'postcode'|@translate}<br />
86+
<input type="checkbox" name="osm_tag_address_country_code" value="true" {if $osm_tag_address_country_code}checked="checked"{/if}/> {'country_code'|@translate}<br />
87+
</div>
88+
<small>{'TAG_ADDRESS_DESC'|@translate}</small>
89+
</li>
90+
</ul>
91+
</fieldset>
92+
93+
<fieldset id="syncOverwrite">
94+
<legend>{'OVERWRITE_LGD'|@translate}</legend>
95+
<ul>
96+
<label><input type="checkbox" name="overwrite" value="1" checked="checked"> {'OVERWRITE'|@translate}</label>
97+
<br/><small>{'OVERWRITE_DESC'|@translate}</small>
98+
</ul>
99+
</fieldset>
100+
101+
<fieldset id="syncSimulation">
102+
<legend>{'Simulation'|@translate}</legend>
103+
<ul>
104+
<li><label><input type="checkbox" name="simulate" value="1" checked="checked" /> {'only perform a simulation (no change in database will be made)'|@translate}</label></li>
105+
</ul>
106+
</fieldset>
107+
108+
<fieldset id="catSubset">
109+
<legend>{'reduce to single existing albums'|@translate}</legend>
110+
<ul>
111+
<li>
112+
<select class="categoryList" name="cat_id" size="10">
113+
{html_options options=$categories selected=$categories_selected}
114+
</select>
115+
</li>
116+
117+
<li><label><input type="checkbox" name="subcats_included" value="1" {$SUBCATS_INCLUDED_CHECKED} /> {'Search in sub-albums'|@translate}</label></li>
118+
</ul>
119+
</fieldset>
120+
121+
<p>
122+
<input type="submit" value="{'Submit'|@translate}" name="osm_submit">
123+
</p>
124+
</form>

0 commit comments

Comments
 (0)