Check layer in specific area using python

4513
12
11-08-2015 06:49 AM
Yaron_YosefCohen
Occasional Contributor II

Hi ,

i try to check if layer "openSpace" are in area coordinate with this code:

import arcpy
from arcpy import env

area= '178000 672000 190000 655000' 
env.workspace = r"C:\Project"
for mxdname in arcpy.ListFiles("*.mxd"):
    print mxdname
    mxd = arcpy.mapping.MapDocument(r"C:\Project\\" + mxdname)
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] 
    df.extent = area
    desc = arcpy.Describe(r"C:\Project\layers\openSpace.shp")
    if (map_Y_Max > layer_Y_max) and (map_X_Max > layer_X_max) and (map_Y_Min < layer_Y_min) and (map_X_Min < layer_X_min):
        print "layer is inside map extent"
    else:
        print "layer is outside map extent"
    mxd.save()
del mxd

but i get an error:

>>> 
antiquities.mxd
Traceback (most recent call last):
  File "C:\Users\yaron.KAYAMOT\Desktop\check if every layer in df extent_geonet.py", line 15, in <module>
    if (map_Y_Max > layer_Y_max) and (map_X_Max > layer_X_max) and (map_Y_Min < layer_Y_min) and (map_X_Min < layer_X_min):
NameError: name 'map_Y_Max' is not defined
>>> 

i don't understand what my mistake because the layer extent is:

1.jpg

thanks for any help.

Tags (2)
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

because you haven't defined the variables that represent the extent, check this help topic

ATTEMPT 3

http:// desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/dataframe-class.htm    remove the space after //

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/dataframe-class.htm

Timothy Hales​ seem to be having issues today with copy-paste urls  don't know if it is an iThingy issue, but it worked fine yesterday

RebeccaStrauch__GISP
MVP Emeritus

Dan, for sone reason your link is coming up in error. (jive us sue again?). 

This may be the topic Dan is trying to show ArcGIS Help (10.2, 10.2.1, and 10.2.2)   .?

0 Kudos
DanPatterson_Retired
MVP Emeritus

I fixed the link...don't know what was going on, it works on the iThingy, but i copied the link and split off the first bit for copy,paste-concatenate

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm still having issues with both of you links ...on my iPad (even rebooted it).  I'll cooy paste the data frame page from my Safari link for 10.3.  If this doesn't work, may be a Timothy Hales​ item

DataFrame—Help | ArcGIS for Desktop

edit....mine is coming up in error on my iPad too.  But I've noticed that all browser searches that past week or so are showing 10.2 and earlier links again, instead on 10.3, so may be related issue.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I posted a 10.3 link and fixed my original post... Grief...in short the topic to check in the help files is listed in the URL so not all is lost

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

but i have those values in the extent of the layer

0 Kudos
DanPatterson_Retired
MVP Emeritus

see these variables map_Y_Max,  layer_Y_max ,  etc etc

they aren't defined in the code anywhere.  Python doesn't automagically assign numbers to variables, you have to assign them to those variables.

So you are either not showing all your code or you didn't assign numbers to those variables.

NeilAyres
MVP Alum

YCC can define an extent like that.

But you have to get the numbers the right way round.

His extent is in the wrong order.

>>> import arcpy
>>> ext = arcpy.Extent(0, 0, 100, 100)
>>> ext.XMin
0.0
0 Kudos
NeilAyres
MVP Alum

I take it back....

arcpy.Extent does do some basic checking by default. Clever, clever esri....

>>> ext = arcpy.Extent(0, 100, 100, 0)
>>> ext.XMin, ext.YMin, ext.XMax, ext.YMax
(0.0, 0.0, 100.0, 100.0)
>>>
0 Kudos