"""Contains functions that maintain/update the cache tables."""
from sqlalchemy import schema
from sqlalchemy.sql import select
+from sqlalchemy.sql.expression import func as sqlfunc
+from osgeo import ogr
import formencode
from wrpylib import mwdb, wrmwdb, mwmarkup, wrmwmarkup
if properties['type'] in wrmwmarkup.WRMAP_POINT_TYPES:
lon, lat = coordinates
label = properties.get('name')
- point_types = {u'gasthaus': u'hut', u'haltestelle': u'busstop', u'parkplatz': u'carpark', u'achtung': u'warning', u'punkt': u'point'}
+ point_types = {u'gasthaus': u'hut', u'haltestelle': u'busstop', u'parkplatz': u'carpark', u'achtung': u'warning', u'foto': u'photo', u'verleih': u'rental', u'punkt': u'point'}
point_type = point_types[properties['type']]
sql = u'insert into wrmappointcache (page_id, type, point, label) values (%s, %s, POINT(%s, %s), %s)'
connection.execute(sql, (sledrun_page.page_id, point_type, lon, lat, label))
transaction.rollback()
raise UpdateCacheError(error_msg, sledrun_page.page_title, e)
transaction.commit()
+
+
+def update_wrregioncache(connection):
+ """Updates the wrregioncache table from the wiki.
+ It relays on the table wrsledruncache to be up-to-date.
+ No exceptions should be raised under normal circumstances.
+
+ >>> from sqlalchemy.engine import create_engine
+ >>> engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1')
+ >>> # or: engine = create_engine('mysql://philipp@localhost:3306/philipp_winterrodeln_wiki?charset=utf8&use_unicode=1&passwd=XXXXX')
+ >>> update_wrregioncache(engine.connect())
+ """
+ metadata = schema.MetaData()
+ wrregion = wrmwdb.wrregion_table(metadata)
+ wrsledruncache = wrmwdb.wrsledruncache_table(metadata)
+ wrregioncache = wrmwdb.wrregioncache_table(metadata)
+
+ transaction = connection.begin()
+
+ # Delete all existing entries in wrregioncache
+ # We rely on transactions MySQL InnoDB
+ connection.execute(wrregioncache.delete())
+
+ # Query all combinations of sledruns and regions
+ sel = select([wrregion.c.id.label('region_id'), sqlfunc.AsWKB(wrregion.c.border).label('border'), wrsledruncache.c.page_id, wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude], sqlfunc.contains(wrregion.c.border, sqlfunc.point(wrsledruncache.c.position_longitude, wrsledruncache.c.position_latitude)))
+ ins = wrregioncache.insert()
+
+ # Refill wrregioncache
+ point = ogr.Geometry(ogr.wkbPoint)
+ result = connection.execute(sel)
+ for row in result:
+ point.SetPoint(0, row.position_longitude, row.position_latitude)
+ if point.Within(ogr.CreateGeometryFromWkb(row.border)):
+ connection.execute(ins.values(region_id=row.region_id, page_id=row.page_id))
+
+ # commit
+ transaction.commit()
+