英文:
How to solve "TypeError: invalid path or file .."?
问题
以下是代码部分的翻译:
我正在使用 rasterstats Python 模块 zonal_stats 来计算每个地块(包含在 shapefile 中)内的太阳辐照度均值(从栅格中获取)。
如果我执行以下代码:
print(irradiance.read().shape)
我会得到:(1, 2852, 2425)
然后,我执行以下操作:
with rioxarray.open_rasterio("C:/Users/Daniele/OneDrive/PhD/GIS + ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif") as src:
    stats = zonal_stats(particles, src)
然后我得到以下错误:
TypeError:这似乎是与使用 float64 或 int32 相关的错误,但我不明白如何使其正常工作。是否有方法可以解决这个问题?而且,一般来说,有没有更容易的方法来计算多边形内一些像素的平均值?(在相同的 shapefile 中,我有许多多边形,我需要对像素进行平均值计算)
请注意,我只翻译了代码部分,并没有包含问题的回答。
英文:
I am using the rasterstats python module zonal_stats to calculate the mean solar irradiance value (taken from a raster) inside each land particle (contained in a shapefile).
If I
print(irradiance.read().shape) I get : (1, 2852, 2425)
Then, I do:
with rioxarray.open_rasterio("C:/Users/Daniele/OneDrive/PhD/GIS + ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif") as src:
    stats = zonal_stats(particles,src)
And i get the following error:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1458822157982.py in <module>
      1 with rioxarray.open_rasterio("C:/Users/Daniele/OneDrive/PhD/GIS + ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif") as src:
----> 2     stats = zonal_stats(particles,src)
~\anaconda3\lib\site-packages\rasterstats\main.py in zonal_stats(*args, **kwargs)
     38     The only difference is that ``zonal_stats`` will
     39     return a list rather than a generator."""
---> 40     return list(gen_zonal_stats(*args, **kwargs))
     41 
     42 
~\anaconda3\lib\site-packages\rasterstats\main.py in gen_zonal_stats(vectors, raster, layer, band, nodata, affine, stats, all_touched, categorical, category_map, add_stats, zone_func, raster_out, prefix, geojson_out, boundless, **kwargs)
    163         band = band_num
    164 
--> 165     with Raster(raster, affine, nodata, band) as rast:
    166         features_iter = read_features(vectors, layer)
    167         for _, feat in enumerate(features_iter):
~\anaconda3\lib\site-packages\rasterstats\io.py in __init__(self, raster, affine, nodata, band)
    272             self.nodata = nodata
    273         else:
--> 274             self.src = rasterio.open(raster, "r")
    275             self.affine = guard_transform(self.src.transform)
    276             self.shape = (self.src.height, self.src.width)
~\anaconda3\lib\site-packages\rasterio\env.py in wrapper(*args, **kwds)
    449 
    450         with env_ctor(session=session):
--> 451             return f(*args, **kwds)
    452 
    453     return wrapper
~\anaconda3\lib\site-packages\rasterio\__init__.py in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs)
    244             or isinstance(fp, (os.PathLike, MemoryFile, FilePath))
    245         ):
--> 246             raise TypeError("invalid path or file: {0!r}".format(fp))
    247     if mode and not isinstance(mode, str):
    248         raise TypeError("invalid mode: {0!r}".format(mode))
TypeError: invalid path or file: <xarray.DataArray (band: 1, y: 3000, x: 2000)>
[6000000 values with dtype=int32]
Coordinates:
  * band         (band) int32 1
  * x            (x) float64 7.6e+05 7.6e+05 7.6e+05 ... 7.8e+05 7.8e+05 7.8e+05
  * y            (y) float64 4.09e+06 4.09e+06 4.09e+06 ... 4.06e+06 4.06e+06
    spatial_ref  int32 0
Attributes: (12/15)
    AREA_OR_POINT:           Area
    BandName:                Band_1
    RepresentationType:      ATHEMATIC
    STATISTICS_COVARIANCES:  8498387208.982427
    STATISTICS_MAXIMUM:      1552768
    STATISTICS_MEAN:         1286479.1959047
    ...                      ...
    STATISTICS_SKIPFACTORY:  1
    STATISTICS_STDDEV:       92186.69757065
    _FillValue:              -1
    scale_factor:            1.0
    add_offset:              0.0
long_name:               Band_1
It seems an error related to the use of float64 or int32, but I can't understand how to make it work. Is there a way to do it? And, in general, what is the easiest way to calculate the average value of some pixels inside polygons? (Given in the same shapefile i have many polygons on which i have to perform the mean of the pixels)
答案1
得分: 0
看起来文件路径不正确。
也许是这个?
"C:/Users/Daniele/OneDrive/PhD/GIS/ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif"
或者
"C:/Users/Daniele/OneDrive/PhD/GIS/" + "ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif"
我假设你想要将这两个路径拼接在一起。
英文:
It appears that the file path is not correct.
Maybe this?
"C:/Users/Daniele/OneDrive/PhD/GIS/ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif"
or
"C:/Users/Daniele/OneDrive/PhD/GIS/" + "ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif"
I am assuming that you want to concat both the paths together.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论