Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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:

enter image description here

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"
share|improve this question
1  
Welcome to GIS SE! Would you be able to edit your question to include the version of ArcGIS for Desktop that you are using, please? My quick advice on this one is NOT to try and debug your script from ModelBuilder but instead to step through one iteration by manually running each tool in sequence and then using Copy As Python Snippet in the Geoprocessing | Results window to write a Python script to repeat what you just did. Once that works showing you how to include iteration in the Python script becomes trivial. –  PolyGeo Jul 23 at 10:28
    
I'm working with ArcMap 10.2.1, thanks for asking. You mean I should first write a skript with the geoprocessingtools for just one asc file and add the loop afterwards? –  Nora Jul 23 at 11:19
    
Yes - I think writing the script using the Geoprocessing tools (and Copy As Python Snippet to get the syntax) for just one *.asc file , and then adding the loop afterwards is a much easier way to do it. –  PolyGeo Jul 23 at 11:22
    
Thanks a lot, I'm sill trying. (Perhaps it will take some time, but I like to find my way. For sure I will pose my result.) –  Nora Jul 23 at 11:55

3 Answers 3

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):

import arcpy
from arcpy import env

env.workspace = r"D:\Julia_T\projekt\MODIS\karadarya\2000_neu\testdatenB"

Use the List Files function to create a list of all the .asc files in the workspace

ascFileList = arcpy.ListFiles("*.asc")
for ascFile in ascFileList:
    # geoprocessing steps

Or, a simpler version that does the same thing:

for ascFile in arcpy.ListFiles("*.asc"):
    # geoprocessing steps

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.)

for ascFile in arcpy.ListFiles("*.asc"):
    # get the file name without extension (replaces the %Name% variable from ModelBuidler)
    ascFileName = os.path.splitext(ascFile)[0]
    # define the output file
    rastFile = ascFileName + "_output.img"
    # run the tool
    arcpy.ASCIIToRaster_conversion(ascFile, rastFile, "INTEGER")

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 be arcpy.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.

share|improve this answer
    
Thanks so much for you answer, you saved my day. I'm excited to try it. But first I should do a research about the part with the name because I tried it before without and for sure it hadn't worked. –  Nora Jul 23 at 12:13
    
I like to use a lot of print statements to check the value of variables before I add in the actual geoprocessing steps. Or, if you're running this as an ArcMap tool instead of in IDLE, using arcpy.AddMessage(variable) –  Erica Jul 23 at 12:16
    
Sorry for asking again, but I don't know what I'm doing wrong. I always get the errow 000865 0. Do I have to use the CatalogPath? –  Nora Jul 23 at 13:34
    
It's not a problem to keep asking, but you should to ask a new Question specific to that error (and show what updated code you're using) -- it's easier to work with than in Comments :) –  Erica Jul 23 at 13:39
    
Ok, I will do this – thanks. –  Nora Jul 23 at 13:44

Another non-proprietary python option to list files is os.walk:

import os

for root, dirs, files in os.walk('C:/Temp'):
    for f in files:
        if f.endswith('.asc'):
            print os.path.join(root, f)

The benefit of this method opposed to

arcpy.ListFiles

is that it can recursively search through sub-directories.

share|improve this answer
    
I wonder speed wise how this matches up against the various arcpy.List*() functions. I usually use a mixture of both, sometimes together. –  Paul Jul 23 at 12:25

Ok, it is done know – my first script. Thanks for your support! `import arcpy import os from arcpy import env

env.overwriteOutput = True
env.workspace = r"D:\Julia_T\projekt\erste_schritte\test_projection"

#folder to list
ascFileList = arcpy.ListFiles("*.asc")

print ascFileList
#works
for ascFile in ascFileList:
# geoprocessing steps
ascFileName = os.path.splitext (ascFile)[0]
print ascFileName
#works
rastFile = ascFileName + "_output.img"
print rastFile
#works
#run the tool
arcpy.ASCIIToRaster_conversion (env.workspace + "\\" + ascFile, env.workspace + "\\" +   rastFile, "INTEGER")

# Process: Projektion definieren
arcpy.DefineProjection_management (env.workspace + "\\" + rastFile,          "GEOGCS['GCS_WGS_1984',DATUM['D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]]")
# Process: Feld hinzufügen
arcpy.AddField_management (env.workspace + "\\" + rastFile,"Prozent","DOUBLE")
# Process: Feld berechnen
arcpy.CalculateField_management(env.workspace + "\\" + rastFile,"Prozent","( [Count] /53959) *100", "VB")`
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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