Select to view content in your preferred language

Raster to polygon script loop failing!! error 99999!

3349
18
Jump to solution
12-07-2010 02:20 AM
AliceDuff1
Emerging Contributor
Hello!

I am have already posted this in the general forum but i managed to solve that issue and now i have a new one!

I keep getting an error 99999 error message at line 37!
( gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features, "SIMPLIFY", "VALUE") )
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"!

Can anyone see why this isnt working? I am very, very new to this and have been staring at this script pulling out my hair for days!! 😮

Here is the script:

# ---------------------------------------------------------------------------
# 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", "")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
V_StuartFoote
MVP Frequent Contributor
Alice,

Welcome back, fun and happy holidays?

So, before you can get back to scripting the creation of your buffers, you MUST correctly establish the projection and registration details for each PNG image you want to process.

So, I'm guessing that your Klic application is the Arcadis Klic viewer and you are dealing with PNG derived from GBKN (Grootschalige Basiskaart Nederland) projected in Dutch RD--EPSG 28992 (worked in ArcGIS as RD New coordinate system) or from other European imagery in WGS84 GCS lat/long.

In any case, the projection details you need are likely available as metadata contained in the XML and PDF files accompanying each image. The XML will be well structured and tagged, although probably not GeoXML. But you will need to study it along with any metadata in the PDF for each PNG image, and from the metadata determine where to position each image in real world coordinates. Fortunately, all the images will probably be in the same projection and the needed information will be encoded the same for all the file sets. You just have to figure out how. You may be given an upper left and lower right coordinate set, or an upper left--a pixel size--and a rotation, or even all four corners of the image.

If you are allowed to post a sample of the XML and an excerpt from the PDF along with a PNG raster and we may be able to help. But you might post that as a new thread on the Imagery and Raster Data forum to get the right folks helping to tease out the georeference details.

Then, once you assemble the registration data for the PNGs, you can either convert them to GeoTIFFs (with a GDAL script) or create an ESRI Grid World file for each.

And finally, you can get back to the geoprocessing of a 5m buffer.

You will have several options once you are able to access the georeferenced image with ArcGIS.

And you can bring each into an ArcMap feature editing session and digitize a centerline of each pipeline. Or, you can return to your gp.RasterToPolygon_conversion(), or perhaps try the RasterToPolyline_conversion() tool. The key will be that by having georeference of each image, you'll then be able to work with accurate measurements in meters rather than just image pixels.

One additional scripting option if you have ArcGIS 10 would be the new Image Classification tools for either unsupervised or supervised with training. A more sophisticated way to extract the pipeline detail out of the PNG raster and pass on for RasterToPolygon or RasterToPolyline conversion.

If someone at the office has ERDAS or ENVI image analysis suite they really are better tools for doing the same feature extractions.

Stuart

View solution in original post

0 Kudos
18 Replies
V_StuartFoote
MVP Frequent Contributor
Alice,

Guess no one is going to answer you. So here is my offering.

1) if you are on ArcGIS 9.3 (you're not on 9.2 are you?) you should create the Geoprocessor object with the 9.3 enhancements. 

[INDENT]gp = arcgisscripting.create(9.3)[/INDENT]

At ArcGIS 10 you can keep the (9.3) arcgisscripting syntax or shift to the more complete ArcPy Site Library.


2) You don't need to add the ArcToolbox code--those functions are included when you create the Geoprocessor in your Python script. And would cause problems if you were to add the script into a custom toolbox.

3) Working with the ShapeFiles.gdb File Geodatabase, you either need to have created it outside the script or create it when opening the script with:

[INDENT]gp.CreateFileGDB ("D:\\J04-0083", "ShapeFiles.gdb")[/INDENT]

4) As scripted the result of the gp.RasterToPolygon is being output as ESRI Feature Classes within the File Geodatabase.  But, you're putting your results into a file geodatabase "D:\\J04-0083\\ShapeFiles.gdb" and trying to name them .shp--that doesn't make them shape files. Rather, using the extension actually causes the gp script to throw an error.  In your script, just drop the .shp and they'll be properly created as Polygon Feature Class in the geodatabase.

5) You went the other way with the Buffer_analysis. Output from that was dropping to the file system as Shape files in the D:\\J04-0083 folder.  It would have been more consistent to keep both the geoprocessing polygon outputs together in the same format.

Again, I would drop the .shp extension, and direct the output into the file geodatabase--i.e., change the output path to D:\\J04-0083\\ShapeFiles.gdb.

[INDENT]Raster_Buffer_shp = "D:/J04-0083/ShapeFiles.gdb/" + "SB_" + filename_zero[/INDENT]

6) Finally, the problem with scripting this way is that you will end up with output from each script run in the working directory. I think the reason you were getting the 99999 errors is probably because you had pieces left over from prior run that had name conflicts.  You have to clear things out at each script restart, i.e. empty the working directory, and empty/delete the File Geodatabase.

Do that and you'll reach the more useful ExecuteErrors.

7) when you are done creating your buffers, work with them in the File Geodatabase. Or, you can export them to Shapefile if you need that format for some other application.

Here is your code cleaned up...I find forward slashes more readable than double backslashes, Python keeps it all straight. Also, if your PNG rasters have some projection details you may need world files for them to end up with usable results. I threw together some simple PNGs to test with, so your buffer value of "5 Meters" is set to "5" default pixels.

# ---------------------------------------------------------------------------
# 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(9.3)

# 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...

gp.workspace = "D:/J04-0083"

