csv转换为shp
一、csv文件内容
csv文件中记录了500行气象信息
我们需要做的是利用Fiona包,将csv的内容读取,并导出为shp格式
二、代码
import fiona
from collections import OrderedDict
import csv
from fiona.crs import from_epsg
with open("./2010-01-01.csv", 'r') as csvfile:
reader = csv.reader(csvfile)
rows = [row for row in reader]
#print(rows)
subway_schema = {'geometry': 'Point', 'properties': OrderedDict([('FID','int'), ('Avst', 'float'), ('Arh','float'),('Mws', 'float'),('Pre', 'float')])}
subway_infor = []
for i in range(1, len(rows)):
lon = float(rows[i][1])
lat = float(rows[i][2])
infor = {'geometry':{'type':'Point', 'coordinates':(lon,lat)},
'properties':OrderedDict([('FID', int(rows[i][0])), ('Avst', rows[i][4]), ('Arh', rows[i][5]),('Mws', rows[i][6]),('Pre', rows[i[7])])}
subway_infor.append(infor)
output = fiona.open('./2010-01-01.csv.shp', 'w', encoding = 'utf8', driver= 'ESRI Shapefile',crs = from_epsg(4326), schema = subway_schema)
print(subway_infor)
output.writerecords(subway_infor)
output.close()