【发布时间】:2014-01-26 19:06:14
【问题描述】:
我对 Python 比较陌生,所以我怀疑我遇到的这个问题是由于幼稚造成的,但我们将不胜感激。
目前我有一个小型海岸演化模型。最初,这使用 NumPy 在某些约束内沿定义的 x 轴随机生成 100 个点。代码的相关部分是这样的:
# assign the number of points along the beach
Num_P_Coast = 100
# node spacing
dx = 10
x_vec = np.zeros(Num_P_Coast)
for i in range(0,Num_P_Coast):
x_vec[i] = dx*i
# initialize beach and cliff with random numbers
Bch_Width = np.random.uniform(0, 30, Num_P_Coast)
Cl_Loc = np.random.uniform(30, 60, Num_P_Coast)
然后模型使用这两条初始线进行模型计算。我正在尝试做的是使用 PyShp 读取在 ArcMap 中绘制的折线以替换这些随机生成的点,然后将其用于模型计算。我正在尝试的代码是这样的:
BS1 = shp.Reader("Beach2.shp") #calling shapefile of beach front location
CL1 = shp.Reader("Cliff2.shp") #calling shapefile of cliff location
p1 = BS1.shapes()
b = p1[0]
BeachShp1 = b.points
p2 = CL1.shapes()
c = p2[0]
CliffShp1 = c.points
Bch_Width = np.random.uniform(0, 30, BS1) #Attempt to use polyline of beach to generate initial beach location
Cl_Loc = np.random.uniform(30, 60, CL1) #Attempt to use polyline of beach to generate initial beach location
当我尝试对代码进行小的更改时,这会不断给出错误,例如“TypeError:需要一个整数”和“ValueError:序列太大;必须小于 32”。
有谁知道需要做什么才能让代码接受折线上的点,而不是随机生成的点?我觉得它应该很简单,但是我在这里浏览了 PyShp 文档和其他问题,似乎找不到该怎么做。
干杯
完整代码:
def toy_beach_model(): #this is the main part of the program, which will call the evol_equations function to do all the jiggery pokery of outputting beach evolution
#where do you want to put it
#OutputDirectory = str(raw_input("Enter output directory:"))
OutputDirectory = 'c:/python_results/graphs/'
OutputModelName = eg.enterbox(title = 'Model name', msg = 'Enter model ID:')
ModelDesc = eg.enterbox(title = 'Model description', msg = 'Enter short description of model' )
# assign the number of points along the beach
Num_P_Coast = 100
# node spacing
dx = 10
x_vec = np.zeros(Num_P_Coast)
# some parameters
Cl_Eros_NoBch = int(eg.enterbox(title = 'Erosion rate', msg = 'Enter erosion rate:'))
Cl_Eros_HRock = Cl_Eros_NoBch * 0.5 #simulating harder rock = headland evolution
Cl_Eros_Efold = 50
Sup_Rate = int(eg.enterbox(title = 'Sediment supply', msg = 'Enter sediment supply rate:'))
K = 0.5
# set up x vector
for i in range(0,Num_P_Coast):
x_vec[i] = dx*i
BS1 = shp.Reader("Beach2.shp") #calling shapefile of beach front location
CL1 = shp.Reader("Cliff2.shp") #calling shapefile of cliff location
p1 = BS1.shapes()
b = p1[0]
BeachShp1 = b.points
p2 = CL1.shapes()
c = p2[0]
CliffShp1 = c.points
# initialize beach and cliff with random numbers
#Bch_Width = np.random.uniform(0, 30, Num_P_Coast)
#Cl_Loc = np.random.uniform(30, 60, Num_P_Coast)
Bch_Width = np.random.uniform(0, 30, BeachShp1) #Attempt to use polyline of beach to generate initial beach location
Cl_Loc = np.random.uniform(30, 60, CliffShp1) #Attempt to use polyline of beach to generate initial beach location
#H_Loc = Cl_Loc
#H_Loc = np.random.uniform(0, 8, Num_P_Coast)
H_Loc = np.random.random_sample(Num_P_Coast)
BcH_Eros = Bch_Width+Cl_Loc
# lets have this evolve through time
# time spacing in years
dt = 10
# number of time steps
n_timesteps = int(eg.enterbox(title = 'Timestep', msg = 'Enter number of iterations'))
# set up the beach erosion vector
Cl_Eros = np.zeros(Num_P_Coast)
H_Eros = np.zeros(Num_P_Coast)
# initial headland erosion
H_Eros = dt*Cl_Eros_HRock*np.exp(-Bch_Width/Cl_Eros_Efold)
# initial erosion
Cl_Eros = dt*Cl_Eros_NoBch*np.exp(-Bch_Width/Cl_Eros_Efold)
【问题讨论】:
标签: python numpy shapefile polyline