Select to view content in your preferred language

If then statement for python

4013
5
01-22-2013 03:46 PM
by Anonymous User
Not applicable
Original User: luke.kaim

Hi Everyone,

I think I am not understanding the logic, but what I want to do is make an if then statement. I want to create a file and then create another file. The code creates the first file, but does not create the second file. What am I doing wrong?

def main():
    try:
        import arcpy, sys, traceback, os, string, locale,arcgisscripting
        arcpy.env.overwriteOutput = True
        

        from arcpy import env
        env.workspace =r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar"

        outputVGIboundingbox= "VGIboundingbox.shp"
        outputGNISboundingbox="GNISboundingbox.shp"
        VGIFile = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\polygon.shp"
        GNIS_FC = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\GNIS.shp"
        if arcpy.Exists(outputVGIboundingbox):
            arcpy.Delete_management(outputVGIboundingbox)
            VGIboundingbox= arcpy.FeatureEnvelopeToPolygon_management(VGIFile,outputVGIboundingbox,"MULTIPART")

        elif arcpy.Exists(outputGNISboundingbox):
            arcpy.Delete_management(outputGNISboundingbox)
            GNISboundingbox= arcpy.FeatureEnvelopeToPolygon_management(GNIS_Geom,outputGNISboundingbox,"MULTIPART")



Thank you in advance

Thank you,
Luke Kaim

Thank you
Luke Kaim (SIE)
Lucas.Kaim@maine.edu
(914)263-7866
"Don�??t complain. Just work harder" (Randy Pausch).
0 Kudos
5 Replies
by Anonymous User
Not applicable
Original User: Caleb1987

Hi Everyone, 

I think I am not understanding the logic, but what I want to do is make an if then statement. I want to create a file and then create another file. The code creates the first file, but does not create the second file. What am I doing wrong?



This is an easy fix. The problem is you used "elif" when you need to use "if". The elif statement means "else if", meaning execute this if the previous "if" (or "elif") statement is not true. If you only wanted to create ONE of these two files, the use of the elif statement would work. But if you just want to this to work if those variables exist, you want to use just if.

def main():
    try:
        import arcpy, sys, traceback, os, string, locale,arcgisscripting
        arcpy.env.overwriteOutput = True
        

        from arcpy import env
        env.workspace =r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar"

        outputVGIboundingbox= "VGIboundingbox.shp"
        outputGNISboundingbox="GNISboundingbox.shp"
        VGIFile = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\polygon.shp"
        GNIS_FC = r"C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\GNIS.shp"
        if arcpy.Exists(outputVGIboundingbox):
            arcpy.Delete_management(outputVGIboundingbox)
            VGIboundingbox= arcpy.FeatureEnvelopeToPolygon_management(VGIFile,outputVGIboundingbox,"MULTIPART")

        if arcpy.Exists(outputGNISboundingbox):
            arcpy.Delete_management(outputGNISboundingbox)
            GNISboundingbox= arcpy.FeatureEnvelopeToPolygon_management(GNIS_Geom,outputGNISboundingbox,"MULTIPART")


Here is a simpler example to illustrate the proper use of if and elif.
>>> # True/False statement
>>> def GreaterThan(x,y):
 if x > y:
  print '%s is greater than %s' %(x,y)
 elif x == y:
  print '%s is equal to %s' %(x,y)
 else:
  print '%s is less than %s' %(x,y)

  
>>> GreaterThan(5,7)
5 is less than 7
>>> GreaterThan(3,3)
3 is equal to 3
>>> GreaterThan(8,2)
8 is greater than 2
>>> 


In the above example, I used all three: if, elif, and else. The else statement could have easily been another "elif" statement but then I would have had to put "elif x < y:". I used an else statement instead since there were no possibilities other than less than since greater than and equal to were already used.
0 Kudos
by Anonymous User
Not applicable
Original User: luke.kaim

I thought I only needed another if statement, but when I had two if statements I got the following error:

Executing: FeatureEnvelopeToPolygon "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\polygon.shp" "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\VGIboundingbox.shp" MULTIPART
Start Time: Tue Jan 22 21:41:11 2013
Succeeded at Tue Jan 22 21:41:12 2013 (Elapsed Time: 1.00 seconds)
  File "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\boundingbox.py", line 27, in main
    GNISboundingbox_Rows = arcpy.SearchCursor(outputGNISboundingbox)

<class 'Queue.Empty'>:
0 Kudos
by Anonymous User
Not applicable
I thought I only needed another if statement, but when I had two if statements I got the following error:

Executing: FeatureEnvelopeToPolygon "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\polygon.shp" "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\VGIboundingbox.shp" MULTIPART
Start Time: Tue Jan 22 21:41:11 2013
Succeeded at Tue Jan 22 21:41:12 2013 (Elapsed Time: 1.00 seconds)
  File "C:\Users\Luke Kaim\Documents\University of Maine\Fall_2012\Volunteer Geographic Information\simalar\boundingbox.py", line 27, in main
    GNISboundingbox_Rows = arcpy.SearchCursor(outputGNISboundingbox)

<class 'Queue.Empty'>:


Ah, I'm sorry. I thought you meant you did not understand the logic of the if and elif statements.  This does show that the second if statement is executing, there is just an error in it.  Is this the full code?  It is showing an error trying to open a Search Cursor for the outputGNISboundingbox variable.  I am not seeing this anywhere in your code though. 

Since you are deleting this variable, I am guessing that wherever the part of your code where you are using a Search Cursor on this table you are getting an exception because there are no rows.

Also you may want to change the indentation for the create feature envelopes.


        if arcpy.Exists(outputVGIboundingbox):
            arcpy.Delete_management(outputVGIboundingbox)
        VGIboundingbox= arcpy.FeatureEnvelopeToPolygon_management(VGIFile,outputVGIboundingbox,"MULTIPART")

        if arcpy.Exists(outputGNISboundingbox):
            arcpy.Delete_management(outputGNISboundingbox)
        GNISboundingbox= arcpy.FeatureEnvelopeToPolygon_management(GNIS_Geom,outputGNISboundingbox,"MULTIPART")
0 Kudos
by Anonymous User
Not applicable
Original User: luke.kaim

The variables are set to:
outputVGIboundingbox= "VGIboundingbox.shp"
outputGNISboundingbox="GNISboundingbox.shp"

This is line 10 of the code.

This variable is being used just to store the output location of the file. Wouldn't the arcpy.FeatureEnvelopeToPolygon_management need to be indented within the if statement?
0 Kudos
by Anonymous User
Not applicable
Wouldn't the arcpy.FeatureEnvelopeToPolygon_management need to be indented within the if statement?


Not necessarily.  For example, if this variable you are wanting to overwrite does not exist, a new variable will not be created to replace it since the if block will only execute if that variable exists in the first place.  Are you using the if statement to test if the output variable exists, and if it does you want to delete it?  If you take the new variable (the result of the create envelopes tool) out of the if block, a variable will be created every time.
0 Kudos