Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to batch process the conversion of shapefiles to rasters. I am stuck on how to specify the name for each output raster file. Part of the trick is that my input shapefiles are the names of frogs in "genus_species" format and go over the 13 character requirement of rasters.

For example, here are the names of my first two shapefiles: Acris_crepitans.shp Anaxyrus_americanus.shp

I would like the output rasters to be as follows (i.e., the first two letters of the genus, underscore, and then however many remaining characters can fit for the species): Ac_crepitans An_americanus

Here is my code thus far (it's probably horrible b/c I'm new to Python):

********************************************************************************
# Import arcpy module
import arcpy
from arcpy import env

#Set working environment
env.workspace = "C:\\GIS_data\\Frog_shps"
Dir = env.workspace

#List FCs
fcList = arcpy.ListFeatureClasses() 

# Loop
for fc in fcList:

  output = Dir + "\\" + [here is where I am stuck]

# Process: Polygon to Raster
arcpy.FeatureToRaster_conversion(fc, "BINOMIAL", output, 1000) 
print "finished polygon to raster"

********************************************************************************
share|improve this question
    
You probably have a reason to use ESRI GRIDs, but if you save to .tif format, you can bypass the name length restriction. –  phloem Jan 27 at 19:05
    
There's nothing horrible with your code - it's great to see you explain where you are stuck using a code snippet rather than a Python application. –  PolyGeo Jan 27 at 22:50

1 Answer 1

up vote 3 down vote accepted

Not very elegant but this should work:

output = Dir + "\\" + (fc[:2]+"_"+fc.split("_")[1][:-4])[:13]

By the way, not sure if there was an indentation issue when you pasted the code in GIS.SE, but the for loop should be something like:

# Loop
for fc in fcList:
  output = Dir + "\\" + (fc[:2]+"_"+fc.split("_")[1][:-4])[:13]

  # Process: Polygon to Raster
  arcpy.FeatureToRaster_conversion(fc, "BINOMIAL", output, 1000) 
  print "finished polygon to raster"

That is, the last 3 lines of the script should be inside the for loop.

share|improve this answer
    
This worked perfectly! –  Sarah H. Jan 27 at 20:41
    
@SarahH. Great! Please consider upvoting (yes, not only marking it as accepted :D) the answer if you consider it to be useful. –  gcarrillo Jan 27 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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