如何解决“TypeError: 无效的路径或文件 ..”?

huangapple go评论94阅读模式
英文:

How to solve "TypeError: invalid path or file .."?

问题

以下是代码部分的翻译:

  1. 我正在使用 rasterstats Python 模块 zonal_stats 来计算每个地块包含在 shapefile 内的太阳辐照度均值从栅格中获取)。
  2. 如果我执行以下代码
  3. print(irradiance.read().shape)
  4. 我会得到(1, 2852, 2425)
  5. 然后我执行以下操作
  6. with rioxarray.open_rasterio("C:/Users/Daniele/OneDrive/PhD/GIS + ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif") as src:
  7. stats = zonal_stats(particles, src)
  8. 然后我得到以下错误
  9. 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

  1. print(irradiance.read().shape) I get : (1, 2852, 2425)

Then, I do:

  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)

And i get the following error:

  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. ~\AppData\Local\Temp\ipykernel_1458822157982.py in <module>
  4. 1 with rioxarray.open_rasterio("C:/Users/Daniele/OneDrive/PhD/GIS + ESOM/GetSolarIrr/Solar_Irradiance/Yearly/Int_AreaSol_3.tif") as src:
  5. ----> 2 stats = zonal_stats(particles,src)
  6. ~\anaconda3\lib\site-packages\rasterstats\main.py in zonal_stats(*args, **kwargs)
  7. 38 The only difference is that ``zonal_stats`` will
  8. 39 return a list rather than a generator."""
  9. ---> 40 return list(gen_zonal_stats(*args, **kwargs))
  10. 41
  11. 42
  12. ~\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)
  13. 163 band = band_num
  14. 164
  15. --> 165 with Raster(raster, affine, nodata, band) as rast:
  16. 166 features_iter = read_features(vectors, layer)
  17. 167 for _, feat in enumerate(features_iter):
  18. ~\anaconda3\lib\site-packages\rasterstats\io.py in __init__(self, raster, affine, nodata, band)
  19. 272 self.nodata = nodata
  20. 273 else:
  21. --> 274 self.src = rasterio.open(raster, "r")
  22. 275 self.affine = guard_transform(self.src.transform)
  23. 276 self.shape = (self.src.height, self.src.width)
  24. ~\anaconda3\lib\site-packages\rasterio\env.py in wrapper(*args, **kwds)
  25. 449
  26. 450 with env_ctor(session=session):
  27. --> 451 return f(*args, **kwds)
  28. 452
  29. 453 return wrapper
  30. ~\anaconda3\lib\site-packages\rasterio\__init__.py in open(fp, mode, driver, width, height, count, crs, transform, dtype, nodata, sharing, **kwargs)
  31. 244 or isinstance(fp, (os.PathLike, MemoryFile, FilePath))
  32. 245 ):
  33. --> 246 raise TypeError("invalid path or file: {0!r}".format(fp))
  34. 247 if mode and not isinstance(mode, str):
  35. 248 raise TypeError("invalid mode: {0!r}".format(mode))
  36. TypeError: invalid path or file: <xarray.DataArray (band: 1, y: 3000, x: 2000)>
  37. [6000000 values with dtype=int32]
  38. Coordinates:
  39. * band (band) int32 1
  40. * x (x) float64 7.6e+05 7.6e+05 7.6e+05 ... 7.8e+05 7.8e+05 7.8e+05
  41. * y (y) float64 4.09e+06 4.09e+06 4.09e+06 ... 4.06e+06 4.06e+06
  42. spatial_ref int32 0
  43. Attributes: (12/15)
  44. AREA_OR_POINT: Area
  45. BandName: Band_1
  46. RepresentationType: ATHEMATIC
  47. STATISTICS_COVARIANCES: 8498387208.982427
  48. STATISTICS_MAXIMUM: 1552768
  49. STATISTICS_MEAN: 1286479.1959047
  50. ... ...
  51. STATISTICS_SKIPFACTORY: 1
  52. STATISTICS_STDDEV: 92186.69757065
  53. _FillValue: -1
  54. scale_factor: 1.0
  55. add_offset: 0.0
  56. 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.

huangapple
  • 本文由 发表于 2023年4月13日 17:59:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76004114.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定