-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraticuleDatabase.py
More file actions
182 lines (162 loc) · 5.8 KB
/
Copy pathGraticuleDatabase.py
File metadata and controls
182 lines (162 loc) · 5.8 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re, sys
import pywikibot
import sqlite3
import functools
re_grat = re.compile('\[\[(.*?)\| *([0-9\-]+, *[0-9\-]+) *\(.*?\) *\]\]')
def inc(num):
if num == "-0":
return "0"
if num=="179":
return "-179"
elif num == "-1":
return "-0"
else:
return(str(int(num)+1))
def dec(num):
if num=="-179":
return "179"
elif num=="0":
return "-0"
else:
return(str(int(num)-1))
def grow(latlon):
lat, lon = latlon
return [(dec(lat), dec(lon)),
(dec(lat), lon),
(dec(lat), inc(lon)),
(lat, dec(lon)),
(lat, lon),
(lat, inc(lon)),
(inc(lat), dec(lon)),
(inc(lat), lon),
(inc(lat), inc(lon))]
class GraticuleDatabase:
def addGraticule(self, lat, lon, page, name, country):
iswater = lat+","+lon in name
if not page:
page = ""
if not name:
name = ""
if not country:
country = ""
ex_string = 'insert or replace into graticules values ("' + lat + '","' + lon + '","'
try:
ex_string += page.decode("utf-8")
except:
ex_string += page
ex_string += '","'
try:
ex_string += name.decode("utf-8")
except:
ex_string += name
ex_string += '","'
try:
ex_string += country.decode("utf-8")
except:
ex_string += country
ex_string += '","'
if iswater:
ex_string += "TRUE\")"
else:
ex_string += "FALSE\")"
self.cur.execute(ex_string) #encode to utf-8 here?
self.db.commit()
def parseGraticulePage(self, page):
text = page.get(get_redirect = False)
result = re_grat.findall(text)
num_results = len(result)
cur_match = 0
for match in result:
page, grat = match
lat, lon = grat.split(", ")
if grat in page:
name = page
country = functools.reduce(lambda x,y:x+" "+y, page.split(" ")[0:-2])
elif "," in page:
cut = page.split(", ")
name = cut[0]
country = cut[-1]
else:
name = page
country = None
cur_match = cur_match + 1
pywikibot.output("Adding Graticule " + str(cur_match) + " of " + str(num_results) + " : " + page)
self.addGraticule( lat, lon, page, name, country )
def __init__(self, filename = None):
if filename:
self.load(filename)
else:
site = pywikibot.Site()
self.load("graticules.sqlite")
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/Eurasia"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/Australasia"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/Africa"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/North America"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/South America"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/Oceans"))
self.parseGraticulePage(pywikibot.Page(site, u"All graticules/Antarctica"))
def dump(self):
self.db.commit()
def load(self, filename):
self.db = sqlite3.connect(filename)
self.cur = self.db.cursor()
self.cur.execute ('create table if not exists graticules (lat text, lon text, page text, name text, country text, water boolean)')
self.cur.execute ('create index if not exists klatlon on graticules (lat, lon)')
self.cur.execute ('create index if not exists kwater on graticules (water)')
self.db.commit()
def getLatLon(self, lat, lon, unknownIsNumeric = False):
try:
execute_text = 'select page, name, country from graticules where lat = \'' + lat + "' and lon = '" + lon + "'"
self.cur.execute(execute_text)
page, name, country = self.cur.fetchone()
return (page, name, country)
except:
if unknownIsNumeric:
return ("%s,%s" % (lat,lon))
else:
return None
def findAll(self, search):
cur.execute('select lat,lon,page from graticules where page like %s', "%"+search+"%")
return cur.fetchall()
def getAllKeys(self):
self.cur.execute('select lat, lon from graticules')
return self.cur.fetchall()
def getAllWaterKeys(self):
self.cur.execute('select lat, lon from graticules where water = TRUE')
return self.cur.fetchall()
def gratlink(self, lat, lon, refcountry = None):
entry = self.getLatLon(lat, lon)
if entry:
page, name, country = entry
link = page
if country == refcountry:
title = name.strip()
elif country:
title = name+", "+country.strip()
else:
title = name.strip()
if lat in page:
title = (page.split(lat)[0]).strip()
if link == title:
return u"[[%s]]" % link
else:
return u"[[%s|%s]]" % (link, title)
else:
return u"[[%s,%s]]" % (lat, lon)
def getTemplate(self, lat, lon, refcountry):
str = u'{{graticule\n'
str += ' | lat= %s\n' % lat
str += ' | lon= %s\n' % lon
str += " | nw = %s\n" % self.gratlink(inc(lat),dec(lon),refcountry)
str += " | n = %s\n" % self.gratlink(inc(lat),lon,refcountry)
str += " | ne = %s\n" % self.gratlink(inc(lat),inc(lon),refcountry)
str += " | w = %s\n" % self.gratlink(lat,dec(lon),refcountry)
str += " | name = %s\n" % self.gratlink(lat,lon,refcountry)
str += " | e = %s\n" % self.gratlink(lat,inc(lon),refcountry)
str += " | sw = %s\n" % self.gratlink(dec(lat),dec(lon),refcountry)
str += " | s = %s\n" % self.gratlink(dec(lat),lon,refcountry)
str += " | se = %s\n" % self.gratlink(dec(lat),inc(lon),refcountry)
str += "}}"
return str