尝试学习一些地理空间Python。或多或少地遵循类注释here。
#!/usr/bin/python
# import modules
import ogr, sys, os
# set working dir
os.chdir('/home/jacques/misc/pythongis/data')
# create the text file we're writing to
file = open('data_export.txt', 'w')
# import the required driver for .shp
driver = ogr.GetDriverByName('ESRI Shapefile')
# open the datasource
data = driver.Open('road_surveys.shp', 1)
if data is None:
print 'Error, could not locate file'
sys.exit(1)
# grab the datalayer
layer = data.GetLayer()
# loop through the features
feature = layer.GetNextFeature()
while feature:
# acquire attributes
id = feature.GetFieldAsString('Site_Id')
date = feature.GetFieldAsString('Date')
# get coordinates
geometry = feature.GetGeometryRef()
x = str(geometry.GetX())
y = str(geometry.GetY()
# write to the file
file.Write(id + ' ' + x + ' ' + y + ' ' + cover + '\n')
# remove the current feature, and get a new one
feature.Destroy()
feature = layer.GetNextFeature()
# close the data source
datasource.Destroy()
file.close()
跑步给我带来了以下好处:
File "shape_summary.py", line 38
file.write(id + ' ' + x + ' ' + y + ' ' + cover + '\n')
^
SyntaxError: invalid syntax
运行Python2.7.1
任何帮助都太棒了!
1)在代码中,write不应该是大写(Python是区分大小写的) 2) 确保id是一个字符串;如果在你的术语中没有使用str(id),那么“cover”、“x”和“y”也一样
前一行缺少右括号:
另外,只需要一个样式注释:最好避免在python中使用变量名
file
,因为它实际上有意义。尝试打开一个新的python会话并运行help(file)
相关问题 更多 >
编程相关推荐