1

Using ArcGIS 10.2.2 I am trying to set the extent in environment settings using arcpy.env.extent

My code is here:

for lyrVec in arcpy.mapping.ListLayers(mxd, "", df):
    if lyrVec.name == "Thresholds":
        desc = arcpy.Describe(lyrVec)
        lyrVecPath = desc.path + '\\' + str(lyrVec) + '.shp'
        fixedpath = os.path.normpath(lyrVecPath)
        arcpy.env.extent = lyrVecPath

Error message is thrown up:

RuntimeError: Object: Error in accessing environment

  • Have you tried it with another shapefile? – whatahitson Feb 13 '17 at 11:04
  • Have you tried printing lyrVecPath? I suspect that it may not be a reachable dataset but, in any event, what you have provided here appears to be a copy/paste of some code from your script instead of a code snippet that works up to where you are stuck. – PolyGeo Feb 13 '17 at 11:15
  • You have specified fixedpath = os.path.normpath(lyrVecPath) but still use arcpy.env.extent = lyrVecPath instead of arcpy.env.extent = fixedpath. Is there a reason you needed to fix the path? – Midavalo Feb 13 '17 at 17:12
1

If you are trying to set the extent using a layer in your MXD you do not need to specify the path of the underlying feature class. arcpy.env.extent can be set to the actual layer.

>>> print arcpy.env.extent
None
>>> arcpy.env.extent = "testPoint"
>>> print arcpy.env.extent
1880391.189 5821879.3835 1885687.1515 5825657.3949 NaN NaN NaN NaN
>>> 

Try changing your code to find your layer and then set extent using that layer name.

for lyrVec in arcpy.mapping.ListLayers(mxd, "", df):
    if lyrVec.name == "Thresholds":
        arcpy.env.extent = lyrVec.name

If there is a reason you do need to specify your underlying shapefile, try the following

for lyrVec in arcpy.mapping.ListLayers(mxd, "", df):
    if lyrVec.name == "Thresholds":
        desc = arcpy.Describe(lyrVec)
        lyrVecPath = os.path.join(desc.path, "{0}.shp".format(lyrVec))
        arcpy.env.extent = lyrVecPath

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.