1

I have DEM.tif (symbology uses equale interval - 100m) and a polygon file (regions.shp) with many regions (names are defined in 'reg_name' column). I would like to use Zonal Histogram to produce tables (and later xlxs files) with two columns: first one should be class (200-300,300-400 etc.) and the second one the frequency. I have DEM.tif and regions.shp in my map document and if I use code in python window for one region (without using a for loop) it works. But when I want to run the code to generate tables for each region it doesn't work! The main problem is probably because after the lineextracted_raster=arcpy.sa.ExtractByMask(in_raster, lyr) the 'extracted_raster' does not actually show in table of contents or in data view. Thus arcpy.ApplySymbologyFromLayer_management("extracted_raster",in_raster) cannot be applied. I noticed that if I run the code only up to arcpy.ApplySymbologyFromLayer_management("extracted_raster",in_raster) the 'extracted_raster' is shown in my TOC only after the last one iteration (the last region in the list). All 'extracted_raster' rasters before the last region are not shown... How can I make them appear? I'm using arcpy.RefreshActiveView() but it doesn't show the raster... And I know that Zonal Histogram produces table with columns for all regions at once, but I'd like to get tables with actuall min and max in a table for each region (without classes with frequency zero before and after). Here is my script:

#Set MapDocument to "CURRENT"...
mxd=arcpy.mapping.MapDocument("CURRENT")

#Defining parameters...
in_raster='DEM.tif' #Raster to be extracted and used for histogram data production...
zone_layer='regions.shp' #This is a layer with regions...
zone_field='reg_name' #The name of the field with names of the regions...


#Code for listing names of regions...
def getValueList (inputTable, field):
    values = set()
    rows = arcpy.SearchCursor(inputTable)
    for row in rows:
            values.add(row.getValue(field))
    return sorted(values)
arcpy.AddMessage("Koda getValuesList je definirana!")

#Listing names of regions...
list=getValueList(zone_layer, zone_field)
lyr=arcpy.mapping.Layer(zone_layer)

where_clause=(str(zone_field))+"="

#Loop for
for zone in list:
    lyr.definitionQuery=where_clause+"'"+str(zone)+"'"
    arcpy.RefreshActiveView()
    extracted_raster=arcpy.sa.ExtractByMask(in_raster, lyr)
    arcpy.RefreshActiveView()
    arcpy.ApplySymbologyFromLayer_management("extracted_raster",in_raster)
    output_table="histogram_data"+str(zone)+".dbf"
    arcpy.sa.ZonalHistogram(lyr,zone_field,"extracted_raster",output_table)
print "Done!

"

0

2 Answers 2

0

Couple of thoughts... I think you should follow the example here and save your extracted data if you want it to be retained. I don't think it saves otherwise, thus it is overwritten when you iterate through the for loop. You could use the same method you have for naming the table by adding your zone to the raster path name. Below from Arc's help.

outExtractByMask = ExtractByMask("elevation", "mask.shp")
outExtractByMask.save("C:/sapyexamples/output/maskextract")

You could use MakeRasterLayer_management from the extracted rasters, which will create temporary layers from in your map document. Then you could apply your symbology.

0

Here is my code that works. The result are not shown in my TOC or Data view (I thought this is necessary for ApplySimbology.. tool), but it works. I made a script that can be used as a tool.

import arcpy
from string import maketrans
arcpy.CheckOutExtension("Spatial")


in_raster=arcpy.GetParameterAsText(0)
zone_layer=arcpy.GetParameterAsText(1)
zone_field=arcpy.GetParameterAsText(2)
files_folder=arcpy.GetParameterAsText(3)
excel_files_folder=arcpy.GetParameterAsText(4)
tmpraster_folder=arcpy.GetParameterAsText(5)
tmpshp_buffer=arcpy.GetParameterAsText(6)

def getValueList (inputTable, field):
    values = set()
    rows = arcpy.SearchCursor(inputTable)
    for row in rows:
            values.add(row.getValue(field))
    return sorted(values)

list=getValueList(zone_layer, zone_field)
lyr=arcpy.mapping.Layer(zone_layer)

where_clause=(str(zone_field))+"="

inchar = " "
outchar = "_"
change = maketrans(inchar, outchar)

x=1
for zone in list:

    name_raster=str(x)+str(zone)[:7]
    strzone=str(zone)

    zone_name_corr=strzone.translate(change)
    lyr.definitionQuery=where_clause+"'"+str(zone)+"'"

    name_buffer=tmpshp_buffer+"\\"+name_raster+"_b50.shp"
    arcpy.Buffer_analysis(lyr, name_buffer, 50)
    extracted_raster=arcpy.sa.ExtractByMask(in_raster, name_buffer)
    extracted_raster.save(tmpraster_folder+"\\"+name_raster)
    raster_lyr=str(zone)+"ly"
    arcpy.MakeRasterLayer_management(tmpraster_folder+"\\"+name_raster, raster_lyr)
    arcpy.RefreshActiveView()
    arcpy.ApplySymbologyFromLayer_management(raster_lyr,in_raster)

    output_table=files_folder+"\\histogram_data"+zone_name_corr+".dbf"

    arcpy.sa.ZonalHistogram(lyr,zone_field,raster_lyr,output_table)

    x=x+1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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