Scripting env.XYResolution settings?

3938
2
Jump to solution
06-08-2015 04:52 AM
JohnLay
Occasional Contributor

I am hoping for a little clarification on how scripting env.XYResolution and/or env.XYTolerance adjustments work.

My script works; I have a question about why it works the way it does.

Because I need to match the work done by another department, I need to create a feature dataset with  resolution values equal to 0.01 feet and all the tolerances equal to 0.02 feet. I thought creating a spatial reference (arcpy.CreateSpatialReference_management) to use in the feature dataset creation would do it, but it only gets me halfway there. I still end up needing to change the XY resolution and tolerance through the environment (env) function.

I’m just confused why it needs to be written the way it does.

This works:

XY_Resolution = arcpy.env.XYResolution
arcpy.env.XYResolution = "0.01 Feet"
XY_Tolerance = arcpy.env.XYTolerance
arcpy.env.XYTolerance = "0.02 Feet"
arcpy.CreateFeatureDataset_management(TerrainGDB, DatasetName, Coordinate_System)
arcpy.env.XYResolution = XY_Resolution
arcpy.env.XYTolerance = XY_Tolerance

This does not:

arcpy.CreateFeatureDataset_management(TerrainGDB, DatasetName, Coordinate_System)
arcpy.env.XYResolution = "0.01 Feet"
arcpy.env.XYTolerance = "0.02 Feet"

Why does the first example work and the second not? The first example just seems overly redundant:

XY_Resolution = arcpy.env.XYResolution = "0.01 Feet" = arcpy.env.XYResolution = XY_Resolution.

Maybe I’m just trying to make it too complicated. Can someone help explain this to me?

I keep thinking I have it figured out. But then I think a little bit more and lose it. Is  XY_Resolution = arcpy.env.XYResolution behaving kind of like Coordinate_System = arcpy.CreateSpatialReference_management only without the “create” verbiage and the parameters? Meaning that arcpy.env.XYResolution can’t be set by anything but another environment setting. But then, arcpy.env.XYResolution is being set by the value “0.01 Feet” and not by another environment setting. The help doesn’t say anything about setting up the code like the first example, only "arcpy.env.XYTolerance = linear_unit". But the first example is what's produced when the process is exported from ModelBuilder.

I feel like I’m missing some giant concept that would make this all make more sense.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

As far as I can tell, your first code snippet reads the current setting for XYResolution and  XYTolerance, then it sets it to the value that you want to use. The you execute the tools (which will use the settings you defined) and afterwards you set them back to what they were before you executed the script.

The second snippet, sets the values after the tool is executed. When running CreateFeatureDataset_management these setting are not available yet and have no influence on the tool.

In short environments need to be set before a tool is run in order to have influence on the tool.

I assume that this code:

arcpy.env.XYResolution = "0.01 Feet" 
arcpy.env.XYTolerance = "0.02 Feet"
arcpy.CreateFeatureDataset_management(TerrainGDB, DatasetName, Coordinate_System) 

... will probably work.

View solution in original post

2 Replies
DanPatterson_Retired
MVP Emeritus

This link XY Resolution (Environment setting)—Help | ArcGIS for Desktop suggests that it needs to be known in order to be changes or if the units are different and you are correct that the script example

import arcpy 

# Set the XYResolution environment to a linear unit

arcpy.env.XYResolution = "0.002 Meters"

​would suggest setting it directly.  It appears though, that your lines 1 and 3 may be redundant in your first code example and in your second code you set them after the createfeaturedataset not before.  Have you actually tried your code example 1 without lines 1 and 3? and/or can you confirm that the units are indeed the same?

Further information can be found in Spatial reference and geoprocessing—Help | ArcGIS for Desktop

XanderBakker
Esri Esteemed Contributor

As far as I can tell, your first code snippet reads the current setting for XYResolution and  XYTolerance, then it sets it to the value that you want to use. The you execute the tools (which will use the settings you defined) and afterwards you set them back to what they were before you executed the script.

The second snippet, sets the values after the tool is executed. When running CreateFeatureDataset_management these setting are not available yet and have no influence on the tool.

In short environments need to be set before a tool is run in order to have influence on the tool.

I assume that this code:

arcpy.env.XYResolution = "0.01 Feet" 
arcpy.env.XYTolerance = "0.02 Feet"
arcpy.CreateFeatureDataset_management(TerrainGDB, DatasetName, Coordinate_System) 

... will probably work.