将 csv 转换为 shp:多个 csv 的重复 shp
Converting csv to shp: Duplicate shp for multiple csv
我的代码旨在将 .csv 文件转换为 .shp 文件。我没有收到任何错误消息,并且为每个 .csv 创建了一个 .shp、.shx 和 .dbf 文件。但是,当我打开 .dbf 文件时,它们都是为第一个 .csv 创建的 .dbf 的副本。
这是我的代码:
import glob
import csv
import shapefile as shp
Lon, Lat, Count = [],[],[]
for filename in glob.glob('C:/Users/brownk98/Downloads/GLM/Headers/*.csv'):
revFilename =
filename.replace('C:/Users/brownk98/Downloads/GLM/Headers\*.csv', "")
rev2Filename = revFilename.replace(".csv", "")
out_file = rev2Filename + '_temp.shp'
with open(rev2Filename + '.csv') as f:
next(f)
r = csv.reader(f, delimiter=',')
#for i,row in enumerate(r):
for row in r:
#if i > 0: #skip header
Lon.append(float(row[0]))
Lat.append(float(row[1]))
Count.append(int(row[2]))
#f.close()
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('Lon', "F",10,10)
w.field('Lat',"F",10,10)
w.field('Count',"N",10)
for j,k in enumerate(Lat):
w.point(Lon[j],Lat[j]) #write the geometry
w.record(Lon[j], Lat[j], Count[j]) #write the attributes
w.save(out_file)
重复项示例:
第一个文件
Lon Lat Count
-135.0000000000 0.0000000000 13320
-135.1523800000 0.3345030000 3
-135.3058900000 0.6699620000 9
-135.4605200000 1.0063680000 61
-135.6163000000 1.3437130000 99
-135.7732400000 1.6819870000 31
第二个文件(所有其他文件也是这样)
Lon Lat Count
-135.0000000000 0.0000000000 13320
-135.1523800000 0.3345030000 3
-135.3058900000 0.6699620000 9
-135.4605200000 1.0063680000 61
-135.6163000000 1.3437130000 99
-135.7732400000 1.6819870000 31
文件二的 lon、lats 计数应该与文件一不同,但我的代码似乎只是将第一个文件中的计数复制到所有后续文件中。
我怎样才能解决这个问题?如有任何帮助,我们将不胜感激!
Question: ... seems to be copying the counts from the first file into all the subsequent files ...
您必须为每个文件初始化 Lon, Lat, Count = [],[],[]
。
更改以下内容:
...
for filename in glob.glob('C:/Users/brownk98/Downloads/GLM/Headers/*.csv'):
...
with open(rev2Filename + '.csv') as f:
next(f)
r = csv.reader(f, delimiter=',')
Lon, Lat, Count = [],[],[]
for row in r:
...
我的代码旨在将 .csv 文件转换为 .shp 文件。我没有收到任何错误消息,并且为每个 .csv 创建了一个 .shp、.shx 和 .dbf 文件。但是,当我打开 .dbf 文件时,它们都是为第一个 .csv 创建的 .dbf 的副本。
这是我的代码:
import glob
import csv
import shapefile as shp
Lon, Lat, Count = [],[],[]
for filename in glob.glob('C:/Users/brownk98/Downloads/GLM/Headers/*.csv'):
revFilename =
filename.replace('C:/Users/brownk98/Downloads/GLM/Headers\*.csv', "")
rev2Filename = revFilename.replace(".csv", "")
out_file = rev2Filename + '_temp.shp'
with open(rev2Filename + '.csv') as f:
next(f)
r = csv.reader(f, delimiter=',')
#for i,row in enumerate(r):
for row in r:
#if i > 0: #skip header
Lon.append(float(row[0]))
Lat.append(float(row[1]))
Count.append(int(row[2]))
#f.close()
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('Lon', "F",10,10)
w.field('Lat',"F",10,10)
w.field('Count',"N",10)
for j,k in enumerate(Lat):
w.point(Lon[j],Lat[j]) #write the geometry
w.record(Lon[j], Lat[j], Count[j]) #write the attributes
w.save(out_file)
重复项示例: 第一个文件
Lon Lat Count
-135.0000000000 0.0000000000 13320
-135.1523800000 0.3345030000 3
-135.3058900000 0.6699620000 9
-135.4605200000 1.0063680000 61
-135.6163000000 1.3437130000 99
-135.7732400000 1.6819870000 31
第二个文件(所有其他文件也是这样)
Lon Lat Count
-135.0000000000 0.0000000000 13320
-135.1523800000 0.3345030000 3
-135.3058900000 0.6699620000 9
-135.4605200000 1.0063680000 61
-135.6163000000 1.3437130000 99
-135.7732400000 1.6819870000 31
文件二的 lon、lats 计数应该与文件一不同,但我的代码似乎只是将第一个文件中的计数复制到所有后续文件中。 我怎样才能解决这个问题?如有任何帮助,我们将不胜感激!
Question: ... seems to be copying the counts from the first file into all the subsequent files ...
您必须为每个文件初始化 Lon, Lat, Count = [],[],[]
。
更改以下内容:
...
for filename in glob.glob('C:/Users/brownk98/Downloads/GLM/Headers/*.csv'):
...
with open(rev2Filename + '.csv') as f:
next(f)
r = csv.reader(f, delimiter=',')
Lon, Lat, Count = [],[],[]
for row in r:
...