首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyShp -让Python识别折线中的点

PyShp -让Python识别折线中的点
EN

Stack Overflow用户
提问于 2014-01-08 23:27:01
回答 1查看 905关注 0票数 1

我对Python比较陌生,所以我怀疑我遇到的这个问题是由幼稚引起的,但如果有任何帮助,我将不胜感激。

目前我有一个小型的海岸进化模型。最初,这将使用NumPy在某些约束内沿定义的x轴随机生成100个点。代码的相关部分如下:

代码语言:javascript
复制
# 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中绘制的折线,以替换这些随机生成的点,然后这些点将用于模型计算。我尝试的代码是这样的:

代码语言:javascript
复制
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文档和其他问题,似乎找不到该做什么。

干杯

完整代码:

代码语言:javascript
复制
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)    
EN

回答 1

Stack Overflow用户

发布于 2014-01-09 00:00:30

错误是因为你试图用Bch_Width = np.random.uniform(0, 30, BeachShp1)生成一个(潜在的)几千维数组,等等。

numpy.random.uniform需要一个低值和高值,以及将要生成的数组的形状。

例如:

代码语言:javascript
复制
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]])

当你运行时:

代码语言:javascript
复制
p1 = BS1.shapes()
b = p1[0]
BeachShp1 = b.points

BeachShp1是一系列x,y坐标。基本上,你正在尝试做一些事情,比如:

代码语言:javascript
复制
np.random.uniform(low, high, [[19.554, 45.998], [20.889, 24.009], ... ])

...which没有任何意义。

您是否正在尝试将随机的海滩宽度添加到shapefile表示的线条中?或者计算悬壁轮廓和海滩轮廓之间的距离?还是其他完全不同的东西?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20999758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档