Dan,
Thanks again for the suggestions. The try-except statement from my initial script had some careless errors on my part. I've now fixed this. Here is the script again with the corrections made.
import sys
import os
import numpy as np
import pandas as pd
import pygal as pg
import arcpy
arcpy.env.overwriteOutput = True
arcpy.env.outputCoordinateSystem = arcpy.SpatialReference(3857)
def plotFAO(df, iso_code, fao_indicator, itemCodeFS, countrySelection, outputLocation):
arcpy.AddMessage("IN FAO LOOP")
df_selected = df[df["Item Code"] == itemCodeFS]
customColorStyle = pg.style.Style(colors = ['
arcpy.AddMessage("added Style")
if itemCodeFS == 21010:
line_chart = pg.Line(title = u"Average Dietary Energy Supply Adequacy - \n" + countrySelection, x_title = "End Year of '3-Year Average'", y_title = "% - 3-Year Average", style = customColorStyle, show_dots = False, stroke_style = {'width': 3}, show_minor_y_labels = True)
line_chart.x_labels = map(str, range(2001, 2018))
country_series = df_selected.loc[df_selected["ISO_code"] == iso_code, "Value"]
low_income_series = df_selected.loc[df_selected["ISO_code"] == "lowestIncome", "Value"]
mid_lower_income_series = df_selected.loc[df_selected["ISO_code"] == "midLowerIncome", "Value"]
mid_upper_income_series = df_selected.loc[df_selected["ISO_code"] == "midUpperIncome", "Value"]
high_income_series = df_selected.loc[df_selected["ISO_code"] == "uppermostIncome", "Value"]
line_chart.add(countrySelection, country_series)
line_chart.add("Low-Income", low_income_series)
line_chart.add("Lower-Middle-Income", mid_lower_income_series)
line_chart.add("Upper-Middle-Income", mid_upper_income_series)
line_chart.add("High-Income", high_income_series)
line_chart.render_to_file(os.path.join(outputLocation, 'test_pygal3.svg'))
def foodSecurity_v1():
fao_clean = "C:\\test\\foodSecurity\\fao_clean.csv"
country_iso_csv = "C:\\test\\foodSecurity\\FAO_Area_Export.csv"
out_src_FeatureSet = r"in_memory\src_feature_set"
src_FeatureSet = arcpy.GetParameterAsText(0)
countrySelection = arcpy.GetParameterAsText(1)
fao_indicator = arcpy.GetParameterAsText(2)
outputLocation = arcpy.GetParameterAsText(3)
try:
arcpy.MakeFeatureLayer_management(src_FeatureSet, out_src_FeatureSet)
aoi = arcpy.CopyFeatures_management(out_src_FeatureSet, os.path.join(arcpy.env.scratchFolder, "AOI_layer.shp"))
df_iso = pd.read_csv(country_iso_csv)
countryDF = df_iso[df_iso["Area"] == countrySelection]
iso_code = countryDF["ISO_code"].iloc[0]
arcpy.AddMessage("country: " + countrySelection + ", code: " + iso_code)
arcpy.AddMessage(iso_code)
if fao_indicator == "Average dietary energy supply adequacy (%) (3-year average)":
itemCodeFS = 21010
if fao_indicator == "Average value of food production (constant I$ per person) (3-year average)":
itemCodeFS = 21011
if fao_indicator == "Share of dietary energy supply derived from cereals, roots and tubers (%) (3-year average)":
itemCodeFS = 21012
if fao_indicator == "Average protein supply (g/capita/day) (3-year average)":
itemCodeFS = 21013
if fao_indicator == "Average supply of protein of animal origin (g/capita/day) (3-year average)":
itemCodeFS = 21014
if fao_indicator == "Rail-lines density (per 100 square km of land area)":
itemCodeFS = 21016
if fao_indicator == "Gross domestic product per capita, PPP (constant 2011 international $)":
itemCodeFS = 22013
if fao_indicator == "Prevalence of undernourishment (%) (3-year average)":
itemCodeFS = 21004
if fao_indicator == "Depth of the food deficit (kcal/capita/day) (3-year average)":
itemCodeFS = 21023
df = pd.read_csv(fao_clean, dtype = {"Area": "S100", "Area Code": "f8", "Element": "S25", "Element Code": "f8", "Flag": "S25",
"Flag Description": "S25", "ISO_code": "S25", "Item": "S150", "Item Code": "i4", "Unit": "S25",
"Value": "f8", "Year": "S25", "Year Code": "f8", "YearEnd": "i4"})
plotFAO(df, iso_code, fao_indicator, itemCodeFS, countrySelection, outputLocation)
arcpy.SetParameter(4, aoi)
except Exception:
e = sys.exc_info()[1]
arcpy.AddError('An error occurred: {}'.format(e.args[0]))
if __name__ == '__main__':
foodSecurity_v1()
I've tried several ways to correct this issue where the tool fails the second time it is run, including removing the try-except statement, rearranging the order in which the Pygal module is imported, and placing the script and all csv files on which it relies in a folder where all spaces in the path have been removed.
Unfortunately, the process still fails the second time the tool is run. So, when I run it the first time, I'm able to produce the FAO food security plot, but the second time I run the tool, I get the same 'NoneType' error. When I restart ArcMap, I'm able to run the tool again (but it then fails the second time the tool is run).
I've tried to isolate the problem, and what I've discovered is that when I comment out 'import pygal as pg' from the module imports at the beginning of the script, the tool will run as many times in succession as I like. But as soon as 'import pygal as pg' is included in my script, it fails the second time the tool runs. So, it isn't a particular Pygal function that is causing the tool to fail, it is the importing of Pygal itself. However, as you suggested, rearranging the order in which the module is imported does not seem to fix the issue.