gp.CreateFileGDB ("D:/J04-0083", "ShapeFiles.gdb")

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_zero
            # Process: Raster to Polygon...

            INPUT_RASTER = os.path.join(root + "/" + filename)
            gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features) 

            Distance__value_or_field_ = "5"
            Raster_Buffer_shp = "D:/J04-0083/ShapeFiles.gdb/" + "SB_" + filename_zero

            # Process: Buffer...
            gp.Buffer_analysis(Output_polygon_features, Raster_Buffer_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE")


Post back if you are still stuck.

Stuart
0 Kudos
JeffLee
Deactivated User
If I may add my two cents,  it may not be good practice to include hyphen's in directory path (i.e. D:\\J04-0083\\IMAGEFILES) when working with the grid module.  A lot of problems can be avoided by eliminating spaces and unusual characters in the directory path.

http://webhelp.esri.com/arcgisdesktop/9.2/index.cfm?TopicName=Naming_output_rasters
0 Kudos
AliceDuff1
Emerging Contributor
Thank you so much for your help!

When i first ran it i got a different error but i removed the "-" from the workspace file name and that error was no longer an issue!

..however..

Now i get a new error which states:

"error 00860 input raster is not the type of composite geodataset, or does not exist"

But then i ran raster to polygon manually for the first LG file in the image files folder - which i assume would be the first one the script would process, and it worked perfectly!! The script gets as far as greating the geodatabase but that is it (error on line 40! gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features))

Im so sorry to take up so much of your time! Thank you so much for debugging my script!!!!
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Alice,

OK, so this was totally MY fault.

What did I suggest changing in handling the file name when selecting the PNG images out of the IMAGES folder? 

See it... on line 39?  OK, and think what happens if there are any files that are not Raster images, like a .PGW world file, or meta data? You'd be trying to pass them in as rasters, but they may not be.

So to fix it... could simply revert to your original .PNG image selection logic.

Change line  #39
            INPUT_RASTER = os.path.join(root + "/" + filename)

to read
            INPUT_RASTER = os.path.join(root + "/" + filename_zero + ".png")

or even hardcode the path as with the earlier Output_Polygon_features assignment

            INPUT_RASTER = "D:/J04-0083/IMAGEFILES/" + filename_zero + ".png"


And see if that was the problem.

Also, as was suggested by celenius over on the GIS Stackexchange thread, you could insert a print (<YourVariableName>) statement just after the selection to help debug the script. Comment it out when the logic is correct.

And thinking about it, maybe this is not robust enough. As it is, you'll need to be sure that only the .PNG image files start with LG--and you won't be able to have the matchiing LG<imagefileName>.PWG colocated in the folder that may be needed for the geoprocessing.

Maybe since you have the file extension available in filename_split[1] from the os.path.splitext, you could make use of that in another "if =="  to ONLY buffer specific image file types, e.g. PNG, png, JPG, jpeg etc. in the IMAGES folder when running through it.

Stuart
0 Kudos
AliceDuff1
Emerging Contributor
success!!! I have successfully made a shapefile and a buffer!! But there is a problem in that at line 47

  gp.Buffer_analysis(Output_polygon_features, Raster_Buffer_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE")


I get an error saying the the file i am trying to create already exists. This makes me think that something has gone wrong where the script doesnt know what it already has modified so it tried to make the same shapefile and buffer again but it cant because they already exist!!
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Alice,

Progress 🙂

So, had you emptied and deleted the ShapeFiles.gdb FGDB--resetting prior to the run?

Or, was the duplicate feature class name occurring during the same script run? If so, what exactly do you have in your IMAGES folder?  Just the PNGs? Any chance the os.path.splitext(filename) is not making the filename unique, possibly extra periods in the image name?  Maybe capture a screen shot of the directory and post it.

Again, you may need to adjust the parsing logic to only attempt the RasterToPolygon on the .PNG by testing the file extension before passing through the filename_zero.

And set a print(Output_polygon_features) statement to keep track on the console while running; or as bwreilly suggested on the GIS Stackexchange thread, run inside the debug module pdb.run() to step through. You should be able to see when the duplicate is occurring.

And I'll split a hair here to say that if you are still using the File Geodatabase as the output container for your geoprocessing you are not creating Shapefiles rather they are Feature Classes. But get the script fully working before worrying about that distinction.

Stuart
0 Kudos
AliceDuff1
Emerging Contributor
Thank you! Im not in the office again until monday so i will test it all then! Thank you again for your help!!! 😄 I will let you know what happens - im sure it is something with pathnames and where im saving everything! Screen shots to come!
0 Kudos
V_StuartFoote
MVP Frequent Contributor
Alice,

OK, will look to continue with it Monday.

And once you get it all worked out please consider posting the clean script up in code brackets for benefit of the next poor soul..

Stuart
0 Kudos
AliceDuff1
Emerging Contributor
Hello!! I checked all the filenames etc and they are all the same!:(

Ive saved some print screen of the contents of the files and the error message i am getting and here is the code!

Thank you again for being so helpful!

# ---------------------------------------------------------------------------
# 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(9.3)

# 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...

gp.workspace = "D:/J040083"

gp.CreateFileGDB ("D:/J040083", "ShapeFiles.gdb")

folder = "D:/J040083/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:/J040083/ShapeFiles.gdb/" + "SH_" + filename_zero
            # Process: Raster to Polygon...

            INPUT_RASTER = os.path.join(root + "/" + filename_zero + ".png")

            gp.RasterToPolygon_conversion(INPUT_RASTER, Output_polygon_features)

            Distance__value_or_field_ = "5"
            Raster_Buffer_shp = "D:/J040083/ShapeFiles.gdb/" + "SB_" + filename_zero

            # Process: Buffer...
            gp.Buffer_analysis(Output_polygon_features, Raster_Buffer_shp, Distance__value_or_field_, "FULL", "ROUND", "NONE")
0 Kudos