Arc


Arc/Info AML programs

ArcInfo Workstation (i.e. the command line program that comes with ArcGIS) is probably the oldest GIS program that is still around. It is also the best program in many respects. It is fast, it is reliable, and - thank heavens - it is command line driven and there is AML. AML, or Arc Macro Language is an easy scripting language.

An AML file is a text file. The convention is for the extension to be .aml So for example: test.aml To run a script type

&run test at the Arc or Grid prompt.

Below are some examples of AML scripts. To use them, copy and paste the code to a text file and save the file using the .aml extension:

  /* importascii.aml
  &TERMINAL 9999
  /* AML script to import all ascii grids in a folder. For ArcInfo command line
  /* limitations: 64 files maximum; assumes that the ascii files have valid grid names
  /* if not use [subst   ]
  /* To run in set the workspace to the right directory and type: &run importascii
  /* Robert Hijmans, Nov 2002. r.hijmans@gmail.com
  
  /* check if twe are on the grid prompt
  &if [show program] <> GRID &then
   &do 
     &sv goarc = .TRUE.
     grid
   &end
  &else &sv goarc = .FALSE.
  
  /* get a list of all ascii files
  &s ascs := [listfile *.asc -file]
  &if [null %ascs%] &then &return No ascii files
  
  &s num := [token %ascs% -count]
  &type %num% files
  
  &do i := 1 &to %num%
    &sv inname = [extract %i% %ascs%] 
    &sv outname = [subst %inname% '.asc' ]
    &type %inname% %outname%
    %outname% = asciigrid(%inname%)
  &end
  
  &if %goarc% &then q
  
  &return


  /* SHAPECLEAN.AML
  /* script to clean a shp file keeping regions (multi-polygon features)
  /* and, importantly, keeping the attributes
  /* RJH September 2006
  
  /* this program expects two input variables, the existing shp filename, and a name 
  /* for the new clean output shp file to be produced
  /* &run shapeclean input output
  
  &args input output
   
  /* call the subroutine to check the input (see below for the code)
  &call check_args
  
  &if [exists cov -cover] &then kill cov
  &if [exists covc -cover] &then kill covc
  &if [exists covr -cover] &then kill covr
  
  shapearc %input% cov type
  clean cov covc 0.000001 0.000001
  regionpoly covc covr type covr.safe
  build covr
  arcshape covr poly %output%
  
  kill cov
  kill covc
  kill covr
  &type ... cleanshape DONE ...
  &return
  
  /*----- this is a subroutine -----
  &routine CHECK_ARGS
  /* Checks if there are two arguments. Perhaps it should
  /* also check that the input files are not the same
  /* and that the output file does not exist
  &if [NULL %input%] or [NULL %output%] &then ~
   &return &inform Usage: cleanshape <input> <output> 
  &return 
  



  /* this example AML loops through all the (polygon) shp files in a folder
  /* converts them into grids and adds all the grids to create a grid with the 
  /* number of classes in each cell (richness).
  /* RJH May 2007
  
  /* delete files that may have been produced during a test run or so
  &if [exists sumgrid -grid] &then kill sumgrid
  
  /* this program runs in GRID (not ARC)
  &if [show program] ne GRID &then grid
  
  /* set up a grid with values of 0 and nodata
  sumgrid = con(isnull(e:\pov_cor\preds\alt), e:\pov_cor\preds\alt, 0)
  
  /* fix extent and resolution
  setwindow sumgrid
  setcell sumgrid
  
  /* listfile can only have 64 files. 
  /* with a few files [listfile ..\shp\*.shp -file] could do.
  /* but for a large number of files you may need a trick like this
  
  &do i &list a b c d e f g h i j k l m n o p q r s t u v w x y z
    &do j &list a b c d e f g h i j k l m n o p q r s t u v w x y z
      &s shps := [listfile ..\shp\%i%%j%*.shp -file]
      &if [null %shps%] &then &type No %i%%j% shps
      &else
      &do
        &s num := [token %shps% -count]
        &type %num% %i%%j% files
        &do k := 1 &to %num%
          &sv theshp = [extract %k% %shps%]
          agrid1 = shapegrid(..\shp\%theshp%, val)
          agrid2 = con(isnull(agrid1, 0, 1))
          sumgrid1 = sumgrid + agrid2
          kill agrid1
          kill agrid2
          &if [exists sumgrid1 -grid] &then kill sumgrid
          rename sumgrid1 sumgrid
        &end
      &end
    &end
  &end


  /* This AML reads a list of names from a text file 
  /* (species names corresponding to shp files in this case) and processes them
  /* Robert Hijmans, March 2008
  /*    
  &sv fil = d:\tst\selspp.txt
  
  /* it would be clean to keep your shp files in a seperate directory; provide that name here
  &sv shpdir := d:\tst\
  
  &sv fieldname = species_co
  &sv cellsize = 0.05
  
  &if ^ [exists %fil% -file] &then &return %fil% does not exist
  
  &if [SHOW PROGRAM] <> GRID &then grid
  /* setwindow .. .. .. ..
  /* setcell ..
  
  &if [exists temp -grid] &then kill temp
  &if [exists temp2 -grid] &then kill temp2
  &if [exists sumgrid -grid] &then kill sumgrid
  
  /* open file
  &setvar filunit := [open %fil% openstatus -read]
  &if %openstatus% <> 0 &then 
      &return cannot open file, restart arc/info 
  
  /* Read first line from file; note that in this example there is no header row
  &setvar species := [read %filunit% readstatus]
  &if %readstatus% <> 0 &then &return Could not read file.
  
  &do &while %readstatus% = 0
    &type %species%
   
    temp = shapegrid (%shpdir%%species%.shp, %fieldname%, %cellsize%)
    temp2 = con(isnull(temp), 0, temp)
    &if [exists sumgrid -grid] &then sumgrid = sumgrid + temp2
    &else sumgrid = temp2 
    kill temp
    kill temp2
    &setvar species := [read %filunit% readstatus]
  &end
  
  /* Close file.
   &if [close %filunit%] <> 0 &then
      &return &warning Unable to close %fil%.
  
  &return Done!


In many cases you can also take a shortcut and simply list all commands in sequential order. For example, if you want to import many ascii grid files to arcinfo grid format:

Open a Command Prompt (DOS) window navigate to the folder where your .asc files are e.g. c:> d: d:\ cd ascii

make a list of the ascii files: d:\ascii\ dir *.asc > asc.txt

open the asc.txt file and paste it into excel to get the filenames in a single column (column B), remove other things. copy the filenames to column C, and in that column do an replace of ".asc" with "". Put "asciigrid" in the first column and use a concatenate formula in column D.

add some columns to get A B C D asciigrid species1.asc species1 =concatenate(A1," ", B1, " ", C1) asciigrid species2.asc species2 =concatenate(A2," ", B2, " ", C2) asciigrid species3.asc species3 =concatenate(A3," ", B3, " ", C3)

Copy the contents of column D to a a text file (Notepad). From there copy it to Word and replace the tabs (^t) with spaces. Copy it back to the text file. Save as e.g. ascgr.aml. Alternatively: copy and paste special (values) column D into a new worksheet. Save that worksheet as space delimited text.

Run the script in arcinfo workstation. e.g. Arc: &run ascgr.aml

Last modified June 5, 2008 12:12 am