# ------------------------------------------------------------------------------------ # GISClimateMapping.py # Modified and Updated by Rex Morgan; Original Max Min Precip Snow .py by Josh Barnwell # Description: Creates GIS maps for Temperatures, and Rain and Snow Amounts. # ------------------------------------------------------------------------------------ # A Work in progress code 2-17-2013 1020 AM # Removed print statements # User Input of Month, Day, and what Variable to be plotted # Not sure why the test9-1.py failed. I originally believed that to be due to the # non capitalization of the Lat and Long variable but that seems to not be the # case, as I have capitalized the variable. import time import csv import array from time import gmtime, strftime print strftime("%Y-%m-%d %H:%M:%S", gmtime()) print ' ' print ' ARCGIS Climate Data Mapping Program' print ' ' fname = 'Z:\\Desktop\\COOP_rainfall2.csv' # read the data file data_list = [] for line in open(fname): # remove trailing newline char line = line.rstrip() # create a list line_list = line.split(',') data_list.append(line_list) # create a months dictionary with month:index pairs mdict = {} for ix, item in enumerate(data_list[0]): if ix > 8: mdict[item] = ix #Gathering input from user month1=raw_input('Input the 3 letter ID of beginning month ')#getting input data from user month2=raw_input('Input the 3 letter ID of ending month ')#getting input data from user #assigning beginning and ending month here month_start = month1 month_end = month2 #Create a new list new_list = [] outgroup = file("Z:\\Desktop\\test.csv", 'w') #this file is initially in the wrong format. for item in data_list[1:]: station = item[0] lat = item[2] #Remembe to change this value if altering the structure of the input file Z:\\Desktop\\COOP_rainfall2.csv long = item[3] start = mdict[month_start] end = mdict[month_end]+1 plist = [float(x) for x in item[start : end] if x] #having trouble with blanks if plist: mysum = sum(plist) new_list.append([station, lat, long, mysum]) for item in new_list: outgroup.write(str(item)) #this does produce a file with the info contained therein but not quite what I want #new_file.write("%s\n" % item) f= open("Z:\\Desktop\\test2.csv", 'w') # This file needs to be different than the one in line 35 of code. for row in new_list: f.write(','.join(map(str,row))+'\n') new_list.insert(0,['Station', 'Lat', 'Long', 'mysum']) # this ran without errors and did add the values to the beginning of list ## this line added at 2-17-2013 933 AM f= open("Z:\\Desktop\\test3.csv", 'w') for row in new_list: f.write(','.join(map(str,row))+'\n') print ' This is the end of the test6.py file. Now for the rest of it. ' Rex = 'Z:\\Desktop\\test3.csv' ### Hack this part back in Precip=[] #creating an array named Precip inp = open (Rex,"r") # Rex defined in line 22 of code for line in inp.readlines(): line.split(',') Precip.append(line) file.close(inp) ##### The part above hacked back in print ' ' print 'This will take about 2 to 5 minutes...' month = month1 + ' through ' + month2 months = month usermonth = "[" + months + "]" # Import arcpy module import arcpy import arcpy.mapping # Check out any necessary licenses arcpy.CheckOutExtension("spatial") import sys, string, os # Directories tempdir = 'CSV\\' directory = 'N:\\climate\\ClimateSheets\\' + str(tempdir) TopoToR = 'R:\\ArcGIS\\default.gdb\\TopoToR' # Local variables: GGW_Counties = "R:\\fxc\\Western Region FXC Packet\\GGW.shp" Var_layer = months # variable assigned in line 40 of code Var_shp = str(directory) + 'Var.shp' Var_new = str(directory) + 'Varnew.shp' Var_img = str(directory) + 'Var.img' Var100_img = str(directory) + 'Var100.img' Vari100 = str(directory) + 'Vari100' Varcontour_shp = str(directory) + 'Varcontour.shp' Output_stream_polyline_features = "" Output_remaining_sink_point_features = "" Output_diagnostic_file = "" Output_parameter_file = "" TopoToRasterExtent= "-108.891024 46.540403 -104.041596 49.000027" spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" print 'Made it through Local variables' import arcview import sys, string, os, arcgisscripting gp = arcgisscripting.create(10.1) gp.SetProduct("ArcView") # or ArcEditor or ArcInfo gp.overwriteoutput = 1 #arcpy.overwriteOutput = True print 'Made it through import arcview' ### Getting error in the arcpy.MakeXYEventLayer_management ---- "Long" does not extist "Lat" does not exist # Process: Make XY Event Layer arcpy.MakeXYEventLayer_management(Rex, "Long", "Lat", Var_layer, spRef, "") #Rex is defined in line 90 of code as Z:\\Desktop\\test3.csv print 'Make XY Event Layer Done' # Process: Copy Features arcpy.CopyFeatures_management(Var_layer, Var_shp, "", "0", "0", "0") print 'Copy Features Done' # Process: Topo to Raster from arcpy import env env.extent = "-108.891024 46.540403 -104.041596 49.000027" from arcpy.sa import * outTTR = TopoToRaster([TopoPointElevation([[Var_shp, Var_layer]])], "0.003", "", "20", "", "", "ENFORCE", "CONTOUR", "", "", "", "", "", "") outTTR.save(TopoToR) print 'Topo To Raster Done' # Process: Clip arcpy.Clip_management(TopoToR, "-108.891024 46.540403 -104.041596 49.000027", Var_img, GGW_Counties, "", "ClippingGeometry") print 'Clip Done' # Process: Define Projection for Shapefile in order to plot record values correctly ggw_shp = 'R:\\fxc\\Western Region FXC Packet\\GGW.shp' dsc = arcpy.Describe(ggw_shp) coord_sys = dsc.SpatialReference arcpy.DefineProjection_management(Var_shp, coord_sys) print 'Define Projection Done' print strftime("%Y-%m-%d %H:%M:%S", gmtime()) print 'Exporting Map' from datetime import date now = date.today().isoformat() mxd1 = arcpy.mapping.MapDocument(r"R:\EventData\CoopPrecip1024.mxd") mxd3 = arcpy.mapping.MapDocument(r"R:\EventData\CoopPrecip13.mxd") #newtitle = arcpy.mapping.ListLayoutElements(mxd3, "TEXT_ELEMENT", "title")[0] if 'YTD' in months: #months defined in line 20 print 'if statement' newtitle = arcpy.mapping.ListLayoutElements(mxd1, "TEXT_ELEMENT", "title")[0] for lyr in arcpy.mapping.ListLayers(mxd1): if lyr.name == "Var": if lyr.supports("LABELCLASSES"): lblClass = lyr.labelClasses[0] #this is the Default label class. lblClass.expression = usermonth newtitle.text = 'Annual COOP Precipitation Year to Date' arcpy.mapping.ExportToPNG(mxd1, r'R:\\Maps\\' + months + str(now) + '.png', resolution=100) #months defined in line 20 else: print 'else statement' newtitle = arcpy.mapping.ListLayoutElements(mxd3, "TEXT_ELEMENT", "title")[0] for lyr in arcpy.mapping.ListLayers(mxd3): if lyr.name == "Var": if lyr.supports("LABELCLASSES"): lblClass = lyr.labelClasses[0] #this is the Default label class. lblClass.expression = usermonth newtitle.text = 'Monthly COOP Precipitation for ' + month # month defined in line 40 arcpy.mapping.ExportToPNG(mxd3, r'R:\\Maps\\' + months + str(now) + '.png', resolution=100) #months defined in line 20 print 'Map is Complete and located in R:\Maps' print strftime("%Y-%m-%d %H:%M:%S", gmtime())
Traceback (most recent call last):
File "\home\rex.morgan\Desktop\Temp\test9-3.py", line 151, in <module> arcpy.MakeXYEventLayer_management(Rex, "Long", "Lat", Var_layer, spRef, "")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py" line 6322, in MakeXYEventLayer raise e
acrgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000728: Field Long does not exist within table
ERROR 000728: Field Lat does not exist within table
Failed to execute (MakeXYEventlayer).
Solved! Go to Solution.
#! /usr/bin/python # # Format a csv for further processing # #import time import csv import array import decimal decimal.getcontext().prec = 4 from time import gmtime, strftime print strftime("%Y-%m-%d %H:%M:%S", gmtime()) # Create an unordered MON to column number dictionary and get user data mdict = {'MAR': 11, 'FEB': 10, 'AUG': 16, 'SEP': 17, 'APR': 12, 'JUN': 14, 'JUL': 15, 'JAN': 9, 'MAY': 13, 'NOV': 19, 'DEC': 20, 'OCT': 18} month_start = raw_input('Input the 3 letter ID of beginning month: ') month_end = raw_input('Input the 3 letter ID of ending month: ') month_start = month_start.upper() month_end = month_end.upper() mon_layer_name = month_start + ' through ' +month_end user_month = '[' + mon_layer_name + ']' start_num = mdict[month_start] end_num = mdict[month_end]+1 new_list = [['Station', 'Lat', 'Long', 'mysum']] with open('R:\\COOP\\temp\\COOP_rainfall2.csv', 'rb') as csvfile: #with open('Z:\\Desktop\\COOP_rainfall2.csv', 'rb') as csvfile: filereader = csv.reader(csvfile) filereader.next() #skip header line for row in filereader: sta = row[0] lat = row[2] lon = row[3] tot = decimal.Decimal(0) for x in row[start_num:end_num]: if x == '' : x = 0 elif x == 'M': x = 0 tot = tot + decimal.Decimal(x) if tot == 0: continue # No zeros allowed so don't append line else: new_list.append([sta, lat, lon, str(tot)]) with open('R:\\COOP\\temp\\output.csv', 'wb') as csvout: #with open('Z:\\Desktop\\output.csv', 'wb') as csvout: filewriter = csv.writer(csvout) for line in new_list: filewriter.writerow(line) Rex = 'R:\\COOP\\temp\\output.csv' Precip=[] #creating an array named Precip inp = open (Rex,"r") for line in inp.readlines(): line.split(',') Precip.append(line) file.close(inp) COOP = 'R:\\COOP\\temp\\output.csv' # # ARCGIS Processing begins here # # # Determine Max, Min, and Diff Values templist = [] data = open (COOP, 'r') lines = str(data.read()).splitlines() for line in lines: if 'Lat,Long' not in line: line = line.split(',') value = line[int(3)] templist.append(float(value)) maxV = max(templist) minV = min(templist) diff = float(maxV) - float(minV) #print 'MAX ' + str(maxV) #print 'MIN ' + str(minV) #print 'DIFF ' + str(diff) month = month_start + ' through ' + month_end months = month usermonth = "[" + months + "]" # Import arcpy module import arcpy import arcpy.mapping # Check out any necessary licenses arcpy.CheckOutExtension("spatial") import sys, string, os # Directories ClipShape = "R:/fxc/Western Region FXC Packet/GGW.shp" webdir = 'R:/COOPImages/' tempdir = 'CSV\\' directory = 'N:\\climate\\ClimateSheets\\' + str(tempdir) # Local variables: GGW_Counties = "R:\\fxc\\Western Region FXC Packet\\GGW.shp" Var_layer = 'mysum' Var_shp = str(directory) + 'Var.shp' spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" print 'Made it through Local variables' # Import Arcview import arcview import sys, string, os, arcgisscripting gp = arcgisscripting.create(10.1) gp.SetProduct("ArcView") # or ArcEditor or ArcInfo gp.overwriteoutput = 1 arcpy.overwriteOutput = True print 'Made it through import arcview' # Process: Make XY Event Layer long = 'Long' lat = 'Lat' arcpy.MakeXYEventLayer_management(COOP, long, lat, Var_layer, spRef, "") #COOP is defined in line 103 of code as Z:\\Desktop\\test3.csv #edited 3-15-2013 print 'Make XY Event Layer Done' # Process: Copy Features arcpy.CopyFeatures_management(Var_layer, Var_shp, "", "0", "0", "0") print 'Copy Features Done' # created the COOP folder on the R drive COOPdir = 'R:/COOP/' COOPtempdir = COOPdir + 'temp/' contour = 'Y' # Conduct remaining code if grid contains values greater than zero if float(maxV) > 0.00: # Execute Interpolation outRaster = COOPtempdir + 'Pre' outContourRaster = COOPtempdir + 'Pre' + 'CRas' from arcpy import env env.extent = "-108.891024 46.540403 -104.041596 49.000024" from arcpy.sa import * #make sure 'mysum' is named correctly when copying over outTTR = TopoToRaster([TopoPointElevation([[Var_shp, 'mysum']])], "0.01", "", "20", "", "", "NO_ENFORCE", "SPOT", "", "", "", "", "", "") #make sure 'mysum' is named correctly outTTR.save(outRaster) outTTR = TopoToRaster([TopoPointElevation([[Var_shp, 'mysum']])], "0.1", "", "20", "", "", "NO_ENFORCE", "SPOT", "", "", "", "", "", "") outTTR.save(outContourRaster) print 'Interpolation Done' #Execute Clip of Raster FinalGridImage = COOPtempdir + 'PrecipTotal.img' arcpy.Clip_management(outRaster, "", FinalGridImage, ClipShape, "", "ClippingGeometry") print 'Raster Clip Done' #Calculate Contour Interval and Layer Value # Contour Interval if contour == 'Y': if float(diff) < 0.25: interval = '0.01' if float(diff) >= 0.25 and float(diff) <= 0.50: interval = '0.05' if float(diff) >= 0.50 and float(diff) <= 1.00: interval = '0.10' if float(diff) >= 1.01 and float(diff) <= 3.00: interval = '0.25' if float(diff) >= 3.01 and float(diff) <= 5.00: interval = '0.50' if float(diff) >= 5.01: interval = '1.00' # Layer Value if float(maxV) < 0.25: imagelayer = 'Precip_0.25' if float(maxV) > 0.25 and float(maxV) <= 0.50: imagelayer = 'Precip_0.50' if float(maxV) > 0.50 and float(maxV) <= 1.00: imagelayer = 'Precip_1' if float(maxV) > 1.00 and float(maxV) <= 2.00: imagelayer = 'Precip_2' if float(maxV) > 2.00 and float(maxV) <= 3.00: imagelayer = 'Precip_3' if float(maxV) > 3.00 and float(maxV) <= 4.00: imagelayer = 'Precip_4' if float(maxV) > 4.00 and float(maxV) <= 6.00: imagelayer = 'Precip_6' if float(maxV) > 6.00 and float(maxV) <= 8.00: imagelayer = 'Precip_8' if float(maxV) > 8.00 and float(maxV) <= 10.00: imagelayer = 'Precip_10' if float(maxV) > 10.00: imagelayer = 'Precip_12' if contour == 'Y': # Execute Contours print 'Contouring Using Interval: ' + str(interval) outContours = COOPtempdir + 'PrecipContours.shp' # still need to create the PrecipContours.shp file Contour(outContourRaster, outContours, interval, '0') # Assign mxd variable # Created the PrecipTotal.mxd file mxd1 = arcpy.mapping.MapDocument(COOPdir + 'PrecipTotal.mxd') # Activate Correct Layer print 'Image Layer: ' + imagelayer for lyr in arcpy.mapping.ListLayers(mxd1): layername = str(lyr) if str(imagelayer) in layername: print 'Using Scale: ' + str(layername) lyr.visible = True break print '-Exporting Precip Graphic-' outputpathfull = webdir + months + ".png" arcpy.mapping.ExportToPNG(mxd1, outputpathfull, resolution=100) # Process: Define Projection for Shapefile in order to plot record values correctly ggw_shp = 'R:\\fxc\\Western Region FXC Packet\\GGW.shp' dsc = arcpy.Describe(ggw_shp) coord_sys = dsc.SpatialReference arcpy.DefineProjection_management(Var_shp, coord_sys) print 'Define Projection Done' print 'Exporting Map' from datetime import date now = date.today().isoformat() mxd1 = arcpy.mapping.MapDocument(COOPtempdir + 'PrecipTotal.mxd') #newtitle = arcpy.mapping.ListLayoutElements(mxd3, "TEXT_ELEMENT", "title")[0] print 'Map is complete and located at ' + outputpathfull
#! /usr/bin/python # # Format a csv for further processing # #import time import csv import array import decimal decimal.getcontext().prec = 4 from time import gmtime, strftime print strftime("%Y-%m-%d %H:%M:%S", gmtime()) # Create an unordered MON to column number dictionary and get user data mdict = {'MAR': 11, 'FEB': 10, 'AUG': 16, 'SEP': 17, 'APR': 12, 'JUN': 14, 'JUL': 15, 'JAN': 9, 'MAY': 13, 'NOV': 19, 'DEC': 20, 'OCT': 18} month_start = raw_input('Input the 3 letter ID of beginning month: ') month_end = raw_input('Input the 3 letter ID of ending month: ') month_start = month_start.upper() month_end = month_end.upper() mon_layer_name = month_start + ' through ' +month_end user_month = '[' + mon_layer_name + ']' start_num = mdict[month_start] end_num = mdict[month_end]+1 new_list = [['Station', 'Lat', 'Long', 'mysum']] with open('R:\\COOP\\temp\\COOP_rainfall2.csv', 'rb') as csvfile: #with open('Z:\\Desktop\\COOP_rainfall2.csv', 'rb') as csvfile: filereader = csv.reader(csvfile) filereader.next() #skip header line for row in filereader: sta = row[0] lat = row[2] lon = row[3] tot = decimal.Decimal(0) for x in row[start_num:end_num]: if x == '' : x = 0 elif x == 'M': x = 0 tot = tot + decimal.Decimal(x) if tot == 0: continue # No zeros allowed so don't append line else: new_list.append([sta, lat, lon, str(tot)]) with open('R:\\COOP\\temp\\output.csv', 'wb') as csvout: #with open('Z:\\Desktop\\output.csv', 'wb') as csvout: filewriter = csv.writer(csvout) for line in new_list: filewriter.writerow(line) Rex = 'R:\\COOP\\temp\\output.csv' Precip=[] #creating an array named Precip inp = open (Rex,"r") for line in inp.readlines(): line.split(',') Precip.append(line) file.close(inp) COOP = 'R:\\COOP\\temp\\output.csv' # # ARCGIS Processing begins here # # # Determine Max, Min, and Diff Values templist = [] data = open (COOP, 'r') lines = str(data.read()).splitlines() for line in lines: if 'Lat,Long' not in line: line = line.split(',') value = line[int(3)] templist.append(float(value)) maxV = max(templist) minV = min(templist) diff = float(maxV) - float(minV) #print 'MAX ' + str(maxV) #print 'MIN ' + str(minV) #print 'DIFF ' + str(diff) month = month_start + ' through ' + month_end months = month usermonth = "[" + months + "]" # Import arcpy module import arcpy import arcpy.mapping # Check out any necessary licenses arcpy.CheckOutExtension("spatial") import sys, string, os # Directories ClipShape = "R:/fxc/Western Region FXC Packet/GGW.shp" webdir = 'R:/COOPImages/' tempdir = 'CSV\\' directory = 'N:\\climate\\ClimateSheets\\' + str(tempdir) # Local variables: GGW_Counties = "R:\\fxc\\Western Region FXC Packet\\GGW.shp" Var_layer = 'mysum' Var_shp = str(directory) + 'Var.shp' spRef = r"Coordinate Systems\Geographic Coordinate Systems\World\WGS 1984.prj" print 'Made it through Local variables' # Import Arcview import arcview import sys, string, os, arcgisscripting gp = arcgisscripting.create(10.1) gp.SetProduct("ArcView") # or ArcEditor or ArcInfo gp.overwriteoutput = 1 arcpy.overwriteOutput = True print 'Made it through import arcview' # Process: Make XY Event Layer long = 'Long' lat = 'Lat' arcpy.MakeXYEventLayer_management(COOP, long, lat, Var_layer, spRef, "") #COOP is defined in line 103 of code as Z:\\Desktop\\test3.csv #edited 3-15-2013 print 'Make XY Event Layer Done' # Process: Copy Features arcpy.CopyFeatures_management(Var_layer, Var_shp, "", "0", "0", "0") print 'Copy Features Done' # created the COOP folder on the R drive COOPdir = 'R:/COOP/' COOPtempdir = COOPdir + 'temp/' contour = 'Y' # Conduct remaining code if grid contains values greater than zero if float(maxV) > 0.00: # Execute Interpolation outRaster = COOPtempdir + 'Pre' outContourRaster = COOPtempdir + 'Pre' + 'CRas' from arcpy import env env.extent = "-108.891024 46.540403 -104.041596 49.000024" from arcpy.sa import * #make sure 'mysum' is named correctly when copying over outTTR = TopoToRaster([TopoPointElevation([[Var_shp, 'mysum']])], "0.01", "", "20", "", "", "NO_ENFORCE", "SPOT", "", "", "", "", "", "") #make sure 'mysum' is named correctly outTTR.save(outRaster) outTTR = TopoToRaster([TopoPointElevation([[Var_shp, 'mysum']])], "0.1", "", "20", "", "", "NO_ENFORCE", "SPOT", "", "", "", "", "", "") outTTR.save(outContourRaster) print 'Interpolation Done' #Execute Clip of Raster FinalGridImage = COOPtempdir + 'PrecipTotal.img' arcpy.Clip_management(outRaster, "", FinalGridImage, ClipShape, "", "ClippingGeometry") print 'Raster Clip Done' #Calculate Contour Interval and Layer Value # Contour Interval if contour == 'Y': if float(diff) < 0.25: interval = '0.01' if float(diff) >= 0.25 and float(diff) <= 0.50: interval = '0.05' if float(diff) >= 0.50 and float(diff) <= 1.00: interval = '0.10' if float(diff) >= 1.01 and float(diff) <= 3.00: interval = '0.25' if float(diff) >= 3.01 and float(diff) <= 5.00: interval = '0.50' if float(diff) >= 5.01: interval = '1.00' # Layer Value if float(maxV) < 0.25: imagelayer = 'Precip_0.25' if float(maxV) > 0.25 and float(maxV) <= 0.50: imagelayer = 'Precip_0.50' if float(maxV) > 0.50 and float(maxV) <= 1.00: imagelayer = 'Precip_1' if float(maxV) > 1.00 and float(maxV) <= 2.00: imagelayer = 'Precip_2' if float(maxV) > 2.00 and float(maxV) <= 3.00: imagelayer = 'Precip_3' if float(maxV) > 3.00 and float(maxV) <= 4.00: imagelayer = 'Precip_4' if float(maxV) > 4.00 and float(maxV) <= 6.00: imagelayer = 'Precip_6' if float(maxV) > 6.00 and float(maxV) <= 8.00: imagelayer = 'Precip_8' if float(maxV) > 8.00 and float(maxV) <= 10.00: imagelayer = 'Precip_10' if float(maxV) > 10.00: imagelayer = 'Precip_12' if contour == 'Y': # Execute Contours print 'Contouring Using Interval: ' + str(interval) outContours = COOPtempdir + 'PrecipContours.shp' # still need to create the PrecipContours.shp file Contour(outContourRaster, outContours, interval, '0') # Assign mxd variable # Created the PrecipTotal.mxd file mxd1 = arcpy.mapping.MapDocument(COOPdir + 'PrecipTotal.mxd') # Activate Correct Layer print 'Image Layer: ' + imagelayer for lyr in arcpy.mapping.ListLayers(mxd1): layername = str(lyr) if str(imagelayer) in layername: print 'Using Scale: ' + str(layername) lyr.visible = True break print '-Exporting Precip Graphic-' outputpathfull = webdir + months + ".png" arcpy.mapping.ExportToPNG(mxd1, outputpathfull, resolution=100) # Process: Define Projection for Shapefile in order to plot record values correctly ggw_shp = 'R:\\fxc\\Western Region FXC Packet\\GGW.shp' dsc = arcpy.Describe(ggw_shp) coord_sys = dsc.SpatialReference arcpy.DefineProjection_management(Var_shp, coord_sys) print 'Define Projection Done' print 'Exporting Map' from datetime import date now = date.today().isoformat() mxd1 = arcpy.mapping.MapDocument(COOPtempdir + 'PrecipTotal.mxd') #newtitle = arcpy.mapping.ListLayoutElements(mxd3, "TEXT_ELEMENT", "title")[0] print 'Map is complete and located at ' + outputpathfull