diff --git a/.gitignore b/.gitignore index 9ed3439..9a04e87 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pyc *~ *.egg-info +*.pytest_cache diff --git a/girder_raster_tasks/tasks.py b/girder_raster_tasks/tasks.py index 004af45..29cb22c 100644 --- a/girder_raster_tasks/tasks.py +++ b/girder_raster_tasks/tasks.py @@ -6,7 +6,7 @@ import rasterio from rasterio.enums import Resampling from rasterio.warp import calculate_default_transform, reproject -from rasterio.tools.mask import mask +from rasterio.mask import mask from shapely.ops import transform from girder_worker.app import app from girder_worker.utils import girder_job @@ -14,10 +14,12 @@ def reprojectGeometry(geometry, projString): + inProj = pyproj.Proj('+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ') + outProj = pyproj.Proj(projString) project = partial( pyproj.transform, - pyproj.Proj(init='epsg:4326'), - pyproj.Proj(projString) + inProj, + outProj ) transformed = mapping(transform(project, shape(geometry))) diff --git a/tests/data/chicago.tif b/tests/data/chicago.tif new file mode 100644 index 0000000..0033499 Binary files /dev/null and b/tests/data/chicago.tif differ diff --git a/tests/data/geometry.json b/tests/data/geometry.json new file mode 100644 index 0000000..57fdf12 --- /dev/null +++ b/tests/data/geometry.json @@ -0,0 +1,27 @@ +{ + "type": "Polygon", + "coordinates": [ + [ + [ + -87.62412071228027, + 41.88406793446202 + ], + [ + -87.59622573852539, + 41.88406793446202 + ], + [ + -87.59622573852539, + 41.902788098873515 + ], + [ + -87.62412071228027, + 41.902788098873515 + ], + [ + -87.62412071228027, + 41.88406793446202 + ] + ] + ] +} diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_tasks.py b/tests/integration/test_tasks.py new file mode 100644 index 0000000..fc4d44a --- /dev/null +++ b/tests/integration/test_tasks.py @@ -0,0 +1,91 @@ +import json +import os +import time +import pytest + + +def _waitForJob(gc, jobId): + # Wait for the clip job to be completed + timeout = 10 + while timeout > 0: + time.sleep(1) + job = gc.get('job/{}'.format(jobId)) + if job['status'] == 3: + timeout = 0 + else: + timeout -= 1 + + +def _uploadFile(gc, admin, tiff): + public = gc.resourceLookup('/user/{}/Public'.format(admin['login'])) + size = os.stat(tiff).st_size + with open(tiff, 'rb') as f: + uploaded = gc.uploadFile(public['_id'], + f, + name=os.path.basename(tiff), + size=size, + parentType='folder') + + return uploaded + + +@pytest.mark.plugin('resonantgeodata') +@pytest.mark.plugin('worker') +def test_clip_task(admin, db, liveServer, fsAssetstore): + liveServer.authenticate(admin['login'], 'password') + # Set worker url + liveServer.put('system/setting', parameters={'key': 'worker.api_url', + 'value': liveServer.urlBase[:-1]}) + + public = liveServer.resourceLookup('/user/{}/Public'.format(admin['login'])) + sampleTiff = 'tests/data/chicago.tif' + output = 'clipped.tif' + uploaded = _uploadFile(liveServer, admin, sampleTiff) + geometry = 'tests/data/geometry.json' + + with open(geometry) as f: + data = json.load(f) + + job = liveServer.get('raster/clip', parameters={ + 'itemId': uploaded['itemId'], + 'geometry': json.dumps(data), + 'name': output, + 'folderId': public['_id'] + }) + + _waitForJob(liveServer, job['_id']) + resultFile = liveServer.get('resource/search', parameters={ + 'q': output, + 'types': '["file"]' + }) + + assert resultFile['file'][0]['size'] > 0 + + +@pytest.mark.plugin('resonantgeodata') +@pytest.mark.plugin('worker') +def test_reproject_task(admin, db, liveServer, fsAssetstore): + liveServer.authenticate(admin['login'], 'password') + # Set worker url + liveServer.put('system/setting', parameters={'key': 'worker.api_url', + 'value': liveServer.urlBase[:-1]}) + + public = liveServer.resourceLookup('/user/{}/Public'.format(admin['login'])) + sampleTiff = 'tests/data/chicago.tif' + output = 'reprojected.tif' + uploaded = _uploadFile(liveServer, admin, sampleTiff) + + job = liveServer.get('raster/reproject', parameters={ + 'itemId': uploaded['itemId'], + 'crs': '+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +units=m +no_defs ', + 'name': output, + 'folderId': public['_id'] + }) + + _waitForJob(liveServer, job['_id']) + resultFile = liveServer.get('resource/search', parameters={ + 'q': output, + 'types': '["file"]' + }) + + assert resultFile['file'][0]['size'] > 0 diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29