#! /usr/bin/python """ A simple demo program that grown beyond, see locateCoordv1.py for the simpler version This should really be scale back infavor of a better backend implementation """ import wikipedia import MySQLdb, cgi, math import cgitb; cgitb.enable(logdir='./logs/') def main(): f = cgi.FieldStorage() dbname = f.getfirst('dbname', 'coord_enwiki') globe = f.getfirst('globe', None) lat1 = f.getfirst('lat1', '') lat2 = f.getfirst('lat2', '') lon1 = f.getfirst('lon1', '') lon2 = f.getfirst('lon2', '') range_km = f.getfirst('range_km', '') offset = f.getfirst('offset', '0') globe = f.getfirst('globe', None) if range_km and not any((lat1, lat2, lon1, lon2)): # based on http://toolserver.org/~kolossos/wp-world/umkreis-source.php # by http://de.wikipedia.org/wiki/Benutzer:Kolossos range_km = float(range_km)/111.3 lat = ((float(f.getfirst('lat'))+270) % 180) - 90 lat1 = lat - (range_km/2) lat2 = lat + (range_km/2) lon = ((float(f.getfirst('lon'))+540) % 360) - 180 lon1 = lon - range_km/2/math.cos(lat/57.27) lon2 = lon + range_km/2/math.cos(lat/57.27) tables = ('coord_commonswiki', 'coord_cawiki', 'coord_cswiki', 'coord_dawiki', 'coord_dewiki', 'coord_enwiki', 'coord_eswiki', 'coord_eowiki', 'coord_frwiki', 'coord_idwiki', 'coord_itwiki', 'coord_huwiki', 'coord_jawiki', 'coord_nlwiki', 'coord_nowiki', 'coord_plwiki', 'coord_ptwiki', 'coord_ruwiki', 'coord_rowiki', 'coord_skwiki', 'coord_fiwiki', 'coord_svwiki', 'coord_trwiki', 'coord_ukwiki', 'coord_vowiki', 'coord_zhwiki',) print """
"""% (dbname in tables and "" % '\n'.join(''%(dbname==s and ' selected="selected"' or '',s) for s in tables) or ""%dbname, # TODO this should be dynamically generated '\n'.join(''%(globe==s and ' selected="selected"' or '',s) for s in ('', 'mercury', 'venus', 'earth', 'moon', 'mars', 'titan', 'ganymend',)), lat1, lat2, lon1, lon2, ) if not (dbname and dbname.replace('_','').isalnum()): return db = MySQLdb.connect(db='u_dispenser_p', host=dbname[6:].replace('_', '-') + '-p.db.toolserver.org', read_default_file="/home/dispenser/.my.cnf") c = db.cursor() c.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED") c.execute(( """ SELECT *, 0 FROM """ +dbname+""" WHERE MBRWithIn(gc_location, GeomFromText(%s)) /*AND gc_globe = %s*/ LIMIT """+(offset.isdigit() and offset or '0')+""", 501 """), ( 'Polygon((%s %s, %s %s, %s %s, %s %s, %s %s))' % (lat1,lon1, lat1,lon2, lat2,lon2, lat2,lon1, lat1,lon1), globe, ) ) print """""" print '| lat | lon | alt | dim | type | typesize | region | globe | title | |
|---|---|---|---|---|---|---|---|---|---|
| %g | %g | %s | %s | %s | %s | %s | %s | %s | %s |
Results were limited to 500, next 500
' % '?dbname=%s&lat1=%s&lat2=%s&lon1=%s&lon2=%s&offset=%s' % (dbname, lat1, lat2, lon1, lon2, parseInt(offset, 0)+500) def parseInt(s, default=0): try: return int(s) except: return default if __name__ == "__main__" and wikipedia.handleUrlAndHeader(): try: wikipedia.startContent(form=False, head="""""") main() finally: wikipedia.endContent() wikipedia.stopme() wikipedia.time.sleep(1)