+
+
+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()
+