0

So I'm converting a set of polygons into rasters and need to remove a portion of their basenames while keeping them unique at the same time. The error code I am getting is "TypeError: unsupported operand type(s) for -: 'unicode' and 'str'" obviously it has something to do with the " - ". I have tried to designate "_buffer" as a string [str("_buffer")] and that didn't work either.

here is the code

import arcpy
from arcpy import env
env.workspace = r"N:\geog327\python15\Project"
env.overwriteOutput = True

DenMetHwyBuff = r"N:\geog327\python15\Project\DenMetHwys_buffer.shp"
DenMetIntBuff = r"N:\geog327\python15\Project\DenMetInts_buffer.shp"
DenMetRiverBuff = r"N:\geog327\python15\Project\DenMetRivers_buffer.shp"
DenMetLakeBuff = r"N:\geog327\python15\Project\DenMetLakes_buffer.shp"

vectorlist4 = [DenMetHwyBuff, DenMetIntBuff, DenMetRiverBuff, 
DenMetLakeBuff]
outraster = desc.basename - "_buffer.shp" + ".tif"
for fc in vectorlist4:
    desc = arcpy.Describe(fc)
    outraster = desc.basename - "_buffer.shp" + ".tif"
    arcpy.PolygonToRaster_conversion (fc, "Bad", outraster) 
7
  • 2
    Minus isn't a string operand, try desc.basename.replace("_buffer.shp","") +".tif" or desc.basename[:-11] + ".tif"; there are better operations in the os lib.. os.path.dirname() os.path.basename() os.path.join() how do you want your output names to come out? Commented Apr 27, 2017 at 5:42
  • I would prefer to add something like "rst" to the file path names but I know I'll go over the 13 character limit for one of them. If they can retain their unique names with the conversion such as DenMetLakes, DenMetHwys, etc. that would be fine. If I run outraster = desc.basename.replace("_buffer.shp") that will keep them unique correct? Do I need to designate something to replace?
    – Noah P
    Commented Apr 27, 2017 at 5:45
  • Only Esri GRID files have a 13 character limit.. TIFF files only have an operating system limit of 4096 characters and not more than 256 folders deep (or something like that). You could use fn, fe = os.path.splitext(fc) then outraster=fn + ".tif" to save in the same place with the same name as the shapefile.. as it has a different extension the files will be different enough, but if you want to get rid of the '_buffer' on the end outraster=fn[:-7]+'.tif' to trim off the last 7 characters and append the .tif extension. Commented Apr 27, 2017 at 5:48
  • Thank you for the help. I got over this hump but am now getting an ERROR 999999. This seems to be an issue we have working with raster over a network. However I am working off of my local disc and yet I'm still getting this error. I think I'll just turn in for the night and ask my professor about it tomorrow unless you might know why. Thanks again Edit: realized I didn't change all my filepaths, changed them to the correct ones and still got the error sigh
    – Noah P
    Commented Apr 27, 2017 at 5:55
  • The string function replace("old text","new text") tutorialspoint.com/python/string_replace.htm will replace the first instance of "old text" with "new text" case sensitive, it would be far better to use the string indexing [:-HowManyToRemove] to trim back the file name stackoverflow.com/questions/663171/… Commented Apr 27, 2017 at 5:56

1 Answer 1

1

I would like to make it very simple by changing outraster variable a litle bit. I hope it will solve your problem:

vectorlist4 = [DenMetHwyBuff, DenMetIntBuff, DenMetRiverBuff, 
DenMetLakeBuff]
#outraster = desc.basename - "_buffer.shp" + ".tif"
for fc in vectorlist4:
    desc = arcpy.Describe(fc)
    basename=desc.basename
    outraster =basename.split("_")[0] +  ".tif"
    arcpy.PolygonToRaster_conversion (fc, "Bad", outraster) 

This will devide the name with two part based on "_". they you have firt element for your raster name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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