You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术咨询:从feature layer提取值为x的要素及排除fieldY=valueX要素的方法

Hey there! Let's break down your two Feature Layer questions with practical, platform-specific solutions:

1. Extract Features with Value "x" from a Feature Layer

Depending on the GIS platform you're using, here are straightforward ways to pull the target features:

ArcPy (ArcGIS Desktop/Pro)

Use the SelectLayerByAttribute tool to target your desired values, then export or work with the selected features:

import arcpy

# Replace with your layer name, target field, and value
target_layer = "my_feature_layer"
target_field = "field_name"
target_value = "x"

# Build the WHERE clause (adjust quotes if your field is numeric)
where_clause = f"{target_field} = '{target_value}'" if isinstance(target_value, str) else f"{target_field} = {target_value}"

# Select matching features
arcpy.SelectLayerByAttribute_management(
    in_layer_or_view=target_layer,
    selection_type="NEW_SELECTION",
    where_clause=where_clause
)

# Optional: Export selected features to a new layer
arcpy.CopyFeatures_management(target_layer, "output_features_with_x")

Note: Omit single quotes around target_value if your field stores numeric data (integers, floats).

ArcGIS API for Python (Online/Enterprise)

Query the Feature Layer directly with a filter to fetch matching features:

from arcgis.gis import GIS
from arcgis.features import FeatureLayer

# Connect to your GIS (replace with your URL/credentials if using Enterprise)
gis = GIS("home")

# Replace with your Feature Layer's REST URL
fl = FeatureLayer("https://your-feature-service-url/arcgis/rest/services/YourService/FeatureServer/0")

# Query for features where field equals "x"
query_result = fl.query(where="field_name = 'x'")

# Access the list of matching features
matching_features = query_result.features

# Optional: Save results to a local GeoJSON file
with open("matching_features.geojson", "w") as f:
    f.write(query_result.to_geojson())

PyQGIS (QGIS)

Use QgsFeatureRequest to filter features directly in QGIS:

from qgis.core import QgsProject, QgsFeatureRequest

# Get your layer from the current QGIS project
layer = QgsProject.instance().mapLayersByName("my_feature_layer")[0]

# Filter for features with value "x" (note the quoted field name)
request = QgsFeatureRequest().setFilterExpression('"field_name" = \'x\'')
matching_features = [feat for feat in layer.getFeatures(request)]

2. Return All Features Except Those Where "fieldY = valueX"

This is just a reverse filter—here's how to implement it across platforms:

ArcPy

Use a "not equal" operator in your WHERE clause to exclude the target features:

import arcpy

target_layer = "my_feature_layer"
exclude_field = "fieldY"
exclude_value = "valueX"

# Build the exclusion WHERE clause
where_clause = f"{exclude_field} <> '{exclude_value}'" if isinstance(exclude_value, str) else f"{exclude_field} <> {exclude_value}"

# Select all features except the excluded ones
arcpy.SelectLayerByAttribute_management(
    in_layer_or_view=target_layer,
    selection_type="NEW_SELECTION",
    where_clause=where_clause
)

# Optional: Export filtered features
arcpy.CopyFeatures_management(target_layer, "output_features_excluded")

You can also use NOT {exclude_field} = '{exclude_value}' instead of <>—both work identically.

ArcGIS API for Python

Adjust the query's WHERE clause to exclude the unwanted features:

# Using the same FeatureLayer object from the first example
query_result = fl.query(where="fieldY <> 'valueX'")

# Access the filtered feature list
filtered_features = query_result.features

PyQGIS

Modify the filter expression to exclude the target condition:

# Using the same layer from the first PyQGIS example
exclude_request = QgsFeatureRequest().setFilterExpression('"fieldY" <> \'valueX\'')
filtered_features = [feat for feat in layer.getFeatures(exclude_request)]

内容的提问来源于stack exchange,提问作者SkiZer0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:15:21