Select to view content in your preferred language

My code is not doing anything

781
5
12-12-2017 05:45 AM
stashavint
Emerging Contributor

I have written a code to check if areas will be inundates when a certain value is added, the code doesn't give any errors but at the same time it does do anything.

Just says running code complete, I am not even sure what the problem is. My code is here

import arcpy
import os, sys, traceback, shutil
import numpy as np

arcpy.env.overwriteOutput = True

try:
     outputfolder = arcpy.GetParameterAsText(0)
     fc = arcpy.GetParameterAsText(1)
     inputraster = arcpy.GetParameterAsText(2)

     #check to see if the arcinfo license is on
     if arcpy.CheckExtension("Spatial") == "Available":
          arcpy.CheckOutExtension("Spatial")
     else:
          arcpy.AddError("Spatial analyst extension is not licensed.")
          sys.exit()

     #set the Progressor
     #arcpy.SetProgessor("step","Checking input requirements....",0,9,1) 

     #Process :create an out put folder
     arcpy.CreateFileGDB_management(outputfolder,"output.gdb")
     outputgdb = outputfolder + "\\output.gdb"+ os.step

     #create path to store raster data
     clippedraster = outputfolder + "clippedraster"
     flooded = outputfolder +"floodedarea"

     #check to see if the inputs are in the same projection
     desccs_ras = arcpy.Describe(inputraster).SpatialReference
     desccs_fc = arcpy.Describe(fc).SpatialReference
     if desccs_ras.name == "Unknown" and desccs_fc.name == "Unknown":
          arcpy.AddMessage("Your data does not have a defined spatial reference")
     else:
          if desccs_ras.projectionName == desccs_fc.projectionName:
               ""
          else:
               arcpy.AddError("The input Data should be in the same projection")
               sys.exit()
          del desccs_fc,desccs_ras

     #remove the areas below sea level
     currentRaster = arcpy.Raster(inputraster)
     rastertoclip = Con(currentRaster>=0,currentRaster,1)

     #clip the raster data to the interested zone extend
     arcpy.Clip_management(rastertoclip,"#",clippedraster,fc,"#","ClippingGeometry","MAINTAIN_EXTENT")
     clip_raster = arcpy.Raster("in_memory")
     areasbelow = clip_raster <= 2
     areasbelow.save(flooded)

     md = arcpy.mapping.MapDocument("CURRENT")
     df = arcpy.mapping.ListDataFrames(md)[0]
     result = arcpy.MakeRasterLayer_management(flooded,"FloodedArea")
     layer = result.getOutput(0)
     arcpy.mapping.AddLayer(df, layer,"AUTO_ARRANGE")

except:
     tb = sys.exc_info()[2]
     tbinfo = traceback.format_tb(tb)[0]
     pymsg = "PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n    " + \
            str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
0 Kudos
5 Replies
KevinDunlop
Frequent Contributor

How are you running your code?  In ArcCatalog, command line, or a python IDE (like pyscripter)?  I believe Arcpy.Addmessage won't print to command line or in python IDE.  So try adding a line below it that uses the print command.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

How ArcPy messages are handled depends on how the ArcPy code is run.  From the interactive Python window in ArcMap/ArcCatalog, the messages effectively disappear.  From scripts tied to toolboxes, the messages get logged with the results.  If you run a stand-alone ArcPy script or work interactively with the Python interpreter directly, the messages will be printed out to console.  If you are working within an IDE, the behavior will likely be the same as working directly with the Python interpreter, but an IDE could be configured the capture the messages and handle them differently.

For debugging purposes, I agree that adding some print statements makes sense because the behavior is more consistent across the various ways a script can be run.

stashavint
Emerging Contributor

I am running the code in a custom toolbox and therefore the use of the arcpy.AddMessage

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

it looks like your except creates a message "pymsg", but you aren't printing it.

Because I switch a lot between the different script windows and tools, etc, I always add a simple function and call it.  It may dupe the response in a python window, but it helps when switching between.  fwiw

def myMsgs(message):
     arcpy.AddMessage("{0}".format(message))
     print("{0}".format(message)  )

then call it with 

myMsgs("my message here, and variable if I want {0}, {1}".format(var1, var2))

DanPatterson_Retired
MVP Emeritus

it is useful... i called my message handler 'tweet'

0 Kudos