使用python将文本文件转换为shapefile

问题描述

我有一个 txt 文件,其中包含 5 列(ID 号、城市、纬度、经度、高度)和制表符分隔符。我想用python将这个txt文件转换成shapefile格式。 我是 python 的初学者,我不知道如何做到这一点。有人可以帮我吗?

提前致谢

解决方法

有一个图书馆可以做到这一点:

pip install pyshp

导入语句:

import shapefile

现在阅读您的文本文件,例如:

w = shapefile.Writer(shapefile.POINT) #shapefile writer
filepath = 'yourfile.txt' #path to your text file
Lines[]=open(filepath).readlines() # read all the lines of your file
i=0 #index to help us get the headers
for line in Lines: 
   if i==0:
       i+=1
       for j in range len(line.split(";")):
           w.field('field{0}'.format(j)) #get the headers and stock them as fields
   
    w.point(line.split(";")[2],line.split(";")[3],line.split(";")[4]) #create points from last three columns
    w.record(field0=line.split(";")[0],field1=line.split(";")[1]) #add the fields you want to record in the file

w.save('outputshapefile') #write to a shapefile

免责声明!代码未经测试只是为了给您一个想法,您可以找到更多文档 Here