Skip to content

Commit

Permalink
WarpedVRT does its thing in __init__ rather than start
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Gillies committed May 16, 2017
1 parent 8a98718 commit 80a4379
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 87 deletions.
35 changes: 14 additions & 21 deletions rasterio/_warp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -627,19 +627,16 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):
if init_dest_nodata is True and 'init_dest' not in warp_extras:
self.warp_extras['init_dest'] = 'NO_DATA'

def start(self):
"""Called to start reading a dataset."""
cdef GDALDriverH driver = NULL
cdef GDALDatasetH hds = NULL
cdef GDALDatasetH hds_warped = NULL
cdef const char *cypath = NULL
cdef char *dst_crs_wkt = NULL
cdef OGRSpatialReferenceH osr = NULL
cdef char **warp_extras = NULL
cdef char **c_warp_extras = NULL
cdef GDALWarpOptions *psWOptions = NULL

cdef float tolerance = self.tolerance
cdef int resampling = self.resampling
cdef float c_tolerance = tolerance
cdef GDALResampleAlg c_resampling = resampling

# Convert destination CRS to a C WKT string.
try:
Expand All @@ -650,13 +647,6 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):

log.debug("Exported CRS to WKT.")

#try:
# with nogil:
# hds = GDALOpen(cypath, <GDALAccess>0)
# hds = exc_wrap_pointer(hds)
#except CPLE_OpenFailedError as err:
# raise RasterioIOError(err.errmsg)

hds = (<DatasetReaderBase?>self.src_dataset).handle()
hds = exc_wrap_pointer(hds)

Expand All @@ -665,24 +655,24 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):
for key, val in self.warp_extras.items():
key = key.upper().encode('utf-8')
val = str(val).upper().encode('utf-8')
warp_extras = CSLSetNameValue(
warp_extras, <const char *>key, <const char *>val)
c_warp_extras = CSLSetNameValue(
c_warp_extras, <const char *>key, <const char *>val)

psWOptions = create_warp_options(
<GDALResampleAlg>self.resampling, self.src_nodata,
self.dst_nodata, GDALGetRasterCount(hds), <const char **>warp_extras)
<GDALResampleAlg>c_resampling, self.src_nodata,
self.dst_nodata, GDALGetRasterCount(hds), <const char **>c_warp_extras)

try:
with nogil:
hds_warped = GDALAutoCreateWarpedVRT(
hds, NULL, dst_crs_wkt, <GDALResampleAlg>resampling,
tolerance, psWOptions)
hds, NULL, dst_crs_wkt, c_resampling,
c_tolerance, psWOptions)
self._hds = exc_wrap_pointer(hds_warped)
except CPLE_OpenFailedError as err:
raise RasterioIOError(err.errmsg)
finally:
CPLFree(dst_crs_wkt)
CSLDestroy(warp_extras)
CSLDestroy(c_warp_extras)
GDALDestroyWarpOptions(psWOptions)


Expand All @@ -702,10 +692,13 @@ cdef class WarpedVRTReaderBase(DatasetReaderBase):

self._closed = False

def start(self):
"""Starts the VRT's life cycle."""

log.debug("Dataset %r is started.", self)

