Vector Layer Editing¶
Vector layer editing involves modifying features such as points, lines, and areas within a spatial dataset. These features can be shaped to represent real-world objects such as buildings and tree canopies. Attribute data is linked to each feature allowing you to assign detailed classifications for analysis and development.
Edit Features Based on Attribute¶
This script iterates through all of the area features in a shapefile. It checks for names of vector areas with a specific attribute value (GREAT BASIN CONIFER WOODLAND) and display name (COMMUNITY). It changes the area’s classification to wood and adds a label to that area.
i = 0
modified_ct = 0
feature = gm.GetAreaFeature(layer_arr[0], i)
# Iterates over area features
while feature:
# Converts from C++ pointer to an array
attr_array = gm.GM_AttrValue_array_frompointer(feature.mFeatureInfo.mAttrList)
# Converts array to a python list
attribute_values = gm.carray_to_list(attr_array, feature.mFeatureInfo.mNumAttrs)
# Iterates through python list of attribute names and attribute values
for attribute in attribute_values:
attr_name = attribute.mName
attr_val = attribute.mVal
# If conditions are met, area feature is assigned to feature class AFC_WOODS
if attr_name == "COMMUNITY" and attr_val == "GREAT BASIN CONIFER WOODLAND":
err_SetFeatureClass = gm.SetFeatureClass(
layer_arr[0], gm.GM_FeatureClass_Area, i, gm.AFC_WOODS
)
modified_ct += 1
# If conditions are met, area feature is given a label
if attr_name == "COMMUNITY" and attr_val == "GREAT BASIN CONIFER WOODLAND":
err_SetFeatureLabel = gm.SetFeatureLabel(
layer_arr[0],
gm.GM_FeatureClass_Area,
i,
"Great Basin Conifer Woodland",
)
# Frees deep copies from memory
# Without this step the script may freeze
gm.FreeAreaFeature(feature)
i += 1
feature = gm.GetAreaFeature(layer_arr[0], i)
# Displays the number of area features that were set to the Feature Class "Area Woods".
print("Set", modified_ct, "area features to the type WOODS")
Delete Holes from Area¶
A sample script that deletes all holes from a shapefile by iterating through each area feature. For each feature that hasn’t already been deleted, it deletes that hole. Afterward, the total number of holes deleted and the layer from which they were removed are output.
# Iterates over area features
i = 0
holes_deleted = 0
feature = gm.GetAreaFeature(layers_list[0], i)
# Goes through all available area features in the provided layer
while feature:
# For each hole in that feature, if it's not deleted, delete it
for j in range(feature.mNumHoles):
err_IsHoleInAreaDeleted, val = gm.IsHoleInAreaDeleted(layers_list[0], i, j)
if not val:
err_DeleteHoleInArea = gm.DeleteHoleInArea(layers_list[0], i, j, True)
holes_deleted += 1
# Frees deep copies from memory
# Without this step the script causes the program to freeze
gm.FreeAreaFeature(feature)
i += 1
feature = gm.GetAreaFeature(layers_list[0], i)
layer_name = gm.GetLayerInfo(layers_list[0]).mDescription
print("\n" + str(holes_deleted), "holes deleted from", layer_name, "\n")
SetFeatureAttrList Sample¶
A demonstration on how to use SetFeatureAttrList. A list of attributes is created for a given feature and then assigned to it.
# Adds two attributes to an area feature
attr_1 = gm.GM_AttrValue_t("GM_TYPE", "Building")
attr_2 = gm.GM_AttrValue_t("AREA_TYPE", "Residential")
attr_list = [attr_1, attr_2]
size = len(attr_list)
attr_array = gm.GM_AttrValue_array(size)
for i in range(size):
attr_array[i] = attr_list[i]
err_SetFeatureAttrList = gm.SetFeatureAttrList(
layers_list[0], gm.GM_FeatureClass_Area, 0, attr_array, size
)