diff --git a/app/migrations/9999_postgis.py b/app/migrations/9999_postgis.py new file mode 100644 index 00000000..1c714b46 --- /dev/null +++ b/app/migrations/9999_postgis.py @@ -0,0 +1,9 @@ +from django.db import migrations + +class Migration(migrations.Migration): + dependencies = [("app", "0001_initial")] + + operations = [ + migrations.RunSQL("SET postgis.enable_outdb_rasters TO True;"), + migrations.RunSQL("SET postgis.gdal_enabled_drivers TO 'GTiff';") + ] \ No newline at end of file diff --git a/app/models.py b/app/models.py index 041ae413..88fb149d 100644 --- a/app/models.py +++ b/app/models.py @@ -243,7 +243,7 @@ class Task(models.Model): # Process this as a new task # Removing its UUID will cause the scheduler # to process this the next tick - self.uuid = None + self.uuid = '' self.console_output = "" self.processing_time = -1 @@ -304,11 +304,11 @@ class Task(models.Model): logger.info("Downloading all.zip for {}".format(self)) # Download all assets - #zip_stream = self.processing_node.download_task_asset(self.uuid, "all.zip") + zip_stream = self.processing_node.download_task_asset(self.uuid, "all.zip") zip_path = os.path.join(assets_dir, "all.zip") - #with open(zip_path, 'wb') as fd: - # for chunk in zip_stream.iter_content(4096): - # fd.write(chunk) + with open(zip_path, 'wb') as fd: + for chunk in zip_stream.iter_content(4096): + fd.write(chunk) logger.info("Done downloading all.zip for {}".format(self)) @@ -319,9 +319,16 @@ class Task(models.Model): logger.info("Extracted all.zip for {}".format(self)) # Add to database orthophoto - orthophoto_path = self.assets_path("odm_orthophoto", "odm_orthophoto.tif") + orthophoto_path = os.path.realpath(self.assets_path("odm_orthophoto", "odm_orthophoto.tif")) if os.path.exists(orthophoto_path): - self.orthophoto = GDALRaster(orthophoto_path, write=True) + orthophoto = GDALRaster(orthophoto_path, write=True) + + # We need to transform to 4326 before we can store it + # as an offdb raster field + orthophoto_4326_path = os.path.realpath(self.assets_path("odm_orthophoto", "odm_orthophoto_4326.tif")) + self.orthophoto = orthophoto.transform(4326, 'GTiff', orthophoto_4326_path) + + logger.info("Imported orthophoto {} for {}".format(orthophoto_4326_path, self)) self.save() except ProcessingException as e: diff --git a/app/tests/test_db.py b/app/tests/test_db.py index 3ab83aa1..928721f0 100644 --- a/app/tests/test_db.py +++ b/app/tests/test_db.py @@ -1,4 +1,6 @@ from django.contrib.gis.gdal import GDALRaster +from django.db import InternalError +from django.db import transaction from .classes import BootTestCase from app.models import Task, Project @@ -13,8 +15,17 @@ class TestApi(BootTestCase): def test_db(self): # Make sure we can use PostGIS raster type + raster = GDALRaster(os.path.realpath(os.path.join("app", "fixtures", "orthophoto.tif")), write=True) + + self.assertTrue(raster.srid == 32615) + with transaction.atomic(): + # We cannot store offdb references with SRID different than the one declared (4326) + self.assertRaises(InternalError, Task.objects.create, project=Project.objects.latest("created_at"), + orthophoto=raster) + + # All OK when we transform to 4326 task = Task.objects.create(project=Project.objects.latest("created_at"), - orthophoto=GDALRaster(os.path.join("app", "fixtures", "orthophoto.tif"), write=True)) + orthophoto=raster.transform(4326)) task.refresh_from_db() self.assertTrue(task.orthophoto.srid == 4326) self.assertTrue(task.orthophoto.width == 252) # not original size, warp happened