def test_clear_data(self): config = RainappConfig(name="test", jdbcsource_id=0, filter_id="test", slug="test") config.save() geo = GeoObject(name="test", x=0, y=0, area=0, geometry=GEOSGeometry(SOME_GEOOBJECT), config=config) count = GeoObject.objects.count() geo.save() self.assertEqual(GeoObject.objects.count(), count + 1) clear_old_data() self.assertEqual(GeoObject.objects.count(), 0)
def load_shapefile(section, options): if 'shapefile' in options: drv = ogr.GetDriverByName('ESRI Shapefile') source = drv.Open(options['shapefile']) else: source = None if source is None: raise ValueError("Shapefile (%s) not found." % (options.get('shapefile', "None"),)) if 'slug' in options: try: rainapp_config = RainappConfig.objects.get(slug=options['slug']) except RainappConfig.DoesNotExist: raise ValueError("RainappConfig with slug '%s' not found." % (options['slug'],)) else: raise ValueError("No Rainapp Config slug defined.") layer = source.GetLayer() logger.info("Importing new geoobjects...") number_of_features = 0 for feature in layer: geom = feature.GetGeometryRef() def get_field(fieldname, default=None): try: name = options[fieldname] return feature.GetField(feature.GetFieldIndex(name)) except ValueError: return default kwargs = { 'municipality_id': get_field('id_field'), 'name': get_field('name_field'), 'x': get_field('x_field'), 'y': get_field('y_field'), 'area': get_field('area_field', -1), 'geometry': GEOSGeometry(geom.ExportToWkt(), srid=4326), 'config': rainapp_config, } geoobject = GeoObject(**kwargs) geoobject.save() number_of_features += 1 logger.info("Added %s polygons.", number_of_features) return number_of_features