Hello all!
I have a polygon shapefile. It contains 200 records and a (long) field with reference numbers (type codes) 0, 1, 2 or 3.
I have a point shapefile with 25,000 records. The points fall inside the various polygons throughout the map.
I want to-
Select all points that intersect the polygons that have a type code of 0, and export this selection of points to a new CSV file.
Then repeat this process for ref number = 1, 2 and 3...
4 seperate scripts (one for each ref number) or 1 script doing it all at once: either would be fine.
I want to do all this using the IDLE python GUI rather than going into ArcMap. So I want the script to point to shapefiles on my hard drive raher than layers in the Table Of Contents.
I'm still a noob at python so all I have on my mind so far is:
# Import the required python modules (I still don't quite know what this does)
??? import arcpy ???
??? from arcpy import env ???
# set the workspace environment
??? env.workspace = "C:\Data\" ???
# Select all polygons where type code = 0 and export it to a temporary feature class
arcpy.Select_analysis('C:\Data\Polygon.shp', r'\\somernt\curo\users\faullt\ArcGIS\Default.gdb\Type_code_0_polygon', '"Type_code" = 0')
# select all points that intersect the new 'type code = 0' feature class
arcpy.SelectLayerByLocation_management('C:\Data\Points.shp', 'INTERSECT', 'Scheme- unknown or no restriction', '#', 'NEW_SELECTION', 'NOT_INVERT')
# export the new selection of points to a CSV file in C:\Data
??? ???
# display a completion message
print "CSV created"
The actual lines of python I have typed above are taken from dragging the results in the geoprocessing results pane into an ArcMap python window...
Any help would be much appreciated!
Thanks
Solved! Go to Solution.
I'm typically using the numpy and pandas libraries and they have good export/saveas functionality. Just convert your Feature Class to a numpy array and then save it to csv.
import numpy
import pandas as pd
fc = r'H:\Documents\ArcGIS\Default.gdb\MyFeatureClass'
nparr = arcpy.da.FeatureClassToNumPyArray(fc,['Field1', 'Field2', 'Field3'])
pdarr = pd.DataFrame(nparr)
pdarr.to_csv(r'H:\nparr.csv')
You say :
The points fall inside the various polygons throughout the map
And
Select all points that intersect the polygons that have a type code of 0, and export this selection of points to a new CSV
So you are not doing a join here? No info from the polygons is joined to the points?
So why not just create 4 layers in your map using definition queries, then use the export to excel tool?
If you know nothing about scripting or python, this will be the simpler way.
No I don't want to join the datasets as I don't need any info from the polygon layer.
I just want a script that creates a CSV or Excel file of all points that fall within all the polygons where Type Code = 0 (and then 1, 2, 3)
I'm starting to believe I won't be able to do any of this without opening ArcMap, as the select by location tool can only deal with layer inputs from the Table of Contents, not shapefile inputs from a C drive location.
I'm typically using the numpy and pandas libraries and they have good export/saveas functionality. Just convert your Feature Class to a numpy array and then save it to csv.
import numpy
import pandas as pd
fc = r'H:\Documents\ArcGIS\Default.gdb\MyFeatureClass'
nparr = arcpy.da.FeatureClassToNumPyArray(fc,['Field1', 'Field2', 'Field3'])
pdarr = pd.DataFrame(nparr)
pdarr.to_csv(r'H:\nparr.csv')
James Crandall Excellent advice to use pandas and numpy. If saved me a lot of headache trying to make arcpy.TableToTable_conversion work in a certain out field order.
By the way for others that don't know you can exclude the index added to the front of each row using index=False
pdarr.to_csv(fileName,header=False, index=False)
See also pandas.DataFrame.to_csv — pandas 0.23.3 documentation