pyshp创建面shp文件并设置投影

  • 使用pyshp创建shapefile,方便快捷
  • 参考官方文档
  • 首先安装pip install pyshp==1.2.3
  • 创建示例
"""
using pyshp create shpfile
(1) type:You can reference shape types by the numbers or by constants defined by PyShp:
shapefile.NULL = 0 shapefile.POINT = 1 shapefile.POLYLINE = 3 shapefile.POLYGON = 5
shapefile.MULTIPOINT = 8 shapefile.POINTZ = 11 shapefile.POLYLINEZ = 13 shapefile.POLYGONZ = 15
shapefile.MULTIPOINTZ = 18 shapefile.POINTM = 21 shapefile.POLYLINEM = 23 shapefile.POLYGONM = 25
shapefile.MULTIPOINTM = 28 shapefile.MULTIPATCH = 31'''
(2) field setting
Field name: the name describing the data at this column index.
Field type: the type of data at this column index. Types can be: Character, Numbers, Longs, Dates, or Memo. The “Memo” type has no meaning within a GIS and is part of the xbase spec instead.
Field length: the length of the data found at this column index. Older GIS software may truncate this length to 8 or 11 characters for “Character” fields.
Decimal length: the number of decimal places found in “Number” fields.

"""

import shapefile
import osr

outshp = r'D:\za\testshp\a.shp'

w = shapefile.Writer(shapeType=5)

#设置字段,最大长度为254,C为字符串
w.field('FIRST_FLD')
w.field('SECOND_FLD','C','40')
#添加几何和添加字段信息,添加两个示例,字段顺序区分先后
w.poly([[[122,37], [117,36], [115,32],[118,20], [123,24],[122,37]]])
w.record('First','Point')
w.poly([[[123,37], [118,36], [116,32],[119,20], [124,24],[123,37]]])
w.record('Second','Point')
#保存
w.save(outshp)

# 设置投影,通过.prj文件设置,需要写入一个wkt字符串
##gdal的GetProjection()返回的是wkt字符串,需要ImportFromWkt
#projstr="""PROJCS["WGS_1984_UTM_zone_50N",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32650"]]'"""
proj = osr.SpatialReference()
proj.ImportFromEPSG(4326)
#或 proj.ImportFromProj4(proj4str)等其他的来源
wkt = proj.ExportToWkt()
#写出prj文件
f = open(outshp.replace(".shp",".prj"), 'w')
f.write(wkt)
f.close()
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
若要批量进行shp文件投影转换,你可以使用Python的os模块来遍历指定目录中的所有shp文件,并对每个文件执行投影转换操作。以下是一个示例代码: ```python import os from osgeo import ogr, osr # 定义原始坐标系和目标坐标系 sourceEPSG = 4326 # 原始坐标系的EPSG代码 targetEPSG = 3857 # 目标坐标系的EPSG代码 # 创建目标坐标系 targetSR = osr.SpatialReference() targetSR.ImportFromEPSG(targetEPSG) # 遍历目录中的所有shp文件 source_dir = 'path/to/source_directory' # 源文件目录 output_dir = 'path/to/output_directory' # 输出文件目录 for filename in os.listdir(source_dir): if filename.endswith('.shp'): # 打开原始shp文件 source_path = os.path.join(source_dir, filename) source = ogr.Open(source_path) layer = source.GetLayer() # 创建输出的shp文件 output_path = os.path.join(output_dir, filename) driver = ogr.GetDriverByName('ESRI Shapefile') output = driver.CreateDataSource(output_path) outLayer = output.CreateLayer('output', geom_type=ogr.wkbPolygon) # 获取原始坐标系 sourceSR = layer.GetSpatialRef() # 创建坐标转换器 transform = osr.CoordinateTransformation(sourceSR, targetSR) # 遍历原始图层中的要素,并进行投影转换 for feature in layer: geom = feature.GetGeometryRef() geom.Transform(transform) # 创建新要素并将转换后的几何体添加到新图层中 newFeature = ogr.Feature(outLayer.GetLayerDefn()) newFeature.SetGeometry(geom) outLayer.CreateFeature(newFeature) newFeature = None # 关闭文件 source = None output = None ``` 请将代码中的`path/to/source_directory`和`path/to/output_directory`替换为你实际的源文件目录和输出文件目录。代码将遍历源文件目录中的所有shp文件,并将转换后的文件保存到输出文件目录中,文件名和文件结构保持一致。同时,你需要修改`sourceEPSG`和`targetEPSG`变量为你实际的原始和目标坐标系的EPSG代码。 这样,你就可以批量进行shp文件投影转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

独孤尚亮dugushangliang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值