Move Map in Cardinal Directions

1052
8
Jump to solution
08-13-2013 06:50 AM
JamesSmith7
New Contributor
I want to move the map by entering the distance.  For example, I want to move the data frame 5 Feet North.  I am using an if elif else statment to provide a statement that moves the map.  The script returns no errors.  I do not want to use df.extent = lyr.getSelectedExtent(), because it zooms to full extent.  I want to be able to move the map.  Similiar to using a pan, but by entering a specific distance in cardinal directions.

import arcpy, os, sys  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, df)[0]  North = arcpy.GetParameterAsText(0) South = arcpy.GetParameterAsText(1) East = arcpy.GetParameterAsText(2) West = arcpy.GetParameterAsText(3)  if North > 0:  whereClause = ''' "North" = '{0}' AND "South" = '{1}' AND "East" = '{2}' AND "West" = '{3}' '''.format(North, South, East, West) elif South > 0:  whereClause = ''' "North" = '{0}' AND "South" = '{1}' AND "East" = '{2}' AND "West" = '{3}' '''.format(North, South, East, West) elif East > 0:  whereClause = ''' "North" = '{0}' AND "South" = '{1}' AND "East" = '{2}' AND "West" = '{3}' '''.format(North, South, East, West) elif West > 0:  whereClause = ''' "North" = '{0}' AND "South" = '{1}' AND "East" = '{2}' AND "West" = '{3}' '''.format(North, South, East, West) else:  whereClause = ''' "North" = '{0}' AND "South" = '{1}' AND "East" = '{2}' AND "West" = '{3}' '''.format(North, South, East, West)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RhettZufelt
MVP Frequent Contributor
I was thinking of using arcpy.mapping to control a closed mxd.  for real time pan of the display, this is that language (at least for the dataframe named "Layers"):

   mxd = arcpy.mapping.MapDocument("CURRENT")  df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] newExtent = df.extent newExtent.XMin = df.extent.XMin + deltaX newExtent.YMin = df.extent.YMin + deltaY newExtent.XMax = df.extent.XMax + deltaX newExtent.YMax = df.extent.YMax + deltaY df.panToExtent(newExtent) arcpy.RefreshActiveView() 


if you pass it a positive value for deltaY, it will pan the display north by the entered value (in map units), negative will shift south
if you pass it a positive value for deltaX, it will pan the display East by the entered value (in map units), negative will shift west

R_

View solution in original post

0 Kudos
8 Replies
markdenil
Occasional Contributor III
I cannot see what you are hoping to do with your code that will modify the map extent in any way at all.

... for a start,

The if, elif, else set is very odd, it rather reads like:
if the sea is blue, do this
or if the sand is white, do the same thing
or if it rains in Spain, do it anyway
...
the tests are not really exclusive... and the outcomes rather similar.

You are also applying a math test (greater than) to a string variable (as is returned by GetParameterAsText()).
0 Kudos
RhettZufelt
MVP Frequent Contributor
You can't modify the extent object directly.  Once you figure out where you want to move it, you will need to make a copy of the extent object.

From the docs:

Provides the ability to get or set the data frame's map extent using map coordinates (i.e., map units). A copy of the Extent object must be made before modifying its properties. The modified copy is then used to set the new extent properties. Note: If you try to set the extent by simply referencing the Extent object, changes won't be saved. For example, df.extent.xMin = some value won't work.

If the aspect ratio of the extent does not match the shape of the data frame, the final extent will be adjusted to fit the new extent within the shape of the data frame. In other words, if you set explicit X, Y coordinates, you may not get the same values returned if you attempt to read them later.

Note: The properties of the Extent object are by default read-only in the help system. A special exception was made for the arcpy.mapping scripting environment to enable changing extents during a map automation process.

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
newExtent.XMin, newExtent.YMin = -180.0, -90.0
newExtent.XMax, newExtent.YMax = 180.0, 90.0
df.extent = newExtent
from here:  http://resources.arcgis.com/en/help/main/10.1/index.html#//00s300000003000000

