【问题标题】:convert text file to shapefile with python使用python将文本文件转换为shapefile
【发布时间】:2021-01-21 10:28:14
【问题描述】:

我有一个 txt 文件,其中包含 5 列(ID 号、城市、纬度、经度、高度)和制表符分隔符。我想用 python 将此 txt 文件转换为 shapefile 格式。 我是python的初学者,我不知道该怎么做。谁能帮帮我?

提前致谢

【问题讨论】:

  • 输出应该是什么?
  • 输出必须是一个 shapefile,其中包含代表每个城市位置的点。每个城市的属性也必须包含在 shapefile 中。

标签: python shapefile txt


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 2018-02-07
    • 2019-03-14
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-09-25
    相关资源
    最近更新 更多