0

I am trying to import data from a CSV file and write a script that will generate points on a new shapefile. I am running into issues for when I try to update the InsertCursor. I get an error of "not enough quota is available to process this command". I am fairly new to Python; does anyone have any idea of what is wrong with my code?

#Import a csv file and take the coordinates of each ASOS and create a polypoint output
import csv
import arcpy
import os
from arcpy import env

#Lets you write over the shape file if needed
arcpy.env.overwriteOutput = True

targetFolder = "C:\\GEOG485\\FinalProject\\OutPut"
env.workspace = targetFolder

#csv file that we are importing
asosFile = "C:\\GEOG485\\FinalProject\\isd-history.csv"

targetState = 'NJ'

try:
    #Create the featureclass ASOS shapefile, 4326 is the factory code for the required coordinate system of the file
    asosStations = arcpy.CreateFeatureclass_management("C:\\GEOG485\\FinalProject\\","ASOS","POINT",'','',"ENABLED",4326)
    print ("success")
    #add a new field called "Station Name", "CTRY", "STATE" and "END"
    arcpy.AddField_management(asosStations,"STATION_NAME", "TEXT")
    arcpy.AddField_management(asosStations,"CTRY","TEXT")
    arcpy.AddField_management(asosStations,"STATE","TEXT")
    arcpy.AddField_management(asosStations,"ICAO","TEXT")
    arcpy.AddField_management(asosStations,"END","TEXT")
    print ("success")

except:
    print ("error")

#asos dictinary
asos = {}

try:
    #Use the with statement to open the csv file and r to read mode
    with open(asosFile, 'r') as csvv:
        #Reading the csv file with DictReader which takes the first row of the file and uses them as keys
        reader = csv.DictReader(csvv)
        #Loops through the csv file
        for row in reader:
            #Dictionary to see what asos's have a key in the dictionary and append the asos location with the coordinates
            if row['ICAO'] not in asos:
                asos[row['ICAO']] = []
            asos[row['ICAO']].append((row['LAT'],row['LON']))
            print ("success2")
except:
    print ("error2")

try:
    #Takes the points that we just appended to each asos station and adds them to the shapefile
    with arcpy.da.InsertCursor(asosStations, ["ICAO","SHAPE@XY"]) as cur:
        #Loop checks for keys (file headers) and values (asos's inside the dictionary)
        for key,value in asos.items():
            print key,value
            #creates a point with the points for each asos station in the shape file
            for coords in value:
                point = arcpy.Point(*coords) 
                #Takes all the keys with the same names and adds all the associated points to them
                row2 = (key,point)
                cur.insertRow(row2)
                print ("success3")
except:
    print ("error3")

closed as off-topic by Vince, whyzar, Mark Ireland, HDunn, ahmadhanb Apr 22 at 5:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – Vince, whyzar, Mark Ireland, HDunn, ahmadhanb
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    Again with the try: except:... Stop it, seriously! By all means add error handling once your script (or a specific section of script) is working correctly, but until then, just don't. That way you can provide the full exception traceback, formatted as code {} in your question and people don't yell at you for only asking half a question ;). Oh, and just use MakeXYEventLayer and FeatureClassToShapefile as suggested in the answer below. – user2856 Apr 18 at 3:04
  • 3
    And as tone is absent in comments - I'm saying that with a smile and not being narky. – user2856 Apr 18 at 3:39
  • Given the code you've provided, there's no way to tell how that error could be generated, much less where. Using large try/except blocks is incompatible with asking for coding help in GIS SE, since you can't report where the error occurs. – Vince Apr 18 at 10:48
  • I recommend geopandas. Very straightforward to import csv and export to shapefile. – BERA Apr 18 at 13:07
  • no worries, i understand what your saying. took me several tries to get the code to paste right in my post. i appreciate all the tips. sorry about the "try-except" again... :-) – Will Apr 18 at 21:24
3

To create a shapefile from CSV data, I would do something like this:

import arcpy

#  These values based on your script
XFieldName = 'LON'
YFieldName = 'LAT'
outFolder = "C:/GEOG485/FinalProject/OutPut"
spatialRef = arcpy.SpatialReference(4326)
csvFilePath = "C:/GEOG485/FinalProject/isd-history.csv"

arcpy.MakeXYEventLayer_management(csvFilePath, XFieldName, YFieldName, newLayerName, spatial_reference=spatialRef)
arcpy.FeatureClassToShapefile_conversion(newLayerName, outFolder)

In ArcGIS an 'XY event layer' is a layer created from a table (either a database table, a spreadsheet, or a CSV) where the X and Y coordinates are taken from specified columns in that table.

So only two commands are required. Firstly to create the XY event layer from the CSV, and secondly to convert that layer to a shapefile saved on disk.

One problem with this approach is that sometimes ArcGIS can guess the wrong data type for a column. This usually happens if you have an integer in the first row and text or float in some later row. ArcGIS assumes it's an integer columns based on the first row, and then errors out when it comes across some other data that it can't make into an integer.

If you have this issue, just create a 'schema.ini' file in the same location as the CSV file and configure it to define the types for the problem fields. These are just small text files with a "ini" extension. So you can create them in notepad, etc.

For information on how to write schema.ini files, see: https://digimap.edina.ac.uk/webhelp/digimapgis/arcgis/standard_arcgis_functions/importing_text_files_arcgis.htm How to auto-create a schema.ini file for a .csv?

  • Thanks "Son of a Beach"! So I am not running into the "scheme.ini" issue, however run I try to run the SelectLayerByAttribute_management" I get the OID doesn't exist. any suggestions? – Will Apr 19 at 15:30
  • 1
    You will get this error if you try to run the select on the XY event layer. Make sure you are running it on a layer based on the new shape file instead. (You May need to do a MakeFeatureLayer on the shapefile first, then select on this layer). – Son of a Beach Apr 19 at 22:21
  • Almost. In your MakeFeatureLayer, you need to specify the path to the shapefile instead of the path to the folder it is in. – Son of a Beach Apr 20 at 1:49
  • 'arcpy.MakeXYEventLayer_management(csvFilePath, XFieldName, YFieldName, asos, spatialRef)` arcpy.FeatureClassToShapefile_conversion(asos, outFolder) arcpy.MakeFeatureLayer_management(asos, "asos_lyr") arcpy.SelectLayerByAttribute_management("asos_lyr", "NEW_SELECTION", '"STATE" = targetState') arcpy.CopyFeatures_management("asos_lyr", "currentasos.shp")' – Will Apr 20 at 1:52
  • asos is your event layer, not your shapefile. The full path to the shapefile will be outFolder appended with the name of the shapefile (which defaults to asos’ value plus ‘.shp’, I think, but give it a try and see hat the shapefile name is). – Son of a Beach Apr 20 at 2:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.