摘要
地图文档 (.mxd) 中找到的用于访问多个数据框属性的 DataFrame 对象。对 DataFrame 对象的引用经常作为多个函数的参数使用,以便在特定数据框中过滤图层或表格。
讨论
DataFrame 对象可用于访问重要的数据框属性。ListDataFrames 函数返回 DataFrame 对象的 Python 列表。随后,必须遍历列表中的每个项目,或指定一个索引号以引用具体的 DataFrame 对象。该对象根据正在使用的属性来使用地图单位和页面单位。例如,该对象可用于设置地图范围、比例、旋转以及如诸如空间参考之类的项目。也可使用页面单位确定 DataFrame 对象在布局上的位置和大小。通过 DataFrame 对象也可以访问信息项(如制作者名单和描述)。
当尝试引用其他对象时,引用至数据框同样有很大帮助。例如,ListLayers 函数可提供一个名为 data_frame 的可选参数,以便可在单个数据框中搜索图层,而不在整个地图文档中搜索。也可将 DataFrame 对象用作可选参数来区分打印或导出数据框和页面布局。因此,为方便引用特定数据框,对每个数据框采用唯一命名非常重要。
数据框范围坐标基于布局视图中的数据框范围,而不是数据视图中的数据框范围。这是因为数据视图中的数据框形状与布局视图中的数据框形状的横纵比不同。
elementPositionX 和 elementPositionY 取决于元素的锚点位置;元素的锚点位置可通过 ArcMap 中“元素属性”对话框的大小和位置选项卡进行设置。
页面单位只能在 ArcMap 中通过自定义 > ArcMap 选项 > 布局视图选项卡来更改。
属性
属性 | 说明 | 数据类型 |
credits (读写) | Provides the ability to either get or set the data frame credits information. | String |
description (读写) | Provides the ability to either get or set the data frame description information. | String |
displayUnits (读写) | Provides the ability to either get or set the data frame distance units. | String |
elementHeight (读写) | The height of the element in page units. The units assigned or reported are in page units. | Double |
elementPositionX (读写) | The X location of the data frame element's anchor position. The units assigned or reported are in page units. | Double |
elementPositionY (读写) | The Y location of the data frame element's anchor position. The units assigned or reported are in page units. | Double |
elementWidth (读写) | The width of the element in page units. The units assigned or reported are in page units. | Double |
extent (读写) | 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.
| Extent |
geographicTransformations (读写) | Provides the ability to either get or set the data frame's geographic transformation(s). The property will return the name(s) of the transformation(s) in a list. Only existing, predefined transformation names (or their corresponding code value) can be used to set a geographic transformation. A complete list of transformations and code values can be found on the ArcGIS Resource Center. The geographicTransformations property cannot be used to create custom transformations. Only predefined methods can be referenced. There is always one geographic transformation loaded into a map document by default: NAD_1927_To_NAD_1983_NADCON. This will be overwritten when setting a new list. Geographic transformations can also be cleared by setting an empty list. The following examples will set two transformation methods using name strings. The first from NAD27 to NAD 83 and the second from NAD83 to HARN. The second example does the same thing but uses transformation codes instead.
| String |
mapUnits (只读) | Returns a string value that reports the current data frame map units. These are based on the data frame's current coordinate system. | String |
name (读写) | Provides the ability to either get or set the data frame's name as it appears in the table of contents in a map document and also the actual name of the element within the layout. | String |
referenceScale (读写) | Provides the ability to either get or set the data frame's reference scale. This is the scale in which all symbol and text sizes used in the data frame will be made relative. | Double |
rotation (读写) | Provides the ability to either get or set the data frame's rotation value. It represents the number of degrees by which the data frame will be rotated, measured counterclockwise from the north. To rotate clockwise, use a negative value. | Double |
scale (读写) | Provides the ability to either get or set the current scale of the active data frame. A numerical (double) value must be used. | Double |
spatialReference (读写) | Provides access to the SpatialReference of the data frame. The spatial reference contains information about the coordinate system and units. | SpatialReference |
time (只读) | Returns the DataFrameTime object that provides access to controlling the display for time-enabled layers. | DataFrameTime |
type (只读) | Returns the element type for any given page layout element.
| String |
方法概述
方法 | 说明 |
panToExtent (extent) | Pans and centers the data frame extent using a new Extent object without changing the data frame's scale |
zoomToSelectedFeatures () | Changes the data frame extent to match the extent of the currently selected features for all layers in a data frame |
方法
panToExtent (extent)
This method is perfect for situations where data frame scale does not change. Rather than setting the extent and then having to reset the scale each time, panToExtent maintains the scale and simply centers the current data frame on the new extent.
The following example will pan the extent of a data frame by passing in the extent of selected features for a specific layer. The Layer class getSelectedExtent method is used for this purpose.
df.panToExtent(lyr.getSelectedExtent())
zoomToSelectedFeatures ()
This performs the same ArcMap operation as Selection > Zoom To Selected Features. One difference is that if no features are selected, it will zoom to the full extent of all layers. If you want to zoom to the extent of selected features for a specific layer, try using the Layer.getSelectedExtent method.
The following example will zoom to the extent of all selected features:
df.zoomToSelectedFeatures()
The following example will zoom to the extent of selected features for a specific layer. This example uses a method on the Layer object.
df.extent = lyr.getSelectedExtent()
代码实例
DataFrame 示例 1
在将数据框名称属性用作输出文件名的情况下,以下脚本将在导出每个数据框至单独的输出 TIFF 文件之前,将每个数据框的 旋转属性设置为 0,同时将 比例属性设置为 1:24000。
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
df.rotation = 0
df.scale = 24000
outFile = r"C:\Project\Output\\" + df.name + ".tif"
arcpy.mapping.ExportToTIFF(mxd, outFile, df)
del mxd
DataFrame 示例 2
以下脚本会将数据框的范围设置为图层中所选要素的范围。通过与比例相乘,将 10% 的缓冲区添加至该范围。该示例针对图层对象使用 getSelectedExtent 方法。该脚本使用 CURRENT 关键字,而非提供至地图文档的路径,在 ArcMap 内的交互式窗口中运行。
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]
accidentLayer = arcpy.mapping.ListLayers(mxd, "accidents", df)[0]
arcpy.SelectLayerByAttribute_management(accidentLayer, "NEW_SELECTION", "\"Accidents\" > 5")
df.extent = accidentLayer.getSelectedExtent(False)
df.scale = df.scale * 1.1
arcpy.mapping.ExportToPDF(mxd, r"C:\Project\Output\FrequentAccidents.pdf", df)
arcpy.RefreshActiveView()
del mxd
DataFrame 示例 3
以下脚本将修改特定数据框在布局上的位置和大小。
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]
df.elementPositionX, df.elementPositionY = 1, 1
df.elementHeight, df.elementWidth = 5, 6.5
mxd.save()
del mxd
DataFrame 示例 4
以下脚本将数据框的范围对象转换为面要素,以便其能够与 SelectLayerByLocation_management 函数配合使用。
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\Project.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Transportation")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Accidents", df)[0]
#The DataFrame extent object is converted into a polygon feature so it can be used with the SelectLayerByLocation function.
dfAsFeature = arcpy.Polygon(arcpy.Array([df.extent.lowerLeft, df.extent.lowerRight, df.extent.upperRight, df.extent.upperLeft]),
df.spatialReference)
arcpy.SelectLayerByLocation_management(lyr, "INTERSECT", dfAsFeature, "", "NEW_SELECTION")
mxd.save()
del mxd