def from_path(path, *args, **kwargs): """Create a session object suited to the data at `path`. Parameters ---------- path : str A dataset path or identifier. args : sequence Positional arguments for the foreign session constructor. kwargs : dict Keyword arguments for the foreign session constructor. Returns ------- Session """ if not path: return DummySession() path = parse_path(path) if isinstance(path, UnparsedPath) or path.is_local: return DummySession() elif path.scheme == "s3" or "amazonaws.com" in path.path: return AWSSession(*args, **kwargs) # This factory can be extended to other cloud providers here. # elif path.scheme == "cumulonimbus": # for example. # return CumulonimbusSession(*args, **kwargs) else: return DummySession()
def test_parse_path_accept_get_params(): # See https://github.com/mapbox/rasterio/issues/1121 parsed = parse_path('http://example.com/index?a=1') assert isinstance(parsed, ParsedPath) assert parsed.path == 'example.com/index?a=1' assert parsed.archive is None assert parsed.scheme == 'http'
def file_in_handler(ctx, param, value): """Normalize ordinary filesystem and VFS paths""" try: path = parse_path(value) if isinstance(path, UnparsedPath): return path.name elif path.scheme and path.is_remote: return path.name elif path.archive: if os.path.exists(path.archive) and rasterio.shutil.exists(value): archive = abspath_forward_slashes(path.archive) return "{}://{}!{}".format(path.scheme, archive, path.path) else: raise IOError( "Input archive {} does not exist".format(path.archive)) else: if os.path.exists(path.path) and rasterio.shutil.exists(value): return abspath_forward_slashes(path.path) else: raise IOError( "Input file {} does not exist".format(path.path)) except Exception: raise click.BadParameter("{} is not a valid input file".format(value))
def wrapper(*args, **kwds): if local._env: env_ctor = Env else: env_ctor = Env.from_defaults with env_ctor() as wrapper_env: if isinstance(args[0], str): path = parse_path(args[0]) scheme = getattr(path, 'scheme', None) if scheme == 's3': wrapper_env.credentialize() log.debug("Credentialized: {!r}".format(getenv())) else: pass else: pass return f(*args, **kwds)
def cls_from_path(path): """Find the session class suited to the data at `path`. Parameters ---------- path : str A dataset path or identifier. Returns ------- class """ if not path: return DummySession path = parse_path(path) if isinstance(path, UnparsedPath) or path.is_local: return DummySession elif path.scheme == "s3" or "amazonaws.com" in path.path: return AWSSession elif path.scheme == "oss" or "aliyuncs.com" in path.path: return OSSSession elif path.path.startswith("/vsiswift/"): return SwiftSession # This factory can be extended to other cloud providers here. # elif path.scheme == "cumulonimbus": # for example. # return CumulonimbusSession(*args, **kwargs) else: return DummySession
def test_parse_path_file_scheme(): """Correctly parse file:// URL""" parsed = parse_path('file://foo.tif') assert parsed.path == 'foo.tif' assert parsed.archive is None assert parsed.scheme == 'file'
def _boundless_vrt_doc(src_dataset, nodata=None, width=None, height=None, transform=None): """Make a VRT XML document.""" nodata = nodata or src_dataset.nodata width = width or src_dataset.width height = height or src_dataset.height transform = transform or src_dataset.transform vrtdataset = ET.Element('VRTDataset') vrtdataset.attrib['rasterYSize'] = str(height) vrtdataset.attrib['rasterXSize'] = str(width) srs = ET.SubElement(vrtdataset, 'SRS') srs.text = src_dataset.crs.wkt geotransform = ET.SubElement(vrtdataset, 'GeoTransform') geotransform.text = ','.join([str(v) for v in transform.to_gdal()]) for bidx, ci, block_shape, dtype in zip(src_dataset.indexes, src_dataset.colorinterp, src_dataset.block_shapes, src_dataset.dtypes): vrtrasterband = ET.SubElement(vrtdataset, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = _gdal_typename(dtype) vrtrasterband.attrib['band'] = str(bidx) if nodata is not None: nodatavalue = ET.SubElement(vrtrasterband, 'NoDataValue') nodatavalue.text = str(nodata) colorinterp = ET.SubElement(vrtrasterband, 'ColorInterp') colorinterp.text = ci.name.capitalize() simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) if src_dataset.nodata is not None: nodata_elem = ET.SubElement(simplesource, 'NODATA') nodata_elem.text = str(src_dataset.nodata) if all(MaskFlags.per_dataset in flags for flags in src_dataset.mask_flag_enums): maskband = ET.SubElement(vrtdataset, 'MaskBand') vrtrasterband = ET.SubElement(maskband, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = 'Byte' simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = 'mask,1' sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = 'Byte' sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) return ET.tostring(vrtdataset)
def test_parse_path_zip_and_file(): """Correctly parse zip+file scheme URL""" parsed = parse_path('zip+file://tests/data/files.zip!foo.tif') assert parsed.path == 'foo.tif' assert parsed.archive == 'tests/data/files.zip' assert parsed.scheme == 'zip+file'
def test_vsi_path_zip(): """A zip:// URLs vsi path is correct (see #1377)""" from rasterio.path import parse_path, vsi_path url = 'zip:///path/to/zip/some.zip!path/to/file.txt' assert vsi_path(parse_path(url)) == '/vsizip//path/to/zip/some.zip/path/to/file.txt'
def test_parse_gdal(): """GDAL dataset identifiers fall through properly""" assert parse_path('GDAL:filepath:varname').path == 'GDAL:filepath:varname'
def test_parse_gdal_vsi_alias(): """Check that the alias function works""" assert parse_path('/vsifoo/bar').path == '/vsifoo/bar'
def test_parse_path_file(): """Correctly parse an ordinary filesystem path""" parsed = parse_path('/foo.tif') assert parsed.path == '/foo.tif' assert parsed.archive is None assert parsed.scheme is None
def _boundless_vrt_doc(src_dataset, nodata=None, background=None, hidenodata=False, width=None, height=None, transform=None): """Make a VRT XML document. Parameters ---------- src_dataset : Dataset The dataset to wrap. background : Dataset, optional A dataset that provides the optional VRT background. NB: this dataset must have the same number of bands as the src_dataset. Returns ------- bytes An ascii-encoded string (an ElementTree detail) """ nodata = nodata or src_dataset.nodata width = width or src_dataset.width height = height or src_dataset.height transform = transform or src_dataset.transform vrtdataset = ET.Element('VRTDataset') vrtdataset.attrib['rasterYSize'] = str(height) vrtdataset.attrib['rasterXSize'] = str(width) srs = ET.SubElement(vrtdataset, 'SRS') srs.text = src_dataset.crs.wkt if src_dataset.crs else "" geotransform = ET.SubElement(vrtdataset, 'GeoTransform') geotransform.text = ','.join([str(v) for v in transform.to_gdal()]) for bidx, ci, block_shape, dtype in zip(src_dataset.indexes, src_dataset.colorinterp, src_dataset.block_shapes, src_dataset.dtypes): vrtrasterband = ET.SubElement(vrtdataset, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = _gdal_typename(dtype) vrtrasterband.attrib['band'] = str(bidx) if nodata is not None: nodatavalue = ET.SubElement(vrtrasterband, 'NoDataValue') nodatavalue.text = str(nodata) if hidenodata: hidenodatavalue = ET.SubElement(vrtrasterband, 'HideNoDataValue') hidenodatavalue.text = "1" colorinterp = ET.SubElement(vrtrasterband, 'ColorInterp') colorinterp.text = ci.name.capitalize() if background is not None: simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(background.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(background.width) srcrect.attrib['ySize'] = str(background.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = '0' dstrect.attrib['yOff'] = '0' dstrect.attrib['xSize'] = str(width) dstrect.attrib['ySize'] = str(height) simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str( (src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str( (src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width * src_dataset.transform.a / transform.a) dstrect.attrib['ySize'] = str(src_dataset.height * src_dataset.transform.e / transform.e) if src_dataset.nodata is not None: nodata_elem = ET.SubElement(simplesource, 'NODATA') nodata_elem.text = str(src_dataset.nodata) if all(MaskFlags.per_dataset in flags for flags in src_dataset.mask_flag_enums): maskband = ET.SubElement(vrtdataset, 'MaskBand') vrtrasterband = ET.SubElement(maskband, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = 'Byte' simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = 'mask,1' sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = 'Byte' sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str( (src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str( (src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) return ET.tostring(vrtdataset)
def test_parse_windows_path(monkeypatch): """Return Windows paths unparsed""" monkeypatch.setattr(sys, 'platform', 'win32') assert parse_path(r'C:\\foo.tif').path == r'C:\\foo.tif'
def test_parse_gdal_vsi(): """GDAL dataset identifiers fall through properly""" assert parse_path('/vsifoo/bar').path == '/vsifoo/bar'
def test_parse_path_file(): """Correctly parse an ordinary filesystem path""" parsed = parse_path('/foo.tif') assert parsed.path == '/foo.tif'
def _boundless_vrt_doc( src_dataset, nodata=None, background=None, hidenodata=False, width=None, height=None, transform=None, masked=False): """Make a VRT XML document. Parameters ---------- src_dataset : Dataset The dataset to wrap. background : int or float, optional The background fill value for the boundless VRT. masked : book If True, the src_dataset is replaced by its valid data mask. Returns ------- str An XML text string. """ nodata = nodata or src_dataset.nodata width = width or src_dataset.width height = height or src_dataset.height transform = transform or src_dataset.transform vrtdataset = ET.Element('VRTDataset') vrtdataset.attrib['rasterYSize'] = str(height) vrtdataset.attrib['rasterXSize'] = str(width) srs = ET.SubElement(vrtdataset, 'SRS') srs.text = src_dataset.crs.wkt if src_dataset.crs else "" geotransform = ET.SubElement(vrtdataset, 'GeoTransform') geotransform.text = ','.join([str(v) for v in transform.to_gdal()]) for bidx, ci, block_shape, dtype in zip(src_dataset.indexes, src_dataset.colorinterp, src_dataset.block_shapes, src_dataset.dtypes): vrtrasterband = ET.SubElement(vrtdataset, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = _gdal_typename(dtype) vrtrasterband.attrib['band'] = str(bidx) if background is not None or nodata is not None: nodatavalue = ET.SubElement(vrtrasterband, 'NoDataValue') nodatavalue.text = str(background or nodata) if hidenodata: hidenodatavalue = ET.SubElement(vrtrasterband, 'HideNoDataValue') hidenodatavalue.text = "1" colorinterp = ET.SubElement(vrtrasterband, 'ColorInterp') colorinterp.text = ci.name.capitalize() complexsource = ET.SubElement(vrtrasterband, 'ComplexSource') sourcefilename = ET.SubElement(complexsource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.attrib["shared"] = "0" sourcefilename.text = parse_path(src_dataset.name).as_vsi() sourceband = ET.SubElement(complexsource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(complexsource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(complexsource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(complexsource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width * src_dataset.transform.a / transform.a) dstrect.attrib['ySize'] = str(src_dataset.height * src_dataset.transform.e / transform.e) if src_dataset.nodata is not None: nodata_elem = ET.SubElement(complexsource, 'NODATA') nodata_elem.text = str(src_dataset.nodata) if src_dataset.options is not None: openoptions = ET.SubElement(complexsource, 'OpenOptions') for ookey, oovalue in src_dataset.options.items(): ooi = ET.SubElement(openoptions, 'OOI') ooi.attrib['key'] = str(ookey) ooi.text = str(oovalue) # Effectively replaces all values of the source dataset with # 255. Due to GDAL optimizations, the source dataset will not # be read, so we get a performance improvement. if masked: scaleratio = ET.SubElement(complexsource, 'ScaleRatio') scaleratio.text = '0' scaleoffset = ET.SubElement(complexsource, 'ScaleOffset') scaleoffset.text = '255' if all(MaskFlags.per_dataset in flags for flags in src_dataset.mask_flag_enums): maskband = ET.SubElement(vrtdataset, 'MaskBand') vrtrasterband = ET.SubElement(maskband, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = 'Byte' simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.attrib["shared"] = "0" sourcefilename.text = parse_path(src_dataset.name).as_vsi() sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = 'mask,1' sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = 'Byte' sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) return ET.tostring(vrtdataset).decode('ascii')
def open(fp, mode='r', driver=None, width=None, height=None, count=None, crs=None, transform=None, dtype=None, nodata=None, sharing=False, **kwargs): """Open a dataset for reading or writing. The dataset may be located in a local file, in a resource located by a URL, or contained within a stream of bytes. In read ('r') or read/write ('r+') mode, no keyword arguments are required: these attributes are supplied by the opened dataset. In write ('w' or 'w+') mode, the driver, width, height, count, and dtype keywords are strictly required. Parameters ---------- fp : str, file object or pathlib.Path object A filename or URL, a file object opened in binary ('rb') mode, or a Path object. mode : str, optional 'r' (read, the default), 'r+' (read/write), 'w' (write), or 'w+' (write/read). driver : str, optional A short format driver name (e.g. "GTiff" or "JPEG") or a list of such names (see GDAL docs at http://www.gdal.org/formats_list.html). In 'w' or 'w+' modes a single name is required. In 'r' or 'r+' modes the driver can usually be omitted. Registered drivers will be tried sequentially until a match is found. When multiple drivers are available for a format such as JPEG2000, one of them can be selected by using this keyword argument. width : int, optional The number of columns of the raster dataset. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. height : int, optional The number of rows of the raster dataset. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. count : int, optional The count of dataset bands. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. crs : str, dict, or CRS; optional The coordinate reference system. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. transform : Affine instance, optional Affine transformation mapping the pixel space to geographic space. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. dtype : str or numpy dtype The data type for bands. For example: 'uint8' or ``rasterio.uint16``. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. nodata : int, float, or nan; optional Defines the pixel value to be interpreted as not valid data. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. sharing : bool; optional To reduce overhead and prevent programs from running out of file descriptors, rasterio maintains a pool of shared low level dataset handles. When `True` this function will use a shared handle if one is available. Multithreaded programs must avoid sharing and should set *sharing* to `False`. kwargs : optional These are passed to format drivers as directives for creating or interpreting datasets. For example: in 'w' or 'w+' modes a `tiled=True` keyword argument will direct the GeoTIFF format driver to create a tiled, rather than striped, TIFF. Returns ------- A ``DatasetReader`` or ``DatasetWriter`` object. Examples -------- To open a GeoTIFF for reading using standard driver discovery and no directives: >>> import rasterio >>> with rasterio.open('example.tif') as dataset: ... print(dataset.profile) To open a JPEG2000 using only the JP2OpenJPEG driver: >>> with rasterio.open( ... 'example.jp2', driver='JP2OpenJPEG') as dataset: ... print(dataset.profile) To create a new 8-band, 16-bit unsigned, tiled, and LZW-compressed GeoTIFF with a global extent and 0.5 degree resolution: >>> from rasterio.transform import from_origin >>> with rasterio.open( ... 'example.tif', 'w', driver='GTiff', dtype='uint16', ... width=720, height=360, count=8, crs='EPSG:4326', ... transform=from_origin(-180.0, 90.0, 0.5, 0.5), ... nodata=0, tiled=True, compress='lzw') as dataset: ... dataset.write(...) """ if not isinstance(fp, str): if not (hasattr(fp, 'read') or hasattr(fp, 'write') or isinstance(fp, Path)): raise TypeError("invalid path or file: {0!r}".format(fp)) if mode and not isinstance(mode, str): raise TypeError("invalid mode: {0!r}".format(mode)) if driver and not isinstance(driver, str): raise TypeError("invalid driver: {0!r}".format(driver)) if dtype and not check_dtype(dtype): raise TypeError("invalid dtype: {0!r}".format(dtype)) if nodata is not None: nodata = float(nodata) if transform: transform = guard_transform(transform) # Check driver/mode blacklist. if driver and is_blacklisted(driver, mode): raise RasterioIOError("Blacklisted: file cannot be opened by " "driver '{0}' in '{1}' mode".format( driver, mode)) # Special case for file object argument. if mode == 'r' and hasattr(fp, 'read'): @contextmanager def fp_reader(fp): memfile = MemoryFile(fp.read()) dataset = memfile.open(driver=driver, sharing=sharing) try: yield dataset finally: dataset.close() memfile.close() return fp_reader(fp) elif mode in ('w', 'w+') and hasattr(fp, 'write'): @contextmanager def fp_writer(fp): memfile = MemoryFile() dataset = memfile.open(driver=driver, width=width, height=height, count=count, crs=crs, transform=transform, dtype=dtype, nodata=nodata, sharing=sharing, **kwargs) try: yield dataset finally: dataset.close() memfile.seek(0) fp.write(memfile.read()) memfile.close() return fp_writer(fp) else: # If a pathlib.Path instance is given, convert it to a string path. if isinstance(fp, Path): fp = str(fp) # The 'normal' filename or URL path. path = parse_path(fp) # Create dataset instances and pass the given env, which will # be taken over by the dataset's context manager if it is not # None. if mode == 'r': s = DatasetReader(path, driver=driver, sharing=sharing, **kwargs) elif mode == "r+": s = get_writer_for_path(path, driver=driver)(path, mode, driver=driver, sharing=sharing, **kwargs) elif mode.startswith("w"): if not driver: driver = driver_from_extension(path) writer = get_writer_for_driver(driver) if writer is not None: s = writer(path, mode, driver=driver, width=width, height=height, count=count, crs=crs, transform=transform, dtype=dtype, nodata=nodata, sharing=sharing, **kwargs) else: raise DriverCapabilityError( "Writer does not exist for driver: %s" % str(driver)) else: raise DriverCapabilityError( "mode must be one of 'r', 'r+', or 'w', not %s" % mode) return s
def proc(url, userdata): with rasterio.DatasetReader(parse_path(url), sharing=False) as f: on_file_cbk(f, userdata)
def test_vsi_path_zip_plus_https(): """A zip+https:// URLs vsi path is correct (see #1151)""" url = 'zip+https://example.com/foo.zip!bar.tif' assert vsi_path(parse_path( url)) == '/vsizip/vsicurl/https://example.com/foo.zip/bar.tif'
def test_vsi_path_zip_plus_https(): """A zip+https:// URLs vsi path is correct (see #1151)""" from rasterio.path import parse_path, vsi_path url = 'zip+https://example.com/foo.zip!bar.tif' assert vsi_path(parse_path(url)) == '/vsizip/vsicurl/https://example.com/foo.zip/bar.tif'
def _boundless_vrt_doc( src_dataset, nodata=None, background=None, hidenodata=False, width=None, height=None, transform=None): """Make a VRT XML document. Parameters ---------- src_dataset : Dataset The dataset to wrap. background : Dataset, optional A dataset that provides the optional VRT background. NB: this dataset must have the same number of bands as the src_dataset. Returns ------- bytes An ascii-encoded string (an ElementTree detail) """ nodata = nodata or src_dataset.nodata width = width or src_dataset.width height = height or src_dataset.height transform = transform or src_dataset.transform vrtdataset = ET.Element('VRTDataset') vrtdataset.attrib['rasterYSize'] = str(height) vrtdataset.attrib['rasterXSize'] = str(width) srs = ET.SubElement(vrtdataset, 'SRS') srs.text = src_dataset.crs.wkt if src_dataset.crs else "" geotransform = ET.SubElement(vrtdataset, 'GeoTransform') geotransform.text = ','.join([str(v) for v in transform.to_gdal()]) for bidx, ci, block_shape, dtype in zip(src_dataset.indexes, src_dataset.colorinterp, src_dataset.block_shapes, src_dataset.dtypes): vrtrasterband = ET.SubElement(vrtdataset, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = _gdal_typename(dtype) vrtrasterband.attrib['band'] = str(bidx) if nodata is not None: nodatavalue = ET.SubElement(vrtrasterband, 'NoDataValue') nodatavalue.text = str(nodata) if hidenodata: hidenodatavalue = ET.SubElement(vrtrasterband, 'HideNoDataValue') hidenodatavalue.text = "1" colorinterp = ET.SubElement(vrtrasterband, 'ColorInterp') colorinterp.text = ci.name.capitalize() if background is not None: simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(background.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(background.width) srcrect.attrib['ySize'] = str(background.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = '0' dstrect.attrib['yOff'] = '0' dstrect.attrib['xSize'] = str(width) dstrect.attrib['ySize'] = str(height) simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width * src_dataset.transform.a / transform.a) dstrect.attrib['ySize'] = str(src_dataset.height * src_dataset.transform.e / transform.e) if src_dataset.nodata is not None: nodata_elem = ET.SubElement(simplesource, 'NODATA') nodata_elem.text = str(src_dataset.nodata) if all(MaskFlags.per_dataset in flags for flags in src_dataset.mask_flag_enums): maskband = ET.SubElement(vrtdataset, 'MaskBand') vrtrasterband = ET.SubElement(maskband, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = 'Byte' simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = 'mask,1' sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = 'Byte' sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) return ET.tostring(vrtdataset)
def test_path_error(path): with pytest.raises(PathError): parse_path(path)
def test_vsi_path_zip(): """A zip:// URLs vsi path is correct (see #1377)""" url = 'zip:///path/to/zip/some.zip!path/to/file.txt' assert vsi_path( parse_path(url)) == '/vsizip//path/to/zip/some.zip/path/to/file.txt'
def test_parse_path(): pathlib = pytest.importorskip("pathlib") assert isinstance(parse_path(pathlib.Path("/foo/bar.tif")), ParsedPath)
def test_driver_prefixed_path(path): parsed = parse_path(path) assert isinstance(parsed, UnparsedPath)
def test_parse_path_win(): pathlib = pytest.importorskip("pathlib") assert isinstance(parse_path(pathlib.PureWindowsPath(r"C:\foo\bar.tif")), ParsedPath)
def _boundless_vrt_doc( src_dataset, nodata=None, background=None, hidenodata=False, width=None, height=None, transform=None, masked=False): """Make a VRT XML document. Parameters ---------- src_dataset : Dataset The dataset to wrap. background : int or float, optional The background fill value for the boundless VRT. masked : book If True, the src_dataset is replaced by its valid data mask. Returns ------- str An XML text string. """ nodata = nodata or src_dataset.nodata width = width or src_dataset.width height = height or src_dataset.height transform = transform or src_dataset.transform vrtdataset = ET.Element('VRTDataset') vrtdataset.attrib['rasterYSize'] = str(height) vrtdataset.attrib['rasterXSize'] = str(width) srs = ET.SubElement(vrtdataset, 'SRS') srs.text = src_dataset.crs.wkt if src_dataset.crs else "" geotransform = ET.SubElement(vrtdataset, 'GeoTransform') geotransform.text = ','.join([str(v) for v in transform.to_gdal()]) for bidx, ci, block_shape, dtype in zip(src_dataset.indexes, src_dataset.colorinterp, src_dataset.block_shapes, src_dataset.dtypes): vrtrasterband = ET.SubElement(vrtdataset, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = _gdal_typename(dtype) vrtrasterband.attrib['band'] = str(bidx) if nodata is not None: nodatavalue = ET.SubElement(vrtrasterband, 'NoDataValue') nodatavalue.text = str(nodata) if hidenodata: hidenodatavalue = ET.SubElement(vrtrasterband, 'HideNoDataValue') hidenodatavalue.text = "1" colorinterp = ET.SubElement(vrtrasterband, 'ColorInterp') colorinterp.text = ci.name.capitalize() if background is not None: complexsource = ET.SubElement(vrtrasterband, 'ComplexSource') sourcefilename = ET.SubElement(complexsource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = '1' sourcefilename.text = 'dummy.tif' # vsi_path(parse_path(background.name)) sourceband = ET.SubElement(complexsource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(complexsource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(complexsource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = '1' # str(background.width) srcrect.attrib['ySize'] = '1' # str(background.height) dstrect = ET.SubElement(complexsource, 'DstRect') dstrect.attrib['xOff'] = '0' dstrect.attrib['yOff'] = '0' dstrect.attrib['xSize'] = '1' # str(width) dstrect.attrib['ySize'] = '1' # str(height) scaleratio = ET.SubElement(complexsource, 'ScaleRatio') scaleratio.text = '0' scaleoffset = ET.SubElement(complexsource, 'ScaleOffset') scaleoffset.text = str(background) complexsource = ET.SubElement(vrtrasterband, 'ComplexSource') sourcefilename = ET.SubElement(complexsource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(complexsource, 'SourceBand') sourceband.text = str(bidx) sourceproperties = ET.SubElement(complexsource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = _gdal_typename(dtype) sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(complexsource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(complexsource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width * src_dataset.transform.a / transform.a) dstrect.attrib['ySize'] = str(src_dataset.height * src_dataset.transform.e / transform.e) if src_dataset.nodata is not None: nodata_elem = ET.SubElement(complexsource, 'NODATA') nodata_elem.text = str(src_dataset.nodata) # Effectively replaces all values of the source dataset with # 255. Due to GDAL optimizations, the source dataset will not # be read, so we get a performance improvement. if masked: scaleratio = ET.SubElement(complexsource, 'ScaleRatio') scaleratio.text = '0' scaleoffset = ET.SubElement(complexsource, 'ScaleOffset') scaleoffset.text = '255' if all(MaskFlags.per_dataset in flags for flags in src_dataset.mask_flag_enums): maskband = ET.SubElement(vrtdataset, 'MaskBand') vrtrasterband = ET.SubElement(maskband, 'VRTRasterBand') vrtrasterband.attrib['dataType'] = 'Byte' simplesource = ET.SubElement(vrtrasterband, 'SimpleSource') sourcefilename = ET.SubElement(simplesource, 'SourceFilename') sourcefilename.attrib['relativeToVRT'] = "0" sourcefilename.text = vsi_path(parse_path(src_dataset.name)) sourceband = ET.SubElement(simplesource, 'SourceBand') sourceband.text = 'mask,1' sourceproperties = ET.SubElement(simplesource, 'SourceProperties') sourceproperties.attrib['RasterXSize'] = str(width) sourceproperties.attrib['RasterYSize'] = str(height) sourceproperties.attrib['dataType'] = 'Byte' sourceproperties.attrib['BlockYSize'] = str(block_shape[0]) sourceproperties.attrib['BlockXSize'] = str(block_shape[1]) srcrect = ET.SubElement(simplesource, 'SrcRect') srcrect.attrib['xOff'] = '0' srcrect.attrib['yOff'] = '0' srcrect.attrib['xSize'] = str(src_dataset.width) srcrect.attrib['ySize'] = str(src_dataset.height) dstrect = ET.SubElement(simplesource, 'DstRect') dstrect.attrib['xOff'] = str((src_dataset.transform.xoff - transform.xoff) / transform.a) dstrect.attrib['yOff'] = str((src_dataset.transform.yoff - transform.yoff) / transform.e) dstrect.attrib['xSize'] = str(src_dataset.width) dstrect.attrib['ySize'] = str(src_dataset.height) return ET.tostring(vrtdataset).decode('ascii')
def test_parse_path_win_no_pathlib(monkeypatch): monkeypatch.setattr(rasterio.path.sys, "platform", "win32") monkeypatch.setattr(rasterio.path, "pathlib", None) assert isinstance(parse_path(r"C:\foo\bar.tif"), UnparsedPath)
def open(fp, mode='r', driver=None, width=None, height=None, count=None, crs=None, transform=None, dtype=None, nodata=None, sharing=True, **kwargs): """Open a dataset for reading or writing. The dataset may be located in a local file, in a resource located by a URL, or contained within a stream of bytes. In read ('r') or read/write ('r+') mode, no keyword arguments are required: these attributes are supplied by the opened dataset. In write ('w' or 'w+') mode, the driver, width, height, count, and dtype keywords are strictly required. Parameters ---------- fp : str, file object or pathlib.Path object A filename or URL, a file object opened in binary ('rb') mode, or a Path object. mode : str, optional 'r' (read, the default), 'r+' (read/write), 'w' (write), or 'w+' (write/read). driver : str, optional A short format driver name (e.g. "GTiff" or "JPEG") or a list of such names (see GDAL docs at http://www.gdal.org/formats_list.html). In 'w' or 'w+' modes a single name is required. In 'r' or 'r+' modes the driver can usually be omitted. Registered drivers will be tried sequentially until a match is found. When multiple drivers are available for a format such as JPEG2000, one of them can be selected by using this keyword argument. width, height : int, optional The numbers of rows and columns of the raster dataset. Required in 'w' or 'w+' modes, they are ignored in 'r' or 'r+' modes. count : int, optional The count of dataset bands. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. dtype : str or numpy dtype The data type for bands. For example: 'uint8' or ``rasterio.uint16``. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. crs : str, dict, or CRS; optional The coordinate reference system. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. transform : Affine instance, optional Affine transformation mapping the pixel space to geographic space. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. nodata : int, float, or nan; optional Defines the pixel value to be interpreted as not valid data. Required in 'w' or 'w+' modes, it is ignored in 'r' or 'r+' modes. sharing : bool A flag that allows sharing of dataset handles. Default is `True`. Should be set to `False` in a multithreaded:w program. kwargs : optional These are passed to format drivers as directives for creating or interpreting datasets. For example: in 'w' or 'w+' modes a `tiled=True` keyword argument will direct the GeoTIFF format driver to create a tiled, rather than striped, TIFF. Returns ------- A ``DatasetReader`` or ``DatasetUpdater`` object. Examples -------- To open a GeoTIFF for reading using standard driver discovery and no directives: >>> import rasterio >>> with rasterio.open('example.tif') as dataset: ... print(dataset.profile) To open a JPEG2000 using only the JP2OpenJPEG driver: >>> with rasterio.open( ... 'example.jp2', driver='JP2OpenJPEG') as dataset: ... print(dataset.profile) To create a new 8-band, 16-bit unsigned, tiled, and LZW-compressed GeoTIFF with a global extent and 0.5 degree resolution: >>> from rasterio.transform import from_origin >>> with rasterio.open( ... 'example.tif', 'w', driver='GTiff', dtype='uint16', ... width=720, height=360, count=8, crs='EPSG:4326', ... transform=from_origin(-180.0, 90.0, 0.5, 0.5), ... nodata=0, tiled=True, compress='lzw') as dataset: ... dataset.write(...) """ if not isinstance(fp, string_types): if not (hasattr(fp, 'read') or hasattr(fp, 'write') or isinstance(fp, Path)): raise TypeError("invalid path or file: {0!r}".format(fp)) if mode and not isinstance(mode, string_types): raise TypeError("invalid mode: {0!r}".format(mode)) if driver and not isinstance(driver, string_types): raise TypeError("invalid driver: {0!r}".format(driver)) if dtype and not check_dtype(dtype): raise TypeError("invalid dtype: {0!r}".format(dtype)) if nodata is not None: nodata = float(nodata) if transform: transform = guard_transform(transform) # Check driver/mode blacklist. if driver and is_blacklisted(driver, mode): raise RasterioIOError( "Blacklisted: file cannot be opened by " "driver '{0}' in '{1}' mode".format(driver, mode)) # Special case for file object argument. if mode == 'r' and hasattr(fp, 'read'): @contextmanager def fp_reader(fp): memfile = MemoryFile(fp.read()) dataset = memfile.open() try: yield dataset finally: dataset.close() memfile.close() return fp_reader(fp) elif mode in ('w', 'w+') and hasattr(fp, 'write'): @contextmanager def fp_writer(fp): memfile = MemoryFile() dataset = memfile.open(driver=driver, width=width, height=height, count=count, crs=crs, transform=transform, dtype=dtype, nodata=nodata, **kwargs) try: yield dataset finally: dataset.close() memfile.seek(0) fp.write(memfile.read()) memfile.close() return fp_writer(fp) else: # If a pathlib.Path instance is given, convert it to a string path. if isinstance(fp, Path): fp = str(fp) # The 'normal' filename or URL path. path = parse_path(fp) # Create dataset instances and pass the given env, which will # be taken over by the dataset's context manager if it is not # None. if mode == 'r': s = DatasetReader(path, driver=driver, **kwargs) elif mode == 'r+': s = get_writer_for_path(path)(path, mode, driver=driver, **kwargs) elif mode.startswith("w"): s = get_writer_for_driver(driver)(path, mode, driver=driver, width=width, height=height, count=count, crs=crs, transform=transform, dtype=dtype, nodata=nodata, **kwargs) else: raise ValueError( "mode must be one of 'r', 'r+', or 'w', not %s" % mode) return s