Select to view content in your preferred language

Setting Mask from Parameter

1279
7
Jump to solution
12-19-2013 11:34 PM
anTonialcaraz
Frequent Contributor
Hi everyone,

This may seem like an easy one but I cannot quite find the way:

When setting the environments, I want to use one of the parameters ("InputZ") as a mask for the different processes within the script.

It works fine like this:
arcpy.env.mask = r"D:\PROGRAMMES\LFP_Source_Rocks\ArcGIS\00_LFP_GLOBAL_MODEL\INPUT.gdb\Maas_Bathymetry"

but I want this:
arcpy.env.mask = (InputZ)

using ArcView 10.0

here is the beginning of the code:

import arcpy import string from arcpy import env arcpy.env.overwriteOutput=True from arcpy.sa import * arcpy.CheckOutExtension("Spatial")  # Set the output location, extent and Mask  OutLocation = arcpy.GetParameterAsText(0)  arcpy.env.extent = arcpy.Extent(-180.0, -90.0, 180.0, 90.0)  arcpy.env.mask = (InputZ)  # Input strings and variables  StageAge = str(arcpy.GetParameterAsText(1))  BSS = arcpy.Raster(arcpy.GetParameterAsText(2))  InputLatitude = arcpy.Raster(arcpy.GetParameterAsText(3))  InputZ = arcpy.Raster(arcpy.GetParameterAsText(4))  InputCountrylines = arcpy.GetParameterAsText(5)  NPPEquation = str(arcpy.GetParameterAsText(6))


Any help will be greatly appreciated.

Many thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
Toni,

Did you remove the 'arcpy.Raster' from the arcpy.GetParameterAsText?  Also, what data type are you setting for this parameter in your script tool?  For example, I had the following script:

import arcpy arcpy.CheckOutExtension("Spatial") from arcpy import env env.workspace = r"C:\data\raster\dem"  InputZ = arcpy.GetParameterAsText(0)  env.mask = InputZ  arcpy.Slope_3d("example","Slope","DEGREE","1")


I then added this to a toolbox and set the parameter to a Raster Dataset:

[ATTACH=CONFIG]30030[/ATTACH]

When running the tool, the slope masked the raster I chose.

View solution in original post

0 Kudos
7 Replies
JakeSkinner
Esri Esteemed Contributor
Try passing the raster as a string rather than a raster object.  Ex:

arcpy.env.mask = InputZ

InputZ = arcpy.GetParameterAsText(4)


I also removed the parentheses for the env.mask.  Not sure if this matters, but adding the parentheses will create a tuple rather than a string.
0 Kudos
anTonialcaraz
Frequent Contributor
Thanks Jake,

I just tried that with and without parentheses for the env.mask but I'm getting the message "InputZ is not defined"

Cannot find the way really...

Many thanks

Toni
0 Kudos
anTonialcaraz
Frequent Contributor
Hi again Jake,

It works like this:

arcpy.env.mask = str(InputZ)

Many thanks again

Toni
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Toni,

Did you remove the 'arcpy.Raster' from the arcpy.GetParameterAsText?  Also, what data type are you setting for this parameter in your script tool?  For example, I had the following script:

import arcpy arcpy.CheckOutExtension("Spatial") from arcpy import env env.workspace = r"C:\data\raster\dem"  InputZ = arcpy.GetParameterAsText(0)  env.mask = InputZ  arcpy.Slope_3d("example","Slope","DEGREE","1")


I then added this to a toolbox and set the parameter to a Raster Dataset:

[ATTACH=CONFIG]30030[/ATTACH]

When running the tool, the slope masked the raster I chose.
0 Kudos
anTonialcaraz
Frequent Contributor
Hi Jake,

It does work your way also.

I did remove the "arcpy.Raster". In my toolbox I set the data as "Raster Layer". I guess it doesn't make any difference compared to"Raster Dataset" in this case (I've tried and both work).

When I tried when you first suggested I had env.mask = InputZ written before InputZ = arcpy.GetParameterAsText(0) when it should go after.

It works also like this: env.mask = str(InputZ)

Many thanks

Toni
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Glad this is working.  Also, wanted to point out, in case you were wondering, the difference between Raster Layer and Raster Dataset is that Raster Layer will allow your tool to have a dropdown and choose a raster within your current map document.
0 Kudos
anTonialcaraz
Frequent Contributor
Thanks a lot Jake. Really useful indeed.

I actually have another issue with the same script related to strings.
I'll explain that in a new post.

Many thanks
0 Kudos