def test_rasterstats_mismatched_crs(monkeypatch): monkeypatch.setattr('pandarus.calculate.gen_zonal_stats', fake_zonal_stats) with tempfile.TemporaryDirectory() as dirpath: fp = os.path.join(dirpath, "test.json") with pytest.warns(UserWarning): raster_statistics(grid, 'name', dem, output=fp)
def test_rasterstats(monkeypatch): monkeypatch.setattr('pandarus.calculate.gen_zonal_stats', fake_zonal_stats) with tempfile.TemporaryDirectory() as dirpath: fp = os.path.join(dirpath, "test.json") result = raster_statistics(grid, 'name', range_raster, output=fp, compress=False) assert result == fp result = json.load(open(fp)) expected = [ ['grid cell 0', 0], ['grid cell 1', 1], ['grid cell 2', 2], ['grid cell 3', 3], ] assert result['metadata'].keys() == {'vector', 'raster', 'when'} assert result['metadata']['vector'].keys() == { 'field', 'filename', 'path', 'sha256' } assert result['metadata']['raster'].keys() == { 'band', 'filename', 'path', 'sha256' } assert result['data'] == expected
def test_rasterstats_new_path(monkeypatch): monkeypatch.setattr('pandarus.calculate.gen_zonal_stats', fake_zonal_stats) fp = raster_statistics(grid, 'name', range_raster, compress=False) assert 'rasterstats' in fp assert '.json' in fp assert os.path.isfile(fp) os.remove(fp)
def test_rasterstats_overwrite_existing(monkeypatch): monkeypatch.setattr('pandarus.calculate.gen_zonal_stats', fake_zonal_stats) with tempfile.TemporaryDirectory() as dirpath: fp = os.path.join(dirpath, "test.json") with open(fp, "w") as f: f.write("Original content") result = raster_statistics(grid, 'name', range_raster, output=fp, compress=False) assert result == fp content = open(result).read() assert content != 'Original content'
def rasterstats_task(vector_id, raster_id, band, output_fp): vector = File.get(File.id == vector_id) raster = File.get(File.id == raster_id) # Job enqueued twice if RasterStats.select().where( (RasterStats.vector == vector) & (RasterStats.raster == raster)).count(): return output = raster_statistics(vector.filepath, vector.field, raster.filepath, output=output_fp, band=band) # Job enqueued twice if RasterStats.select().where( (RasterStats.vector == vector) & (RasterStats.raster == raster)).count(): return RasterStats( vector=vector, raster=raster, output=output ).save()
def test_rasterstats_invalid(): with pytest.raises(AssertionError): raster_statistics(grid, 'name', square)