Setting arcpy.env tolerance and resolution in standalone script

1251
8
06-05-2020 11:49 AM
MarvisKisakye1
Regular Contributor

I have a template file from which I am retrieving tolerance and resolution settings as in the code below. Using arcpy.AddMessage, I am able to confirm that the settings were retrieved from the template file and applied. I then run FeatureClassToFeatureClass in a bid to have the newly set tolerance and resolution values applied to the output feature class. Unfortunately, these settings are not applied to the output file. When I run this same code in the python window, the settings are applied as expected. I am using ArcGIS Pro 2.5.1. Dan Patterson

templateFile=arcpy.GetParameterAsText(0)
if templateFile != "":
    sr_object=arcpy.Describe(templateFile).spatialreference
    arcpy.env.MTolerance=sr_object.MTolerance
    arcpy.env.MResolution=sr_object.MResolution
    arcpy.env.XYTolerance="{0} {1}".format(sr_object.XYTolerance,sr_object.linearUnitName)
    arcpy.env.XYResolution="{0} {1}".format(sr_object.XYResolution,sr_object.linearUnitName)

arcpy.AddMessage(arcpy.env.MTolerance)
arcpy.AddMessage(arcpy.env.MResolution)
arcpy.AddMessage(arcpy.env.XYTolerance)
arcpy.AddMessage(arcpy.env.XYResolution)
arcpy.FeatureClassToFeatureClass_conversion("Mylayer","//Path//to//gdb", "OutLayer")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
0 Kudos
8 Replies
DavidPike
MVP Frequent Contributor

I know it makes little sense but it may be an arcpy quirk, have you tried something like:

MTolerance = sr_object.MTolerance

arcpy.env.MTolerance = MTolerance

arcpy.FeatureClassToFeatureClass_conversion()

0 Kudos
MarvisKisakye1
Regular Contributor

I tried setting the environments as you did above but they are still not applied to the output files.

0 Kudos
DavidPike
MVP Frequent Contributor

can you humour me and show the print/add message results please

0 Kudos
MarvisKisakye1
Regular Contributor

Here are the AddMessage results:

6.213699494949262e-07
6.213699494949262e-08
0.003280839895013 Foot
0.0010763910416709318 Foot

But when I look at the properties of the output, it has different values from above. 

0 Kudos
DavidPike
MVP Frequent Contributor

I would say that "0.003280839895013 Foot" etc. is being passed as an incorrect variable which is ignored by the environment. 

"0.003280839895013 Feet" should be passed?

0 Kudos
MarvisKisakye1
Regular Contributor

I also tried running it with the code below to ensure that the linear units show up as feet instead of foot.

templateFile=arcpy.GetParameterAsText(0)
if templateFile != "":
    sr_object=arcpy.Describe(templateFile).spatialreference
    units=""
    if sr_object.linearUnitCode==9002:
	units="Feet"
    else:
	units=sr_object.linearUnitName
    arcpy.env.MTolerance=sr_object.MTolerance
    arcpy.env.MResolution=sr_object.MResolution
    arcpy.env.XYTolerance="{0} {1}".format(sr_object.XYTolerance,units)
    arcpy.env.XYResolution="{0} {1}".format(sr_object.XYResolution,units)

arcpy.AddMessage(arcpy.env.MTolerance)
arcpy.AddMessage(arcpy.env.MResolution)
arcpy.AddMessage(arcpy.env.XYTolerance)
arcpy.AddMessage(arcpy.env.XYResolution)
arcpy.FeatureClassToFeatureClass_conversion("Mylayer","//Path//to//gdb", "OutLayer")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The AddMessage results then were:

6.213699494949262e-07
6.213699494949262e-08
0.003280839895013 Feet
0.0010763910416709318 Feet‍‍‍‍

This didn't make a difference in the behaviour of the script. It's still pretty erratic. It doesn't work 99% of the time and then it will randomly work out of the blue with no changes to the code. 

0 Kudos
DavidPike
MVP Frequent Contributor

Are you writing to a feature dataset? I guess that would ignore any environments to preserve topology etc?

0 Kudos
MarvisKisakye1
Regular Contributor

No. I am writing to a geodatabase in ArcGIS Pro.

0 Kudos