技术咨询:从feature layer提取值为x的要素及排除fieldY=valueX要素的方法
Hey there! Let's break down your two Feature Layer questions with practical, platform-specific solutions:
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)]
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

