summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--chimere/migrations/0003_convert_tiny_urls.py110
1 files changed, 110 insertions, 0 deletions
diff --git a/chimere/migrations/0003_convert_tiny_urls.py b/chimere/migrations/0003_convert_tiny_urls.py
new file mode 100644
index 0000000..fb39210
--- /dev/null
+++ b/chimere/migrations/0003_convert_tiny_urls.py
@@ -0,0 +1,110 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.conf import settings
+from django.db import migrations
+import ogr
+import osr
+
+
+def convert_tiny_urls(apps, schema_editor):
+ TinyUrl = apps.get_model('chimere', 'TinyUrl')
+ in_ref = osr.SpatialReference()
+ in_ref.ImportFromEPSG(3857)
+ out_ref = osr.SpatialReference()
+ out_ref.ImportFromEPSG(settings.CHIMERE_EPSG_DISPLAY_PROJECTION)
+ transform = osr.CoordinateTransformation(in_ref, out_ref)
+
+ for tiny_url in TinyUrl.objects.all():
+ new_param = {
+ 'zoom': '',
+ 'lat': '',
+ 'lon': '',
+ 'checked_categories': '',
+ 'current_feature': '',
+ 'routing_speed': '',
+ 'routing_transport': '',
+ 'routing_start_lat': '',
+ 'routing_start_lon': '',
+ 'routing_end_lat': '',
+ 'routing_end_lon': '',
+ 'steps': ''
+ }
+ for parameter in tiny_url.parameters.split('&'):
+ items = parameter.split('=')
+ if len(items) != 2:
+ continue
+ param, value = items
+ if param in ('zoom', 'routing_speed', 'routing_transport',
+ 'routing_start_lat', 'routing_start_lon',
+ 'routing_end_lat', 'routing_end_lon'):
+ new_param[param] = value
+ elif param in ('lat', 'lon'):
+ x, y = 242500, 6230000
+ if param == 'lat':
+ y = int(float(value))
+ else:
+ x = int(float(value))
+ point = ogr.Geometry(ogr.wkbPoint)
+ point.AddPoint(x, y)
+ point.Transform(transform)
+ if param == 'lat':
+ new_param[param] = point.GetY()
+ else:
+ new_param[param] = point.GetX()
+ elif param == 'checked_categories':
+ if '%2C' in value:
+ value = '-'.join(value.split('%2C'))
+ if '_' in value:
+ value = '-'.join(value.split('_'))
+ new_param[param] = value
+ elif param == 'current_feature':
+ if value:
+ new_param[param] = 'marker-' + value
+ elif param == 'routing_steps':
+ if not value:
+ continue
+ vals = value.split('%2C')
+ steps = []
+ for idx in range(int(len(vals) / 2)):
+ steps.append("{}:{}".format(vals[2 * idx],
+ vals[2 * idx + 1]))
+ new_param['steps'] = ';'.join(steps)
+
+ if not new_param['zoom'] or int(new_param['zoom']) < 12:
+ new_param['zoom'] = '12'
+
+ new_param['coord'] = ''
+ if new_param['lon'] and new_param['lat']:
+ new_param['coord'] = '{lon}:{lat}'.format(**new_param)
+ new_param['routing_start'] = ''
+ if new_param['routing_start_lat'] and new_param['routing_start_lon']:
+ new_param['routing_start'] = \
+ "{routing_start_lat}:{routing_start_lon}".format(**new_param)
+ new_param['routing_end'] = ''
+ if new_param['routing_end_lat'] and new_param['routing_end_lon']:
+ new_param['routing_end'] = \
+ "{routing_end_lat}:{routing_end_lon}".format(**new_param)
+
+ tiny_url.parameters = \
+ "{zoom};{coord};"\
+ ";{checked_categories};{current_feature}"\
+ "{routing_speed};{routing_transport};"\
+ "{routing_start};"\
+ "{routing_end};"\
+ "{steps}".format(**new_param)
+ if TinyUrl.objects.filter(parameters=tiny_url.parameters).count():
+ tiny_url.delete()
+ else:
+ tiny_url.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('chimere', '0002_property_route'),
+ ]
+
+ operations = [
+ migrations.RunPython(convert_tiny_urls),
+ ]