Part 6 - Standard Geography Queries¶
# Import Libraries
from arcgis.gis import GIS
from arcgis.geoenrichment import Country, enrich, service_limits, standard_geography_query
# Create a GIS Connection
gis = GIS(profile='your_online_profile')
# Get Country
usa = Country.get('USA')
Standard Geography Queries¶
Previously in Geoenrichment you have learnt that a study area
is used to define the location of the point or area that you want to enrich with additional information. Now, you will be introduced a new form of study area
- the Standard Geography Area
which lets you define an area by the ID of a standard geographic statistical feature, such as a census or postal area. For example, to obtain enrichment information for a U.S. state, county or ZIP Code or a Canadian province or postal code. The most common workflow for this service is to find a FIPS (standard geography ID) for a geographic name.
standard_geography_query
method allows you to query for standard geography IDs and features at the supported geographic levels, and then they can be used to obtain facts about the location using the enrich
method or create reports using create_report
.
Using Standard Geography Query¶
Let's look at an example to find the standard geography ID for all Orange counties in U.S. We will then use one of these IDs and enrich()
the area with information from Age
data collection.
We will use US
as the source country and specify US.Counties
as the standard geographic layer to be queried since we are looking for Orange counties across U.S. We will use orange
as the text for the service to query.
# Find FIPS for all Orange counties in US
orange = standard_geography_query(source_country='US', layers='US.Counties', geoquery='orange')
orange
The resulting dataframe shows DatasetID, DataLayerID
which are the IDs for dataset and layer being queried. AreaID
is the unique ID for each area in the results. AreaName
is Orange County as we looked for Orange counties across U.S. MajorSubdivisionName, MajorSubdivisionAbbr and MajorSubdivisionType
show the type of major subdivision i.e. State
along with state name and abbrevation.
Enrich using results from Standard Geography Query¶
The standard_geography_query
returns a list of Orange counties for different states, with the state name shown as field MajorSubdivisionName
. Now, let's enrich()
Orange County in California using AreaID: 06059
.
or_ca = {"sourceCountry":"US","layer":"US.Counties","ids":["06059"]}
orange_df = enrich(study_areas=[or_ca], data_collections=['Age'] )
orange_df
orange_df.columns
Enrichment using Age
data collection resulted in many columns for various age groups. Other columns such as Standard Geography ID, Name, Level, country, and populationToPolygonSizeRating were also added with enrichment.
Visualize on a Map¶
Let's visualize the enriched geography on a map.
or_ca_map = gis.map('Los Angeles, CA')
or_ca_map
orange_df.spatial.plot(or_ca_map)
Customizing your Query¶
geoquery
parameter is used to specify the search criteria in order to query for the standard geography layers desired. A query is broken up into terms and operators. Multiple terms can be combined together with Boolean operators to form more complex queries. Learn more about using geoquery
to create more complex queries here.
Let's look at an example of grouping the search terms to find all Orange or Lake
counties in US. Search supports using parentheses to group clauses to form subqueries. This can be useful if you want to control the Boolean logic for a query.
or_lake = standard_geography_query(source_country='US', layers='US.Counties', geoquery='(Orange OR Lake)')
or_lake
We see that there are multiple Orange and Lake counties in US. Let's get the results for Orange or Lake county in California.
or_lake_ca = standard_geography_query(source_country='US', layers='US.Counties', geoquery='(Orange OR Lake) AND CA')
or_lake_ca
Enrich using results from Standard Geography Query¶
The standard_geography_query
gave us details of Orange and Lake counties in California. Now, let's enrich()
these counties using AreaID
.
or_lk = {"sourceCountry":"US","layer":"US.Counties","ids":["06059","06033"]}
or_lake_df = enrich(study_areas=[or_lk], data_collections=['Age'] )
or_lake_df
Visualize on a Map¶
Let's visualize the enriched counties on a map.
or_lake_map = gis.map('California, US',6)
or_lake_map