《Python地理空间分析指南 第2版》学习笔记-5.4重投影

13 篇文章 4 订阅
6 篇文章 0 订阅

第5章 Python与地理信息系统

5.4重投影

借助osr实现数据的重投影一个Shapefile文件,不过现在这种使用方式很少了,可借助其他第三方库进行重投影。

需要安装的第三包如下:
osgeo下载
地理数据处理软件包GDAL教程

在本示例中,将使用包含Lambert等角投影的纽约市博物馆和画廊位置的点Shapefile文件。可以把它转换成WGS84坐标系统。你可以通过如下地址获取该示例Shapefile文件的zip压缩格式。

from osgeo import ogr
from osgeo import osr
import os
import shutil

#1、定义文件路径,包括原始文件和重投影后的文件
#尽量命名绝对路径
srcName = r"D:\xxx\Jgy_PIE\3Python_Code\Python_proj\01Python_Learn\Learning_GeospatialAnalysiswithPython\NO_5_Python与地理信息系统\5-4重投影\NYC_MUSEUMS_LAMBERT\NYC_MUSEUMS_LAMBERT.shp"
tgtName = "GEO-WGS84.shp"

# 2、定义坐标系统
#osr.SpatialReference 和 osr.CoordinateTransformation 类
# 提供了用来描绘坐标系统(投影和基准面)以及坐标系统相互转换的服务。
tgt_spatRef = osr.SpatialReference()
# 定义要转换后的坐标 4326 是 WGS84
tgt_spatRef.ImportFromEPSG(4326)

#我国常用坐标系的对应编码
# spatialReference.importFromEPSG(4326)WGS84
# spatialReference.importFromEPSG(4214)BeiJing54
# spatialReference.importFromEPSG(4610)XIAN80
# spatialReference.importFromEPSG(4490)CGCS2000

#3、获取矢量文件
# 要读取某种类型的数据,必须要先载入数据驱动,也就是初始化一个对象,
# 让它“知道”某种数据结构。
driver = ogr.GetDriverByName("ESRI Shapefile")

# 其中update为0是只读,为1是可写,注意open(<filename>, <update>)中
# filename一定要写绝对路径!
src = driver.Open(srcName, 0)
srcLyr = src.GetLayer()

src_spatRef = srcLyr.GetSpatialRef()
# 如果文件存在,删除
if os.path.exists(tgtName):
    driver.DeleteDataSource(tgtName)
    
# 4、创建新文件,但是这个文件不能已经存在了,否则会出错
tgt = driver.CreateDataSource(tgtName)
lyrName = os.path.splitext(tgtName)[0]

#使用WKB格式声明几何图形
# wkt(OGC well-known text)和wkb(OGC well-known binary)是OGC制定的空间数据的组织规范,
# wkt是以文本形式描述,wkb是以二进制形式描述
# 使用wkt和wkb能够很好到和其他系统进行数据交换,目前大部分支持空间数据存储的数据库构造空间数据都采用这两种方式。

# 创建矢量,(图层名称,几何类型wkbPoint为点类型)
tgtLyr = tgt.CreateLayer(lyrName, geom_type=ogr.wkbPoint)
featDef = srcLyr.GetLayerDefn()
# 投影转换,将New York 3104 转换为WGS84
trans = osr.CoordinateTransformation(src_spatRef, tgt_spatRef)
srcFeat = srcLyr.GetNextFeature()

#  5、遍历每个点要素进行投影转换,遍历模板:
# feat = layer.GetNextFeature()  #读取下一个
# while feat:
#     feat = layer.GetNextFeature()

while srcFeat:
    geom = srcFeat.GetGeometryRef()
    geom.Transform(trans)
    feature = ogr.Feature(featDef)
    feature.SetGeometry(geom)
    tgtLyr.CreateFeature(feature)
    # 关闭数据源,相当于文件系统操作中的关闭文件,释放内存
    feature.Destroy()
    srcFeat.Destroy()
    srcFeat = srcLyr.GetNextFeature()
src.Destroy()
tgt.Destroy()

#6、导出投影文件将几何图形转换为Esri的WKT格式

tgt_spatRef.MorphToESRI()
prj = open(lyrName + ".prj", "w")
prj.write(tgt_spatRef.ExportToWkt())
prj.close()
srcDbf = os.path.splitext(srcName)[0] + ".dbf"
tgtDbf = lyrName + ".dbf"
shutil.copyfile(srcDbf, tgtDbf)

投影结果对比
原始影像:
在这里插入图片描述

重投影后结果:
在这里插入图片描述


总结

请问大家现在有什么好的重投影方法呢,欢迎大家在评论区交流讨论下,互相学习。

《Python地理空间分析指南 第2版》学习笔记,仅供学习,如有侵权请联系删除。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
What this book covers Chapter 1, Package Installation and Management, explains how to install and manage the code libraries used in the book. Chapter 2, Introduction to Geospatial Code Libraries, covers the major code libraries used to process and analyze geospatial data. Chapter 3, Introduction to Geospatial Databases, introduces the geospatial databases used for data storage and analysis. Chapter 4, Data Types, Storage, and Conversion, focuses on the many different data types (both vector and raster) that exist within GIS. Chapter 5, Vector Data Analysis, covers Python libraries such as Shapely, OGR, and GeoPandas. which are used for analyzing and processing vector data. Chapter 6, Raster Data Processing, explores using GDAL and Rasterio to process raster datasets in order to perform geospatial analysis. Chapter 7, Geoprocessing with Geodatabases, shows the readers how to use Spatial SQL to perform geoprocessing with database tables containing a spatial column. Chapter 8, Automating QGIS Analysis, teaches the readers how to use PyQGIS to automate analysis within the QGIS mapping suite. Chapter 9, ArcGIS API for Python and ArcGIS Online, introduces the ArcGIS API for Python, which enables users to interact with Esri's cloud platform, ArcGIS Online, using Python 3. Chapter 10, Geoprocessing with a GPU Database, covers using Python tools to interact with cloud-based data to search and process data. Chapter 11, Flask and GeoAlchemy2, describes how to use the Flask Python web framework and the GeoAlchemy ORM to perform spatial data queries. Chapter 12, GeoDjango, covers using the Django Python web framework and the GeoDjango ORM to perform spatial data queries. Chapter 13, Geospatial REST API, teaches the readers how to create a REST API for geospatial data. Chapter 14, Cloud Geodatabase Analysis and Visualization, introduces the readers to the CARTOframes Python package, enabling the integration of Carto maps, analysis, and data services into data science workflows. Chapter 15, Automating Cloud Cartography, covers a new location data visualization library for Jupyter Notebooks. Chapter 16, Python Geoprocessing with Hadoop, explains how to perform geospatial analysis using distributed servers.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值