My script is supposed to pull weekly flu data from a department of health website, parse it for that week, and then push the data up to our ArcSDE server so that it can be displayed on our Web Mapping application. The data is in a .csv format and the script parses the data just fine, but when it comes to doing a join with a feature class with county outlines I get a
Here is my code
import osimport arcpyfrom arcpy import envimport mathimport numpyimport csvimport urllib2import tempfile#working directoryfludirectory = 'C:\\Users\\username\\Desktop\\Projects\\flu_mapping\\GIS_data\\'#pull flu data from ND Department of healthfludata = urllib2.urlopen ('http://www.ndflu.com/data/weekenddata.txt')#arcpy workspacearcpy.env.workspace = "in_memory"#load data so that numpy can use itrows = numpy.loadtxt(fludata, int, delimiter=",",skiprows=4, usecols =(2,3,4,5,6,7,8,9))#parse the data and write it to the CSV filecreatecsv = fludirectory + "\\" + 'output_data.csv'outputcsv = open (createcsv, 'wb')week = max(rows [:,0])with outputcsv as csvfile: flucsv = csv.writer(csvfile, delimiter=",", quotechar='|', quoting=csv.QUOTE_MINIMAL) flucsv.writerow (["Statecode","flu_num"]) for row in rows: if row[0] == week: countid = row[1] totalflu = sum(row[2:]) csvoutput = '%s, %s' %(countid, totalflu) print csvoutput flucsv.writerow ([countid, totalflu]) outputcsv.close#make a copy of the county dataset#county datasetcountydata = fludirectory + "\\" + 'county_ID_code.shp'#make a temporary feature layerarcpy.MakeFeatureLayer_management (countydata, "memorycounty")#join output csvoutput to the in_memory shapefilearcpy.JoinField_management ("memorycounty", "ID", outputcsv, "Statecode",)#test the scriptarcpy.FeatureClassToFeatureClass_conversion ("memorycounty", fludirectory, 'testoutput.shp')Also at the end I am using Feature Class to Feature Class to see if everything worked (obviously it's not) and that will be replaced by code that pushes the feature class to the ArcSDE server.
أكثر...
Traceback (most recent call last): File "C:\Users\username\Desktop\Projects\flu_mapping\script\flu_data0.6.0.py", >line 55, in arcpy.JoinField_management ("memorycounty", "ID", outputcsv, "Statecode",) File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 5383, >in JoinField raise e RuntimeError: Object: Error in executing tool
This wasn't an issues until I started using the open() command to create a new .csv file to parse the data to, instead of manually making one.
Here is my code
import osimport arcpyfrom arcpy import envimport mathimport numpyimport csvimport urllib2import tempfile#working directoryfludirectory = 'C:\\Users\\username\\Desktop\\Projects\\flu_mapping\\GIS_data\\'#pull flu data from ND Department of healthfludata = urllib2.urlopen ('http://www.ndflu.com/data/weekenddata.txt')#arcpy workspacearcpy.env.workspace = "in_memory"#load data so that numpy can use itrows = numpy.loadtxt(fludata, int, delimiter=",",skiprows=4, usecols =(2,3,4,5,6,7,8,9))#parse the data and write it to the CSV filecreatecsv = fludirectory + "\\" + 'output_data.csv'outputcsv = open (createcsv, 'wb')week = max(rows [:,0])with outputcsv as csvfile: flucsv = csv.writer(csvfile, delimiter=",", quotechar='|', quoting=csv.QUOTE_MINIMAL) flucsv.writerow (["Statecode","flu_num"]) for row in rows: if row[0] == week: countid = row[1] totalflu = sum(row[2:]) csvoutput = '%s, %s' %(countid, totalflu) print csvoutput flucsv.writerow ([countid, totalflu]) outputcsv.close#make a copy of the county dataset#county datasetcountydata = fludirectory + "\\" + 'county_ID_code.shp'#make a temporary feature layerarcpy.MakeFeatureLayer_management (countydata, "memorycounty")#join output csvoutput to the in_memory shapefilearcpy.JoinField_management ("memorycounty", "ID", outputcsv, "Statecode",)#test the scriptarcpy.FeatureClassToFeatureClass_conversion ("memorycounty", fludirectory, 'testoutput.shp')Also at the end I am using Feature Class to Feature Class to see if everything worked (obviously it's not) and that will be replaced by code that pushes the feature class to the ArcSDE server.
أكثر...