Skip to main content
20 votes
Accepted

Converting NetCDF dataset array to GeoTiff using rasterio Python

I would recommend looking into rioxarray for your dataset. You can open your dataset like so: import rioxarray import xarray xds = xarray.open_dataset('D:\Weather data\et_01012018.nc') If your CRS ...
snowman2's user avatar
  • 7,419
16 votes
Accepted

Masking NetCDF time series data from shapefile using Python

Here is an example of using rioxarray to mask out data with a shapefile: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import geopandas import rioxarray import xarray from ...
snowman2's user avatar
  • 7,419
15 votes
Accepted

Extracting data from a raster

You can use rioxarray for this: import rioxarray from pyproj import Transformer # convert coordinate to raster projection lon = 15.174866 lat = 43.169129 rds = rioxarray.open_rasterio("C:/path-to-...
snowman2's user avatar
  • 7,419
12 votes
Accepted

Adding projection to Satellite NetCDF file

It appears that the projection information is stored using CF conventions. import rioxarray # for 'rio' accessor import xarray xds = xarray.open_dataset("glm.nc") Here is what is in xds: &...
snowman2's user avatar
  • 7,419
10 votes
Accepted

Convert raster to CSV with lat, lon, and value columns

You can do this with rioxarray: Note: I used data for the column name as values is a pandas specific attribute. import rioxarray import pandas rds = rioxarray.open_rasterio( "...
snowman2's user avatar
  • 7,419
10 votes
Accepted

Vectorizing a raster containing holes using rasterio

You just need to use shapely.geometry.shape() instead of trying to parse the coordinates yourself. E.g: import rasterio from rasterio.features import shapes from shapely.geometry import shape from ...
user2856's user avatar
  • 66.5k
9 votes
Accepted

Using Rioxarray / QGIS Projection

Upon inspecting the dataset, I realized that the units of the data are in radians. import xarray import rioxarray from pyproj import CRS xds = xarray.open_dataset("OR_ABI-L2-LSTF-...
snowman2's user avatar
  • 7,419
9 votes
Accepted

How to save an xarray.DataArray directly as a Cloud Optimized GeoTIFF?

rds.rio.to_raster(raster_path="cogeo.tif", driver="COG") https://gdal.org/drivers/raster/cog.html
snowman2's user avatar
  • 7,419
8 votes
Accepted

Converting a NetCDF from 0 to 360 to -180 to 180 via xarray

This method worked: with xr.open_dataset(nc_fp, mask_and_scale=True, decode_times=True) as src: ds = src.sel(time="2020-01-01").max('time') ds.rio.write_crs("epsg:4326", ...
JWB's user avatar
  • 604
6 votes

Can I open remote datasets with Rasterio?

You can open remote dataset with Rasterio. It's really similar to GDAL like illustrated below import rasterio mmap_name = '/vsicurl/https://esgf.nccs.nasa.gov/thredds/fileServer/BioClim/WRE/...
ThomasG77's user avatar
  • 30.9k
6 votes
Accepted

Issue in calculating NDVI using Rasterio Python

In order to solve your problem, you need to ensure the grids cover the same area and have the same dimensions. One method to achieve this is with the reproject_match method in rioxarray (geospatial ...
snowman2's user avatar
  • 7,419
6 votes
Accepted

How to find coordinates of pixels of a GeoTIFF image with Python

Your raster (2015LSTDay.tif, band 5) You can use rasterio with pandas: import rasterio as rio import pandas as pd with rio.open('2015LSTDay.tif') as dataset: val = dataset.read(5) # band 5 ...
gene's user avatar
  • 55k
5 votes
Accepted

Extracting data within geometry (shape)

I believe what you are looking for is rioxarray. An example of what you want to do is at: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import rioxarray geometries = [ { ...
snowman2's user avatar
  • 7,419
5 votes
Accepted

No data found in bounds using rioxarray to clip GeoTiff to shapefile

Your polygon is very small compared to the pixel size and it doesn't contain the centre of any pixels. Just to demonstrate, here's an image showing the outlines of the pixels of your raster with their ...
user2856's user avatar
  • 66.5k
5 votes
Accepted

Raster conversion from NetCDF to GeoTIFF changes data values

As @user30184 pointed out, there is a scale factor that is applied to the NetCDF when it is read with xarray. When opening the .tiff with rasterio, this scaling factor must be multiplied with the ...
ColinB's user avatar
  • 233
5 votes
Accepted

Why can't xarray find this variable?

gdalinfo shows just a single band raster: Driver: GRIB/GRIdded Binary (.grb, .grb2) Files: MRMS_ReflectivityAtLowestAltitude_00.50_20211212-172838.grib2 Size is 7000, 3500 Coordinate System is: ...
user2856's user avatar
  • 66.5k
4 votes

Masking NetCDF data using shapefile (xarray & geopandas)

You can use rioxarray. Here is an example: https://corteva.github.io/rioxarray/stable/examples/clip_geom.html import rioxarray import geopandas geodf = geopandas.read_file(...) xds = rioxarray....
snowman2's user avatar
  • 7,419
4 votes

Updating a subset of raster values using open source Python?

I would recommend using rioxarray. import numpy import rioxarray from shapely.geometry import mapping xds = rioxarray.open_rasterio("NI_RIVER_ALR_1000.tif").sel(band=1).squeeze() gdb_vml_polygs = '...
snowman2's user avatar
  • 7,419
4 votes
Accepted

Can I open remote datasets with Rasterio?

Yes, you can. Rasterio is a Python wrapper for GDAL, so it should be able to read in anything that the GDAL can. Here is an example of opening a remote file with rioxarray: https://corteva.github.io/...
snowman2's user avatar
  • 7,419
4 votes
Accepted

How to save a clipped NetCDF data as a new NetCDF file in Python?

xarray.Dataset has a to_netcdf method...so it should be clipped.to_netcdf(path_on_disk) http://xarray.pydata.org/en/stable/generated/xarray.Dataset.to_netcdf.html
bcollins's user avatar
  • 3,563
4 votes
Accepted

Resample (spatially-average) DataArray / raster to match the grid of another DataArray in Python

Solved! rioxarray has the method 'reproject_match', which does exactly what I describe above. For more info: https://corteva.github.io/rioxarray/stable/rioxarray.html?highlight=write_crs#rioxarray....
marcgm's user avatar
  • 95
4 votes

"ValueError: negative dimensions are not allowed" when using raster created from NetCDF

Actually, it's related to a positive number, the y pixel dimension should be negative (pixel coords are top->bottom, but map coords are bottom->top), but it isn't. This is why a negative ...
user2856's user avatar
  • 66.5k
4 votes

Vectorizing a raster containing holes using rasterio

With geocube 0.4+: import rioxarray from geocube.vector import vectorize data = rioxarray.open_rasterio("test.tif", mask_and_scale=True).squeeze() data.name = "class" gdf = ...
snowman2's user avatar
  • 7,419
4 votes
Accepted

How to deal with Nan value when using rioxarray rio.reproject()?

You haven't actually set a NoData value. Explicitly set nan as the NoData value. import xarray as xr import rioxarray as rxr import numpy as np from rasterio.enums import Resampling data = [[1, 2, 3,...
user2856's user avatar
  • 66.5k
4 votes

Is it possible to change a coordinate in an netCDF loaded with xarray?

You created a new coordinate but it also created a new dimension instead of using the "band" dimension. @Bert Coever's suggestion should work. Also note that every operation creates a new ...
ZZZ's user avatar
  • 530
3 votes

Convert raster to CSV with lat, lon, and value columns

Even easier using GDAL from the command line, nothing to install! Create a text file with your longitudes and latitudes, call it xy.txt: -7, 41 -0.5, 51.4 Create the header of your shiny CSV file ...
Jose's user avatar
  • 3,342
3 votes

Updating a subset of raster values using open source Python?

With rasterio filled=False gives back masked array which can be utilised to replace with mean value. Make sure to set crop=False and set the indexes to the band you want to replace it with. import ...
Aman Bagrecha's user avatar
3 votes
Accepted

Convert LR/LOLA data to latitude and longitude

Using pyproj.CRS you can get the geodetic CRS of the projected CRS. from pyproj import CRS crs = CRS(infile.crs) print(repr(crs.geodetic_crs)) <Geographic 2D CRS: GEOGCRS["GCS_Moon",DATUM["...
snowman2's user avatar
  • 7,419
3 votes
Accepted

Extract population count from GeoTIFF based on mask layer

This is something you can do with geocube and rioxarray. Here is an example I followed to generate this: https://corteva.github.io/geocube/stable/examples/zonal_statistics.html import geopandas ...
snowman2's user avatar
  • 7,419
3 votes
Accepted

Issue while performing merge/mosaic of multiple TIFF files using Rioxarray Python

Cross posted: https://github.com/corteva/rioxarray/issues/172 I see that you have both EPSG:32643 and EPSG:32644 as the CRS of the datasets. The merge function assumes that they are all the same CRS. ...
snowman2's user avatar
  • 7,419

Only top scored, non community-wiki answers of a minimum length are eligible