這里用pyshp完成轉換。
坐標糾偏(gcj02轉wgs84)和定義投影(proj.4)酌情操作。
#-*-coding:utf-8-*- import shapefile as shp import csv import codecs import os def trans_point(folder, fn, delimiter=','): '''transfer a csv file to shapefile''' # create a point shapefile output_shp = shp.Writer(folder + "%s.shp"%fn.split('.')[0], shp.POINT) # for every record there must be a corresponding geometry. output_shp.autoBalance = 1 # create the field names and data type for each you can omit fields here # 順序一定要與下面的保持一致 #關鍵步 output_shp.field('PointUid', 'C', 80) # string, max-length #output_shp.field('KeyName', 'C', 20) output_shp.field('BusName', 'C', 70) output_shp.field('StationName', 'C', 50) output_shp.field('LON', 'C', 15) output_shp.field('LAT', 'C', 15) counter = 1 # count the features # access the CSV file with codecs.open(folder + fn, 'rb', 'utf-8') as csvfile: reader = csv.reader(csvfile, delimiter=delimiter) next(reader, None) # skip the header #loop through each of the rows and assign the attributes to variables for row in reader: try: ###只有上海數據有KeyName #關鍵步 PointUid = row[0] #KeyName= row[1] BusName = row[1] StationName = row[2] LON = row[3] LAT = row[4] output_shp.point(float(LON), float(LAT)) # create the point geometry output_shp.record(PointUid,BusName,StationName,LON,LAT) # 關鍵步add attribute data if counter % 10000 == 0: print("Feature " + str(counter) + " added to Shapefile.") counter = counter + 1 except Exception as e: print(e) print(row) #listJS = ["changzhou","huaian","lianyungang","nanjing","nantong","suqian","suzhou","taizhou","wuxi","xuzhou","yancheng","yangzhou","zhenjiang"] for i in listJS: trans_point("E:\\公交站點csv轉shp\\","{}點總量.csv".format(i))
最后結果:
厭世寫手不想再寫py爬蟲了,立個flag,這個號不會再更新爬蟲了。