在WKT和Proj4字符串之间进行编程转换的最佳方法是什么?


14

某些shapefile具有与其关联的.prj文件,并且.prj文件包含WKT格式的shapefile的投影信息。有时我需要将WKT转换为proj4字符串,有时我需要将其转换回。

有没有现成的图书馆可以做到这一点?

Answers:


12

GDAL的OGR空间参考部分应该可以解决这个问题。capooti 为另一个问题提供了一个很好的答案,该问题演示了如何执行从shapefile到WKT的转换。您可能还想查看类参考。相反就是:

from osgeo import osr

srs = osr.SpatialReference()
wkt_text = 'GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",' \
           'SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],'\
           'UNIT["Degree",0.017453292519943295]]'
# Imports WKT to Spatial Reference Object
srs.ImportFromWkt(wkt_text)
srs.MorphToESRI() # converts the WKT to an ESRI-compatible format
print "ESRI compatible WKT for use as .prj:" % srs.ExportToWkt()

任何人都有不需要的解决方案osgeo,这似乎不适用于Python 3?
Dan Nguyen

OP询问了Proj4。您是说ExportToProj4()最后一行吗?
astrojuanlu

2

您也可以使用PyCRS

import pycrs

print(pycrs.parser.from_esri_wkt(wkt_text).to_proj4())
# +proj=longlat +ellps=WGS84 +a=6378137.0 +f=298.257223563 +pm=0.0  +no_defs


0

我需要从语法上转换为基于proj4text字符串的自定义投影,因此

projection = '+proj=lcc +lat_1=53 +lat_2=70 +lat_0=0 +lon_0=136 +x_0=0 +y_0=0 +ellps=intl +units=m +no_defs'

source = osr.SpatialReference() source.ImportFromEPSG(4326) target = osr.SpatialReference() target.ImportFromProj4(projection) transform = osr.CoordinateTransformation(source, target)

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.