シェイプファイルをPythonで書き込んだり、読み込んだりするのに問題があります。 pyshpを使ってポリゴンに書きたい点の配列があります。コードの関連部分は以下のとおりです。シェイプファイルに書き込む
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
からfield
とrecords
行を削除した場合::
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')
は、その後、私はAssertionErrorがを得る
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)
これはエラーが発生しますファイルからレコードを読み取るとき:
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:
ループを削除して1つのレコードを書き込んだとき、正常に動作したようです。どうしたの?
これは、ありがとう!私はそれを見ていないと信じることはできませんが、私は何時間も見ていますが... – David
あなたは大歓迎です! –