addBasemap
汇总
Map 对象是参考和管理 ArcGIS Pro 工程中的图层和表的主要对象。
讨论
ArcGIS Pro 中的 Map 用于表示表格与符号化地理图层的集合,同时用于保留坐标系、默认数据视图和其他各种元数据的信息。 对地图内容进行可视化的唯一方式是在 地图视图 (即在具有自己的内容列表的应用程序中作为选项卡)或在 布局 上的地图框中进行。 可在多个地图视图或地图框中显示同一地图。 如果已将图层添加到地图,则所有引用此地图的地图视图和地图框将显示已添加的图层。 如果希望在不同视图中显示其他图层或表集合,则需要构建和使用不同的地图。
地图使用 ArcGISProject 对象中的 listMaps 函数进行访问,该函数将返回 Map 对象的 Python 列表。 需要对每个地图进行唯一命名,以轻松在使用 name 属性的 wildcard 参数中参考特定地图。 地图还可以从 MapFrame 对象使用 map 属性进行访问。
ArcGISProject 类上的 createMap 方法用于在工程中创建地图。 如果在应用程序中运行脚本,则将采用底图设置。 如果在应用程序外部运行脚本,将会自动添加地形底图并且您需要移除不需要的底图图层。
还可通过 openView 方法打开地图视图。 您可能希望在调用 openView 之前执行一些操作。 首先,在打开新视图之前设置 defaultCamera 可控制初始范围。 其次,您可能希望在打开新视图之前,使用 ArcGISProject 类上的 closeViews 方法关闭其他视图。 下面的第三个示例演示了此工作流程。
Map 对象上有许多可用方法,用于管理其表格和符号化地理图层集合。 图层和表可以在现有图层和表列表中进行添加( addLayer 、 addLayerToGroup 、 addTable 或 insertLayer )、移除( removeLayer 或 removeTable )和重新排列 (moveLayer)。 listLayers 和 listTables 方法为如何在地图中引用 图层 和 表 。
所有地图都具有 mapType 属性。 该属性可以具有值 MAP (表示 2D 地图)或值 SCENE (表示 3D 地图)。 需要知道 mapType 属性的示例之一为要设置 defaultCamera 属性时。 不能将 3D Camera 对象应用于 2D 地图,反之亦然。 因此,您需要提前检查 mapType 值。
地图类还支持 getDefinition 和 setDefinition 方法。 有关更详细的帮助,请参阅 Python CIM 访问 。
语法
addBasemap()
属性
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
colorModel (读取和写入) |
返回分配给 |
String |
|
defaultCamera (读取和写入) |
用于获取或设置地图的默认 |
Camera |
|
defaultView (只读) |
与 Web 地图打印 Web 工具中的 |
MapView |
|
excludedLayersFromClipping (读取和写入) |
如果 |
List |
|
mapType (只读) |
返回一个字符串,用于报告
|
String |
|
mapUnits (只读) |
返回一个字符串值,该值表示为 |
String |
|
metadata (读取和写入) |
获取或设置地图的 注:设置元数据取决于 |
Metadata |
|
name (读取和写入) |
用于在 |
String |
|
referenceScale (读取和写入) |
用于获取或设置 |
Double |
|
spatialReference (读取和写入) |
用于获取或设置与地图相关联的 |
SpatialReference |
|
transformations (只读) |
将与地图关联的水平和垂直变换作为 Python 字典返回。 水平变换的字典键为 2D ,垂直变换的字典键为 3D 。 要修改现有变换,使用 |
Dictionary |
|
URI (只读) |
地图的统一资源指示符。 这是项目中地图的唯一标识符,使用 |
String |
方法
addBasemap(basemap_name)
addBasemap 方法可用于在地图内添加或替换底图图层。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
basemap_name |
The name of the basemap as it appears in the basemap gallery. |
String |
addDataFromPath(data_path, {web_service_type}, {custom_parameters})
addDataFromPath 允许通过提供本地路径或 URL 向工程 (.aprx) 中的地图添加 图层 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
data_path |
A string that represents a local path or URL. (默认值为 None) |
String |
|
web_service_type (可选) |
A string that represents the type of web service connection. When you are working with a service URL that is hosted on a third-party server and the URL is not deterministic, you must specify the service type from the list to add the data. The default value of AUTOMATIC will not work if the
(默认值为 AUTOMATIC) |
String |
|
custom_parameters (可选) |
A Python dictionary of custom connection parameters. The KML (默认值为 None) |
Dictionary |
返回值
| 数据类型 | 说明 |
|---|---|
|
Layer |
一个 图层 对象。 |
addLayer(add_layer_or_layerfile, {add_position})
用于使用基本放置选项向工程 (.aprx) 内的地图添加 Layer 或 LayerFile 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
add_layer_or_layerfile |
Layer |
|
|
add_position (可选) |
此常数用于确定在地图中添加的单个或多个图层的放置位置。
(默认值为 AUTO_ARRANGE) |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
Layer 对象的 Python 列表。 |
addLayerToGroup(target_group_layer, add_layer_or_layerfile, {add_position})
用于使用基本放置选项向工程 (.aprx) 内地图中的现有图层组添加 Layer 或 LayerFile 内容。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
target_group_layer |
对现有 Layer 对象组的引用。 |
Layer |
|
add_layer_or_layerfile |
Layer |
|
|
add_position (可选) |
此常数用于确定在
(默认值为 AUTO_ARRANGE) |
String |
addTable(add_table)
用于向工程 (.aprx) 内的地图中添加 Table 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
add_table |
对 Table 对象(表示要添加的表)的引用。 |
Table |
返回值
| 数据类型 | 说明 |
|---|---|
|
Table |
引用 Table 对象。 |
addTableToGroup(target_group_layer, add_table)
用于向工程 (.aprx) 内的地图中现有图层组添加 Table 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
target_group_layer |
A reference to an existing group layer. |
Layer |
|
add_table |
A reference to a Table object. |
Table |
clearSelection()
清除地图中所有图层和表的选择。
clipLayers(clip_object, {selection})
clipLayers 方法用于设置地图的裁剪选项。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
clip_object |
This object can be a polygon feature layer, an extent object, a graphics layer that includes polygons, or a custom polygon object. The clipping options can be cleared if set to |
Object |
|
selection (可选) |
Specifies whether a selection will be used for clipping. If set to 注:A selection only applies to polygon feature layers and graphics layers that include polygon elements. Also, a selection only applies to graphics layers when a script is run from within the application because a graphic layer element selection is not saved with a project. (默认值为 ALL) |
String |
此示例代码设置了一个地图系列中一张地图的裁剪图层和排除图层。 它还设置了 clipToIndexFeature ,以启用对索引要素的裁剪。
p = arcpy.mp.ArcGISProject('current')
m = p.listMaps('Yosemite National Park MS')[0]
indexLyr = m.listLayers('IndexLayer')[0]
excludeLyr = m.listLayers('Topo*')[0]
m.clipLayers(indexLyr)
m.excludedLayersFromClipping = [excludeLyr]
lyt = p.listLayouts('*MS')[0]
ms = lyt.mapSeries
ms.clipToIndexFeature = True
copyBookmark(bookmark, {name})
copyBookmark 方法可将对 书签 的引用复制到地图中。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
bookmark |
A reference to an existing bookmark in a map either in the same map or a different map. |
Bookmark |
|
name (可选) |
A string that represents the name of the new bookmark. If a name is not provided, the default value will follow the sequencing nomenclature, for example, Bookmark, Bookmark [1], Bookmark [2]. |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
Bookmark |
书签 对象。 |
createGraphicsLayer({name})
此 createGraphicsLayer 方法用于在地图中创建图形图层。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
name (可选) |
The name of the new graphics layer. If no name is provided, the default |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
Layer |
对新图形图层的引用。 |
createGroupLayer(name, {group_layer})
允许您在工程 (.aprx) 内的地图中创建图层组。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
name |
A string that represents the name of the new group layer. |
String |
|
group_layer (可选) |
A reference to an existing group layer into which to insert the new group layer. Use this parameter to create nested group layers. |
Layer |
返回值
| 数据类型 | 说明 |
|---|---|
|
Layer |
对新图层组的引用。 |
exportBookmarks(out_bkmx)
该 exportBookmarks 方法可将与地图相关的所有书签导出到一个书签文件中 (.bkmx)。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
out_bkmx |
A string that represents the path and file name for the output bookmark file ( |
String |
exportToMAPX(out_mapx)
用于将 Map 导出至地图文件。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
out_mapx |
用于将 |
String |
getDefinition(cim_version)
获取地图的 CIM 定义。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
cim_version |
A string that represents the major version of the CIM. |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
Object |
返回 |
getWebLayerSharingDraft(server_type, service_type, service_name, {layers_and_tables})
在可配置并共享到 ArcGIS Enterprise 或 ArcGIS Online 的地图中创建共享草稿。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
server_type |
表示服务器类型的字符串。 支持的服务器类型如下:
提示:The |
String |
|
service_type |
表示服务类型的字符串。 支持的服务类型如下:
|
String |
|
service_name |
用于表示服务名称的字符串。 用户可以看到该名称并使用该名称来识别服务。 该名称可以包含字母数字字符、空格和下划线。 不允许使用任何特殊字符。 名称长度不能超过 120 个字符。 |
String |
|
layers_and_tables (可选) |
地图中的图层和表格列表。 如果留空,则将发布整个地图。 该参数允许您从地图中选择图层和表格的子集进行发布。 图层和表格必须来自正在发布的同一地图。 |
List |
返回值
| 数据类型 | 说明 |
|---|---|
|
Object |
返回 FeatureSharingDraft 、 TileSharingDraft 、 MapImageSharingDraft 、 SceneLayerSharingDraft 或 VectorTileSharingDraft 类对象。 |
importBookmarks(bkmx_path)
importBookmarks 方法可将书签文件中的所有书签 (.bkmx) 导入地图中。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
bkmx_path |
A string that represents the path and file name to a bookmark file ( |
String |
insertLayer(reference_layer, insert_layer_or_layerfile, {insert_position})
用于通过指定特定位置向工程 (.aprx) 内的地图添加 Layer 或 LayerFile 。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
reference_layer |
A Layer object representing an existing layer that determines the location where the new layer will be inserted. |
Layer |
|
insert_layer_or_layerfile |
A reference to a Layer or LayerFile object representing the layer or layers to be added. |
Layer |
|
insert_position (可选) |
A constant that determines the placement of the added layer or layers relative to the
(默认值为 BEFORE) |
String |
listBookmarks({wildcard})
返回 Map 中的 bookmark 对象的 Python 列表。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
wildcard (可选) |
通配符基于书签名称且不区分大小写。星号 (*) 和字符的组合可用于帮助限制生成的列表。 (默认值为 None) |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
|
listBrokenDataSources()
返回地图中到原始源数据的连接存在断开情况的 Layer 或 Table 对象的 Python 列表。
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
listLayers({wildcard})
返回存在于地图中的 Layer 对象的 Python 列表。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
wildcard (可选) |
通配符基于图层名称且不区分大小写。星号 (*) 和字符的组合可用于帮助限制生成的列表。 (默认值为 None) |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
返回地图中的 Layer 对象的 Python 列表。 |
listTables({wildcard})
返回存在于地图中的 Table 对象的 Python 列表。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
wildcard (可选) |
通配符基于图层名称且不区分大小写。星号 (*) 和字符的组合可用于帮助限制生成的列表。 (默认值为 None) |
String |
返回值
| 数据类型 | 说明 |
|---|---|
|
List |
地图中的 Table 对象的 Python 列表。 |
moveLayer(reference_layer, move_layer, {insert_position})
用于将地图中的图层或图层组移至相同地图内的特定位置。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
reference_layer |
A Layer object representing an existing layer that determines where the |
Layer |
|
move_layer |
A reference to a Layer object representing the layer to be moved. |
Layer |
|
insert_position (可选) |
A constant that determines the placement of the moved layer relative to the
(默认值为 BEFORE) |
String |
openView()
在应用程序中打开并激活一个新的地图视图窗格。
removeBookmark(remove_bookmark)
removeBookmark 方法用于从地图中移除书签。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
remove_bookmark |
A reference to a Bookmark object representing the bookmark to be removed. |
Bookmark |
removeLayer(remove_layer)
用于从工程内的地图中移除图层。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
remove_layer |
引用表示要移除的图层的 Layer 对象。 |
Layer |
removeTable(remove_table)
用于从工程内的地图中移除表。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
remove_table |
A reference to a Table object representing the layer to be removed. |
Table |
setColorModel(color_model)
用于设置与地图关联的颜色模型的字符串常量。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
color_model |
以下是有效字符串列表。
|
String |
setDefinition(definition_object)
设置地图的 CIM 定义。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
definition_object |
使用 |
Object |
updateConnectionProperties(current_connection_info, new_connection_info, {auto_update_joins_and_relates}, {validate}, {ignore_case})
updateConnectionProperties 方法使用工作空间字典或路径替换连接属性。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
current_connection_info |
A string that represents the workspace path or a Python dictionary that contains connection properties to the source you want to update. If an empty string or |
String |
|
new_connection_info |
A string that represents the workspace path or a Python dictionary that contains connection properties with the new source information. |
String |
|
auto_update_joins_and_relates (可选) |
If set to (默认值为 True) |
Boolean |
|
validate (可选) |
If set to (默认值为 True) |
Boolean |
|
ignore_case (可选) |
Determines whether searches will be case sensitive. By default, queries are case sensitive. To perform queries that are not case sensitive, set (默认值为 False) |
Boolean |
updateTransformations(transformations)
updateTransformations 方法使用字典替换地图的变换。
| 名称 | 说明 | 数据类型 |
|---|---|---|
|
transformations |
The dictionary keys are defined below.
|
Dictionary |
代码示例
下面的脚本引用了图层文件并将图层插入地图中已存在的图层上方:
import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
insertLyr = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\LayerFiles\Ranger Stations.lyrx")
m = aprx.listMaps("Yosemite National Park")[0]
refLyr = m.listLayers("Points of Interest")[0]
m.insertLayer(refLyr, insertLyr, "BEFORE")
aprx.saveACopy(r"C:\Projects\YosemiteNP\Yosemite_updated.aprx")
以下脚本将设置当前位于工程中的所有地图和场景的 defaultCamera 属性。 将从具有所需查看器设置的现有地图框中复制照相机属性。 所有地图将从 2D 地图框中进行复制,所有场景查看器设置将从 3D 地图框中进行复制。
import arcpy
aprx = arcpy.mp.ArcGISProject(r"C:\Projects\YosemiteNP\Yosemite.aprx")
lyt = aprx.listLayouts("Main Attractions*")[0]
mpFrm2D = lyt.listElements("mapframe_element", "Yose*")[0]
mpFrm3D = lyt.listElements("mapframe_element", "Inset1")[0]
for m in aprx.listMaps():
if m.mapType == "MAP":
m.defaultCamera = mpFrm2D.camera
elif m.mapType == "SCENE":
m.defaultCamera = mpFrm3D.camera
aprx.save()
del aprx
下面的脚本使用了关键字 current,因此可从 Python 窗口运行此脚本。 该脚本用于新建地图并向其添加新图层。 将更新地图的默认照相机属性,该属性用于控制新打开的视图的范围。 然后将地图视图导出为 PDF。 新建的地图随即从工程中移除。
aprx = arcpy.mp.ArcGISProject("CURRENT")
#Create a copy of an existing map
newMap = aprx.createMap("Ranger Stations", "Map")
#Add ranger stations layer file
lyrx1 = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\LayerFiles\Park Boundary.lyrx")
newMap.addLayer(lyrx1)
lyrx2 = arcpy.mp.LayerFile(r"C:\Projects\YosemiteNP\LayerFiles\Ranger Stations.lyrx")
newMap.addLayer(lyrx2)
#Close any current map or layout views
aprx.closeViews("MAPS_AND_LAYOUTS")
#Set the default map camera to the extent of the park boundary before opening the new view
#default camera only affects newly opened views
lyr = newMap.listLayers("*Park Boundary")[-1]
newMap.defaultCamera.setExtent(arcpy.Describe(lyr).extent)
newMap.openView()
#export the newly opened active view to PDF, then delete the new map
mv = aprx.activeView
mv.exportToPDF(r"C:\Temp\RangerStations.pdf", width=700, height=500, resolution=96)
aprx.deleteItem(newMap)