Automatisation of Arc-process in Python

Discussion created by zakh on Jul 17, 2013
Latest reply on Jul 17, 2013 by zakh
Hi all,

I'm kind of a newby in python, as I just started to use it for a project.

For the context (might help...) :

I'm trying to generate some shapefiels by different Selections on attributes and then make a spatial  join on another shapefile (a grid) in order to have presence/absence of some species of fish.

The input "fish" file consists of points and two parameters are tested : the year of observation and the specie itself. Then, three different grids are used in order to have for each timestep, the absence/presence of the fish in three spatial resolutions.

Basically, I did a Model Builder that I exported in order to add loops so that I don't have to run it like 200+ times... (and also for the fun of coding)

I still have some problems that I can't handle :
the sys.argv's do not work (I tried to comment them and it seems to work... why???)

Python does not find the empty grids, used for the spatial join, saying it is either not existing or not taken into account... Could it come from the mdb?

Is it necessary to trash the data used to prepare the join? (e.g. the poissons_A_shp and poissons_A_E_shp in the code?)

So here's my code : (Watch out, the last line is the spacial join and thus is pretty disgusting... It is as given by the export of my model builder)

# ---------------------------------------------------------------------------
# Created on: ven. juin 28 2013 02:15:15 
#   (generated by ArcGIS/ModelBuilder)
# Usage: Script_Grid_V2 <Selection_Date> <Grille_test_shp> <grid> <Expression> 
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting
import xlrd
import numpy


# Load le fichier xls de correspondance d'OBP :
corr = xlrd.open_workbook('P:/Nature/ByUser/xffmtt/Geonis/PERSONNEL/Projets_mxd/Peche/Inventaires/correspondance.xls')
data = corr.sheet_by_index(0)

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

# Script arguments...
# Selection de la plage temporelle :
# Variable vérifiée = Annee, preparee préalanlement par un autre MB :

#Selection_Date = sys.argv[1]
for i in range(1,4):

    if i==1:
        Selection_Date = "\"Annee\"<=1990" # Selection de la première plage temporelle : A in [1960:1990]
        Annee = "1990"
    elif i == 2:
        Selection_Date = "\"Annee\">1990 AND \"Annee\"<=2003" # Sélection de la 2ème plage temporelle : A in [1991:2003]
        Annee = "2003"
    elif i == 3:
        Selection_Date = "\"Annee\">2003" # Selection de la 3ème plage temporelle : A in [2003:2013]
        Annee = "2013"

    Selection_Date = "\"Annee\">2003"
# ******************
# Selection espece :
# ******************
    #Expression = sys.argv[2]    
    for L in range(data.nrows): # parcour le nombre de ligne
        Expression = ("obp_LvNomE ="+str(data.cell_value(2,L)))# Le code va chercher l'element L de la 2eme colone qui contient les codes poisson d'OBP
                                      # Comme il s'agit de préparer une commande SQL, il faut repasser l'element en "text"
                                      # pour que la requete soit lue.
# ******************
# Selection grille :
# ******************
        #grid = sys.argv[3]
        for g in range(1,4):
            if g == 1:
                grid = "O:\\Sig\\xArcView_Perso\\Grilles\\grille.mdb\\grille_2_5.shp" # provide a default value if unspecified
            elif g == 2:
                grid = "O:\\Sig\\xArcView_Perso\\Grilles\\grille.mdb\\grille_5km.shp" # provide a default value if unspecified
            elif g == 3:
                grid = "O:\\Sig\\xArcView_Perso\\Grilles\\grille.mdb\\grille_10km.shp" # provide a default value if unspecified

            #Grille_test_shp = sys.argv[4]
# Definit le nom de la couche en sortie!!! --> c'est ici qu'il faut pouvoir intervenir
            Grille_test_shp = "P:\\Nature\\ByUser\\xffmtt\\Geonis\\PERSONNEL\\Projets_mxd\\Peche\\Inventaires\\Test_grids\\Grille_poissons\\"+str(Expression)+"_"+str(grid)+str(Annee)+".shp" # provide a default value if unspecified

# Local variables...
            poissons_A_shp = "P:\\Nature\\ByUser\\xffmtt\\Geonis\\PERSONNEL\\Projets_mxd\\Peche\\Inventaires\\poissons_A.shp"
            poissons_A_E_shp = "P:\\Nature\\ByUser\\xffmtt\\Geonis\\PERSONNEL\\Projets_mxd\\Peche\\Inventaires\\poissons_A_E.shp"
            poissons_full = "P:\\Nature\\ByUser\\xffmtt\\Geonis\\PERSONNEL\\Projets_mxd\\Peche\\Inventaires\\poissons_full.shp"

# Process: Selectionner annee...
            gp.Select_analysis(poissons_full, poissons_A_shp, Selection_Date)

# Process: Selectionner espece...
            gp.Select_analysis(poissons_A_shp, poissons_A_E_shp, Expression)

# Process: Jointure spatiale...
            gp.SpatialJoin_analysis(grid, poissons_A_E_shp, Grille_test_shp, (.... things about the attributs, sadly too long to quote).... )

Thanks for the help and sorry for the "heavyness" of the questions