import arcpy, os, random from arcpy import env, mapping from arcpy.sa import * def calcZonalHistograms(polygonFile, zoneField, inRaster, outDir = None, outTbls=None, cleanup=1): if outTbls: outHistTbl, outStatTbl = outTbls else: outHistTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneHist_tbl.dbf") outStatTbl = os.path.join(os.path.dirname(inRaster), os.path.basename(inRaster).split(".")[0]+"_zoneStat_tbl.dbf") # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Find frequencies for histogram. This will use 256 bins of equal intervals spanning the range of values in the area. (?) ZonalHistogram(polygonFile, zoneField, inRaster, outHistTbl) # Execute ZonalStatisticsAsTable outZSaT = ZonalStatisticsAsTable(polygonFile, zoneField, inRaster, outStatTbl, "NODATA") # Find bin sizes and values # Put histogram frequencies in a dictionary; key word equal to field name (Poly_Area) fieldList = arcpy.ListFields(outHistTbl) info_dict = {} binSizes = {} for field in fieldList: if field.name == "OID": pass else: info_dict[field.name] = [] rows = arcpy.SearchCursor(outHistTbl) i = 0 for row in rows: for field in fieldList: if field.name != "OID": info_dict[field.name].append(row.getValue(field.name)) i+=1 # Find bin Size from outStatTbl rows = arcpy.SearchCursor(outStatTbl) for row in rows: binSizes[row.getValue("POLY_AREA")] = [row.getValue("MIN"), (float(row.getValue("MAX"))-float(row.getValue("MIN")))/i] # Export to txt for use in other programs (excel, etc...) if not outDir: outDir = os.path.dirname(inRaster) for key in info_dict.keys(): exportHistogramToTxt(binSizes[key][1], binSizes[key][0], info_dict[key], key, os.path.join(outDir, key+"_hist.txt"))
How do I determine the ranges of the bins using python?
import arcpy arcpy.CheckOutExtension("spatial") raster = arcpy.Raster("c:\\workspace\\input_raster") range = raster.maximum - raster.minimum bins = 256 bin_size = (range) / bins # now we know the bin width, can multiply that by the OBJECTID for the lower bound of each bin lower_bin_value = [bin_size * i for i in range(bins)]
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.