通过电子邮件提供的答案非常有用。为了分享答案,我提供了下面的代码来进行未嵌套的流域子流域计算。一个小评论:我不得不分批输入坐标,因为坐标列表超出了窗口可以处理的最大字符长度。
感谢@Huidae Cho,调用 R.accumulate 来计算子流域和最长流路径现在可以在一个调用中完成,而不是两个单独的调用。
输出是未嵌套的盆地。较大的子流域与较小的子流域分开,而不是被剪裁成较小的流域。这与输出是栅格格式这一事实有关,其中每个像元只能代表一个盆地。
gs.run_command('g.mapset',mapset='Watersheds')
gs.run_command('g.region', rast='direction@PERMANENT')
StationIds = list(gs.vector.vector_db_select('locations_snapped_new', columns = 'StationId')["values"].values())
XY = list(gs.vector.vector_db_select('locations_snapped_new', columns = 'x_real,y_real')["values"].values())
for j in range(0,len(XY),255):
output_ws = "watershed_batch_{}@Watersheds".format(j)
output_lfp = "lfp_batch_{}@Watersheds".format(j)
output_lfp_unique = "lfp_unique_batch_{}@Watersheds".format(j)
gs.run_command("r.accumulate", direction='direction@PERMANENT', subwatershed=output_ws, flags = "ar", coordinates = XY[j:j+255],lfp=output_lfp, id=StationIds[j:j+255], id_column="id",overwrite=True)
gs.run_command("r.to.vect", input=output_ws, output=output_ws, type="area", overwrite=True)
gs.run_command("v.extract", input=output_lfp, where="1 order by id", output=output_lfp_unique,overwrite=True)
为了导出独特的流域,我使用了以下代码。我必须将 longest_flow_path 转换为指向一些 longest_flow_paths 与其旁边的分水岭的角边界相交。因此,一些最长的水流路径并未完全位于次流域内。请参见下图,其中红线(最长的水流路径)接触子流域边界:
enter image description here
gs.run_command('g.mapset',mapset='Watersheds')
lfps= gs.list_grouped('vect', pattern='lfp_unique_*')['Watersheds']
ws= gs.list_grouped('vect', pattern='watershed_batch*')['Watersheds']
files=np.stack((lfps,ws)).T
#print(files)
for file in files:
print(file)
ids = list(gs.vector.vector_db_select(file[0],columns="id")["values"].values())
for idx in ids:
idx=int(idx[0])
expr = f'id="{idx}"'
gs.run_command('v.extract',input=file[0], where=expr, output="tmp_lfp",overwrite=True)
gs.run_command("v.to.points", input="tmp_lfp", output="tmp_lfp_points", use="vertex", overwrite=True)
gs.run_command('v.select', ainput= file[1], binput = "tmp_lfp_points", output="tmp_subwatersheds", overwrite=True)
gs.run_command('v.db.update', map = "tmp_subwatersheds",col= "value", value=idx)
gs.run_command('g.mapset',mapset='vector_out')
gs.run_command('v.dissolve',input= "tmp_subwatersheds@Watersheds", output="subwatersheds_{}".format(idx),col="value",overwrite=True)
gs.run_command('g.mapset',mapset='Watersheds')
gs.run_command("g.remove", flags="f", type="vector",name="tmp_lfp,tmp_subwatersheds")
我最终得到了每个子流域的向量