def stop(self):
"""Ends the dataset's life cycle"""
"""Ends the VRT's life cycle"""
if self._hds != NULL:
GDALFlushCache(self._hds)
GDALClose(self._hds)
Expand Down
18 changes: 13 additions & 5 deletions rasterio/vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class WarpedVRT(WarpedVRTReaderBase, WindowMethodsMixin,
----------
src_dataset : dataset
The dataset object that is virtually warped.
The dataset object to be virtually warped.
dst_crs : CRS or str
The warp operation's destination coordinate reference system.
resampling : int
Expand All @@ -28,12 +28,14 @@ class WarpedVRT(WarpedVRTReaderBase, WindowMethodsMixin,
tolerance : float
The maximum error tolerance in input pixels when approximating
the warp transformation. The default is 0.125.
src_nodata : float, int, or None
The nodata value for the source dataset.
dst_nodata : float, int, or None
src_nodata : float
A nodata value for the source data. It may be a value other
than the nodata value of src_dataset.
dst_nodata : float, int
The nodata value for the virtually warped dataset.
warp_extras : dict
GDAL extra warp options.
GDAL extra warp options. See
http://www.gdal.org/structGDALWarpOptions.html.
Example
-------
Expand All @@ -53,4 +55,10 @@ def __enter__(self):
return self

def __exit__(self, *args, **kwargs):
self.close()

def __del__(self):
self.close()

def close(self):
self.stop()
61 changes: 0 additions & 61 deletions tests/test_warp_wrapper.py

This file was deleted.

78 changes: 78 additions & 0 deletions tests/test_warpedvrt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import boto3
from packaging.version import parse
import pytest

import rasterio
from rasterio.enums import Resampling
from rasterio.vrt import WarpedVRT
from rasterio.windows import Window


# Custom markers.
mingdalversion = pytest.mark.skipif(
parse(rasterio.__gdal_version__) < parse('2.1.0dev'),
reason="S3 raster access requires GDAL 2.1")

credentials = pytest.mark.skipif(
not(boto3.Session()._session.get_credentials()),
reason="S3 raster access requires credentials")


def test_warped_vrt(path_rgb_byte_tif):
"""A VirtualVRT has the expected VRT properties."""
with rasterio.open(path_rgb_byte_tif) as src:
vrt = WarpedVRT(src, dst_crs='EPSG:3857')
assert vrt.dst_crs == 'EPSG:3857'
assert vrt.src_nodata is None
assert vrt.dst_nodata is None
assert vrt.tolerance == 0.125
assert vrt.resampling == Resampling.nearest
assert vrt.warp_extras == {'init_dest': 'NO_DATA'}


def test_warped_vrt_source(path_rgb_byte_tif):
"""A VirtualVRT has the expected source dataset."""
with rasterio.open(path_rgb_byte_tif) as src:
vrt = WarpedVRT(src, dst_crs='EPSG:3857')
assert vrt.src_dataset == src


def test_wrap_file(path_rgb_byte_tif):
"""A VirtualVRT has the expected dataset properties."""
with rasterio.open(path_rgb_byte_tif) as src:
vrt = WarpedVRT(src, dst_crs='EPSG:3857')
assert vrt.crs == 'EPSG:3857'
assert tuple(round(x, 1) for x in vrt.bounds) == (
-8789636.7, 2700460.0, -8524406.4, 2943560.2)
assert vrt.name.startswith('WarpedVRT(')
assert vrt.name.endswith('tests/data/RGB.byte.tif)')
assert vrt.indexes == (1, 2, 3)
assert vrt.nodatavals == (0, 0, 0)
assert vrt.dtypes == ('uint8', 'uint8', 'uint8')
assert vrt.read().shape == (3, 736, 803)


def test_warp_extras(path_rgb_byte_tif):
"""INIT_DEST warp extra is passed through."""
with rasterio.open(path_rgb_byte_tif) as src:
with WarpedVRT(src, dst_crs='EPSG:3857', init_dest=255) as vrt:
rgb = vrt.read()
assert (rgb[:, 0, 0] == 255).all()


@mingdalversion
@credentials
@pytest.mark.network
def test_wrap_s3():
"""A warp wrapper's dataset has the expected properties"""
L8TIF = "s3://landsat-pds/L8/139/045/LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF"
with rasterio.open(L8TIF) as src:
with WarpedVRT(src, dst_crs='EPSG:3857') as vrt:
assert vrt.crs == 'EPSG:3857'
assert tuple(round(x, 1) for x in vrt.bounds) == (
9556764.6, 2345109.3, 9804595.9, 2598509.1)
assert vrt.name == 'WarpedVRT(s3://landsat-pds/L8/139/045/LC81390452014295LGN00/LC81390452014295LGN00_B1.TIF)'
assert vrt.indexes == (1,)
assert vrt.nodatavals == (None,)
assert vrt.dtypes == ('uint16',)
assert vrt.shape == (7827, 7655)

0 comments on commit 80a4379

Please sign in to comment.