Convert raster in model builder: unable to open feature

1777
6
04-24-2017 09:50 AM
johnpolo
New Contributor III

Tried to use model builder to convert several rasters to polygons. Here is the script exported to Python:

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# convertraster.py
# Created on: 2017-04-24 11:38:52.00000
#   (generated by ArcGIS/ModelBuilder)
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy

# Load required toolboxes
arcpy.ImportToolbox("Model Functions")


# Local variables:
copiesx = "C:\\Documents\\GIS\\temp\\copiesx"
tx_107_tif = "C:\\Documents\\GIS\\temp\\copiesx\\tx_107.tif"
v_name__shp = "C:\\Documents\\GIS\\temp\\shapx\\%name%.shp"

# Process: Iterate Rasters
arcpy.IterateRasters_mb(copiesx, "", "", "NOT_RECURSIVE")

# Process: Raster to Polygon
arcpy.RasterToPolygon_conversion(tx_107_tif, v_name__shp, "NO_SIMPLIFY", "Value")

I've restarted ArcMap (10.2. ) several times because the model keeps failing with the following message:

ERROR 010157: Unable to open feature class ... Failed to execute (Raster to Polygon).

I checked the error code. '... This may be due to incorrect paths or lack of permissions to access folders.' Don't think it's that, I know where the files are and made them myself, I have the permissions. It also says that the files could be corrupt, but I can load the files without problem. I executed one Raster to Polygon outside the model and it worked. Any guesses how to get the model to work?

0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

iterators can only be used in modelbuilder the fine-print is hidden at the top of the documentation here  which isn't repeated elsewhere... but it states for all iterators (except for and while...)

Note:

The tools are intended for use only in ModelBuilder and not in Python scripting.

johnpolo
New Contributor III

Thanks, Dan, for replying. Very sorry that I was unclear; I was trying to use the iterator and model inside the model window. I did not try to use the iterator or model in Python. I only exported the model to Python because I thought it might make the model steps appear clear without having to make a screen shot of the model. I should have wrote that.

I wonder if the problem was with using .tifs as my input rasters, instead of the native format, .grid or whathaveyou.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Ok... try a different folder then... anything in 'Documents' could be iffy, but your paths print fine, so not 'finding' them is not the issue I suspect

0 Kudos
curtvprice
MVP Esteemed Contributor

I'm with Dan, it looks an awful lot like a pathname issue, for example if the output model element %Name% contains spaces within an iteration you will see an error message like this as shapefile names can't have spaces, so the feature class (shapefile) can't be created with that pathname. Similarly make sure you are avoiding special characters.  

An automated fix in Model Builder is to use the Calculate Value tool to make the name legal. Rename the Calculate Value output element to %Name1%, connect it to your Raster To Polygon tool as a precondition, and then, in your output path, replace %Name% with %Name1%. This will convert an ugly name like "z1&.tif" to "z1__tif" that can be safely used for a shapefile name.

Expression

fix_name(r"%Name%")‍‍‍

Code Block

def fix_name(nm):
  return arcpy.ValidateTableName(nm)‍‍‍‍‍‍
johnpolo
New Contributor III

Curtis,

Thanks for your response. I did as you said. Here are screenshots of the steps.

screen shot of Raster to Polygon settings with path and new file indicated

screen shot of Create Value output changed to match value in Raster to Polygon path

screen shot of model builder with Create Value tool inserted and pointing to Raster to Polygon as a precondition

The model ran. While it ran, it seemed to iterate through all the rasters. However, it only created one polygon, the last one in my list.

0 Kudos
curtvprice
MVP Esteemed Contributor

Make tx107.tif a precondition to the Calculate Value to make sure Name1 gets refreshed for every iteration.