3

I have created a series of roated Grid Index Features. I wrote an ArcPy script to generate the grids and adds a rotation value field. My plan is to run another ArcPy script that zooms to each grid and exports a PDF. I am using ArcPy instead of Data Driven Pages because of some additional requirements ArcPy fulfills.

The problem I am experiencing is that I'm getting unexpected zoom results when the dataframe is rotated. In the example below, I rotate the dataframe 47 degrees to "straighten" the grid. When I use Zoom to Layer or Zoom to Selected Feature it doesn't provide a tight zoom around the polygon. If the dataframe is not rotated, the zoom works as expected. Rotating the dataframe causes the view to zoom out too far.

Interestingly, when using Data Driven Pages and providing the rotation field, the zoom behaves as I expect. Below are some examples of this phenomenon. How can I get the results of third image (the Data Driven method) using ArcPy?

EDIT 1:

The ArcPy code is simply using the Grid Index Features Geoprocessor to build the grids. Where my code comes into play is when I rotate that geometry. I write the results of the following function to the geometry of each grid with an Update Cursor. The code I'm using for calculating the rotation is this:

def RotateAxis(AnchorX,AnchorY,inputx,inputy, rAngle):
    x = inputx - AnchorX
    y = inputy - AnchorY
    resultx = ((x * math.cos(rAngle)) + (y * math.sin(rAngle))) + AnchorX
    resulty = ((x * math.sin(rAngle)) - (y * math.cos(rAngle))) + AnchorY
    return (resultx,resulty)

However, even when I manually rotate the grid in ArcMap with the rotate tool, it provides the same results as the code.

Furthermore, it seems like the actual extent doesn't wrap around these polygons. When you use the edit tool and click on the geometry you can see the outline of the extent. I've included another screenshot of this. (I did thicken the outline in the screenshot so it would show up better, its really faint in ArcMap.) I suspect this extent may have something to do with it, but again, I get the same issue even with manually rotated geometry.

I have not found a way to create geometry that is rotated at the time of its creation. Grid Index Features only creates unrotated geometry and thus my work around in using Arcpy to rotate after the fact.

Edit 2 :

Just to add further clarification, all of the grids are different sizes and rotations, so defining a set scale and moving the dataframe extent will not work. The aspect ratio of them however is consistent.

Zoom to Layer - No Dataframe Rotation:

Zoom to Layer - No Rotation

Zoom to Layer - 47 Degree Dataframe Rotation:

Zoom to Layer - 47 Degree Rotation

Using Data Driven Pages using the Rotation Field:

Data Driven Page

Rotated Dataframe (317 degrees) with Polygon Extent Visible:

Rotated Dataframe with Polygon Extent Visible

2
  • 1
    Would you be able to edit your code to include some code, please? Not you whole code - just a snippet that show how you created your index, and then how you use that index to export your pages.
    – PolyGeo
    Commented May 21, 2014 at 2:23
  • @PolyGeo I've updated the question with the relevant code, some additional information as well as another screenshot.
    – amasephy
    Commented May 21, 2014 at 12:46

1 Answer 1

3

The zoomToSelectedFeatures() method of the arcpy.mapping.DataFrame class zooms to the extent (i.e. bounding box) of the selected features in the original, unrotated coordinate system. It's equivalent to MyDataFrame.extent = MyLayer.getSelectedExtent(). Since the extent of your rotated features (in their own coordinate system) is much larger than the features themselves, using zoomToSelectedFeatures() results in lots of padding around your features.

To be honest, I'm surprised that Data Driven Pages is "smart" enough to zoom in to the polygon boundaries as shown in your third screenshot. As far as I know, there is no ArcPy method to reproduce the zooming functionality of data driven pages.

There is a workaround, however. Assuming all your grids are consistent size, you can determine the map scale that comfortably fits all grids. After centering on the rotated grid, you can just update the scale only, leaving the center point of your data frame unchanged. For example, if you knew your scale should be 1:1,014,461 in order to comfortably fit your grid cell, you would do:

MyDataFrame.zoomToSelectedFeatures() #Centers on grid with wrong scale
MyDataFrame.scale = 1014461 #Corrects scale, center unchanged
arcpy.RefreshActiveView() #Force map refresh after rescaling

If your grid cells are not the same size or have different rotations, a more general function for rescaling would be necessary. This would be more time consuming to write, since it depends on the aspect ratios of your data frame and your grid cell. In most cases, you would need to define a polygon in unrotated space whose bounding box in rotated space is equivalent to your grid.

For your case where the size changes but the aspect ratio is the same, you could use the midpoints of the sides of your grid rectangle. Here's some psuedocode for how it could be implemented:

  • Get the midpoints of all four sides of your grid.
  • Determine the pair of midpoints that represent the axis of the grid rectangle to which you want the view to be zoomed. This will usually be the long axis of the rectangle. These are highlighted in the example image below.

Midpoints in unrotated space

  • Apply the grid rotation, as shown below. I believe you've already taken care of this part.

Midpoints in rotated space

  • Set the extent of the dataframe to be the extent of those points using the DataFrame.extent property.

Grid with proper zoom

7
  • Thanks for the response, unfortunately my grids are all different sizes and rotations. I am not sure how to go about what you describe in your final paragraph. As a note, if the scale were to be the same, I believe I could have simply used the .panToExtent(extent) method.
    – amasephy
    Commented May 21, 2014 at 17:19
  • @amasephy Are your grids always the same aspect ratio? I'm assuming you are created a printable map book, so should the maps always be 8.5" by 11" or something like that?
    – dmahr
    Commented May 21, 2014 at 17:27
  • Yes, they are all the same aspect ratio.
    – amasephy
    Commented May 21, 2014 at 17:28
  • Very creative work around, I've got it working exactly as I wanted. I determined the order the coordinates were being read in by ArcPy, and only calculated the midpoint of the sides I was looking for. Thank you!
    – amasephy
    Commented May 23, 2014 at 18:40
  • @amasephy Glad that it worked out for you. ArcMap's dataframe rotation functionality is implemented in a klugey way that isn't well supported by ArcPy.
    – dmahr
    Commented May 23, 2014 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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