Permalink
Browse files

merged roland's changes

  • Loading branch information...
2 parents 306a9d5 + fdacb1e commit 137c11fb9a17eb346219a1a2c657c0462d5ed5d1 @reinout reinout committed May 25, 2012
View
@@ -3,10 +3,6 @@
from lizard_blockbox import models
-class ReachAdmin(admin.ModelAdmin):
- prepopulated_fields = {'slug': ('name',)}
-
-
-admin.site.register(models.Reach, ReachAdmin)
+admin.site.register(models.Reach)
admin.site.register(models.Measure)
admin.site.register(models.WaterLevelDifference)
View
@@ -0,0 +1,17 @@
+from django.db import models
+from django.utils.translation import ugettext_lazy as _
+
+from south.modelsinspector import add_introspection_rules
+
+
+class EmptyStringFloatField(models.FloatField):
+ empty_strings_allowed = True
+ description = _("Floating point number: converts empty string to None")
+
+ def get_prep_value(self, value):
+ if isinstance(value, basestring) and value.strip() == '':
+ return None
+ return super(EmptyStringFloatField, self).get_prep_value(value)
+
+# Add introspection rules for EmptyStringFloatField
+add_introspection_rules([], ['lizard_blockbox.fields.EmptyStringFloatField'])
@@ -1,48 +0,0 @@
-from django.db import transaction
-
-from lizard_blockbox import models
-
-from lizard_blockbox.management.commands import import_measure_xls
-
-
-class Command(import_measure_xls.Command):
- """Import command for measures excel sheets from Rijn river."""
-
- @transaction.commit_on_success
- def parse_sheet(self, sheet):
- measure, created = models.Measure.objects.get_or_create(
- short_name=sheet.name)
- if not created:
- #print 'This measure already exists: %s' % sheet.name
- return
- #print 'New measure: %s' % sheet.name
- # Flooding chance is always T1250, except for some parts of the Maas.
- flooding_T1250, _ = models.FloodingChance.objects.get_or_create(
- name='T1250')
- for row_nr in xrange(1, sheet.nrows):
- location, _, _, reference, _, difference, reach_slug = \
- sheet.row_values(row_nr)
- if not location.is_integer():
- continue
- try:
- riversegment = models.RiverSegment.objects.get(
- location=location, reach__slug=reach_slug)
- except:
- print 'This location does not exist: %i %s' % (
- location, reach_slug)
- continue
-
- ref_val, _ = models.ReferenceValue.objects.get_or_create(
- riversegment=riversegment,
- flooding_chance=flooding_T1250,
- defaults={'reference': reference,
- 'target': reference - 0.1}
- )
-
- models.WaterLevelDifference.objects.create(
- riversegment=riversegment,
- measure=measure,
- flooding_chance=flooding_T1250,
- reference_value=ref_val,
- level_difference=difference,
- )
@@ -0,0 +1,48 @@
+import sys
+
+import xlrd
+
+from django.core.management.base import BaseCommand
+from django.db import transaction
+
+from lizard_blockbox import models
+
+
+class Command(BaseCommand):
+ args = '<excelfile excelfile ...>'
+ help = ("Imports the measures table excelfile, "
+ "To flush use the management command: import_measure_xls --flush")
+
+ def handle(self, *args, **options):
+ if len(args) == 0:
+ print "Pass excel files as arguments."
+ sys.exit(1)
+
+ map(self.parse, args)
+
+ def parse(self, excel):
+ wb = xlrd.open_workbook(excel)
+ map(self.parse_sheet, wb.sheets())
+
+ @transaction.commit_on_success
+ def parse_sheet(self, sheet):
+ col_names = (
+ 'name', 'short_name', 'measure_type', 'km_from', 'km_to',
+ 'reach', 'riverpart', 'mhw_profit_cm', 'mhw_profit_m2',
+ 'investment_costs', 'benefits', 'b_o_costs', 'reinvestment',
+ 'damage', 'total_costs', 'quality_of_environment'
+ )
+
+ col_index = dict(zip(col_names, xrange(len(col_names))))
+
+ for row_nr in xrange(1, sheet.nrows):
+ row_values = sheet.row_values(row_nr)
+ default_values = dict(zip(col_names, row_values))
+ default_values['reach'], _ = models.Reach.objects.get_or_create(
+ slug=default_values['reach'],
+ defaults={'name': default_values['reach']})
+ measure, _ = models.Measure.objects.get_or_create(
+ short_name=row_values[col_index['short_name']])
+
+ models.Measure.objects.filter(id=measure.id).update(
+ **default_values)
@@ -7,7 +7,6 @@
from django.db import transaction
from lizard_blockbox import models
-from lizard_map.coordinates import transform_point
class Command(BaseCommand):
@@ -29,7 +28,7 @@ def handle(self, *args, **options):
# Delete all objects from models.
for model in ('RiverSegment', 'FloodingChance', 'Measure',
'ReferenceValue', 'WaterLevelDifference',
- 'Reach'):
+ 'Reach', 'NamedReach', 'SubsetReach'):
getattr(models, model).objects.all().delete()
if len(args) == 0:
@@ -46,54 +45,50 @@ def parse(self, excel):
@transaction.commit_on_success
def parse_sheet(self, sheet):
- reach, _ = models.Reach.objects.get_or_create(name='Maas', slug="MA")
- measure = models.Measure.objects.create(short_name=sheet.name)
- flooding_T250, _ = models.FloodingChance.objects.get_or_create(
- name='T250')
+ measure, created = models.Measure.objects.get_or_create(
+ short_name=sheet.name)
+ if not created:
+ #print 'This measure already exists: %s' % sheet.name
+ return
+ #print 'New measure: %s' % sheet.name
+ # Flooding chance is always T1250, except for some parts of the Maas.
flooding_T1250, _ = models.FloodingChance.objects.get_or_create(
name='T1250')
-
for row_nr in xrange(1, sheet.nrows):
- row = sheet.row_values(row_nr)
- location = row[0]
- #Parse N/Z reaches for river maas, only use north.
+ location, reference, _, difference, reach_slug = \
+ sheet.row_values(row_nr)
+
+ reach, _ = models.Reach.objects.get_or_create(slug=reach_slug)
+
+ #The Meuse has both North and South (Z) kilometers with the same
+ #kilometer identifier.
+ #XXX: ToDo 68_N > 68, 69_N > 68.5, 68_Z -> 69, 69_Z -> 69.5
if isinstance(location, basestring):
if not location.endswith('_N'):
- # Take only the North reaches
+ # Take only the North reaches for Now
continue
else:
location = float(location.strip('_N'))
- # Only use a kilomter resolution, which are integers
+
if not location.is_integer():
- continue
+ continue
try:
- riversegment = models.RiverSegment.objects.get(
+ riversegment, _ = models.RiverSegment.objects.get_or_create(
location=location, reach=reach)
- except models.RiverSegment.DoesNotExist:
- the_geom = transform_point(
- row[1], row[2], from_proj='rd', to_proj='wgs84')
- riversegment = models.RiverSegment.objects.create(
- location=location, the_geom=the_geom, reach=reach)
- # XXX: Named tuple?
- # Easy datastructure for the columns.
- chances = ((flooding_T250, 3, 7), (flooding_T1250, 4, 8))
- for chance in chances:
- d = {'riversegment': riversegment,
- 'flooding_chance': chance[0]}
- #Reference value can differ because
- #river segments can be defined twice.
- try:
- ref_val = models.ReferenceValue.objects.get(
- **d)
- except models.ReferenceValue.DoesNotExist:
- #target is -1 to just have a target that's different from
- #the reference value.
- ref_val = models.ReferenceValue.objects.create(
- reference=row[chance[1]],
- target=row[chance[1]] - 1,
- **d)
- d['measure'] = measure
- d['reference_value'] = ref_val
- models.WaterLevelDifference.objects.create(
- level_difference=row[chance[2]],
- **d)
+ except:
+ print 'This location does not exist: %i %s' % (
+ location, reach_slug)
+ continue
+
+ ref_val, _ = models.ReferenceValue.objects.get_or_create(
+ riversegment=riversegment,
+ flooding_chance=flooding_T1250,
+ defaults={'reference': reference})
+
+ models.WaterLevelDifference.objects.create(
+ riversegment=riversegment,
+ measure=measure,
+ flooding_chance=flooding_T1250,
+ reference_value=ref_val,
+ level_difference=difference,
+ )
@@ -1,36 +0,0 @@
-from django.core.management.base import BaseCommand
-from django.utils import simplejson as json
-
-from lizard_blockbox import models
-from lizard_map.coordinates import transform_point
-
-
-class Command(BaseCommand):
- args = '<geojson geojson ...>'
- help = "Imports the rijntakken geojson file"
-
- def handle(self, *args, **options):
- map(self.parse, args)
-
- def parse(self, json_file):
- data = json.load(open(json_file, 'rb'))
- features = data['features']
- for feature in features:
- properties = feature['properties']
- reach_name = properties["KENMERK"]
- location, reach_slug = properties["MODELKM"].split('_')
-
- # Use a kilometer resolution.
- location = float(location)
- if not location.is_integer():
- continue
-
- x, y = feature["geometry"]["coordinates"][0]
- the_geom = transform_point(
- x, y, from_proj='google', to_proj='wgs84')
-
- reach, _ = models.Reach.objects.get_or_create(
- name=reach_name, slug=reach_slug)
-
- models.RiverSegment.objects.create(
- location=location, the_geom=the_geom, reach=reach)
@@ -0,0 +1,39 @@
+import sys
+
+import xlrd
+
+from django.core.management.base import BaseCommand
+from django.db import transaction
+
+from lizard_blockbox import models
+
+
+class Command(BaseCommand):
+ args = '<excelfile excelfile ...>'
+ help = ("Imports the measures table excelfile, "
+ "To flush use the management command: import_measure_xls --flush")
+
+ def handle(self, *args, **options):
+ if len(args) == 0:
+ print "Pass excel files as arguments."
+ sys.exit(1)
+
+ map(self.parse, args)
+
+ def parse(self, excel):
+ wb = xlrd.open_workbook(excel)
+ map(self.parse_sheet, wb.sheets())
+
+ @transaction.commit_on_success
+ def parse_sheet(self, sheet):
+ for row_nr in xrange(1, sheet.nrows):
+ name, reach_slug, km_from, km_to = sheet.row_values(row_nr)
+ km_from, km_to = int(km_from), int(km_to)
+ reach = models.Reach.objects.get(slug=reach_slug)
+ named_reach, _ = models.NamedReach.objects.get_or_create(name=name)
+
+ models.SubsetReach.objects.get_or_create(
+ reach=reach,
+ named_reach=named_reach,
+ km_from=km_from,
+ km_to=km_to)
@@ -0,0 +1,68 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Deleting field 'ReferenceValue.target'
+ db.delete_column('lizard_blockbox_referencevalue', 'target')
+
+
+ def backwards(self, orm):
+
+ # User chose to not deal with backwards NULL issues for 'ReferenceValue.target'
+ raise RuntimeError("Cannot reverse this migration. 'ReferenceValue.target' and its values cannot be restored.")
+
+
+ models = {
+ 'lizard_blockbox.floodingchance': {
+ 'Meta': {'object_name': 'FloodingChance'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '10'})
+ },
+ 'lizard_blockbox.measure': {
+ 'Meta': {'ordering': "('km_from',)", 'object_name': 'Measure'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'km_from': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'km_to': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'measure_type': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+ 'short_name': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+ 'traject': ('django.db.models.fields.CharField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'})
+ },
+ 'lizard_blockbox.reach': {
+ 'Meta': {'object_name': 'Reach'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'})
+ },
+ 'lizard_blockbox.referencevalue': {
+ 'Meta': {'object_name': 'ReferenceValue'},
+ 'flooding_chance': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.FloodingChance']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'reference': ('django.db.models.fields.FloatField', [], {}),
+ 'riversegment': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.RiverSegment']"})
+ },
+ 'lizard_blockbox.riversegment': {
+ 'Meta': {'object_name': 'RiverSegment'},
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'location': ('django.db.models.fields.IntegerField', [], {}),
+ 'reach': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.Reach']"}),
+ 'the_geom': ('django.contrib.gis.db.models.fields.PointField', [], {'null': 'True', 'blank': 'True'})
+ },
+ 'lizard_blockbox.waterleveldifference': {
+ 'Meta': {'object_name': 'WaterLevelDifference'},
+ 'flooding_chance': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.FloodingChance']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level_difference': ('django.db.models.fields.FloatField', [], {}),
+ 'measure': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.Measure']"}),
+ 'reference_value': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.ReferenceValue']"}),
+ 'riversegment': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['lizard_blockbox.RiverSegment']"})
+ }
+ }
+
+ complete_apps = ['lizard_blockbox']
Oops, something went wrong.

0 comments on commit 137c11f

Please sign in to comment.