Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Esri Developer

ArcGIS API for Python

Tutorial: Query a feature layer (SQL)

Learn how to execute a SQL query to access polygon features in a feature layer.

Python mapping widget showing parcels queried with a SQL expression.

Afeature layer can contain a large number offeatures. To access a subset of these features, you can execute an SQL or spatial query, either together or individually. The results can contain theattributes,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 perform server-side SQL queries to return a subset of the features from theLA County Parcels feature service. The feature layer contains over 2.4 million features.

Prerequisites

The ArcGIS API for Python tutorials useJupyter Notebooks to execute Python code. If you are new to this environment, please see the guide toinstall the API anduse notebooks locally.

Steps

Import modules and log in to portal

  1. Import thearcgis.gis module.

    Use dark colors for code blocks
    1234567891011121314151617181920212223242526272829303132333435from arcgis.gisimport GIS
  2. Create an anonymous connection to ArcGIS Online to access public 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
    1234567891011121314151617181920212223242526272829303132333435from arcgis.gisimport GISportal = GIS()

Access the feature layer by itemId

  1. Use theContentManager class to access the dataset byItem ID.

Run the query

  1. Create a string variable containing the SQL statement. This will query only the features of this feature layer where theUseType attribute is set toResidential. Pass this in thewhere parmeter to thequery() method of theFeatureLayer object. Theas_df parameter is set toFalse so that the results will be returned asFeatureSet. Thereturn_all_records andresult_record_cout parameters are used to only return the first 100 records satisfying the query. Set theout_fields as an array of field names to return in the results.

    Use dark colors for code blocks
    1234567891011121314151617181920212223242526272829303132333435parcel_layer_item = portal.content.get("a6fdf2ee0e454393a53ba32b9838b303")parcel_layer = parcel_layer_item.layers[0]where_clause ="UseType = 'Residential'"results = parcel_layer.query(  where = where_clause,  as_df =False,  return_all_records =False,  result_record_count =100,  out_fields ="APN, UseType")

Display the results

  1. Use themap method to create a map widget. Use theadd method to add theFeatureSet results to the map contents and thezoom_to_layer() method to set the maps extent so the query results are visible. Import thearcgis.map.popups module and use thePopupInfo 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 dark colors for code blocks
    1234567891011121314151617181920212223242526272829303132333435where_clause ="UseType = 'Residential'"results = parcel_layer.query(  where = where_clause,  as_df =False,  return_all_records =False,  result_record_count =100,  out_fields ="APN, UseType")map = portal.map()from arcgis.map.popupsimport PopupInfomap.content.add(results, popup_info=PopupInfo(title="{UseType} Parcel", description ="Parcel number: {APN}"))mapmap.zoom_to_layer(results)
  2. Optional: Use theexport_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
    1234567891011121314151617181920212223242526272829303132333435map = portal.map()from arcgis.map.popupsimport PopupInfomap.content.add(results, popup_info=PopupInfo(title="{UseType} Parcel", description ="Parcel number: {APN}"))mapmap.zoom_to_layer(results)import osfrom osimport path, getcwdexport_dir = path.join(getcwd(),"home")ifnot path.isdir(export_dir):    os.mkdir(export_dir)export_path = path.join(export_dir,"query-a-feature-layer-sql.html")map.export_to_html(export_path, title="Query a feature layer (SQL))

When the map displays, you should see the results records displayed in the center of the map. Click on a parcel to show a pop-up with thefeatures attributes.

What's next?

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


[8]ページ先頭

©2009-2025 Movatter.jp