更新
今天發現之前的代碼運作會報錯,發現shapefile接口有更新,最近比較忙,沒有去确認是否是因為python版本不同的問題。之前的代碼是基于python2.7的,下面這部分更新後的代碼是基于python3的。
#-*-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('photo_url', 'C', 50) # string, max-length
output_shp.field('longitude', 'F', 10, 8) # float
output_shp.field('latitude', 'F', 10, 8) # float
output_shp.field('scene', 'C', 20) # string, max-length
output_shp.field('scene_id', 'N') # int
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:
photo_url = row[0]
lng= float(row[1])
lat = float(row[2])
scene = row[3]
scene_id = int(row[4])
output_shp.point(lng, lat) # create the point geometry
output_shp.record(photo_url, lng, lat, scene, scene_id) # add attribute data
if counter % 10000 == 0:
print("Feature " + str(counter) + " added to Shapefile.")
counter = counter + 1
except:
print(row)
學地信的經常會有這樣的需求,即将csv格式的檔案轉換為shapefile格式加載到arcMap中進行顯示與分析,arcMap本身提供了這樣的功能,即
檔案->添加資料->添加XY資料:選擇檔案路徑,指定X字段(longitude)、Y字段(latitude)即可,預設坐标系為WGS84坐标系。
arcMap的這個功能非常友善,但因為我的資料量較大,有接近100萬條資料,故通過代碼進行格式轉換效率更高點。
下面的代碼參考如下連結:Using pyshp to convert .csv file to .shp?
需要用到pyshp子產品、csv子產品、codecs子產品,pip install 安裝即可。
下面貼上代碼:
目前隻能轉換point類型資料
#-*-coding:utf-8-*-
import shapefile as shp
import csv
import codecs
import os
def trans_point(folder, fn, idlng, idlat, delimiter=','):
# create a point shapefile
output_shp = shp.Writer(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('id','N') # number
output_shp.field('longitude', 'F', 10, 8) # float
output_shp.field('latitude', 'F', 10, 8) # float
output_shp.field('locname','C',100) # string, max-length
# access the CSV file
with codecs.open(folder + fn, 'rb', 'utf-8') as csvfile:
reader = csv.reader(csvfile, delimiter=delimiter)
# skip the header
next(reader, None)
#loop through each of the rows and assign the attributes to variables
for row in reader:
# idx = row[0]
locname= row[1]
lng= float(row[idlng])
lat = float(row[idlat])
print lng, lat
# create the point geometry
output_shp.point(lng, lat)
# add attribute data
output_shp.record(lng, lat, locname)
output_shp.save(folder + "%s.shp"%fn.split('.')[0]) # save the Shapefile
if __name__ == '__main__':
folder = 'C:\Users\MaMQ\Desktop' + os.sep
fn = 'test.csv'
trans_point(folder, fn, 2, 3)
示例資料
id | locname | x | y |
---|---|---|---|
1 | Haryana, Haryana, India | 76.9806 | 29.6161 |
2 | Mumbai, Maharashtra, India | 72.8258 | 18.975 |
結果
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CXxMGVOJTSU9keJpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DNwQzNwgTN2EjMwkDM4EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
參考資料
[1] Using pyshp to convert .csv file to .shp?