# Calculate density

A density analysis resulting in a new layer with traffic crashes per square mile in San Francisco.

## What is a density analysis?

A density analysis is the process of spreading out known quantities of point attributes and classifying the areas on a scale of least to most dense per square mile or per square kilometer. To execute the analysis, use the spatial analysis service and the `CalculateDensity` operation.

Real-world examples of this analysis include the following:

• Visualizing population density.
• Finding the density of points of interest (POI) for site selection.
• Visualizing the density of crimes within a city.
• Finding the areas with more forest fires and other natural disasters.

## How to perform a calculate density analysis

The general steps to calculating density are as follows:

1. Review the parameters for the `CalculateDensity` operation.
2. Send a request to get the spatial analysis service URL.
3. Execute a job request with the following URL and parameters:
• URL: `https://<YOUR_ANALYSIS_SERVICE>/arcgis/rest/services/tasks/GPServer/CalculateDensity/submitJob`
• Parameters:
• `inputLayer`: Your dataset as a hosted feature layer or feature collection.
• `areaUnits`: Such as square miles.
• `classificationType`: Equal interval, natural breaks etc.
• `outputName`: A string representing the name of the hosted feature layer to reutrn with the results.
4. Check the status.
5. Get the output layer results.

To see examples using ArcGIS API for Python, ArcGIS REST JS, and the ArcGIS REST API, go to Examples below.

## URL request

Use dark colors for code blocksCopy
``https://analysis3.arcgis.com/arcgis/rest/services/tasks/GPServer/CalculateDensity/submitJob?<parameters>``

### Required parameters

NameDescriptionExamples
`f`The format of the data returned.`f=json` `f=pjson`
`token`An OAuth 2.0 access token. Learn how to get an access token in Security and authentication.`token=<ACCESS_TOKEN>`
`inputLayer`The point or polyline features from the feature dataset.`{"url":"https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/OSM_POI/FeatureServer/0","name":"OpenStreetMap - Points of interest (bèta) - Punten"}`

### Key parameters

NameDescriptionExamples
`areaUnits`The units of the calculated density values.`SquareMiles`, `SquareKilometers`
`boundingPolygonLayer`The polygon(s) from a hosted feature layer or feature collection in which you want densities to be calculated.`{"url":"https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Portland_boundary/FeatureServer/0"}`
`classificationType`How density values will be classified.`NaturalBreaks`
`numClasses`Number used to divide the range of predicted values into distinct classes.`10`
`outputName`A string representing the name of the hosted feature layer to return with the results. NOTE: If you do not include this parameter, the results are returned as a feature collection (JSON).`{"serviceProperties": {"name": "<SERVICE_NAME>"}}`
`context`A bounding box or output spatial reference for the analysis.`"extent":{"xmin:", "ymin:", "xmax:", "ymax:"}`

## Example

### Calculate the density of POI

This example finds where there are the most POIs per square mile in the area surrounding Nijmegen. The `inputLayer` value is the OpenStreetMap - Points of interest (bèta) hosted feature layer. The density values are calculated by `NaturalBreaks` with `10` classes within a `boundingPolygonLayer`.

Calculate density analysis showing the density of points of interest in Nijmegen.

#### APIs

ArcGIS API for PythonArcGIS API for PythonArcGIS REST JS
Expand
Use dark colors for code blocksCopy
``````osm_poi = "https://services.arcgis.com/nSZVuSZjHpEZZbRo/arcgis/rest/services/OSM_POI/FeatureServer/0"
boundary = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Nijmegen_boundary_layer/FeatureServer/3"

results = calculate_density(
input_layer=osm_poi,
bounding_polygon_layer=boundary,
context={
"extent": {
"xmin": 636264.364494962,
"ymin": 6760768.706403579,
"xmax": 669246.9421999712,
"ymax": 6781311.157755191,
"spatialReference": {"wkid": 102100, "latestWkid": 3857},
},
"outSR": {"wkid": 3857},
},
# Outputs results as a hosted feature layer.
output_name="Calculate density results",
)

result_features = results.layers[0].query()

print(f"The density layer has {len(result_features.features)} new records")
``````

#### Service requests

##### Request
HTTPHTTPcURL
Use dark colors for code blocksCopy
``````POST arcgis.com/sharing/rest/portals/self HTTP/1.1
Content-Type: application/x-www-form-urlencoded

&f=json
&token=<ACCESS_TOKEN>``````
##### Response (JSON)
Use dark colors for code blocksCopy
``````{
"helperServices": {
// Other parameters
"analysis": {
},
"geoenrichment": {
"url": "https://geoenrich.arcgis.com/arcgis/rest/services/World/GeoenrichmentServer"
}
}
}``````

### Calculate the density of bike routes

This example finds where there are the most bike routes per square mile within the boundary of Portland. The `inputLayer` value is the Bike routes hosted feature layer. The density values are calculated by `NaturalBreaks` with `10` classes within a `boundingPolygonLayer`.

Calculate density analysis showing the density of bike routes per square mile in a section of Portland.

#### APIs

ArcGIS API for PythonArcGIS API for PythonArcGIS REST JS
Expand
Use dark colors for code blocksCopy
``````bike_routes = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Portland Bike Routes/FeatureServer/0"
boundary = "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Portland_boundary/FeatureServer/0"

results = calculate_density(
input_layer=bike_routes,
bounding_polygon_layer=boundary,
classification_type="GeometricInterval",
area_units="SquareMiles",
# Outputs results as a hosted feature layer.
output_name="Calculate density results",
)

result_features = results.layers[0].query()

print(f"The density layer has {len(result_features.features)} new records")
``````

#### Service requests

##### Request
HTTPHTTPcURL
Use dark colors for code blocksCopy
``````POST arcgis.com/sharing/rest/portals/self HTTP/1.1
Content-Type: application/x-www-form-urlencoded

&f=json
&token=<ACCESS_TOKEN>``````
##### Response (JSON)
Use dark colors for code blocksCopy
``````{
"helperServices": {
// Other parameters
"analysis": {
},
"geoenrichment": {
"url": "https://geoenrich.arcgis.com/arcgis/rest/services/World/GeoenrichmentServer"
}
}
}``````

## Tutorials

Learn how to perform related analyses interactively with Map Viewer and programmatically with ArcGIS API for Python, ArcGIS REST JS, and ArcGIS REST API.

## Services

### Spatial analysis service

Process spatial datasets to discover relationships and patterns.