For Loop: Convert Point to Raster and Keep Base File Name In Output

1407
7
05-03-2012 03:46 PM
MichaelBrady
New Contributor
Hi,

I'm very new to Python. I'm attempting to convert multiple point shapefiles to GRIDs and retain the base file name of each input shapefile. When I run my script, Python crashes with no helpful error message. I've also noticed that when I try to run the script a second time after I've made edits, the data become inaccessible and I have to close all programs, and delete the folder containing my shapefiles before Python will work (up to where it crashes). I've been working on this for days and have made no progress. I'd really appreciate any pointers.

Note: I've successfully run the script when I convert one file and specify a string name. I believe the problem I'm having has to do with how I'm naming the output.

My script:

# Set local environment:
Path = "E:\\MichaelBrady\\Graduate_work\\MAIN\\Classes\\spring2012\\ind_study\\nexrad\\data\\irene_nexrad\\"
Working = Path + "nexrad_Irene_Working" 
arcpy.env.workspace = Working
arcpy.env.overwriteOutput = 1
# Set local variables:
NEXRAD2grid = arcpy.ListFeatureClasses()
HRAP_Projection = "PROJCS['HRAP_Projection',GEOGCS['HRAP_GCS',DATUM['D_HRAP',SPHEROID['HRAP_Sphere',6371200.0,0.0]],PRIMEM['<custom>',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Stereographic_North_Pole'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-105.0],PARAMETER['Standard_Parallel_1',60.0],UNIT['HRAP_Grid',4762.5]]"

arcpy.env.outputCoordinateSystem = HRAP_Projection
arcpy.env.extent = "-236 -1476 661 -806"
arcpy.env.cellSize = "1"


for fc in NEXRAD2grid:
    desc = arcpy.Describe(fc)
    filename = desc.name
    NEXRADgrids = filename[:-4]
    arcpy.PointToRaster_conversion(fc, "Globvalue", NEXRADgrids, "MAXIMUM", "NONE", "1") #execute point to raster
    print NEXRADgrids
Tags (2)
0 Kudos
7 Replies
StephenKruzik
New Contributor III
I don't know the exact issue with your script, but are you running this within ArcGIS or as a stand alone python script?

If it's stand alone, my next question would be which python handler are you using?

I personally use IDLE, and when I've had trouble with it I use the debugger to step through each line to find where it's causing problems.

If you're unsure of using the debugger there's a plethora of info on it by searching for it.  It may not solve the exact problem, but at the very least, it should 100% confirm the exact point the script is failing.
0 Kudos
curtvprice
MVP Esteemed Contributor
When I run my script, Python crashes with no helpful error message.


I suggest looking into the online help examples on exception handling -- these techniques can really help - for example, you can print which line the script failed on.

Arc 10 Help: Error handling with Python

Here's a try/except wrapped around your code that will do that:

import sys
import traceback
import arcpy
try:
    # Set local environment:
    Path = "E:\\MichaelBrady\\Graduate_work\\MAIN\\Classes\\spring2012\\ind_study\\nexrad\\data\\irene_nexrad\\"
    Working = Path + "nexrad_Irene_Working" 
    arcpy.env.workspace = Working
    arcpy.env.overwriteOutput = 1
    # Set local variables:
    NEXRAD2grid = arcpy.ListFeatureClasses()
    HRAP_Projection = "PROJCS['HRAP_Projection',GEOGCS['HRAP_GCS',DATUM['D_HRAP',SPHEROID['HRAP_Sphere',6371200.0,0.0]],PRIMEM['<custom>',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Stereographic_North_Pole'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-105.0],PARAMETER['Standard_Parallel_1',60.0],UNIT['HRAP_Grid',4762.5]]"

    arcpy.env.outputCoordinateSystem = HRAP_Projection
    arcpy.env.extent = "-236 -1476 661 -806"
    arcpy.env.cellSize = "1"

    for fc in NEXRAD2grid:
        desc = arcpy.Describe(fc)
        filename = desc.name
        NEXRADgrids = filename[:-4]
        arcpy.PointToRaster_conversion(fc, "Globvalue", NEXRADgrids, "MAXIMUM", "NONE", "1") #execute point to raster
        print NEXRADgrids
except:
    # print python errors
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    print tbinfo
    # print geprocessing errors messages (if any)
    print arcpy.GetMessages(2)


PS Please tags when posting Python code.
0 Kudos
MichaelBrady
New Contributor
I don't know the exact issue with your script, but are you running this within ArcGIS or as a stand alone python script?

If it's stand alone, my next question would be which python handler are you using?

I personally use IDLE, and when I've had trouble with it I use the debugger to step through each line to find where it's causing problems.

If you're unsure of using the debugger there's a plethora of info on it by searching for it.  It may not solve the exact problem, but at the very least, it should 100% confirm the exact point the script is failing.


Hi,

Thanks for your reply.

I'm using the IDLE and am now trying to debug. I know the problem has to do with how I'm naming the output file, but I'm not sure what exactly the problem is. Will the debugger only help me identify where the problem is or also what the problem might be (I'm researching this now)?

-Mike
0 Kudos
MichaelBrady
New Contributor
I suggest looking into the online help examples on exception handling -- these techniques can really help - for example, you can print which line the script failed on. 

