Select to view content in your preferred language

Python if ... then Statement

4950
18
08-02-2010 09:29 PM
DaveMarkey
Regular Contributor
Hi.

I am attempting to add an If statement to my Python script. What I am wanting to do is query a field in a shapefile and if it returns true delete a different shapefile. The principle idea behind my code is; If "Region" in the Clip_Feature shapefile = 'Murray Inland' then delete the contour shapefile. My problem is I don't know how to get focus onto the the Clip_Feature shapefile.  The code I have so far is:

import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)

Contours = "\\\\Atlas\\data\\geodata\\state\\contour\\arc"
Crossings = "\\\\Atlas\\data\\geodata\\state\\crossings\\arc"
Contour = "Contour.shp"
Crossing = "Crossing.shp"
P_L = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Lamberts_Projection\\Pts_Lines"
Clip_Feature = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Clip_Feature\\Clip_Feature.shp"

gp.FeatureClassToFeatureClass_conversion(Contours, P_L, Contour, "")
gp.FeatureClassToFeatureClass_conversion(Crossings, P_L, Crossing, "")

gp.workspace = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Clip_Feature"

if "Region_No" == 3:
gp.Delete_management("C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Lamberts_Projection\\Pts_Lines\\Contour.shp")

Thanks in advance.
0 Kudos
18 Replies
ChrisMathers
Deactivated User
You need a search cursor to look over the shapefile and see if that value occurs.

in 9.3:
scursor=gp.SearchCursor(Clip_Feature)
row=scursor.Next()
while row:
    if row.GetValue("Region_No") == 3:
        gp.Delete_management("C:\\Daves_Stuff\\ArcPad_Data \\Standard_Layers\\Lamberts_Projection\\Pts_Lines\\Contour.shp")
del scursor


in 10:
for row in arcpy.SearchCursor(Clip_Feature):
    if row.Region_No == 3:
        arcpy.Delete_management("C:\\Daves_Stuff\\ArcPad_Data \\Standard_Layers\\Lamberts_Projection\\Pts_Lines\\Contour.shp")
0 Kudos
DaveMarkey
Regular Contributor
Hi.

In your script does the first line restrict the loop to only the reference layer? I need the If statement to look at the referred layer only as there is a master layer in the same directory that would have 'Murray Inland'.  This script is part of a geoprocessor model where the 'clip feature' is derived from user input.  I have also noticed the 'while' loop significantly increases processing time.  Could a 'for' loop be used instead of 'while'? The loop I'm attempting is:

fieldname = "Region"
gp.workspace = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Clip_Feature"
shp = gp.ListFeatureClasses()
for shp in shp:
    if fieldname == 'Murray Inland':
        gp.Delete_management("C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Lamberts_Projection\\Pts_Lines\\Contour.shp")

This loop has the problem of not being specific to the 'clip feature' layer and also looks at the master layer.

I have attempted to run the supplied while loop script which executes successfully but gives an error that the contour layer does not exist. My complete script is shown below:

import sys, string, os, arcgisscripting
gp = arcgisscripting.create(9.3)

Contours = "\\\\Atlas\\data\\geodata\\state\\contour\\arc"
Crossings = "\\\\Atlas\\data\\geodata\\state\\crossings\\arc"
Contour = "Contour.shp"
Crossing = "Crossing.shp"
P_L = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Lamberts_Projection\\Pts_Lines"
Clip_Feature = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Clip_Feature\\Clip_Feature.shp"

gp.FeatureClassToFeatureClass_conversion(Contours, P_L, Contour, "")
gp.FeatureClassToFeatureClass_conversion(Crossings, P_L, Crossing, "")

gp.workspace = "C:\\Daves_Stuff\\ArcPad_Data\\Standard_Layers\\Clip_Feature"
scursor=gp.SearchCursor(Clip_Feature)
row=scursor.Next()
while row:
    if row.GetValue("Region") == 'Murray Inland':
        gp.Delete_management("C:\\Daves_Stuff\\ArcPad_Data \\Standard_Layers\\Lamberts_Projection\\Pts_Lines\\Contour.shp")
del scursor
0 Kudos
ChrisMathers
Deactivated User
I think you are going too deep in your workspace. Is '\\Cl ip_Feature' a directory? I thought that was the name of a shapefile. Also you cant do a for loop on at 9.3 cursor. You have to use a while loop. It seems strange but that is changed at 10 so you can use for.
0 Kudos
DaveMarkey
Regular Contributor
Hi.

'Clip_Feature' is both a directory and a shapefile within this directory.  The file path for this shapefile is; C:\Daves_Stuff\ArcPad_Data\Standard_Layers\Clip_Feature\Clip_Feature.shp
0 Kudos
DaveMarkey
Regular Contributor
Hi.

I have tried changing the file path of the gp.workspace but I am still getting the file does not exist error. I have also tested this script with the clip feature being a different region, thus the statement is now false, therefore the contour dataset should not be deleted. The script appears not to be completing (endless loop?).  I killed it after a while, changed the clip feature to be again the Murray Inland region and ran the script. The script then deletes the contour dataset but gives the error this layer does not exist.  It appears the loop is not ending.

Does the reference to 'Clip_Feature' in the search cursor statement restrict the loop to this dataset only?
0 Kudos
ChrisMathers
Deactivated User
The search cursor will only apply to the single feature class given when the object is created.

So the script deletes the file but still has an error? Could you post the error?

The while shouldnt fall into an infinite loop with such a simple function inside it. 'while row:' is the same as 'while row != none:'. The loop will continue until cursor.Next() returns a result of none. The while row: loop of geoprocessing cursors work like a for row in cursor: on a normal python cursor.
0 Kudos
DaveMarkey
Regular Contributor
Hi.

Detailed below is the error I am recieving.  I have confirmed the referred dataset is created in the specified location.  That is, the contour dataset is created in the Temp directory.  I do not recieve an error when I comment out the while loop section. Could the fact I am dealing with shapefiles cause the problem. Shapefiles are needed as this data is used by ArcPad.

Traceback (most recent call last):
  File "C:\Daves_Stuff\Scripts\Test.py", line 43, in <module>
    gp.Delete_management("C:\\Temp\\Contour.shp")
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000732: Input data element: Dataset C:\Temp\Contour.shp does not exist or is not supported
Failed to execute (Delete).
0 Kudos
ChrisMathers
Deactivated User
Well you could try gp.delete_management(path, "shapefile") which tells the tool explicitly you are deleting a shapefile, or you can try deleting it with os.remove(). With os.remove() you will have to delete all the files that make up the shapefile individually though. os.remove() is pure python and doesnt muck about with the geoprocessor.

for ext in [".shp",".shx",".sbn",".dbf",".prj",".sbx"]:
os.remove("C:\\Temp\\Contour"+ext)
0 Kudos
DaveMarkey
Regular Contributor
Hi.

Unfortunately this has not fixed my problem. The deleting of the shapefile is not the problem. It is exiting the loop that is the problem.  I tried executing this script when the condition evaluates to false and the script appears to go into a never ending loop.
0 Kudos