Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyShp-让Python从多段线识别点_Python_Numpy_Shapefile_Polyline - Fatal编程技术网

PyShp-让Python从多段线识别点

PyShp-让Python从多段线识别点,python,numpy,shapefile,polyline,Python,Numpy,Shapefile,Polyline,我对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 ran

我对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)    

这个错误是因为你试图用
Bch\u Width=np.random.uniform(0,30,BeachShp1)
等创建一个(可能)几千维数组

需要一个低、高和将要生成的阵列形状

例如:

In [1]: import numpy as np

In [2]: np.random.uniform(0, 5) # Generate a single random number between 0 and 5
Out[2]: 4.149771995503083

In [3]: np.random.uniform(0, 5, 3) # Generate an array of three numbers
Out[3]: array([ 2.25725653,  0.70070352,  0.62541689])

In [4]: np.random.uniform(0, 5, (2,2)) # Generate a 2x2 array of 4 numbers
Out[4]:
array([[ 0.89355128,  3.30796407],
       [ 1.23816971,  1.12224456]])
运行时:

p1 = BS1.shapes()
b = p1[0]
BeachShp1 = b.points
BeachShp1
是x,y坐标序列。基本上,您正在尝试执行以下操作:

np.random.uniform(low, high, [[19.554, 45.998], [20.889, 24.009], ... ])
…这毫无意义


是否尝试将随机海滩宽度添加到由shapefile表示的线中?或者计算悬崖轮廓和海滩轮廓之间的距离?还是别的什么?

为答案干杯!我想我明白了如何将random.uniform和BeachHP结合起来是行不通的。我希望形状文件中的多段线将代表海滩的起始“形状”和位置(以及悬崖的相应形状文件)。然后,该模型的目的是随着时间的推移穿越海滩/悬崖侵蚀。然而,模型方程依赖于作为海滩/悬崖起始位置生成的点。因此,我希望我能以某种方式将这些点替换为海滩的实际轮廓,以增加模型的真实感。