Select to view content in your preferred language

Why are the environment settings not honored in my python script?

606
4
06-01-2023 02:03 AM
Nicole_Ueberschär
Esri Regular Contributor

I am testing a workflow in python (3.9) with a couple of raster calculations that require environment settings like the mask, output coordinate system and cellSize settings. While it respects the output coordinate system in one part of the script, it ignores the cellSize as well as the mask in the next function. When I am running the raster calculator tool in ArcGIS Pro with the environment settings I am getting the expected outcome. I tried using with arcpy.EnvManager(...) and setting arcpy.env.cellSize etc. but it seems to just ignore me. I also tried using ResetEnvironments() to make sure that it is not using other settings but no change. I tried giving the cellSize as a number (100) or as reference to a raster dataset. Also tried to run the script as script tool and neither from the script nor from the tool the environment settings seem to be honored. Syntax itself seems to be correct as I don't get any errors in pyScripter. Any idea? Happy to share them full script if someone wants to have a look...

This is one of the calculations:

 

 

with arcpy.EnvManager(cellSize=100):
        raster2=[rainday,"C:/data/EWIS.gdb/land100"]
        landslidealarm=RasterCalculator(
            rasters=raster2,
            input_names=["raster1","raster2"],
            expression="raster1 * raster2"
            )

 

 

 

 

0 Kudos
4 Replies
by Anonymous User
Not applicable

This is a head scratcher.

You could print out all the env settings with the Listenvironments() method to see if its setting as a before and after as a peek into the env values:

import arcpy

environments = arcpy.ListEnvironments()

# Sort the environment names
environments.sort()

for environment in environments:
    print(f"{environment}:\t{arcpy.env[environment]}")

arcpy.env.cellSize = 100
arcpy.env.workspace = 'Hello!'

environments = arcpy.ListEnvironments()

# Sort the environment names
environments.sort()

for environment in environments:
    print(f"{environment}:\t{arcpy.env[environment]}")

with arcpy.EnvManager(workspace='Hi Back'):
    environments = arcpy.ListEnvironments()

    # Sort the environment names
    environments.sort()

    for environment in environments:
        print(f"{environment}:\t{arcpy.env[environment]}")

 

 

Nicole_Ueberschär
Esri Regular Contributor

Thanks for the hint and for including directly the code. Very helpful!

Unfortunately considering the prints I still can't see why the output is wrong. Before my function the environment settings show

cellSize: MAXOF and
mask: None
 
within my function it shows
cellSize: 100 and
mask: C:/data/2023/MINEMA_NECC/EWIS/EWIS.gdb/Country_boundary_TMRwanda
 
which is as expected but not represented in the output. Any other idea?
0 Kudos
Nicole_Ueberschär
Esri Regular Contributor

I did some more testing with the two settings separately but both seem to be ignored. I also tried a different layer as mask, setting the cellSize as file and as number - no change 😞

0 Kudos
melissaur1039
New Contributor II

Did you ever find a solution to this issue? I am running in to the same problem, unfortunately.

0 Kudos