&quot“ valueerror:阵列的条件形状必须与自我”相同。
我正在尝试关注
from osgeo import gdal
from PIL import Image
import numpy as np
import matplotlib as mtp
import matplotlib.pyplot as plt
import pandas as pd
import geopandas as gpd
import earthpy.plot as ep
import rasterio
from rasterio.plot import reshape_as_raster, reshape_as_image
get_ipython().run_line_magic('matplotlib', 'inline')
pd.options.display.max_colwidth = 89
# In[2]:
#setting the path for image
S1_S2_stack = 'S1_S2_stack.tif'
#path to training and validation data
training_points = 'testing.shp'
validation_points = 'training.shp'
# In[3]:
colors = dict ((
(0, (0,76,153,255)), #wheat
(1, (0,153,0,255)), #corn
(2, (255,0,0,255)), #other
(3, (255,153,51,255)),
(4, (255,255,0,255))
))
# In[4]:
for k in colors:
v = colors [k]
_v = [_v / 255.0 for _v in v]
colors[k] = _v
index_colors = [colors[key] if key in colors else (1,1,1,0) for key in range (0,5)]
cmap = plt.matplotlib.colors.ListedColormap(index_colors, 'Classification', 5)
# In[5]:
src = rasterio.open(S1_S2_stack)
bands = src.read()
# In[6]:
stack =src. read()
print(stack.shape)
fig, (ax1, ax2) = plt.subplots(1,2,figsize= (20,10))
ax1 = ep.plot_rgb(arr = stack, rgb =(3, 2, 1), stretch=True, ax = ax1, title = "RGB Composite - Sentinel-2")
stack_s1 =np.stack ((stack[28],stack[29],stack[29]/stack[28]))
ax2 = ep.plot_rgb(arr = stack_s1, rgb = (1,0,2), stretch=True, ax = ax2, title= "RGB Composite - Sentinel-1 (VV, VH, VV/VH)")
plt.tight_layout()
# In[7]:
img = src.read()
profile = src.profile
with rasterio.io.MemoryFile () as memfile:
with memfile.open(**profile) as dst:
for i in range(0, src.count):
dst.write(img[i], i+1)
dataset = memfile.open()
从这里起作用。但是,当我运行此代码时:
#read points from shapefile
train_pts = gpd.read_file (training_points)
train_pts = train_pts[[ 'CID','class', 'POINT_X','POINT_Y']] #attribute fields in shapefile
train_pts.index = range(len(train_pts))
coords = [(x,y) for x, y in zip(train_pts.POINT_X, train_pts.POINT_Y)] #create list of point coordinates
#sample each band of raster dataset at each point in the coordinate list
train_pts ['Raster Value'] = [x for x in dataset.sample(coords)] #all band values saved as a list in the Raster Value column
#Unpack the raster value column to separate column for each band - band names retrieved from snappy in the video but I was looking for an option
train_pts[bands] = pd.DataFrame(train_pts['Raster Value'].tolist(), index = train_pts.index)
train_pts = train_pts.drop(['Raster Value'], axis=1) #drop raster value column
#change the values for last three classes
train_pts['CID'] = train_pts['CID'].replace([7,8,15],[5,6,7])
train_pts.to.csv('train_pts.csv') #save as csv
train_pts.head () #see first column
我会收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [9], in <cell line: 10>()
8 train_pts ['Raster Value'] = [x for x in dataset.sample(coords)] #all band values saved as a list in the Raster Value column
9 #Unpack the raster value column to separate column for each band - band names retrieved from snappy in the video but I was looking for an option
---> 10 train_pts[src1] = pd.DataFrame(train_pts['Raster Value'].tolist(), index = train_pts.index)
11 train_pts = train_pts.drop(['Raster Value'], axis=1) #drop raster value column
12 #change the values for last three classes
File ~\.conda\envs\geocomp3_clone\lib\site-packages\pandas\core\frame.py:3643, in DataFrame.__setitem__(self, key, value)
3641 self._setitem_frame(key, value)
3642 elif isinstance(key, (Series, np.ndarray, list, Index)):
-> 3643 self._setitem_array(key, value)
3644 elif isinstance(value, DataFrame):
3645 self._set_item_frame_value(key, value)
File ~\.conda\envs\geocomp3_clone\lib\site-packages\pandas\core\frame.py:3685, in DataFrame._setitem_array(self, key, value)
3680 else:
3681 # Note: unlike self.iloc[:, indexer] = value, this will
3682 # never try to overwrite values inplace
3684 if isinstance(value, DataFrame):
-> 3685 check_key_length(self.columns, key, value)
3686 for k1, k2 in zip(key, value.columns):
3687 self[k1] = value[k2]
File ~\.conda\envs\geocomp3_clone\lib\site-packages\pandas\core\indexers\utils.py:428, in check_key_length(columns, key, value)
426 if columns.is_unique:
427 if len(value.columns) != len(key):
--> 428 raise ValueError("Columns must be same length as key")
429 else:
430 # Missing keys in columns are represented as -1
431 if len(columns.get_indexer_non_unique(key)[0]) != len(value.columns):
ValueError: Columns must be same length as key
我的问题如下:
- 我用来导入的方法是否有问题 Shapefile的乐队?
- 我需要在输入的代码中写下所有字段 ShapeFile的属性信息?还是我应该编辑这些 GIS程序中的字段?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
则从频段说明中创建新的数据框列
如果您说
然后是一个3维numpy
ndarray
(一系列的栅格帧), 。将其用于索引Train_pts [bands]
,其中Train_pts_pts
是一个数据框,这是没有意义的。但是我认为您想参考乐队名称。如果是这样,请尝试src.descriptions
而不是:要知道
band_names
的某些陷阱应该是列表,以便我们可以将它们用作train_pts [band_names]
trib_pts
/代码>。至于src.descriptions
,我们必须将其转换为list
。Creating new DataFrame columns from band descriptions
If you say
then
bands
is a 3-dimensional Numpyndarray
(a series of raster frames). It makes no sense to use it for indexing liketrain_pts[bands]
, wheretrain_pts
is a data frame. But I assume you want to refer to the band names. If so, trysrc.descriptions
instead:Some pitfalls to be aware of
band_names
should be a list so we can use them as indexes intrain_pts[band_names]
. As far assrc.descriptions
is a tuple, we have to transform it into alist
.