Tutorial: Query a feature layer (spatial)

Learn how to execute a spatial query to access features from a feature layer.

A feature layer can contain a large number of features. To access a subset of these features, you can execute an SQL or spatial query, either together or individually. The results can contain the attributes, geometry, or both for each record. SQL and spatial queries are useful when a feature layer is very large and you want to access only a subset of its data.

In this tutorial, you access a hosted feature layer and query the service with a geometry and spatial operator to only return intersecting records.

Prerequisites

The ArcGIS API for Python tutorials use Jupyter Notebooks to execute Python code. If you are new to this environment, please see the guide to install the API and use notebooks locally.

Steps

Import modules and log in

  1. Import the arcgis.gis module.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    from arcgis.gis import GIS
    
    
    
  2. Log in anonymously to ArcGIS Online to access publicly shared data. Since this dataset is public you do not need credentials to access it. If it were a private dataset, you would be required to log in

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    from arcgis.gis import GIS
    
    
    portal = GIS()
    
    

Access the feature layer

  1. Use the ContentManager class to access the dataset by Item ID.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    portal = GIS()
    
    parcel_layer_item = portal.content.get("a6fdf2ee0e454393a53ba32b9838b303")
    
    parcel_layer = parcel_layer_item.layers[0]
    
    

Run the query

  1. Create an Extent object as a geometry filter. The contains method will return only the features that are wholly contained within this extent. The as_df parameter is set to False so that the results will be returned as FeatureSet. Set the out_fields as an array of field names to be included in the results.
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    parcel_layer_item = portal.content.get("a6fdf2ee0e454393a53ba32b9838b303")
    
    parcel_layer = parcel_layer_item.layers[0]
    
    from arcgis.geometry.filters import contains
    
    query_extent = {
        "xmin": -13198303.18,
        "ymin": 4059062.83,
        "xmax": -13197797.98,
        "ymax": 4059421.13,
        "spatialReference": {"wkid": 102100},
    }
    
    query_filter = contains(query_extent, sr=102100)
    
    results = parcel_layer.query(
        geometry_filter=query_filter, out_fields="APN, UseType", as_df=False
    )
    results
    
    
    

Display the results

  1. Use the map method to create a map widget. Import the arcgis.map.popups module and use the PopupInfo class to define and enable the popups. The curly braces in the popup content are templates that will use the field's value at run time. Use the add method to add the FeatureSet results and popup info to the map content and the zoom_to_layer() method to set the maps extent so the query results are visible.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    from arcgis.geometry.filters import contains
    
    query_extent = {
        "xmin": -13198303.18,
        "ymin": 4059062.83,
        "xmax": -13197797.98,
        "ymax": 4059421.13,
        "spatialReference": {"wkid": 102100},
    }
    
    query_filter = contains(query_extent, sr=102100)
    
    results = parcel_layer.query(
        geometry_filter=query_filter, out_fields="APN, UseType", as_df=False
    )
    results
    
    
    map = portal.map()
    map
    
    from arcgis.map.popups import PopupInfo
    map.content.add(results, popup_info=PopupInfo(title= "{UseType} Parcel", description = "Parcel number: {APN}"))
    
    map.zoom_to_layer(results)
    
    
  2. Optional: Use the export_to_html method to export the current state of the map widget to a static HTML file which can be viewed in any web browser.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    
    map = portal.map()
    map
    
    from arcgis.map.popups import PopupInfo
    map.content.add(results, popup_info=PopupInfo(title= "{UseType} Parcel", description = "Parcel number: {APN}"))
    
    map.zoom_to_layer(results)
    
    import os
    
    file_path = os.path.join(os.getcwd(), "home", "query-a-feature-layer-sql.html")
    
    map.export_to_html(file_path, title="Query a feature layer (SQL)")

When the map displays, the features returned from the query are displayed in the center of the map. Click on a parcel to show a pop-up with the features attributes.

What's next?

Learn how to use additional functionality in these tutorials:

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.