I'm trying to execute zonal statistics but I'm experiencing an error like this:
C:\Python27\ArcGIS10.5\python.exe "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py"
Running against: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:19:22) [MSC v.1500 32 bit (Intel)]
Running python 32 bit
> v0.3 (dev_gg)
Starting at 20190219155051
Traceback (most recent call last):
File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 179, in <module>
zonal_statistics(*argv)
File "D:/WWF - Sustainable Hydropower/FRA/Code/f1_zonal_stats.py", line 48, in zonal_statistics
set_environment(workspace, fd.masked_layer)
NameError: global name 'fd' is not defined
This is my code:
import csv
import datetime
import os
import sys
import time
from collections import defaultdict
import arcpy
from arcpy.sa import *
def zonal_statistics(*argv):
"""
Calculates the Zonal statistics usinf a raster grid ans a zone grid/vector and extracts the results to a feature class
:param values_path: Path to the values grid
:param zones_path: Path to the zone grid
:param 'out_feature_class': Feature class as target to append data
:param type of statistics. Can be 'sum', 'avg', 'min', or 'max'
:return: adds statistics values to 'out_feature_class'
"""
print ("> v0.3 (dev_gg)")
# Jan 6, 2017:
# Added newly created RDD_B1_URB and NLI_B1_URB
# Limited Nightlights to Urban areas
# Limited Roads to outside urban areas (correlation with urban)
# Changed ABS to CON (Consumption)
# Calculates river reach zonal stats from a set of input layers as defined in 'csv_name'
""" The output layer is defined in the CSV file"""
# Its here: C:\temp\FFR_Results\1_ZonalResults\ZonalResults.gdb\streams_results_ffr
start = time.clock()
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
TIMESTAMP = int(datetime.datetime.fromtimestamp(time.time()).strftime('%Y%m%d%H%M%S'))
print "Starting at %s" % (str(TIMESTAMP))
workspaceFolder = os.path.join(r"C:\temp\FFR_Results\1_ZonalResults", 'Temp')
make_sure_path_exists(workspaceFolder)
workspace = createWorkspace(workspaceFolder, TIMESTAMP)
# Set environment
# csv_name = r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_India_input.csv"
csv_name = r"E:\Dropbox\Projects\FFR\Code\ZonalGridTransfer\FFR_input.csv"
set_environment(workspace, fd.masked_layer)