Python 写入形状文件
我在用python编写/读取形状文件时遇到问题。我有一个点数组,我想用pyshp写入多边形。守则的有关部分包括:Python 写入形状文件,python,shapefile,Python,Shapefile,我在用python编写/读取形状文件时遇到问题。我有一个点数组,我想用pyshp写入多边形。守则的有关部分包括: dividedRects = [(7598325.0, 731579.0, 7698325.0, 631579.0), (7598325.0, 631579.0, 7698325.0, 611641.0), (7698325.0, 731579.0, 7728636.0, 631579.0), (7698325.0, 631579.0, 7728636.0, 611641.0)]
dividedRects = [(7598325.0, 731579.0, 7698325.0, 631579.0), (7598325.0, 631579.0, 7698325.0, 611641.0), (7698325.0, 731579.0, 7728636.0, 631579.0), (7698325.0, 631579.0, 7728636.0, 611641.0)]
def createPolys(dividedRects):
w = shapefile.Writer(shapefile.POLYGON)
for i in range(0, len(dividedRects)):
print i
topLeft = [dividedRects[i][0],dividedRects[i][1]]
topRight = [dividedRects[i][2], dividedRects[i][1]]
bottomRight = [dividedRects[i][2], dividedRects[i][3]]
bottomLeft = [dividedRects[i][0], dividedRects[i][3]]
w.poly(parts=[[topLeft,topRight,bottomRight,bottomLeft]])
w.field("ID", "C", "40")
w.field("Events", "C", "40")
w.record(str(i), str(0))
w.save('cellFile')
createPolys(dividedRects)
这会导致一个错误:
IndexError Traceback (most recent call last)
<ipython-input-36-503affbe838b> in <module>()
----> 1 createPolys(dividedRects)
<ipython-input-35-4c552ae29bc7> in createPolys(dividedRects)
10 w.field("ID", "C", "40")
11 w.field("Events", "C", "40")
---> 12 w.record(str(i), str(0))
13 w.save('cellFile')
14 # topLeft = [dividedRects[1][0],dividedRects[1][1]]
C:\Users\Me\Anaconda2\lib\site-packages\shapefile.pyc in record(self, *recordList, **recordDict)
967 if self.fields[0][0].startswith("Deletion"): fieldCount -= 1
968 if recordList:
--> 969 [record.append(recordList[i]) for i in range(fieldCount)]
970 elif recordDict:
971 for field in self.fields:
IndexError: tuple index out of range
然后,在从文件读取记录时,我会得到一个断言错误:
createPolys(dividedRects)
sf2 = shapefile.Reader("cellFile")
print sf2.records()
shapes = sf2.shapes()
bbox = shapes[1].bbox
#['%.3f' % coord for coord in bbox]
print bbox
points = shapes[1].points
print points
AssertionError Traceback (most recent call last)
<ipython-input-37-597af0b882ba> in <module>()
1 sf2 = shapefile.Reader("cellFile")
----> 2 print sf2.records()
3 shapes = sf2.shapes()
4 bbox = shapes[1].bbox
5 #['%.3f' % coord for coord in bbox]
C:\Users\Me\Anaconda2\lib\site-packages\shapefile.pyc in records(self)
528 """Returns all records in a dbf file."""
529 if not self.numRecords:
--> 530 self.__dbfHeader()
531 records = []
532 f = self.__getFileObj(self.dbf)
C:\Users\Me\Anaconda2\lib\site-packages\shapefile.pyc in __dbfHeader(self)
464 self.fields.append(fieldDesc)
465 terminator = dbf.read(1)
--> 466 assert terminator == b("\r")
467 self.fields.insert(0, ('DeletionFlag', 'C', 1, 0))
468
AssertionError:
createPolys(dividedRects)
sf2=shapefile.Reader(“cellFile”)
打印sf2.records()
shapes=sf2.shapes()
bbox=形状[1]。bbox
#['%.3f'%bbox中的坐标对应的坐标]
打印框
点=形状[1]。点
打印点
AssertionError回溯(上次最近的调用)
在()
1 sf2=shapefile.Reader(“cellFile”)
---->2打印sf2.记录()
3个形状=sf2.shapes()
4 bbox=形状[1]。bbox
5#['%.3f'%bbox中的坐标对应坐标]
C:\Users\Me\Anaconda2\lib\site packages\shapefile.pyc in records(self)
528“返回dbf文件中的所有记录。”“”
529如果不是self.numRecords:
-->530 self.\uu dbfHeader()
531记录=[]
532 f=self.\uu getFileObj(self.dbf)
C:\Users\Me\Anaconda2\lib\site packages\shapefile.pyc在\uuu dbfHeader(self)中
464 self.fields.append(fieldDesc)
465终止符=dbf.read(1)
-->466断言终止符==b(“\r”)
467自我字段。插入(0,('deletingflag','C',1,0))
468
断言者错误:
当我删除循环并写下一条记录时,它似乎工作正常。发生了什么事?我不知道
pyshp
库,但我会尽力帮助你
两个w.field()
命令出现在for
循环中。这可能会导致两列“ID”和“Events”被多次定义。当您只写一条记录(多边形)时,它工作正常(即w.record()
命令包含两个值)。在第一次迭代之后,将有4、6等列。这可以解释你描述的行为
尝试将两行w.field()
移到循环之前
在注释w.record()
时,您将获得一个shp
(和shx
)文件,其中的记录数与相应的dbf
文件不同。这解释了读取时的断言错误
与您的问题无关,您还可以使用enumerate
(内置函数)简化代码
(我无法测试,因为我没有pyshp
)祝你好运 成功了,谢谢!我不敢相信我没有看到,尽管我已经看了几个小时了。。。
createPolys(dividedRects)
sf2 = shapefile.Reader("cellFile")
print sf2.records()
shapes = sf2.shapes()
bbox = shapes[1].bbox
#['%.3f' % coord for coord in bbox]
print bbox
points = shapes[1].points
print points
AssertionError Traceback (most recent call last)
<ipython-input-37-597af0b882ba> in <module>()
1 sf2 = shapefile.Reader("cellFile")
----> 2 print sf2.records()
3 shapes = sf2.shapes()
4 bbox = shapes[1].bbox
5 #['%.3f' % coord for coord in bbox]
C:\Users\Me\Anaconda2\lib\site-packages\shapefile.pyc in records(self)
528 """Returns all records in a dbf file."""
529 if not self.numRecords:
--> 530 self.__dbfHeader()
531 records = []
532 f = self.__getFileObj(self.dbf)
C:\Users\Me\Anaconda2\lib\site-packages\shapefile.pyc in __dbfHeader(self)
464 self.fields.append(fieldDesc)
465 terminator = dbf.read(1)
--> 466 assert terminator == b("\r")
467 self.fields.insert(0, ('DeletionFlag', 'C', 1, 0))
468
AssertionError:
w = shapefile.Writer(shapefile.POLYGON)
w.field("ID", "C", "40")
w.field("Events", "C", "40")
for i,rect1 in enumerate(dividedRects):
print i
topLeft = [rect1[0],rect1[1]]
topRight = [rect1[2], rect1[1]]
bottomRight = [rect1[2], rect1[3]]
bottomLeft = [rect1[0], rect1[3]]
....