I have a point shapefile with 0.01 degrees spacing. The points are confined within an irregular shapefile boundary for example Alaska state boundary. I have used the point shapefile and retrieved some data from different rasters and did some algebraic calculations. Now finally, I have a data frame with three columns i.e., latitude, longitude, and targetvar. I am looking to convert this into a GeoTIFF raster. I have tried rasterio, and GDAL, but not been able to convert it into raster.
I am sharing the code that I have tried, and the error I have received. How can I resolve this?
print(da)
da dataset characteristics:
xarray.Dataset
Dimensions:
LATITUDE: 1893LONGITUDE: 4252
Coordinates:
LATITUDE
(LATITUDE)
float64
52.26 52.27 52.28 ... 71.38 71.39
LONGITUDE
(LONGITUDE)
float64
-172.9 -172.9 ... -130.0 -130.0
Data variables:
SD
(LATITUDE, LONGITUDE)
float32
nan nan nan nan ... nan nan nan nan
Attributes: (0)
da.SD.rio.to_raster(r"D:\Srinivas\reconstructSD\outputSD.tif")
error:
MissingSpatialDimensionError Traceback (most recent call last)
~\AppData\Local\Temp\2\ipykernel_14484\2450085635.py in <module>
----> 1 da.SD.rio.to_raster(r"D:\Srinivas\reconstructSD\outputSD.tif")
~\AppData\Local\Programs\Python\Python37\lib\site-packages\rioxarray\raster_array.py in to_raster(self, raster_path, driver, dtype, tags, windowed, recalc_transform, lock, compute, **profile_kwargs)
1006 tags=tags,
1007 driver=driver,
-> 1008 height=int(self.height),
1009 width=int(self.width),
1010 count=int(self.count),
~\AppData\Local\Programs\Python\Python37\lib\site-packages\rioxarray\rioxarray.py in height(self)
819 if self._height is not None:
820 return self._height
--> 821 self._height = self._obj[self.y_dim].size
822 return self._height
823
~\AppData\Local\Programs\Python\Python37\lib\site-packages\rioxarray\rioxarray.py in y_dim(self)
801 return self._y_dim
802 raise MissingSpatialDimensionError(
--> 803 "y dimension not found. 'rio.set_spatial_dims()' or "
804 "using 'rename()' to change the dimension name to 'y' can address this."
805 f"{_get_data_var_message(self._obj)}"
MissingSpatialDimensionError: y dimension not found. 'rio.set_spatial_dims()' or using 'rename()' to change the dimension name to 'y' can address this. Data variable: SD
with gdal:
dfp.to_csv(r"D:\Srinivas\reconstructSD\outputDF.xyz",index=False,header='None',sep=' ')
sdtif = gdal.Translate(r"D:\Srinivas\reconstructSD\outputSD.tif",r"D:\Srinivas\reconstructSD\outputDF.xyz",outputSRS="EPSG:4326")
dfp is the dataframe that contains data comprising of latitude, longitude, and SD. For the gdal following error I have received:
alueError Traceback (most recent call last)
~\AppData\Local\Temp\2\ipykernel_14484\4109273232.py in <module>
----> 1 sdtif = gdal.Translate(r"D:\Srinivas\reconstructSD\outputSD.tif",r"D:\Srinivas\reconstructSD\outputDF.xyz",outputSRS="EPSG:4326")
~\AppData\Local\Programs\Python\Python37\lib\site-packages\osgeo\gdal.py in Translate(destName, srcDS, **kwargs)
455 srcDS = Open(srcDS)
456
--> 457 return TranslateInternal(destName, srcDS, opts, callback, callback_data)
458
459 def WarpOptions(options=None, format=None,
~\AppData\Local\Programs\Python\Python37\lib\site-packages\osgeo\gdal.py in TranslateInternal(*args)
4191 def TranslateInternal(*args):
4192 r"""TranslateInternal(char const * dest, Dataset dataset, GDALTranslateOptions translateOptions, GDALProgressFunc callback=0, void * callback_data=None) -> Dataset"""
-> 4193 return _gdal.TranslateInternal(*args)
4194 class GDALWarpAppOptions(object):
4195 r"""Proxy of C++ GDALWarpAppOptions class."""
ValueError: Received a NULL pointer.