I'm super new on here and have also not much experience with Python but I really like to learn to work with this language. I'm tying to write a script for this:
I think I need a for loop instead the 'Iterate Datasets' from the ModelBuilder but how can I program it with using arcpy.ASCIIToRaster_conversion, arcpy.DefineProjection_management, arcpy.AddField_management, arcpy.CalculateField_management.
This is what I could export from the ModelBulider to a Python script:
# Import arcpy module
import arcpy
# Load required toolboxes
arcpy.ImportToolbox("Modellfunktionen")
arcpy.ImportToolbox("D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx")
# Local variables:
testdatenB = "D:\\Julia_T\\projekt\\MODIS\\karadarya\\2000_neu\\testdatenB"
File_asc = "D:\\Julia_T\\projekt\\MODIS\\karadarya\\2000_neu\\testdatenB\\2000112_cloud_fr=e.asc"
v_Name__output_img = "D:\\Julia_T\\projekt\\erste_schritte\\zweiter_tag_a1\\ergebnisse\\test_c\\ergebnisse\\%Name%_output.img"
v_Name__output_img__2_ = "D:\\Julia_T\\projekt\\erste_schritte\\zweiter_tag_a1\\ergebnisse\\test_c\\ergebnisse\\%Name%_output.img"
v_Name__output_img__3_ = "D:\\Julia_T\\projekt\\erste_schritte\\zweiter_tag_a1\\ergebnisse\\test_c\\ergebnisse\\%Name%_output.img"
# Process: Dateien iterieren
arcpy.IterateFiles_mb(testdatenB, "*", "asc", "NOT_RECURSIVE")
# Process: ASCII in Raster
arcpy.gp.toolbox = "D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx";
# Warning: the toolbox D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx DOES NOT have an alias.
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.ASCIIToRaster(...) with arcpy.ASCIIToRaster_ALIAS(...)
arcpy.gp.ASCIIToRaster(File_asc, v_Name__output_img, "INTEGER")
# Process: Projektion definieren
arcpy.gp.toolbox = "D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx";
# Warning: the toolbox D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx DOES NOT have an alias.
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.DefineProjection(...) with arcpy.DefineProjection_ALIAS(...)
arcpy.gp.DefineProjection(v_Name__output_img, "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRI MEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Feld hinzufügen
arcpy.gp.toolbox = "D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx";
# Warning: the toolbox D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx DOES NOT have an alias.
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.AddField(...) with arcpy.AddField_ALIAS(...)
arcpy.gp.AddField(v_Name__output_img__2_, "Prozent", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
# Process: Feld berechnen
arcpy.gp.toolbox = "D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx";
# Warning: the toolbox D:/Julia_T/projekt/erste_schritte/zweiter_tag_a1/a1_prozent.tbx DOES NOT have an alias.
# Please assign this toolbox an alias to avoid tool name collisions
# And replace arcpy.gp.CalculateField(...) with arcpy.CalculateField_ALIAS(...)
arcpy.gp.CalculateField(v_Name__output_img__3_, "Prozent", "([Count]/53959)*100", "VB", "")
I thing my main problem is how to program the loop by using the last created raster in the next tool.
I have folders (15) with around 350 asc files which should turn through this loop. The result should be written in a new folder with the same name plus the new type (img). Is there something I need to do with the workspace or is it like this?
import arcpy
from arcpy import env
env.workspace = r"D:\Julia_T\projekt\MODIS\karadarya\2000_neu\testdatenB"
Best Answer
The ModelBuilder functions (like
arcpy.IterateFiles_mb
) only work within ModelBuilder, and don't do as desired within a Python script. But,for
loops do just as well (if not better).In this case, you want to loop through all the .asc files in a workspace.
Define the workspace (you got that far):
Use the List Files function to create a list of all the .asc files in the workspace
Or, a simpler version that does the same thing:
Run arcpy tools on each file by putting them under the
for
loop. (Note: pay attention to indentation. It is one of the few things Python is picky about.)A couple of notes about exported python scripts -- not only do iterators not work, but the tool syntax is old (for example,
arcpy.gp.ASCIIToRaster
should now bearcpy.ASCIIToRaster_conversion
. I always recommend quickly looking up the ArcGIS Help pages for any given geoprocessing function to see what the current syntax and input variables should be. It is often faster to make the script from scratch instead of exporting a ModelBuilder tool and cleaning up the code.