arcsmith.lyr
Layer
Helpers for adding layers to a map and applying .lyrx symbology programmatically inside 'Tool.execute' and 'Tool.postExecute' functions.
- Add data sources to a map with optional symbology and display name
- Apply .lyrx symbology to existing layers by name or data source path
- Retrieve or remove layers from the map TOC with optional geometry type filtering
Functions of the layer module
add
Adds a data source to a map as a new layer, with optional symbology and display name.
| Parameter | Type | Default | Description |
|---|---|---|---|
lyr_src |
str, Path, or arcpy.Result |
required | Path to the data source (feature class, raster, etc.) to add. arcpy Result objects (e.g. the return value of arcpy.management.Dissolve) are accepted and resolved to their output path automatically. |
target_map |
arcpy.mp.Map |
required | Map object to add the layer to. |
lyr_name |
str |
None |
Display name for the layer in the TOC. Defaults to the source name. |
lyrx_src |
str or Path |
None |
Path to a .lyrx file. If provided, its symbology is applied to the new layer. |
Returns
arcpy.mp.Layer: the newly added layer.
Memory workspace
Feature classes written to the memory workspace during execute are
not visible in the map TOC until explicitly added with this function.
Add them here first before calling get_lyr, apply_lyrx, or remove.
Examples
# Add a layer with default symbology
lyr = arcsmith.lyr.add("path/to/fc", target_map)
# Add a layer with a custom display name
lyr = arcsmith.lyr.add("path/to/fc", target_map, lyr_name="Study Area")
# Add a layer with a name and symbology
lyr = arcsmith.lyr.add("path/to/fc", target_map, lyr_name="Rivers", lyrx_src="path/to/sym.lyrx")
# Pass an arcpy Result directly (e.g. from Dissolve) and add to map with symbology
result = arcpy.management.Dissolve(in_fc, output_path, dissolve_field)
lyr = arcsmith.lyr.add(result, target_map, lyr_name="State Boundaries", lyrx_src="path/to/sym.lyrx")
apply_lyrx
Applies symbology from a .lyrx file to one or more layers already present in the map TOC, matched by display name or data source path.
| Parameter | Type | Default | Description |
|---|---|---|---|
target_map |
arcpy.mp.Map |
required | Map object containing the layer(s) to update. |
lyrx_src |
str or Path |
required | Path to the .lyrx file containing the symbology to apply. |
lyr_name |
str |
None |
Display name to match. All layers with this name are updated. |
lyr_source |
str or Path |
None |
Data source path to match. Only the first exact match is updated. |
geom_type |
'Point', 'Polyline', 'Polygon', or 'Multipoint' |
None |
Geometry type filter when matching by lyr_name. Case-insensitive. Ignored when matching by lyr_source. |
Returns
list of arcpy.mp.Layer: all layers that were updated.
Raises
ValueError if neither or both of lyr_name and lyr_source are provided.
Raises
ValueError if no matching layers are found in the map.
Note
Exactly one of lyr_name or lyr_source must be provided on each call. Matching by lyr_name updates all layers with that name; matching by lyr_source updates only the first exact match.
Memory workspace
Memory layers should always be matched by lyr_name using their TOC
display name. Their internal data source path is not predictable and
cannot be used reliably with lyr_source.
Examples
# Update all layers named "rivers"
lyrs = arcsmith.lyr.apply_lyrx(target_map, "path/to/sym.lyrx", lyr_name="rivers")
# Update only polyline "rivers" layers
lyrs = arcsmith.lyr.apply_lyrx(target_map, "path/to/sym.lyrx", lyr_name="rivers", geom_type="Polyline")
# Update a single layer by data source path
lyrs = arcsmith.lyr.apply_lyrx(target_map, "path/to/sym.lyrx", lyr_source="path/to/fc")
get_lyr
Retrieves layer(s) from the map TOC by display name or data source path.
| Parameter | Type | Default | Description |
|---|---|---|---|
target_map |
arcpy.mp.Map |
required | Map object to search. |
lyr_name |
str |
None |
Display name to match. All layers with this name are returned. |
lyr_source |
str or Path |
None |
Data source path to match. Only the first exact match is returned. |
geom_type |
'Point', 'Polyline', 'Polygon', or 'Multipoint' |
None |
Geometry type filter when matching by lyr_name. Case-insensitive. Ignored when matching by lyr_source. |
Returns
list of arcpy.mp.Layer: all matching layers. When matching by lyr_source, the list contains at most one entry.
Raises
ValueError if neither or both of lyr_name and lyr_source are provided.
Raises
ValueError if no matching layers are found in the map.
Note
Exactly one of lyr_name or lyr_source must be provided on each call. Matching by lyr_name returns all layers with that name; matching by lyr_source returns only the first exact match.
Memory workspace
Memory layers should always be matched by lyr_name using their TOC
display name. Their internal data source path is not predictable and
cannot be used reliably with lyr_source.
Examples
# Get all layers named "rivers"
lyrs = arcsmith.layer.get_lyr(target_map, lyr_name="rivers")
# Get only polyline "rivers" layers
lyrs = arcsmith.layer.get_lyr(target_map, lyr_name="rivers", geom_type="Polyline")
# Get a single layer by data source path
lyrs = arcsmith.layer.get_lyr(target_map, lyr_source="path/to/fc")
# Get a memory layer by its TOC display name
lyrs = arcsmith.layer.get_lyr(target_map, lyr_name="my_memory_layer")
remove
Removes layer(s) from the map TOC by display name or data source path.
| Parameter | Type | Default | Description |
|---|---|---|---|
target_map |
arcpy.mp.Map |
required | Map object to remove layer(s) from. |
lyr_name |
str |
None |
Display name to match. All layers with this name are removed. |
lyr_source |
str or Path |
None |
Data source path to match. Only the first exact match is removed. |
geom_type |
'Point', 'Polyline', 'Polygon', or 'Multipoint' |
None |
Geometry type filter when matching by lyr_name. Case-insensitive. Ignored when matching by lyr_source. |
Returns
int: number of layers removed.
Raises
ValueError if neither or both of lyr_name and lyr_source are provided.
Raises
ValueError if no matching layers are found in the map.
Note
Exactly one of lyr_name or lyr_source must be provided on each call. Matching by lyr_name removes all layers with that name; matching by lyr_source removes only the first exact match.
Memory workspace
Memory layers should always be matched by lyr_name using their TOC
display name. Their internal data source path is not predictable and
cannot be used reliably with lyr_source.
Examples
# Remove all layers named "rivers"
n = arcsmith.layer.remove(target_map, lyr_name="rivers")
# Remove only polyline "rivers" layers
n = arcsmith.layer.remove(target_map, lyr_name="rivers", geom_type="Polyline")
# Remove a single layer by data source path
n = arcsmith.layer.remove(target_map, lyr_source="path/to/fc")
# Remove a memory layer by its TOC display name
n = arcsmith.layer.remove(target_map, lyr_name="my_memory_layer")