-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_tile.php
119 lines (88 loc) · 2.19 KB
/
api_tile.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
// CouchDB as map tile server
require_once (dirname(__FILE__) . '/couchsimple.php');
// tile request will supply x,y and z (zoom level)
if (isset($_GET['x']))
{
$x = (Integer)$_GET['x'];
}
if (isset($_GET['y']))
{
$y = (Integer)$_GET['y'];
}
if (isset($_GET['z']))
{
$zoom = (Integer)$_GET['z'];
}
$startkey = array($zoom, $x, $y);
$endkey = array($zoom, $x, $y, "zzz","zzz", "zzz", 256);
$url = '_design/geo/_view/tile?startkey=' . urlencode(json_encode($startkey))
. '&endkey=' . urlencode(json_encode($endkey))
. '&group_level=8';
//$url .= '&limit=1000';
if ($config['stale'])
{
$url .= '&stale=ok';
}
$resp = $couch->send("GET", "/" . $config['couchdb_options']['database'] . "/" . $url);
$response_obj = json_decode($resp);
//echo $resp;
// Create SVG tile
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
width="256" height="256px" >
<style type="text/css">
<![CDATA[
]]>
</style>
<g >';
// viewBox="-10 -10 266 266" overflow="visible"
// transform="translate(0 0)"
//$xml .= '<rect x="0" y="0" width="256" height="256" style="stroke-width:1;fill:none;stroke:rgb(255,0,0);" />';
foreach ($response_obj->rows as $row)
{
$x_pos = $row->key[3];
$y_pos = $row->key[4];
$x_pos = floor($x_pos/4) * 4;
$y_pos = floor($y_pos/4) * 4;
$xml .= '<rect id="dot" x="' . ($x_pos - 2) . '" y="' . ($y_pos - 2) . '" width="4" height="4" style="stroke-width:1;"';
// Colours
if (1)
{
// black
//$fill = 'rgba(0,0,0,0.5)';
// purple
$fill="rgba(128,0,64,0.5)";
}
else
{
// colours
$fill = "rgba(255,255,0 ,0.5)";
if ($row->value > 5)
{
$fill="rgba(255,127,0,0.5)";
}
if ($row->value > 10)
{
$fill="rgba(255,0,0,0.5)";
}
if ($row->value > 20)
{
$fill="rgba(128,0,64,0.5)";
}
}
$xml .= ' fill="'. $fill . '"';
$xml .= ' stroke="rgb(128,0,64)"';
$xml .= '/>';
}
$xml .= '
</g>
</svg>';
// Serve up tile
header("Content-type: image/svg+xml");
// Comments this out if we are populating CouchDB and want to see map grow,
// but in production uncomment so tiles are cached
header("Cache-control: max-age=3600");
echo $xml;
?>