gpf.tools.maputils module

Module that simplifies working with layers in ArcMap.

gpf.tools.maputils.get_mxd(path: str = None) → _arcpy.mapping.MapDocument[source]

Gets a MapDocument instance for the given MXD path. If path is None, the current map document is retrieved. This will only work when this function is called by a script running within the context of ArcMap.

Parameters:path – Path to the MXD or None.
Raises:ValueError – If the MXD was not found or could not be read.
gpf.tools.maputils.find_dataframe(mxd: _arcpy.mapping.MapDocument, name: str = None, case_sensitive: bool = False)[source]

Finds a data frame by its (case-insensitive) name in the given ArcMap document (MXD) and returns it. If no name is specified, the active data frame in the given Map Document is returned. If no data frame was found at all, None is returned. This can only happen, when looking for a specific name.

Parameters:
  • mxd – A arcpy.mapping.MapDocument instance.
  • name – The name of the data frame to search for.
  • case_sensitive – If True, the data frame name needs to match exactly. If False (default), the data frame character case is ignored.
Return type:

arcpy._mapping.DataFrame

gpf.tools.maputils.find_layer(name: str, mxd: Union[str, _arcpy.mapping.MapDocument] = None, dataframe: str = None, case_sensitive: bool = False) → _arcpy.mapping.Layer[source]

Finds a single layer by its (case-insensitive) name in the specified ArcMap document (or current one). If the layer was not found, None is returned.

Parameters:
  • name – Name of the layer to find. If the layer name exists multiple times in different group layers, you can prefix the layer with the group layer name followed by a forward slash (/).
  • mxd – The path to the ArcMap Document (MXD) or a MapDocument instance in which to find the layer. If no MXD is specified, the search will take place in the current MXD (if any).
  • dataframe – The name of the data frame in which to find the layer. If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame.
  • case_sensitive – If True, the layer name needs to match exactly. If False (default), the layer character case is ignored. Note that this setting also affects the dataframe argument, when specified.
Raises:

ValueError – If no layer name was specified, or if no map document was found.

gpf.tools.maputils.find_layers(wildcard: str = None, mxd: Union[str, _arcpy.mapping.MapDocument] = None, dataframe: str = None) → List[_arcpy.mapping.Layer][source]

Finds all layers that match a certain wild card expression in the specified ArcMap document (or current one). All matching layers are returned as a list of Layer instances. If no layers were found, an empty list is returned.

Parameters:
  • wildcard – Layer name search string (with wild card characters). Unlike the find_layer() method, it is not possible to use a group layer prefix here. Furthermore, the search string is case sensitive. If this value is not specified, all layers in the map document will be returned.
  • mxd – The path to the ArcMap Document (MXD) or a MapDocument instance in which to find the layer(s). If no MXD is specified, the search will take place in the current MXD (if any).
  • dataframe – The case-insensitive name of the data frame in which to find the layer(s). If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame.
Raises:

ValueError – If no map document was found.

gpf.tools.maputils.get_referenced_layers(dataset_path: str, mxd: Union[str, _arcpy.mapping.MapDocument] = None, dataframe: str = None, strict: bool = True) → List[_arcpy.mapping.Layer][source]

Returns a list of all layers in which dataset_path is used as the data source. The search takes place in the specified mxd (or current one) in the given dataframe (or active one). All matching layers are returned as a list of Layer instances. If no layers were found, an empty list is returned.

Parameters:
  • dataset_path – The full path to the dataset (e.g. feature class, table) to find. The path is case-insensitive, but if the layer data source is expected to have database qualifiers, these should be included as well, unless strict is False.
  • mxd – An optional path of the ArcMap document (MXD) to search through. If no MXD is specified, the search will take place in the current MXD.
  • dataframe – The case-insensitive name of the data frame in which to find the layer(s). If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame.
  • strict – If True (default) the case-insensitive data source path of the layer needs to exactly match the dataset_path. For SDE connections, this could mean that the data source will never be found. If strict is set to False, only the feature class name (and feature dataset name, if applicable) is matched.
Raises:

ValueError – If no map document was found.

gpf.tools.maputils.get_layer_selection(layer: Union[str, _arcpy.mapping.Layer], mxd: Union[str, _arcpy.mapping.MapDocument] = None, dataframe: str = None, case_sensitive: bool = False) → set[source]

Returns a set of selected Object ID’s for the given layer name or Layer instance. Note that this might return an empty set if no features or rows are selected for the given layer. This behaviour is different from the standard call to getSelectionSet() on an ArcMap Layer object: if no selection is present, arcpy returns None instead.

Parameters:
  • layer – Layer instance or the name of the layer for which to get the selected Object ID’s. If the layer name exists multiple times in different group layers, you can prefix the layer with the group layer name followed by a forward slash (/). If a Layer instance is used as input, all other arguments are ignored and the selection set is returned immediately.
  • mxd – The path to the ArcMap Document (MXD) or a MapDocument instance in which to find the layer. If no MXD is specified, the search will take place in the current MXD (if any).
  • dataframe – The name of the data frame in which to find the layer. If no data frame is specified and/or there is only 1 data frame, the search will take place in the active data frame.
  • case_sensitive – If True, the layer name needs to match exactly. If False (default), the layer character case is ignored. Note that this setting also affects the dataframe argument, when specified.