Thursday 6 February 2020

arcpy - Failed to execute (RastertoPolygon) Syntax error at or near symbol SPACE


I'm converting series of raster to polygon using RastertoPolygon in arcpy.


#import the module
import arcpy
from arcpy.sa import *
from arcpy import env
arcpy.CheckOutExtension("Spatial")
env.overwriteOutput = True


#set the workspace
env.workspace = r"C:\thesis\for sampling\sampling_outputs\raster_per_biogeoregion"

#Get a list of rasters and convert to shapefile
for raster in arcpy.ListRasters("*Luzon_*.tif", "TIF"):
print raster #check the presence of rasters
arcpy.RasterToPolygon_conversion(raster, raster.replace("tif", "shp"), "NO_SIMPLIFY")
print "Finish converting all the rasters to polygon"


After several checks on the syntax, formatting, I've always been stuck with this error:


 Traceback (most recent call last):
File "C:\Users\brentiebark\Dropbox\Python Scripts and mxds\batch_convert_raster_to_polygon_simple.py", line 14, in
arcpy.RasterToPolygon_conversion(raster, raster.replace("tif", "shp"), "NO_SIMPLIFY")
File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\conversion.py", line 178, in RasterToPolygon
raise e
ExecuteError: ERROR 010328: Syntax error at or near symbol SPACE.
ERROR 010267: Syntax error in parsing grid expression.
Failed to execute (RasterToPolygon).


Are there things that I missed from this code?



Answer



The error in your "RastertoPolygon" script is due to a space in the file name of one or more of your input rasters. To confirm this answer, I ran your script on a folder of raster files and it worked perfectly. I added a space to the file name of one of the rasters and it failed with the message "ExecuteError: ERROR 010328: Syntax error at or near symbol SPACE", which is the same error message that you got.


Arcpy does not allow spaces or periods in file names. Please make sure that none of your input file names have special characters like spaces or periods in them and try again.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...