Vector Query Samples

Vector queries allow users to search and identify vector features based on their spatial and/or attribute data. This is useful when a user only wants to work with a limited dataset, based on specific vector attributes and features, or if multiple vector features need to be modified.

Display Vector Attributes with NumPy and Matplotlib

Note

Before running this sample, configure these variables and modules:

  • export_dir: the directory for resulting files to be exported to

  • import numpy as np

  • import matplotlib.pyplot as plt

A sample script that gathers all of the Arizona Biotic Communities from a shapefile and then iterates though the gathered communities and determines how many unique attributes each one has. This data is then formatted into a bar-graph using NumPy and Matplotlib and exported as a JPG.

i = 0
feature = gm.GetAreaFeature(layer_arr[0], i)
# Will hold attributes of with value 'COMMUNITY' as python list
az_communities = []

# 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
        # Places all attributes with name 'COMMUNITY' into a list
        if attr_name == "COMMUNITY":
            az_communities.append(attr_val)

    # Frees deep copies from memory
    # Without this step the script causes the program to freeze
    gm.FreeAreaFeature(feature)
    i += 1
    feature = gm.GetAreaFeature(layer_arr[0], i)

# Example of using numpy array to display data
# az_com_array converts python list into a numpy arrary
az_com_array = np.array(az_communities)
# Converts characters in az_com_array to title case for stylistic purposes
az_com_title = np.char.title(az_com_array)
# Counts each unique attribute within attribute name "COMMUNITY"
az_coms, az_com_count = np.unique(az_com_title, return_counts=True)

# Uses Matplotlib to create a bar graph of Arizona Biotic Communities
fig, ax = plt.subplots()
fig.set_figheight(7)
fig.set_figwidth(9)
x = az_coms
y = az_com_count
ax.bar(x, y, edgecolor="white")
ax.set_ylabel("Total Number of Arizona Biotic Communities")
ax.set_title("Count of Arizona Biotic Communities")
plt.xticks(x, rotation="vertical", fontsize=8, wrap=True)
plt.savefig(export_dir + "arizona_biotic_communities.jpg")

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")