Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to make a script which selects every .png file in a folder beginning with the letters "LG". I then want the scipt create a shapefile, replacing the "LG" with "SH", and then i want the script to buffer that shapefile and rename the buffer with the first 2 letters being "SB".

I keep getting an error 99999 error message at line 37! ( gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features, "SIMPLIFY", "VALUE") )

I have posted this on stackoverflow.com but probably here is a better place as scripting for arcmap is probably quite specialised!

Can anyone offer me any advice to why this script doesnt work? I am running ArcMap 9.3.1. and (attempting) to run the script from pyscripter!

# ---------------------------------------------------------------------------
# RASTER2POLYGON.py
# Created on: Wed Dec 01 2010 12:20:48 PM
#   (generated by ArcGIS/ModelBuilder)
# Usage: RASTER2POLYGON <INPUT_RASTER> <Output_polygon_features> <Raster_Buffer_shp>
# ---------------------------------------------------------------------------

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

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

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

# Script arguments...

folder = "D:\\J04-0083\\IMAGEFILES"
for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        filename_split = os.path.splitext(filename) # filename and extensionname (extension in [1])
        filename_zero = filename_split[0]
    try:
        first_2_letters = filename_zero[0] + filename_zero[1]
    except:
        first_2_letters = "XX"

    if first_2_letters == "LG":
        Output_polygon_features = "D:\\J04-0083\\ShapeFiles.gdb\\" + "SH_" + filename + ".shp"

        # Process: Raster to Polygon...

        INPUT_RASTER = os.path.join(root + "\\" + filename_zero + ".png")
        gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features, "SIMPLIFY", "VALUE") 

        Distance__value_or_field_ = "5 Meters"
        Raster_Buffer_shp = "SB_" + filename + ".shp"

        # Process: Buffer...
        gp.Buffer_analysis(Output_polygon_features, Raster_Buffer_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE", "")
share|improve this question
    
Does the conversion work when you try to do it manually? –  AndOne Dec 8 '10 at 14:21
    
Sadly yes! So the problem lies in the script! I made it initially in model builder and it ran fine, its just now i want to run it for multiple files is there a problem! –  Alice Dec 8 '10 at 14:28
add comment

4 Answers

up vote 5 down vote accepted

Alice,

Answered you over on the ESRI Python Forum

http://forums.arcgis.com/threads/18825-Raster-to-polygon-script-loop-failing!!-error-99999!?p=60693#post60693

Full annotation and reworked script, but the essence of the problem was trying to name the feature class in the File Geodatabase with a .shp extension. And then not cleaning up after each script run.

Stuart

share|improve this answer
    
Thank you!!!! I have a new error now! I have posted the details on the ESRI forum! –  Alice Dec 9 '10 at 7:41
    
My fault, problem was with selection logic to isolate the image file to be used. Corrected over on the ESRI Python forum thread as linked above. –  V Stuart Foote Dec 9 '10 at 15:07
add comment

Okay, here are a couple of things:

  1. I don't think the indention level of the code you posted is correct. The variable filename is used outside its scope, for example.

  2. Some uses of os.path.join in that code are pointless. The string fragments should be arguments: os.path.join(root, filename_zero + ".png")

  3. It is a good practice to avoid using try..except in this manner. Here is an alternative syntax that should do the same thing but is more Pythonic.

    for filename in filenames:
       name = path.splitext(filename)[0]
       prefix = name[0:2] or "XX"
    

Finally, use the pdb module to step through the code (link). Much easier to catch future input errors this way.

share|improve this answer
add comment

Try changing

Output_polygon_features = "D:\\J04-0083\\ShapeFiles.gdb\\" + "SH_" + filename + ".shp"

to

Output_polygon_features = "D:\\J04-0083\\ShapeFiles\\" + "SH_" + filename + ".shp"

And rename your output directory accordingly.

I think ArcGIS is getting confused between creating a feature class called "SH_" + filename + ".shp" in a file geodatabase called ShapeFiles, and creating a shapefile in a folder called ShapeFiles.gdb

Caveat: this made the difference for me, but I have v10, and cannot test on 9.3

share|improve this answer
    
Thank you for your help! –  Alice Dec 9 '10 at 7:40
add comment

I would suggest trying to print out the output at each step of your program. For example:

for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        print filename
        filename_split = os.path.splitext(filename) 
        print filename_split
        filename_zero = filename_split[0]
        print filename_zero

An alternative could be to use, gp.AddMessage( filename ) etc, if you are running it from ArcGIS too.

Also, do you need '\' after "D:\J04-0083\IMAGEFILES" ?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.