Should be able to shift the Ymin Ymax by 5 feet north.

R_
0 Kudos
JamesSmith7
New Contributor
You can't modify the extent object directly.  Once you figure out where you want to move it, you will need to make a copy of the extent object.

From the docs:

from here:  http://resources.arcgis.com/en/help/main/10.1/index.html#//00s300000003000000

Should be able to shift the Ymin Ymax by 5 feet north.

R_


Thanks, I was struggling to find similiar questions or documentation.  I will attempt to modify my script.  But, I want the ability to move the map(after I read the info provided in the link) N, S, E, or W and by any distance.
0 Kudos
JamesSmith7
New Contributor
Just to clarify:

You are suggesting using the following as a variable.  Then, setting up the if statement?

df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
newExtent.XMin, newExtent.YMin = -180.0, -90.0
newExtent.XMax, newExtent.YMax = 180.0, 90.0
df.extent = newExtent
0 Kudos
RhettZufelt
MVP Frequent Contributor
Nope.  Just saying that is how you would shift the dataframe once you know how far you want to move it.

My data is in State Plane meters, so the output of my df.extent(s) are:

>>> df.extent.YMin
150602.61868704704
>>> df.extent.YMax
152774.79701295635
>>> 



## So, after you have done whatever if statements you need to determine how far to shift it, you use the code.

if MyShiftNorthTest == true:
  deltaY = 5             # this is my variable set to 5 "map units" that I want to shift my extent North

if MyShiftEastTest == true:
  deltaX = 5             # this is my variable set to 5 "map units" that I want to shift my extent East

##Then  (and I'll break it up to read it more easily):


df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
newExtent.XMin = df.extent.XMin + deltaX
newExtent.YMin = df.extent.YMin + deltaY
newExtent.XMax = df.extent.XMax + deltaX
newExtent.YMax = df.extent.YMax + deltaY
df.extent = newExtent


So, since my map units are meters,  that would shift my extent north and/or east by 5 meters.  If I pass it a negative value, would shift the other way.

R_
0 Kudos
JamesSmith7
New Contributor
How far to move the data frame will vary.  I want to use the script similiar to the pan tool.  Except you are able to enter a value and the map moves, based on the value entered.  Are we speaking the same language? :confused:
0 Kudos
RhettZufelt
MVP Frequent Contributor
I was thinking of using arcpy.mapping to control a closed mxd.  for real time pan of the display, this is that language (at least for the dataframe named "Layers"):

   mxd = arcpy.mapping.MapDocument("CURRENT")  df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] newExtent = df.extent newExtent.XMin = df.extent.XMin + deltaX newExtent.YMin = df.extent.YMin + deltaY newExtent.XMax = df.extent.XMax + deltaX newExtent.YMax = df.extent.YMax + deltaY df.panToExtent(newExtent) arcpy.RefreshActiveView() 


if you pass it a positive value for deltaY, it will pan the display north by the entered value (in map units), negative will shift south
if you pass it a positive value for deltaX, it will pan the display East by the entered value (in map units), negative will shift west

R_
0 Kudos
JamesSmith7
New Contributor
I was thinking of using arcpy.mapping to control a closed mxd.  for real time pan of the display, this is that language (at least for the dataframe named "Layers"):




mxd = arcpy.mapping.MapDocument("CURRENT")

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
newExtent = df.extent
newExtent.XMin = df.extent.XMin + deltaX
newExtent.YMin = df.extent.YMin + deltaY
newExtent.XMax = df.extent.XMax + deltaX
newExtent.YMax = df.extent.YMax + deltaY
df.panToExtent(newExtent)
arcpy.RefreshActiveView()



if you pass it a positive value for deltaY, it will pan the display north by the entered value (in map units), negative will shift south
if you pass it a positive value for deltaX, it will pan the display East by the entered value (in map units), negative will shift west

R_


This is what I had in mind.  I was unaware of which tool and function would accomplish the job.  Thanks!
0 Kudos