forked from emijrp/commons-coverage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajaxcoverage.php
96 lines (87 loc) · 2.9 KB
/
ajaxcoverage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/* Original code: https://github.com/chillly/plaques/blob/master/ajax/ajxplaques.php
*
* Created by Chris Hill <[email protected]> and contributors.
* Adapted for Wiki Loves Monuments by Emijrp <[email protected]>
*
* This software and associated documentation files (the "Software") is
* released under the CC0 Public Domain Dedication, version 1.0, as
* published by Creative Commons. To the extent possible under law, the
* author(s) have dedicated all copyright and related and neighboring
* rights to the Software to the public domain worldwide. The Software is
* distributed WITHOUT ANY WARRANTY.
*
* If you did not receive a copy of the CC0 Public Domain Dedication
* along with the Software, see
* <http://creativecommons.org/publicdomain/zero/1.0/>
*/
// uncomment below to turn error reporting on
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*
* ajaxcoverage.php
* returns image points as geojson
*/
if (isset($_GET['bbox'])) {
$bbox=$_GET['bbox'];
} else {
// invalid request
$ajaxres=array();
$ajaxres['resp']=4;
$ajaxres['dberror']=0;
$ajaxres['msg']='missing bounding box';
sendajax($ajaxres);
}
// split the bbox into it's parts
list($left,$bottom,$right,$top)=explode(",",$bbox);
// open the database
$dbmycnf = parse_ini_file("../replica.my.cnf");
$dbuser = $dbmycnf['user'];
$dbpass = $dbmycnf['password'];
unset($dbmycnf);
$dbhost = "commonswiki.labsdb";
$dbname = "commonswiki_p";
try {
$db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf8', $dbuser, $dbpass);
} catch(PDOException $e) {
// send the PDOException message
$ajaxres=array();
$ajaxres['resp']=40;
$ajaxres['dberror']=$e->getCode();
$ajaxres['msg']=$e->getMessage();
sendajax($ajaxres);
}
try {
$limit = 20000;
$sql="SELECT DISTINCT gt_lat, gt_lon FROM geo_tags WHERE gt_lon>=:left AND gt_lon<=:right AND gt_lat>=:bottom AND gt_lat<=:top LIMIT ".$limit;
$stmt = $db->prepare($sql);
$stmt->bindParam(':left', $left, PDO::PARAM_STR);
$stmt->bindParam(':right', $right, PDO::PARAM_STR);
$stmt->bindParam(':bottom', $bottom, PDO::PARAM_STR);
$stmt->bindParam(':top', $top, PDO::PARAM_STR);
$stmt->execute();
} catch(PDOException $e) {
// send the PDOException message
$ajaxres=array();
$ajaxres['resp']=40;
$ajaxres['dberror']=$e->getCode();
$ajaxres['msg']=$e->getMessage();
sendajax($ajaxres);
}
$ajaxres=array(); // place to store the geojson result
// go through the list adding each one to the array to be returned
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$coords=array();
$coords[0]=floatval($row['gt_lat']);
$coords[1]=floatval($row['gt_lon']);
$ajaxres[]=$coords;
}
// tidy up the DB
$db = null;
sendajax($ajaxres); // no return from there
function sendajax($ajax) {
// encode the ajx array as json and return it.
$encoded = json_encode($ajax);
exit($encoded);
}
?>