I'm trying yo execute the RastertoPolygon conversion tool in ArcGIs 10.1. I'm executing it through a for loop, then it resulted to an error (Unable to open feature class Failed to execute (RasterToPolygon)) at the python console. As I checked for some output, the first raster from the list is successfully converted to shp file while the rest are not. Any suggestions? Note: (All of my rasters are already in integer data type)
#import the module
import arcpy
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("Spatial")
env.overwriteOutput = True
#set the workspace
arcpy.env.workspace = r"C:\Users\Windows\Documents\JO_GIS_Analyst"
#Get a list of rasters and convert to shapefile
for raster in arcpy.ListRasters("nofpt*", "TIF"):
print raster #check the presence of rasters"
#convert the raster to polygon
arcpy.RasterToPolygon_conversion(raster, raster +".shp", "SIMPLIFY")
print "Finish converting the rasters to polygon"
Solved! Go to Solution.
Ok, does it print the other raster names and just not produce anything? if it prints the other names, have you looked on disk to confirm whether they are or are not there. I am not sure about the wildcard you are using either,
are these lines indented? your code isn't formatted correctly if it isn't
print raster #check the presence of rasters"
#convert the raster to polygon
arcpy.RasterToPolygon_conversion(raster, raster +".shp", "SIMPLIFY")
oh, sorry for my formatting, yes my for loop is indented.
Ok, does it print the other raster names and just not produce anything? if it prints the other names, have you looked on disk to confirm whether they are or are not there. I am not sure about the wildcard you are using either,
Apparently the wildcard is working, assuming that the rasters of interest start with "nofpt" are TIF fiels and reside in the workspace indicated.
Some remarks:
Have a look at the code below. This will remove the .tif extension from the output shapefile name (see line 9).
import arcpy
arcpy.env.overwriteOutput = True
#set the workspace
arcpy.env.workspace = r"C:\Users\Windows\Documents\JO_GIS_Analyst"
#Get a list of rasters and convert to shapefile
for raster in arcpy.ListRasters("nofpt*", "TIF"):
shp_name = "{0}.shp".format(os.path.splitext(raster)[0])
print "Converting raster '{0}' to polygons '{1}'".format(raster, shp_name)
#convert the raster to polygon
arcpy.RasterToPolygon_conversion(raster, shp_name, "SIMPLIFY")
print "Finish converting the rasters to polygon"
For testing you could comment out line 13 and see what it prints. How many rasters are found?
Kind regards, Xander
thank you for this! Now, I've successfully converted my rasters.
Great to hear you managed to solve your problem