Repository navigation
DOC: Add writing files section to getting started #952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Deelay1403
wants to merge
2
commits into
vylor:master
Choose a base branch
from
Deelay1403:doc/584-writing-files
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| .. _file_io: | ||
|
|
||
| File I/O | ||
| ======== | ||
|
|
||
| This page covers reading and writing raster files with rioxarray. For the basics of | ||
| opening files, see :ref:`getting_started`. | ||
|
|
||
| Reading Files | ||
| ------------- | ||
|
|
||
| Cloud storage | ||
| ~~~~~~~~~~~~~~ | ||
|
|
||
| Paths are passed through to rasterio/GDAL, so you can also read files directly | ||
| from HTTP(S) URLs and cloud storage (e.g. ``s3://``). See :ref:`gdal_env` for | ||
| configuring GDAL when working with cloud-stored data. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import xarray | ||
|
|
||
| xds = xarray.open_dataset("s3://bucket/my.tif", engine="rasterio") | ||
|
|
||
|
|
||
| Writing Files | ||
| ------------- | ||
|
|
||
| GeoTIFF | ||
| ~~~~~~~~ | ||
|
|
||
| Use ``rio.to_raster`` on an ``xarray.DataArray`` or ``xarray.Dataset`` to write a raster. | ||
| The driver is detected from the ``.tif``/``.tiff`` extension. Additional keyword arguments | ||
| are passed to rasterio as GDAL creation options. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import rioxarray | ||
|
|
||
| xds = rioxarray.open_rasterio("my.tif") | ||
| xds.rio.to_raster("out.tif") | ||
| xds.rio.to_raster("out_lzw.tif", compress="LZW") | ||
|
|
||
| Cloud Optimized GeoTIFF (COG) | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| To write a COG, use GDAL's ``COG`` driver: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| xds.rio.to_raster("out_cog.tif", driver="COG") | ||
|
|
||
| For more examples, including writing large rasters with ``windowed=True`` or with dask, | ||
| see :doc:`../examples/convert_to_raster`, :doc:`../examples/COG`, | ||
| and :doc:`../examples/dask_read_write`. | ||
|
|
||
| Cloud storage | ||
| ~~~~~~~~~~~~~~ | ||
|
|
||
| To write directly to cloud storage, use a | ||
| `GDAL virtual file system <https://gdal.org/en/stable/user/virtual_file_systems.html>`__ | ||
| path such as ``/vsis3/`` (``/vsigs/`` and ``/vsiaz/`` work similarly). | ||
| These file systems only support sequential writes, so set | ||
| ``CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE`` to have GDAL write to a local temporary file | ||
| and upload it when the file is closed: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import rasterio | ||
|
|
||
| with rasterio.Env(CPL_VSIL_USE_TEMP_FILE_FOR_RANDOM_WRITE="YES"): | ||
| xds.rio.to_raster("/vsis3/bucket/out.tif", driver="COG") | ||
|
|
||
| Alternatively, ``rio.to_raster`` accepts a file-like object, such as an ``io.BytesIO`` | ||
| buffer or a file opened with `fsspec <https://filesystem-spec.readthedocs.io>`__ | ||
| (writing to ``s3://`` with fsspec requires `s3fs <https://s3fs.readthedocs.io>`__). | ||
| The ``driver`` must be set explicitly because it cannot be detected from a file extension. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import fsspec | ||
|
|
||
| with fsspec.open("s3://bucket/out.tif", "wb") as fobj: | ||
| xds.rio.to_raster(fobj, driver="COG") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.