-1
import arcpy, glob, os
from arcpy import env
from arcpy.sa import *

# Set the input workspace
arcpy.env.workspace = r"F:\madhavi\images_to_clip"

arcpy.CheckOutExtension("Spatial")

# Absolute path to your mask layer
mask = "F:\madhavi\shapefile\shp_gang_only_final.shp"

# Copying all the input rasters in an array
rasters = arcpy.ListRasters()

# Loop through rasters, append names and save files
for raster in rasters:
    output_raster = raster.replace(".TIF", "_clip.TIF")
    rasterObject = arcpy.gp.ExtractByMask_sa(raster, mask, output_raster)
    rasterObject.save(r"F:\madhavi\clipped_images")    

There are two problems that I am encountering with this Python script and they are as follows:

Firstly, the script is clipping only the first raster in the folder: F:\madhavi\images_to_clip and not for all the rasters in that folder. Therefore, the for loop is not working properly.

By the way, the folder on which the for loop is working contains only two .TIF images.

Secondly, the script is not saving the outputs in the desired folder.

I am still getting "Parsing Error SyntaxError: EOL while scanning string literal (line 18)". Therefore, there is a problem in "output_raster = raster.replace(".TIF", "_clip.TIF")".

Sujai Banerji
  • 27
  • 1
  • 9
  • Is the indentation in your question the same as in your script? (Please [edit] to correct, if not.) – Erica Apr 17 '17 at 11:13
  • @Erica: I have corrected the indenting for the lines 18, 19 and 20. However, the same error persists. – Sujai Banerji Apr 17 '17 at 18:03
  • If you add two lines `print raster` and `print raster.replace(".TIF", "_clip.TIF")` in your `for` loop (with the rest of the logic commented out for now), what is the output? If it's got a problem with the string syntax, that is the simplest way to identify (and then correct). – Erica Apr 17 '17 at 18:05
  • There are a lot of ArcPy questions answered at the [gis.se] Stack Exchange where this would be more on-topic. – PolyGeo Apr 17 '17 at 20:27
  • @Erica: When I add "print raster" and "print raster.replace(".TIF, "_clip.TIF")", I get the correct outputs. Therefore, I think there is something wrong in the last two lines. – Sujai Banerji Apr 18 '17 at 08:22
  • @PolyGeo: I had posted this question on Geographical Information Systems Stack Exchange, but I did not get a satisfactory response from the community members. – Sujai Banerji Apr 18 '17 at 08:25

1 Answers1

1

I think the problem is in this variable assignment:

mask = "F:\madhavi\shapefile\shp_gang_only_final.shp"

The error message is complaining about string literal -- so try instead ensuring it is a string literal:

mask = r"F:\madhavi\shapefile\shp_gang_only_final.shp"
Community
  • 1
  • 1
Erica
  • 2,399
  • 5
  • 26
  • 34