Note
Go to the end to download the full example code.
Open/save a raster#
This example demonstrates opening a raster with open_raster() and saving it with the rst accessor.
We open an example raster. The data is, by default, unloaded.
import geoutils as gu
filename_rast = gu.examples.get_path("everest_landsat_b4")
rast = gu.open_raster(filename_rast)
rast
<xarray.DataArray (y: 655, x: 800)> Size: 2MB
[524000 values with dtype=float32]
Coordinates:
* y (y) float64 5kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
* x (x) float64 6kB 4.78e+05 4.78e+05 ... 5.02e+05 5.02e+05
band int64 8B 1
spatial_ref int64 8B 0
Attributes:
AREA_OR_POINT: Point
scale_factor: 1.0
add_offset: 0.0
overview_factors: []
overview_selected_level: -1
overview_selected_factor: 1
block_shape: (10, 800)- y: 655
- x: 800
- ...
[524000 values with dtype=float32]
- y(y)float643.108e+06 3.108e+06 ... 3.089e+06
array([3108125., 3108095., 3108065., ..., 3088565., 3088535., 3088505.], shape=(655,)) - x(x)float644.78e+05 4.78e+05 ... 5.02e+05
array([478015., 478045., 478075., ..., 501925., 501955., 501985.], shape=(800,))
- band()int641
array(1)
- spatial_ref()int640
- crs_wkt :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- WGS 84
- horizontal_datum_name :
- World Geodetic System 1984
- projected_crs_name :
- WGS 84 / UTM zone 45N
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- 87.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- GeoTransform :
- 478000.0 30.0 0.0 3108140.0 0.0 -30.0
array(0)
- AREA_OR_POINT :
- Point
- scale_factor :
- 1.0
- add_offset :
- 0.0
- overview_factors :
- []
- overview_selected_level :
- -1
- overview_selected_factor :
- 1
- block_shape :
- (10, 800)
A raster is composed of four main attributes: a data array, an affine transform, a coordinate reference system and a nodata value. All other attributes are derivatives of those or the file on disk, and can be found in the dedicated section of the API. See also The georeferenced raster.
Note
A raster can also be opened from a rasterio.io.DatasetReader or a rasterio.io.MemoryFile, see From/to Rasterio.
We can print more info on the raster.
Driver: GTiff
Filename: /home/docs/checkouts/readthedocs.org/user_builds/geoutils/checkouts/latest/geoutils/example_data/Everest_Landsat/LE71400412000304SGS00_B4.tif
Loaded? False
Grid size: 800, 655
Number of bands: 1
Data types: float32
Coordinate system: ['WGS 84 / UTM zone 45N']
Nodata value: None
Pixel interpretation: Point
Pixel size: 30.0, 30.0
Upper left corner: 478000.0, 3108140.0
Lower right corner: 502000.0, 3088490.0
The data will be loaded explicitly by any function requiring its values, such as plotting.
rast.rst.plot(cmap="Greys_r")

