Set scale in python

16400
12
Jump to solution
07-22-2013 08:25 AM
josesanchez1
New Contributor III
Hi all,

this is my code:

..
zoom = arcpy.mapping.ListLayers(mxd,cut,"")[0] # we locate cut layer in TOC. cut is a variable
df.extent = zoom.getExtent() # Zoom to Layer zoom
df.scale = 20000 # we set the scale to 1:20,000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.save()


it should work, right?

Well, when I open the map in PDF that I have generated and..time after time, I see Scale = 1:8,223, which means that after the Zoom to Layer, the line:

df.scale = 20000 does not work at all!! (df is the DATA FRAME previously created)

Any suggestions?

Thanks in advance.

Jose.
Tags (2)
1 Solution

Accepted Solutions
josesanchez1
New Contributor III
Well, I got it!. Thanks anyway for all your comments.

NOW, my PDF does not have any 1:8.223, just 1:20.000!!!!

After the code lines:

list2 = [0,6,7,8,9] # we list the positions of the elements that not may appear in this map.
for i in list2:
    lyr = arcpy.mapping.ListLayers(mxd,"","")
    lyr.visible = False # we turn off these layers.
zoom = arcpy.mapping.ListLayers(mxd,cut,"")[0] # we locate cut (Study area extent) in TOC.


and before the line:

df.extent = zoom.getExtent()


I put:

arcpy.RefreshActiveView()
arcpy.RefreshTOC()


So, in the end, I have learned that after turning off layers in TOC, you must refresh everything..in order to get the desired scale.

Jose.

View solution in original post

0 Kudos
12 Replies
RhettZufelt
MVP Frequent Contributor
Are you sure it isn't being set?  normally, when you export to pdf, you export the Layout, not the dataframe.

After it has run, open the mxd in ArcMap, switch to data view and see what your "dataframe" scale is set to.

Depending on your constraints, etc. the Layout view scale is not the same as Data frame scale.

R_
0 Kudos
josesanchez1
New Contributor III
It sets the scale properly into ArcMap, but not when it goes into the PDF.

arcpy.mapping.ExportToPDF(mxd, dir + "\FILE.pdf")
0 Kudos
markdenil
Occasional Contributor III
Sometimes you have to set such settings twice with python
(the snake has a bug).
But you say it does seem to change in ArcMap...
You don't however, provide your ExportToPDF parameters....

It is also unclear if you are exporting your layout or just a data frame.
Exporting a layout is almost always a better idea.
0 Kudos
RhettZufelt
MVP Frequent Contributor
It sets the scale properly into ArcMap, but not when it goes into the PDF.

arcpy.mapping.ExportToPDF(mxd, dir + "\FILE.pdf")


In where in ArcMap?  The layout or the dataframe as you are setting?

Unless you have went into the dataframe properties in ArcMap and set the extent to "Fixed Scale", you will likely never get the same scale for the dataframe that you have for the Layout (which is what is exported to pdf unless you specify otherwise).

If you want it to be 1:20000 all the time, just set it in the dataframe properties as fixed scale.

R_
0 Kudos
josesanchez1
New Contributor III
First, thanks to everybody for considering my issue. then:

1.- What I am trying to do is exporting into a PDF my Layout View using python. The scale of this map may be 1:20.000 (1:20000)

2.- Before executing the script: Data Frame Properties --> Extent : Automatic

3.- Before executing the script: The scale is set to 30000 (purposely) in order to see if the script works or not.

4.- The code would be:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
cut = arcpy.GetParameterAsText(0) # from TOC by Default (LAYER).
dir = arcpy.GetParameterAsText(1) # folder type
list2 = [0,6,7,8,9] # we list the positions of the elements that not may appear in this map.
for i in list2:
    lyr = arcpy.mapping.ListLayers(mxd,"","")
    lyr.visible = False # we turn off these layers. CUT IS NUMBER 5, SO IT IS NOT AFFECTED HERE
zoom = arcpy.mapping.ListLayers(mxd,cut,"")[0] # we locate cut (Study area extent) in TOC.
df.extent = zoom.getExtent() # Zoom to Layer
df.scale = 20000 # we set the scale of the output to 1:20,000
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
mxd.save()
arcpy.mapping.ExportToPDF(mxd, dir + "\file.pdf") # we export the map into a PDF file.


5.- OK, when I run the script, I look at the Map Scale (Data View) and the sequence I see with my own eyes is:

....1:30,000 (good).......1:20,000 (good!).....1:8,223..(not good).

Result: PDF Scale --> 1:8,223

That is all I can say so far.

Jose.
0 Kudos
RhettZufelt
MVP Frequent Contributor
First, thanks to everybody for considering my issue. then:

1.- What I am trying to do is exporting into a PDF my Layout View using python. The scale of this map may be 1:20.000 (1:20000)

2.- Before executing the script: Data Frame Properties --> Extent : Automatic

3.- Before executing the script: The scale is set to 30000 (purposely) in order to see if the script works or not.


5.- OK, when I run the script, I look at the Map Scale (Data View) and the sequence I see with my own eyes is:

....1:30,000 (good).......1:20,000 (good!).....1:8,223..(not good).

Result: PDF Scale --> 1:8,223

That is all I can say so far.

Jose.



So, understand the 1:30,000 as you set it manually in the mxd
the 1:20,000 is what your script tells it to do
Where is the 1:8223 coming from?  you say you get all of them, but  you only have one spot in the code that is making a change, why (or when) do you get three different values?


Just to make sure, you do understand, that, unless you set Data Frame Properties --> Fixed Scale, you will likely "never" get the Layout scale to be exactly 1:20000 as long as you are also zooming to some extent in the dataframe?


R_
0 Kudos
josesanchez1
New Contributor III
Well, I got it!. Thanks anyway for all your comments.

NOW, my PDF does not have any 1:8.223, just 1:20.000!!!!

After the code lines:

list2 = [0,6,7,8,9] # we list the positions of the elements that not may appear in this map.
for i in list2:
    lyr = arcpy.mapping.ListLayers(mxd,"","")
    lyr.visible = False # we turn off these layers.
zoom = arcpy.mapping.ListLayers(mxd,cut,"")[0] # we locate cut (Study area extent) in TOC.


and before the line:

df.extent = zoom.getExtent()


I put:

arcpy.RefreshActiveView()
arcpy.RefreshTOC()


So, in the end, I have learned that after turning off layers in TOC, you must refresh everything..in order to get the desired scale.

Jose.
0 Kudos
RobertBorchert
Frequent Contributor III

I would like to do something along this line and query data frame scale and put it in definition query.

i.e. I would like create an attribute and call it vscale  ( visible scale) can be anything this is arbitrary.

I would like to create a definition query as such.

vscale >= CurrentDataFrameScale

So when I am at or above whatever value i have in vscale my feature becomes visible.

0 Kudos
BlakeTerhune
MVP Regular Contributor

I don't think you can combine a definition query and visible scale range with Python like that. You could use that definition query but it would never change the visible scale range in the MXD on its own. I recommend you try using layer files. When you create a layer file, it will save the visible scale range you set (as well as definition queries, symbology, labels, etc). So instead of adding the feature class from a geodatabase, just add it from the layer file and it will already have all that stuff set up. If you want your visible scale range to change, you'd have to save over the layer file with the new visible scale range set.

Your other option would be to run a Python script that checks for a vscale field in all (or certain) layers in the map and sets the visible scale range; it would have nothing to do with a definition query.

0 Kudos