化妆品网站推广策划书,asp.net mvc5网站开发之美 pdf,上海装修设计,达州做网站的公司老样子最近有项目需要将N个矢量文件合并成一个#xff0c;总不能用ArcGIS一个个导入吧。所以我就想着用Python编个程序实现批量合并矢量。我之前也发了一些关于Python操作矢量数据的文章#xff1a;【PythonGIS】Python处理矢量数据的基本操作#xff08;查询、修改、删… 老样子最近有项目需要将N个矢量文件合并成一个总不能用ArcGIS一个个导入吧。所以我就想着用Python编个程序实现批量合并矢量。我之前也发了一些关于Python操作矢量数据的文章【PythonGIS】Python处理矢量数据的基本操作查询、修改、删除、新建,如果大家感兴趣可以去我的主页看看给我点个关注
一、导入库
import os
from osgeo import ogr
二、合并shp 整体的思路就是创建一个空的shp资源然后遍历文件夹中所有的shp然后针对每一个shp再遍历它的要素将每个要素写入创建的新shp中。需要注意的是最后需要释放内存不然数据不会写入shp。
def Merge_shp(path):print(-----------------合并shp-----------------)path_lists os.listdir(path)src_proj Nonefor c in path_lists:if c.endswith(.shp):print(c)ds ogr.Open(pathc)layer ds.GetLayer()# 打开需要转换的矢量数据获取图层src_proj layer.GetSpatialRef()break# 获取其源坐标信息output_file pathMerge.shpdriver ogr.GetDriverByName(ESRI Shapefile)output_ds driver.CreateDataSource(output_file)output_layer output_ds.CreateLayer(Shp, srssrc_proj, geom_typeogr.wkbMultiPolygon)new_field ogr.FieldDefn(value, ogr.OFTReal) # 给目标shp文件添加一个字段用来存储原始栅格的pixel valueoutput_layer.CreateField(new_field)for i in range(0, len(path_lists)):if path_lists[i].endswith(.shp):print(正在合并%s...... % path_lists[i])input_ds ogr.Open(path path_lists[i])input_layer input_ds.GetLayer()for feature in input_layer:output_layer.CreateFeature(feature.Clone())input_ds Noneoutput_ds None
三、获取要素面积 我这里给shp添加了一个新的字段用来计算面积。大家视情况而定这个可以没有。
def Get_polygon_area(path_shp)::param path_shp: 输入矢量文件:return:print(---------------获取矢量面积---------------)driver ogr.GetDriverByName(ESRI Shapefile) # 创建数据驱动ds driver.Open(path_shp, 1) # 创建数据资源layer ds.GetLayer()new_field ogr.FieldDefn(Area, ogr.OFTReal) # 创建新的字段# new_field.SetWidth(32)# new_field.SetPrecision(16)layer.CreateField(new_field)for feature in layer:geom feature.GetGeometryRef()geom2 geom.Clone()area geom2.GetArea() # 默认为平方米# area area / 1000000 # 转化为平方公里feature.SetField(Area, area)# feature.GetField(Area)layer.SetFeature(feature)ds None
四、完整代码
# -*- coding: utf-8 -*-Time 2023/10/20 11:56
Auth RS迷途小书童
File Vector Data Batch Merge.py
IDE PyCharm
Purpose矢量数据批量合并成一个文件并计算面积import os
from osgeo import ogrdef Merge_shp(path):print(-----------------合并shp-----------------)path_lists os.listdir(path)src_proj Nonefor c in path_lists:if c.endswith(.shp):print(c)ds ogr.Open(pathc)layer ds.GetLayer()# 打开需要转换的矢量数据获取图层src_proj layer.GetSpatialRef()break# 获取其源坐标信息output_file pathMerge.shpdriver ogr.GetDriverByName(ESRI Shapefile)output_ds driver.CreateDataSource(output_file)output_layer output_ds.CreateLayer(Shp, srssrc_proj, geom_typeogr.wkbMultiPolygon)new_field ogr.FieldDefn(value, ogr.OFTReal) # 给目标shp文件添加一个字段用来存储原始栅格的pixel valueoutput_layer.CreateField(new_field)for i in range(0, len(path_lists)):if path_lists[i].endswith(.shp):print(正在合并%s...... % path_lists[i])input_ds ogr.Open(path path_lists[i])input_layer input_ds.GetLayer()for feature in input_layer:output_layer.CreateFeature(feature.Clone())input_ds Noneoutput_ds Nonedef Get_polygon_area(path_shp)::param path_shp: 输入矢量文件:return:print(---------------获取矢量面积---------------)driver ogr.GetDriverByName(ESRI Shapefile) # 创建数据驱动ds driver.Open(path_shp, 1) # 创建数据资源layer ds.GetLayer()new_field ogr.FieldDefn(Area, ogr.OFTReal) # 创建新的字段# new_field.SetWidth(32)# new_field.SetPrecision(16)layer.CreateField(new_field)for feature in layer:geom feature.GetGeometryRef()geom2 geom.Clone()area geom2.GetArea() # 默认为平方米# area area / 1000000 # 转化为平方公里feature.SetField(Area, area)# feature.GetField(Area)layer.SetFeature(feature)ds Noneif __name__ __main__:path1 rG:/2/Merge_shp(path1)Get_polygon_area(path1Merge.shp) 大家在使用时只需要将你要合并的shp全部放入一个文件夹然后再将main中文件夹路径改成自己的就行了程序最后会在该目录下生成Merge.shp文件这个就是合并之后的结果。 文章转载自: http://www.morning.rszwc.cn.gov.cn.rszwc.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.jzdfc.cn.gov.cn.jzdfc.cn http://www.morning.brlgf.cn.gov.cn.brlgf.cn http://www.morning.rbkgp.cn.gov.cn.rbkgp.cn http://www.morning.btlmb.cn.gov.cn.btlmb.cn http://www.morning.bhpsz.cn.gov.cn.bhpsz.cn http://www.morning.xhftj.cn.gov.cn.xhftj.cn http://www.morning.ltdxq.cn.gov.cn.ltdxq.cn http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.rkhhl.cn.gov.cn.rkhhl.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.xgxbr.cn.gov.cn.xgxbr.cn http://www.morning.mpmtz.cn.gov.cn.mpmtz.cn http://www.morning.gyqnp.cn.gov.cn.gyqnp.cn http://www.morning.lzrpy.cn.gov.cn.lzrpy.cn http://www.morning.kgfsz.cn.gov.cn.kgfsz.cn http://www.morning.wmlby.cn.gov.cn.wmlby.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.ghgck.cn.gov.cn.ghgck.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.grxyx.cn.gov.cn.grxyx.cn http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn http://www.morning.fbdkb.cn.gov.cn.fbdkb.cn http://www.morning.nckzt.cn.gov.cn.nckzt.cn http://www.morning.kybpj.cn.gov.cn.kybpj.cn http://www.morning.rlhgx.cn.gov.cn.rlhgx.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.qyqdz.cn.gov.cn.qyqdz.cn http://www.morning.etsaf.com.gov.cn.etsaf.com http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.gkfwp.cn.gov.cn.gkfwp.cn http://www.morning.qctsd.cn.gov.cn.qctsd.cn http://www.morning.drndl.cn.gov.cn.drndl.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.nzcgj.cn.gov.cn.nzcgj.cn http://www.morning.rtspr.cn.gov.cn.rtspr.cn http://www.morning.mxptg.cn.gov.cn.mxptg.cn http://www.morning.fxzw.cn.gov.cn.fxzw.cn http://www.morning.tnqk.cn.gov.cn.tnqk.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.nhgkm.cn.gov.cn.nhgkm.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.ktqtf.cn.gov.cn.ktqtf.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.hdqqr.cn.gov.cn.hdqqr.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.c7497.cn.gov.cn.c7497.cn http://www.morning.kdjtt.cn.gov.cn.kdjtt.cn http://www.morning.gxklx.cn.gov.cn.gxklx.cn http://www.morning.qkkmd.cn.gov.cn.qkkmd.cn http://www.morning.wnkjb.cn.gov.cn.wnkjb.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.kkzwn.cn.gov.cn.kkzwn.cn http://www.morning.dpgdj.cn.gov.cn.dpgdj.cn http://www.morning.rhpy.cn.gov.cn.rhpy.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.qzqjz.cn.gov.cn.qzqjz.cn http://www.morning.srxhd.cn.gov.cn.srxhd.cn http://www.morning.lwgsk.cn.gov.cn.lwgsk.cn http://www.morning.bgygx.cn.gov.cn.bgygx.cn http://www.morning.fcqlt.cn.gov.cn.fcqlt.cn http://www.morning.rlwgn.cn.gov.cn.rlwgn.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.qnqt.cn.gov.cn.qnqt.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.ktblf.cn.gov.cn.ktblf.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.xtdtt.cn.gov.cn.xtdtt.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn