Learn how to query global demographic information for locations around the world with the GeoEnrichment service.
The GeoEnrichment service provides global demographic data for 170 countries and regions. To get globally available information, you use the KeyGlobalFacts data collection, which returns information for the total population, total households, average household size, and total population for males and females for a study area.
In this tutorial, you use ArcGIS REST JS to access the GeoEnrichment service and display global data for Eastern Europe.
To access location services, you need an API key or OAuth 2.0 access token. To learn how to create and scope your key, visit the Create an API key tutorial.
Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.
In CodePen, update apiKey to use your key.
Use dark colors for code blocks
Change line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
const apiKey = "YOUR_API_KEY";
const basemapEnum = "arcgis/streets";
const map = new maplibregl.Map({
container: "map", // the id of the div elementstyle: `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapEnum}?token=${apiKey}`,
zoom: 12, // starting zoomcenter: [-118.805, 34.027] // starting location [longitude, latitude]});
The GeoEnrichment service has data sources for many countries around the world, including most of Europe. Change the position of the map to center on eastern Europe.
Update the center parameter to [18.88, 47.33] and set zoom to 5.
You need a location before calling the GeoEnrichment service. To get a location, you can add a handler to the Map's click event. The click handler will be called with an object containing a LngLat.
You can change the mouse cursor to a crosshair to make it easier to click precisely. Use map.getCanvas to get the canvas, then modify its style property.
After the map initialization code, set the mouse cursor style to crosshair.
You pass one or more study areas to arcgisRest.queryDemographicData to specify the location of your query. To query a buffer around a point, pass a geometry object with x and y parameters. The default search radius is one mile.
Create a new arcgisRest.ApiKey to access the feature service.
Inside the click handler, call arcgisRest.queryDemographicData. Set the studyAreas parameter to a point geometry made from the event's lngLat property. Set the dataCollections parameter to to ["KeyGlobalFacts"] to obtain global data for your location.
queryDemographicData is an asynchronous function. Using await, and declaring the click handler async, means the rest of the click handler will wait for a response from the GeoEnrichment service before continuing.
If the query is successful, the response will contain a results array with a value containing a FeatureSet. The FeatureSet contains attributes such as population within the study area, the number of males and females, and the average household size. A message will display if there is no data available for a location selected. To learn more, visit the GeoEnrichment service page.
You can use a pop-up to show the results of the query at the location where you clicked on the map.
To add a pop-up to the map, you use the Popup class in MapLibre GL JS. See the Display a pop-up tutorial for more information.
Set a variable containing the contents of the pop-up, using values from the query response.