Select to view content in your preferred language

Extract by Mask invalid characters

799
2
Jump to solution
11-18-2022 10:28 AM
JacobSpear1
Occasional Contributor

I am trying to extract a landsat8 image and SRTM terrain to a study area's extent. I first projected the data (which runs fine) and I have run this section of code before with no issues. Now it is saying that it is unable to execute the extract by mask because of invalid characters. I have double-checked the names and file paths but there are no spaces or other invalidating naming conventions (as far as I can tell).

Can anyone help point me to where I am wrong?  For clarification, projecting the study area polygon and the imagery layer works fine, but it fails on the first extract by mask

 

#Import modules
import arcpy
from arcpy.sa import *
from sys import argv
import os

#Set environment settings
arcpy.env.overwriteOutput = True
arcpy.env.workspace = 'Project_5.gdb'
arcpy.env.scratchWorkspace = 'Project_5_Scratch.gdb'

#Establish variables
imagery = arcpy.Raster("D:\\Documents\\School\\USC\\586\\586_Project_5\\DATA\\Landsat\\LC08_L2SP_015036_20221108_20221115_02_T1\\LC08_L2SP_015036_20221108_20221115_02_T1_MTL.txt")
terrain = arcpy.Raster("D:\\Documents\\School\\USC\\586\\586_Project_5\\Project_5\\Project_5.gdb\\STRM_VoidFilled")
studyextent = "onslow_county"
projection = arcpy.SpatialReference(32145)
classificationmodel = "LandCoverClassification.dlpk"

#Input data preperation. projects input data into specified coordinate system and clips rasters to study area. 
studyareaprj = "studyareaprj"
arcpy.management.Project(in_dataset = studyextent, out_dataset = studyareaprj, out_coor_system = projection)

terrainprj = "terrainprj"
arcpy.management.ProjectRaster(in_raster = terrain, out_raster = terrainprj, out_coor_system = projection)
terrainprj = arcpy.Raster(terrainprj)

terrainmskprj = "terrainmskprj"
outterrainmsk = arcpy.sa.ExtractByMask(in_raster = terrainprj, in_mask_data = studyareaprj, extraction_area="INSIDE")
outterrainmsk.save(terrainmskprj)

imageryprj = "imageryprj"
arcpy.management.ProjectRaster(in_raster = imagery, out_raster = imageryprj, out_coor_system = projection)
imageryprj = arcpy.Raster(imageryprj)

imagerymskprj = "imagerymskprj"
outimagemsk = arcpy.sa.ExtractByMask(in_raster = imageryprj, in_mask_data = studyareaprj, extraction_area="INSIDE")
outimagemsk.save(imagerymskprj)

prepmsg = "Input data has been projected to specified coordinates and clipped to study extent."
print(prepmsg)
arcpy.AddMessage(prepmsg)

 

Traceback (most recent call last):
File "D:\Documents\School\USC\586\586_Project_5\Project_5\SiteSuitability.py", line 30, in <module>
outterrainmsk = arcpy.sa.ExtractByMask(in_raster = terrainprj, in_mask_data = studyareaprj, extraction_area="INSIDE")
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 4237, in ExtractByMask
return Wrapper(
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Utils.py", line 55, in swapper
result = wrapper(*args, **kwargs)
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\sa\Functions.py", line 4230, in Wrapper
result = arcpy.gp.ExtractByMask_sa(
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 512, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000354: The name contains invalid characters
Failed to execute (ExtractByMask).

0 Kudos
1 Solution

Accepted Solutions
JacobSpear1
Occasional Contributor

Fixed it!

It was being really picky with how I specified the workspace. The errors were fixed when I included the entire file path for setting the env workspace.

View solution in original post

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Code formatting ... the Community Version - Esri Community

line numbers would help

your in_raster = 

statements are both text strings and not variable names/file path references (eg they should be in double quotes)


... sort of retired...
0 Kudos
JacobSpear1
Occasional Contributor

Fixed it!

It was being really picky with how I specified the workspace. The errors were fixed when I included the entire file path for setting the env workspace.

0 Kudos