CSV TO SHAPEFILE - Using pyshp
CSV TO SHAPEFILE - Using pyshp
I need a help.
I got some CSV files with geographic information and I need to convert it to SHP... I'm trying the following code, but it's not working..
This is my first time working with python.. I'd really appreciate if you could help me.
import shapefile as shp
import csv
out_file = 'test.shp'
#Set up blank lists for data
lat,lon=,
#read data from csv file and store in lists
with open('input.csv', 'rt') as csvfile:
r = csv.reader(csvfile, delimiter=';')
for i,row in enumerate(r):
if i > 0: #skip header
lat.append((row[0]))
lon.append((row[1]))
#Set up shapefile writer and create empty fields
w = shp.Writer(shp.POINT)
w.autoBalance = 1 #ensures gemoetry and attributes match
w.field('lat','F',10,8)
w.field('lon','F',10,8)
w.field('Date','D')
w.field('Target','C',50)
w.field('ID','N')
#loop through the data and write the shapefile
for j,k in enumerate(x):
w.point(k,y[j]) #write the geometry
w.record(k,y[j],date[j], target[j], id_no[j]) #write the attributes
#Save shapefile
w.save(out_file)
This is what the spyder returns to me:
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.4.0 -- An enhanced Interactive Python.
Traceback (most recent call last):
File "<ipython-input-1-dece8279ec6d>", line 1, in <module>
runfile('C:/Users/gcnxq/Downloads/py/csv.py', wdir='C:/Users/gcnxq/Downloads/py')
File "C:UsersgcnxqAppDataLocalContinuumanaconda3libsite-packagesspyderutilssitesitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:UsersgcnxqAppDataLocalContinuumanaconda3libsite-packagesspyderutilssitesitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/gcnxq/Downloads/py/csv.py", line 18, in <module>
w = shp.Writer(shp.POINT)
File "C:UsersgcnxqDownloadspyshapefile.py", line 1018, in __init__
self.shp = self.__getFileObj(os.path.splitext(target)[0] + '.shp')
File "C:UsersgcnxqAppDataLocalContinuumanaconda3libntpath.py", line 224, in splitext
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not int
1 Answer
1
I was able to accomplish this using arcpy modules MakeXYEventLayer_management followed by CopyFeatures_management. Example below
import arcpy
arcpy.env.workspace = r"C:/home"
csvFile = r"C:/home/some.csv"
in_x_field = "long"
in_y_field = "lat"
out_layer = "csvLayer"
spatial_reference = 4326
csvLayer=arcpy.MakeXYEventLayer_management(csvFile,in_x_field,in_y_field,out_layer,spatial_reference)
arcpy.CopyFeatures_management(csvLayer,"shapename.shp")
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.