Opening can be performed with several parameters, for instance choosing a single band with index and re-sampling with downsample, to subset a 3-band
raster to its second band, and using 1 pixel out of 4.
rast = gu.open_raster(gu.examples.get_path("everest_landsat_rgb"), downsample=4).sel(band=2)
rast
<xarray.DataArray (y: 163, x: 200)> Size: 130kB
[32600 values with dtype=float32]
Coordinates:
* y (y) float64 1kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
* x (x) float64 2kB 4.781e+05 4.782e+05 ... 5.018e+05 5.019e+05
band int64 8B 2
spatial_ref int64 8B 0
Attributes:
scale_factor: 1.0
add_offset: 0.0
overview_factors: []
overview_selected_level: -1
overview_selected_factor: 1
block_shape: (3, 800)- y: 163
- x: 200
- ...
[32600 values with dtype=float32]
- y(y)float643.108e+06 3.108e+06 ... 3.089e+06
array([3108080., 3107960., 3107840., 3107720., 3107600., 3107480., 3107360., 3107240., 3107120., 3107000., 3106880., 3106760., 3106640., 3106520., 3106400., 3106280., 3106160., 3106040., 3105920., 3105800., 3105680., 3105560., 3105440., 3105320., 3105200., 3105080., 3104960., 3104840., 3104720., 3104600., 3104480., 3104360., 3104240., 3104120., 3104000., 3103880., 3103760., 3103640., 3103520., 3103400., 3103280., 3103160., 3103040., 3102920., 3102800., 3102680., 3102560., 3102440., 3102320., 3102200., 3102080., 3101960., 3101840., 3101720., 3101600., 3101480., 3101360., 3101240., 3101120., 3101000., 3100880., 3100760., 3100640., 3100520., 3100400., 3100280., 3100160., 3100040., 3099920., 3099800., 3099680., 3099560., 3099440., 3099320., 3099200., 3099080., 3098960., 3098840., 3098720., 3098600., 3098480., 3098360., 3098240., 3098120., 3098000., 3097880., 3097760., 3097640., 3097520., 3097400., 3097280., 3097160., 3097040., 3096920., 3096800., 3096680., 3096560., 3096440., 3096320., 3096200., 3096080., 3095960., 3095840., 3095720., 3095600., 3095480., 3095360., 3095240., 3095120., 3095000., 3094880., 3094760., 3094640., 3094520., 3094400., 3094280., 3094160., 3094040., 3093920., 3093800., 3093680., 3093560., 3093440., 3093320., 3093200., 3093080., 3092960., 3092840., 3092720., 3092600., 3092480., 3092360., 3092240., 3092120., 3092000., 3091880., 3091760., 3091640., 3091520., 3091400., 3091280., 3091160., 3091040., 3090920., 3090800., 3090680., 3090560., 3090440., 3090320., 3090200., 3090080., 3089960., 3089840., 3089720., 3089600., 3089480., 3089360., 3089240., 3089120., 3089000., 3088880., 3088760., 3088640.]) - x(x)float644.781e+05 4.782e+05 ... 5.019e+05
array([478060., 478180., 478300., 478420., 478540., 478660., 478780., 478900., 479020., 479140., 479260., 479380., 479500., 479620., 479740., 479860., 479980., 480100., 480220., 480340., 480460., 480580., 480700., 480820., 480940., 481060., 481180., 481300., 481420., 481540., 481660., 481780., 481900., 482020., 482140., 482260., 482380., 482500., 482620., 482740., 482860., 482980., 483100., 483220., 483340., 483460., 483580., 483700., 483820., 483940., 484060., 484180., 484300., 484420., 484540., 484660., 484780., 484900., 485020., 485140., 485260., 485380., 485500., 485620., 485740., 485860., 485980., 486100., 486220., 486340., 486460., 486580., 486700., 486820., 486940., 487060., 487180., 487300., 487420., 487540., 487660., 487780., 487900., 488020., 488140., 488260., 488380., 488500., 488620., 488740., 488860., 488980., 489100., 489220., 489340., 489460., 489580., 489700., 489820., 489940., 490060., 490180., 490300., 490420., 490540., 490660., 490780., 490900., 491020., 491140., 491260., 491380., 491500., 491620., 491740., 491860., 491980., 492100., 492220., 492340., 492460., 492580., 492700., 492820., 492940., 493060., 493180., 493300., 493420., 493540., 493660., 493780., 493900., 494020., 494140., 494260., 494380., 494500., 494620., 494740., 494860., 494980., 495100., 495220., 495340., 495460., 495580., 495700., 495820., 495940., 496060., 496180., 496300., 496420., 496540., 496660., 496780., 496900., 497020., 497140., 497260., 497380., 497500., 497620., 497740., 497860., 497980., 498100., 498220., 498340., 498460., 498580., 498700., 498820., 498940., 499060., 499180., 499300., 499420., 499540., 499660., 499780., 499900., 500020., 500140., 500260., 500380., 500500., 500620., 500740., 500860., 500980., 501100., 501220., 501340., 501460., 501580., 501700., 501820., 501940.]) - band()int642
array(2)
- spatial_ref()int640
- crs_wkt :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- WGS 84
- horizontal_datum_name :
- World Geodetic System 1984
- projected_crs_name :
- WGS 84 / UTM zone 45N
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- 87.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- GeoTransform :
- 478000.0 120.0 0.0 3108140.0 0.0 -120.0
array(0)
- scale_factor :
- 1.0
- add_offset :
- 0.0
- overview_factors :
- []
- overview_selected_level :
- -1
- overview_selected_factor :
- 1
- block_shape :
- (3, 800)
The data is not loaded by default, even if when specifying a band or re-sampling.
We can load it explicitly by calling load().
<xarray.DataArray (y: 163, x: 200)> Size: 130kB
array([[255., 255., 255., ..., 255., 255., 255.],
[255., 255., 255., ..., 255., 255., 255.],
[255., 255., 255., ..., 255., 255., 255.],
...,
[ 45., 67., 52., ..., 121., 150., 164.],
[ 54., 67., 93., ..., 120., 145., 161.],
[ 86., 37., 32., ..., 121., 142., 167.]],
shape=(163, 200), dtype=float32)
Coordinates:
* y (y) float64 1kB 3.108e+06 3.108e+06 ... 3.089e+06 3.089e+06
* x (x) float64 2kB 4.781e+05 4.782e+05 ... 5.018e+05 5.019e+05
band int64 8B 2
spatial_ref int64 8B 0
Attributes:
scale_factor: 1.0
add_offset: 0.0
overview_factors: []
overview_selected_level: -1
overview_selected_factor: 1
block_shape: (3, 800)- y: 163
- x: 200
- 255.0 255.0 255.0 255.0 255.0 255.0 ... 106.0 115.0 121.0 142.0 167.0
array([[255., 255., 255., ..., 255., 255., 255.], [255., 255., 255., ..., 255., 255., 255.], [255., 255., 255., ..., 255., 255., 255.], ..., [ 45., 67., 52., ..., 121., 150., 164.], [ 54., 67., 93., ..., 120., 145., 161.], [ 86., 37., 32., ..., 121., 142., 167.]], shape=(163, 200), dtype=float32) - y(y)float643.108e+06 3.108e+06 ... 3.089e+06
array([3108080., 3107960., 3107840., 3107720., 3107600., 3107480., 3107360., 3107240., 3107120., 3107000., 3106880., 3106760., 3106640., 3106520., 3106400., 3106280., 3106160., 3106040., 3105920., 3105800., 3105680., 3105560., 3105440., 3105320., 3105200., 3105080., 3104960., 3104840., 3104720., 3104600., 3104480., 3104360., 3104240., 3104120., 3104000., 3103880., 3103760., 3103640., 3103520., 3103400., 3103280., 3103160., 3103040., 3102920., 3102800., 3102680., 3102560., 3102440., 3102320., 3102200., 3102080., 3101960., 3101840., 3101720., 3101600., 3101480., 3101360., 3101240., 3101120., 3101000., 3100880., 3100760., 3100640., 3100520., 3100400., 3100280., 3100160., 3100040., 3099920., 3099800., 3099680., 3099560., 3099440., 3099320., 3099200., 3099080., 3098960., 3098840., 3098720., 3098600., 3098480., 3098360., 3098240., 3098120., 3098000., 3097880., 3097760., 3097640., 3097520., 3097400., 3097280., 3097160., 3097040., 3096920., 3096800., 3096680., 3096560., 3096440., 3096320., 3096200., 3096080., 3095960., 3095840., 3095720., 3095600., 3095480., 3095360., 3095240., 3095120., 3095000., 3094880., 3094760., 3094640., 3094520., 3094400., 3094280., 3094160., 3094040., 3093920., 3093800., 3093680., 3093560., 3093440., 3093320., 3093200., 3093080., 3092960., 3092840., 3092720., 3092600., 3092480., 3092360., 3092240., 3092120., 3092000., 3091880., 3091760., 3091640., 3091520., 3091400., 3091280., 3091160., 3091040., 3090920., 3090800., 3090680., 3090560., 3090440., 3090320., 3090200., 3090080., 3089960., 3089840., 3089720., 3089600., 3089480., 3089360., 3089240., 3089120., 3089000., 3088880., 3088760., 3088640.]) - x(x)float644.781e+05 4.782e+05 ... 5.019e+05
array([478060., 478180., 478300., 478420., 478540., 478660., 478780., 478900., 479020., 479140., 479260., 479380., 479500., 479620., 479740., 479860., 479980., 480100., 480220., 480340., 480460., 480580., 480700., 480820., 480940., 481060., 481180., 481300., 481420., 481540., 481660., 481780., 481900., 482020., 482140., 482260., 482380., 482500., 482620., 482740., 482860., 482980., 483100., 483220., 483340., 483460., 483580., 483700., 483820., 483940., 484060., 484180., 484300., 484420., 484540., 484660., 484780., 484900., 485020., 485140., 485260., 485380., 485500., 485620., 485740., 485860., 485980., 486100., 486220., 486340., 486460., 486580., 486700., 486820., 486940., 487060., 487180., 487300., 487420., 487540., 487660., 487780., 487900., 488020., 488140., 488260., 488380., 488500., 488620., 488740., 488860., 488980., 489100., 489220., 489340., 489460., 489580., 489700., 489820., 489940., 490060., 490180., 490300., 490420., 490540., 490660., 490780., 490900., 491020., 491140., 491260., 491380., 491500., 491620., 491740., 491860., 491980., 492100., 492220., 492340., 492460., 492580., 492700., 492820., 492940., 493060., 493180., 493300., 493420., 493540., 493660., 493780., 493900., 494020., 494140., 494260., 494380., 494500., 494620., 494740., 494860., 494980., 495100., 495220., 495340., 495460., 495580., 495700., 495820., 495940., 496060., 496180., 496300., 496420., 496540., 496660., 496780., 496900., 497020., 497140., 497260., 497380., 497500., 497620., 497740., 497860., 497980., 498100., 498220., 498340., 498460., 498580., 498700., 498820., 498940., 499060., 499180., 499300., 499420., 499540., 499660., 499780., 499900., 500020., 500140., 500260., 500380., 500500., 500620., 500740., 500860., 500980., 501100., 501220., 501340., 501460., 501580., 501700., 501820., 501940.]) - band()int642
array(2)
- spatial_ref()int640
- crs_wkt :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- semi_major_axis :
- 6378137.0
- semi_minor_axis :
- 6356752.314245179
- inverse_flattening :
- 298.257223563
- reference_ellipsoid_name :
- WGS 84
- longitude_of_prime_meridian :
- 0.0
- prime_meridian_name :
- Greenwich
- geographic_crs_name :
- WGS 84
- horizontal_datum_name :
- World Geodetic System 1984
- projected_crs_name :
- WGS 84 / UTM zone 45N
- grid_mapping_name :
- transverse_mercator
- latitude_of_projection_origin :
- 0.0
- longitude_of_central_meridian :
- 87.0
- false_easting :
- 500000.0
- false_northing :
- 0.0
- scale_factor_at_central_meridian :
- 0.9996
- spatial_ref :
- PROJCS["WGS 84 / UTM zone 45N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",87],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32645"]]
- GeoTransform :
- 478000.0 120.0 0.0 3108140.0 0.0 -120.0
array(0)
- scale_factor :
- 1.0
- add_offset :
- 0.0
- overview_factors :
- []
- overview_selected_level :
- -1
- overview_selected_factor :
- 1
- block_shape :
- (3, 800)
Finally, a raster is saved using to_file():
rast.rst.to_file("myraster.tif")
/home/docs/checkouts/readthedocs.org/user_builds/geoutils/conda/latest/lib/python3.14/site-packages/sphinx_gallery/gen_rst.py:1053: SerializationWarning: saving variable None with floating point data as an integer dtype without any _FillValue to use for NaNs
mem_last, _ = call_memory(
Total running time of the script: (0 minutes 0.690 seconds)