Arc 10 Help: Error handling with Python

Here's a try/except wrapped around your code that will do that: 

import sys
import traceback
import arcpy
try:
    # Set local environment:
    Path = "E:\\MichaelBrady\\Graduate_work\\MAIN\\Classes\\spring2012\\ind_study\\nexrad\\data\\irene_nexrad\\"
    Working = Path + "nexrad_Irene_Working" 
    arcpy.env.workspace = Working
    arcpy.env.overwriteOutput = 1
    # Set local variables:
    NEXRAD2grid = arcpy.ListFeatureClasses()
    HRAP_Projection = "PROJCS['HRAP_Projection',GEOGCS['HRAP_GCS',DATUM['D_HRAP',SPHEROID['HRAP_Sphere',6371200.0,0.0]],PRIMEM['<custom>',0.0],UNIT['Degree',0.0174532925199433]],PROJECTION['Stereographic_North_Pole'],PARAMETER['False_Easting',0.0],PARAMETER['False_Northing',0.0],PARAMETER['Central_Meridian',-105.0],PARAMETER['Standard_Parallel_1',60.0],UNIT['HRAP_Grid',4762.5]]"

    arcpy.env.outputCoordinateSystem = HRAP_Projection
    arcpy.env.extent = "-236 -1476 661 -806"
    arcpy.env.cellSize = "1"

    for fc in NEXRAD2grid:
        desc = arcpy.Describe(fc)
        filename = desc.name
        NEXRADgrids = filename[:-4]
        arcpy.PointToRaster_conversion(fc, "Globvalue", NEXRADgrids, "MAXIMUM", "NONE", "1") #execute point to raster
        print NEXRADgrids
except:
    # print python errors
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    print tbinfo
    # print geprocessing errors messages (if any)
    print arcpy.GetMessages(2)


PS Please tags when posting Python code.


Hi,

Thanks for your reply.

I should have said that I get no error message at all. Python crashes (i.e. "Python has topped working, checking for solution, etc.". When I entered your code, Python crashed. I'm continuing to research the problem and am open to other suggestions for trouble shooting. I am 99% where the problem is - the method I'm using to name my output fcs (i.e. "NEXRADgrids"), but I don't know what exactly what the problem is. When it crashes, I get 1 empty grid file with the naming I want.

In the future, I will use code tags, though the link you provided did not work.

Mike
0 Kudos
curtvprice
MVP Esteemed Contributor
Will the debugger only help me identify where the problem is or also what the problem might be?


Maybe. The debugger can be very helpful because you can step through the code line at a time and easily examine values of variables while the code is executing.

For me, using the debugger is usually overkill if I have wrapped my code as above, as then I get much more complete error messaging that usually makes the problem obvious. I usually just use print statements to help me check values of variables, but that's mostly because I haven't become very skilled using debugging software.

When I entered your code, Python crashed.


To learn how to use try/except blocks, I'd start with a simpler test than entering the whole kaboodle above, or the examples in the online help I pointed you too.

For the thread, [thread=48475]here's that code tag link, repaired.[/thread]
0 Kudos
BenjaminGale
New Contributor III
I'm not 100% sure which piece of your code is giving you the error.
I use a similar code and was able to preform the process on some of my own data.
I didn't include the output coordinate so that would have to be added back in.

#Set Variables
inWS = r"C:\My_Stuff\ConvertPTtoRaster\SHP"     # In workspace
outWS = r"C:\My_Stuff\ConvertPTtoRaster\Output" # Out workspace
arcpy.env.workspace = inWS
arcpy.env.overwriteOutput = 1
arcpy.env.extent = "2470500 7661500 2477000 7666800"

arcpy.env.cellSize = 25

ptFeatures = list()
ptFeatures = arcpy.ListFeatureClasses("","Point") #Only use points features

for fc in ptFeatures:
    fName = arcpy.Describe(fc).basename 
    arcpy.PointToRaster_conversion(fc,"Z",outWS + "\\" + fName,"MAXIMUM","",25)


With your code.

inWS = "E:\\MichaelBrady\\Graduate_work\\MAIN\\Classes\\spring2012\\ind_study\\nexrad\\data\\irene_nexrad\\nexrad_Irene_Working"     # In workspace
outWS = inWS            # Out workspace
arcpy.env.workspace = inWS
arcpy.env.overwriteOutput = 1
arcpy.env.extent = "-236 -1476 661 -806"

arcpy.env.cellSize = 1

ptFeatures = list()
ptFeatures = arcpy.ListFeatureClasses("","Point") #Only use points features

for fc in ptFeatures:
    fName = arcpy.Describe(fc).basename 
    arcpy.PointToRaster_conversion(fc,"Globvalue",outWS + "\\" + fName,"MAXIMUM","NONE",1)


Hope it works!
0 Kudos
MichaelBrady
New Contributor
Hi Benjamin,

Thanks for providing this code.  I tried it, but Python still crashed. That you specified a separate out workspace in your code (before modified for me), made me think to try to store the grids in a separate folder than my workspace folder. This worked, but I'm not sure why. Apparently when you use the point to raster tool in Python, the output workspace where the grids get stored should be different than the input workspace where the point data are stored. It would be good if someone can confirm this is the case.

Thanks again.
Mike
0 Kudos