-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.cgi
More file actions
executable file
·75 lines (57 loc) · 1.95 KB
/
tile.cgi
File metadata and controls
executable file
·75 lines (57 loc) · 1.95 KB
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
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
import psycopg2, psycopg2.extras
import math, time
import simplejson
print "Content-type: text/json"
print
querystring = cgi.FieldStorage()
if "x" not in querystring or "y" not in querystring or "z" not in querystring:
print "<h1>error: need xyz: url?x={X}&y={Y}&z={Z}</h1>"
print
exit()
x = int(querystring["x"].value)
y = int(querystring["y"].value)
z = int(querystring["z"].value)
tile_id = None
if "id" in querystring:
tile_id = str(querystring["id"].value)
#print "x, y, z: " + str(x) + ", " + str(y) + ", " + str(z)
# get all the tiles within this shape (down to z+5)
# for each zoom, it is x*2, y*2, z+1
conn = psycopg2.connect("dbname=DBNAME user=DBUSER")
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
if 'layer' in querystring:
basemap = """ AND basemap = '{0}'""".format(str(querystring["layer"].value))
else:
#basemap = """ AND basemap = 'watercolor'"""
#basemap = """ AND (basemap = 'toner' OR basemap = 'terrain' OR basemap = 'watercolor')"""
basemap = """ AND (basemap = 'toner' OR basemap = 'watercolor')"""
zooms = 10
between = ''
for zoom in range(z, z+zooms):
dz = zoom - z
pz = pow(2,dz)
if dz >= 1: between += " OR "
between += """(
x BETWEEN {0} AND {1}
AND y BETWEEN {2} AND {3}
AND z = {4}
)""".format(x*pz, x*pz+pz-1, y*pz, y*pz+pz-1, zoom)
sql = """SELECT DISTINCT x,y,z
FROM tilelogs
WHERE ({0}){1}""".format(between, basemap)
if 'exit' in querystring:
print sql
exit()
cur.execute(sql)
rows = cur.fetchall()
output = {'zooms':{}, 'tile_id':tile_id}
for row in rows:
x = int(row['x'])
y = int(row['y'])
z = int(row['z'])
if z not in output['zooms']: output['zooms'][z] = []
output['zooms'][z].append({'x':int(row['x']), 'y':int(row['y']), 'z':int(row['z'])})
print simplejson.dumps(output, separators=(',',':'))