Is there a python script for XY spreadsheet to geodatabase?

3686
8
Jump to solution
01-03-2017 11:00 AM
BETHHultman
New Contributor

I found the XY spreadsheet to layer file python script, but I would like to take a csv and create the feature class in a geodatabase.  Is that possible?

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

Once you've made your feature layer, run CopyFeatures to save it to a feature class (I save to shapefile below, but you can save it to a gdb):

>>> my_csv = r'C:\junk\csv.csv'
... lyr = arcpy.MakeXYEventLayer_management(my_csv,'x','y','lyr')
... fc = arcpy.CopyFeatures_management(lyr,r'C:\junk\outshp.shp')

View solution in original post

8 Replies
DarrenWiens2
MVP Honored Contributor

Once you've made your feature layer, run CopyFeatures to save it to a feature class (I save to shapefile below, but you can save it to a gdb):

>>> my_csv = r'C:\junk\csv.csv'
... lyr = arcpy.MakeXYEventLayer_management(my_csv,'x','y','lyr')
... fc = arcpy.CopyFeatures_management(lyr,r'C:\junk\outshp.shp')
BETHHultman
New Contributor

Perfect, thanks!

0 Kudos
curtvprice
MVP Esteemed Contributor

If you don't specify a coordinate system up front with the Make XY Event Layer tool, do so after you've created the feature class with Define Projection. This coordinate system is the coordinate system of the XY in the table (often GCS NAD83 or WGS84), not your ArcMap data frame.

In Python the eaisiest way to specify this is with the WKID:

SR = arcpy.SpatialReference(4269) # GCS NAD83
lyr = arcpy.MakeXYEventLayer_management(my_csv,'x','y','lyr',SR)‍‍
RebeccaStrauch__GISP
MVP Emeritus

thanks Blake, I was looking for those this morning.  They are hard to find in the help it seems.

Another site of interest Coordinate system - GIS Wiki | The GIS Encyclopedia 

although some of the links are old (to ArcGIS 9.3. help docs)

0 Kudos
BrittneyWhite1
Esri Contributor

Another trick is to search for the coordinate system you want on the Coordinate System tab of the Data Frame Properties in ArcMap. The WKID is included as part of the definition:

WKID

curtvprice
MVP Esteemed Contributor

And http://spatialreference.org -- just make sure the code has been tagged an Esri code to ensure it's in the ArcGIS installed database.

I added this link to my first reply above.

RickGeittmann
Occasional Contributor II

This may help - you may have to tweek the code a bit

https://github.com/tgirgin23/csv2gdb