Python GDAL坐标变换:非类型错误
我正在尝试变换shapefile的坐标,并且在用于不同的shapefile时,从这个脚本中得到了混合的结果Python GDAL坐标变换:非类型错误,python,gis,gdal,shapefile,Python,Gis,Gdal,Shapefile,我正在尝试变换shapefile的坐标,并且在用于不同的shapefile时,从这个脚本中得到了混合的结果 from osgeo import ogr, osr import os driver = ogr.GetDriverByName('ESRI Shapefile') # input SpatialReference inSpatialRef = osr.SpatialReference() inSpatialRef.ImportFromEPSG(28356) # output Sp
from osgeo import ogr, osr
import os
driver = ogr.GetDriverByName('ESRI Shapefile')
# input SpatialReference
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(28356)
# output SpatialReference
outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(4326)
# create the CoordinateTransformation
coordTrans = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# get the input layer
inDataSet = driver.Open(r'Rail_network.shp')
inLayer = inDataSet.GetLayer()
# create the output layer
outputShapefile = r'Rail_network_WGS84.shp'
if os.path.exists(outputShapefile):
driver.DeleteDataSource(outputShapefile)
outDataSet = driver.CreateDataSource(outputShapefile)
#for point replace Polygon with Point or MultiPoint, for line replace with LineString or MultiLineString
outLayer = outDataSet.CreateLayer("basemap_4326", geom_type=ogr.wkbMultiLineString)
# add fields
inLayerDefn = inLayer.GetLayerDefn()
for i in range(0, inLayerDefn.GetFieldCount()):
fieldDefn = inLayerDefn.GetFieldDefn(i)
outLayer.CreateField(fieldDefn)
# get the output layer's feature definition
outLayerDefn = outLayer.GetLayerDefn()
# loop through the input features
inFeature = inLayer.GetNextFeature()
while inFeature:
# get the input geometry
geom = inFeature.GetGeometryRef()
# reproject the geometry
geom.Transform(coordTrans)
# create a new feature
outFeature = ogr.Feature(outLayerDefn)
# set the geometry and attribute
outFeature.SetGeometry(geom)
for i in range(0, outLayerDefn.GetFieldCount()):
outFeature.SetField(outLayerDefn.GetFieldDefn(i).GetNameRef(), inFeature.GetField(i))
# add the feature to the shapefile
outLayer.CreateFeature(outFeature)
# dereference the features and get the next input feature
outFeature = None
inFeature = inLayer.GetNextFeature()
# Save and close the shapefiles
inDataSet = None
outDataSet = None
#create prj file
spatialRef = osr.SpatialReference()
spatialRef.ImportFromEPSG(4326)
spatialRef.MorphToESRI()
file = open('Rail_network_WGS84.prj', 'w')
file.write(spatialRef.ExportToWkt())
file.close()
错误是:
第44行,在
几何变换(坐标变换)
AttributeError:“非类型”对象没有属性“转换”`
我在QGIS上测试了形状文件,结果显示它们很好。有人能识别出哪里出了问题吗?输出是什么样的?while循环在出错之前执行了多少次?我猜上一次运行
geo=inFeature.GetGeometryRef()
时,geo
变成了无。实际上只有一次。这是否表明文件本身存在问题?好问题。不幸的是,我不熟悉地理信息系统。可能是形状文件未正确读取?对于Rail\u network.shp
,您希望在then层中有多少功能?我猜inFeature.GetGeometryRef()
返回None
。因此,geom
是None,因此,None.transform()
抛出错误。输出是什么样的?while循环在出错之前执行了多少次?我猜上一次运行geo=inFeature.GetGeometryRef()
时,geo
变成了无。实际上只有一次。这是否表明文件本身存在问题?好问题。不幸的是,我不熟悉地理信息系统。可能是形状文件未正确读取?对于Rail\u network.shp
,您希望在then层中有多少功能?我猜inFeature.GetGeometryRef()
返回None
。因此,geom
是None,因此,None.transform()
抛出错误。