Help please, using Python to iterate though a directory. For each file create event l

المشرف العام

Administrator
طاقم الإدارة
I am relatively new to Python for ArcGIS, but have been using ArcGIS a while.I have a large directory of tables with point data. For each file of each folder I want to create a kernel density layer.I have managed to write a script that finds all the files (using os.walk) and creates an event layer however the resulting kernel density raster is blank, i.e. all cells have the same value.When I use the GUI to create the kernel density, using the event layers that I created with the script the kernel density layer comes out beautifully.

I also think there is something wrong with the way I have set up my coordinate system, but I'm not sure. Currently trying to use a geographic (lat, lon) system.

Below is my script, any help would be much appreciated.

# import system modules import arcpyimport osfrom arcpy.sa import *# Set environment settingsarcpy.env.workspace = r"D:\phythonPrj\GIS_stuff"def createXYlayer(inTable, saved_layer, number, directory): try: in_Table = inTable x_coords = "pointX" y_coords = "pointY" savedLayer = directory + "\\" + saved_layer out_Layer = "table%s" %number infc = inTable sr = arcpy.SpatialReference(28355) spRef = arcpy.DefineProjection_management(infc, sr)# Make the XY event layer... arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef)# Print the total rows print arcpy.GetCount_management(out_Layer)# Save to a layer file arcpy.SaveToLayerFile_management(out_Layer, savedLayer) print(savedLayer, "done")except:# If an error occurred print the message to the screen print arcpy.GetMessages()def findFiles(topDirect): #name of the log file that will be saved in the same directory as this script# The top argument for walk: where you define your directorytopdir = topDirect# The extension to search forexten = '.txt'ignore = ['images', 'scripts', 'processed'] # folders to ignorecount = 0countIn = 0 number = 0for dirpath, dirnames, files in os.walk(topdir):# Remove directories in ignore# directory names must match exactly! for idir in ignore: if idir in dirnames: dirnames.remove(idir) for name in files: number += 1 if name.lower().endswith(exten) and name.lower().startswith("part_"): countIn += 1 inTable = '%s\t' % os.path.join(dirpath, name) savedLayer = "part%s" %countIn createXYlayer(inTable, savedLayer, number, dirpath) elif name.lower().endswith(exten) and name.lower().startswith("out_part"): count += 1 inTable = '%s\t' % os.path.join(dirpath, name) savedLayer = "outPrt%s" %count createXYlayer(inTable, savedLayer, number, dirpath)def findLayers(topDirect): # The top argument for walk: where you define your directorytopdir = topDirect# The extension to search forexten = '.lyr'ignore = ['images', 'scripts', 'processed'] # folders to ignorefor dirpath, dirnames, files in os.walk(topdir):# Remove directories in ignore# directory names must match exactly! for idir in ignore: if idir in dirnames: dirnames.remove(idir) for name in files: if name.lower().endswith(exten) and name.lower().startswith("part"): RunKernel(dirpath + '\\' + name)def RunKernel(feature):inFeatures = featurepopulationField = "group"cellSize = 1.5E-3searchRadius = 5000#out_workspace = r"D:\Modelling\SLIMWhitsunday2014\PhD_Python\sensitivityKernels"exten = ".tif" # extention of output file# Check out the ArcGIS Spatial Analyst extension licensearcpy.CheckOutExtension("Spatial")# Execute KernelDensitytry: outKernelDensity = KernelDensity(inFeatures, populationField, cellSize, searchRadius, "SQUARE_MAP_UNITS") featureName = feature.replace(".lyr", exten) print(featureName)# Save the output outKernelDensity.save(featureName) print (feature, "done")except Exception as err: print(err)def main():try: #findFiles(".") findLayers("directory")except Exception as err: print(err)main()

أكثر...
 
أعلى