# Geospatial Data Operations

Studio's numerous geospatial operations are designed to minimize time spent outside of the application.

This page contains a number of guides for conducting geospatial data operations in Foursquare Studio.

| Geospatial Data Operations | Description |
| --- | --- |
| [Filter by Geometry](https://docs.foursquare.com/analytics-products/docs/use-cases-geospatial-data-operations#filter-by-geometry) | Filter a map to display only content that is located within the bounds of the selected geometries. |
| [Geometry Centroid](https://docs.foursquare.com/analytics-products/docs/use-cases-geospatial-data-operations#geometry-centroid) | Find the centroid of geometry objects. |
| [Spatial Buffer](https://docs.foursquare.com/analytics-products/docs/use-cases-geospatial-data-operations#spatial-buffer) | Create a geometry of a specified distance around a shape or a point. |
| [Spatial Join](https://docs.foursquare.com/analytics-products/docs/use-cases-geospatial-data-operations#spatial-join) | Join or combine attributes from one dataset to another based on their spatial relationship. |
| [Spatial Merge/Dissolve](https://docs.foursquare.com/analytics-products/docs/use-cases-geospatial-data-operations#spatial-merge) | Merge several geometries into a single one, dissolving borders between shapes. |

## Filter by Geometry

You can create filters that limit the map's scope, only showing the selected geometries.

### Select a Boundary

To select a geometry on the map, click within its boundary, then click **Select Geometry**. Repeat this process for any additional geometries you wish to include in your selection.

### Draw a Boundary

If you wish to specify an area to filter by, use the **Draw** tool to create a boundary. Drawing options include a rectangle or a custom polygon. [Learn more about drawing boundaries.](https://docs.foursquare.com/analytics-products/docs/filters-geospatial#drawing-boundaries)

> Note: If you want to deselect a geometry, right-click the border of a selected geometry, then click **Delete**.

### Enable Filter

To enable a filter, simply right-click your selection, move the mouse over **Filter Layers**, then select the layer you wish to filter on. All areas outside of the region will no longer be visible.

Geospatial Filter from Foursquare on Vimeo

### Extract Filtered Data

One of the primary reasons to create geospatial filters is the extraction of data for a new dataset. This is often necessary to explore broad, national-scale datasets on a local level, optimizing color breaks for the area of study.

To do so, simply right-click the border of your selected geometry, then select **Extract Data**.

In the following menu, choose any columns you wish to include or exclude in the extracted dataset. Then, in the **Extract Dataset** window, select **Filtered** data extent, then optionally rename the dataset.

### Example

Extract Region from Foursquare on Vimeo

In the above example, we have two datasets: `Districts`, a GeoJSON file containing `MultiPolygon` objects representing congressional districts, and `Census 2020: Age`, a publically-available census dataset available from our Data Catalog. This dataset has been enriched with Hex Tiles, scaling data-rich `h3` cells as we zoom in and out.

When we run the extraction, a new layer appears visualizing the extracted dataset. Studio immediately adjusts the color scale to fit the area of focus. This map now shows age throughout the selected congressional district.

## Geometry Centroid

Using the [`centroid()` expression](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#centroid), you can generate points from the representative center of each input GeoJSON feature.

### Example

In the below example, we will find the centroid of each Chicago neighborhood in our dataset.

First, navigate to the **Columns** tab in the left-hand sidebar, then click **Add Column**.

From the **Add Column** panel, simply call the `centroid()` function, passing your GeoJSON feature as an argument.

### Visualize Centroids

To visualize centroids, navigate to the **Layers** tab. Then, select the layer of your choice.

You can use the [**Point Layer**](https://docs.foursquare.com/analytics-products/docs/layer-point) to show the centroid as a point. This is the most obvious choice for many use cases; a point's radius, color, and stroke can scaled by any value in your dataset.

## Spatial Buffer

You can use the [`buffer()` expression](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#buffer) to create buffer polygons surrounding each set of latitude/longitude coordinates.

You can also create buffers around GeoJSON features using [`bufferFeature()`](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#bufferfeature), or surrounding H3 cells using [`bufferH3()`](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#bufferh3).

### Example

In the previous section, we created a `GeoJSON` point feature representing the centroid of each Chicago neighborhood. We have also included a new `Parks - Chicago Park District` dataset that shows each park in the Chicago city limits.

If we are interested in how many parks exist within 0.5 miles of a neighborhood's center point, we can start by creating a buffer surrounding the centroid.

First, navigate to the **Columns** tab in the left-hand sidebar, then click **Add Column**.

From the **Add Column** panel, simply call the [`bufferFeature()`](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#bufferfeature) function, passing your GeoJSON point as an argument. Alternatively, if your point is represented by lat/lng columns, use the [`buffer()` expression](https://docs.foursquare.com/analytics-products/docs/expressions-geometric#buffer).

Then, create the buffer column. Parameters available include:

| Parameter | Type | Description |
| --- | --- | --- |
| `feature` | GeoJSON `feature` | Required. The name of the GeoJSON feature column to surround with buffer polygons. |
| `bufferDistance` | `number` | Required. The length of the buffer from the edge of each feature. |
| `distanceUnit` | `string` | The distance unit to use for the buffer, either `Meter`/`Meters`, or `Mile`/`Miles` (not case-sensitive). Default: `Mile` |
| `pointsPerArc` | `number` | The number of points to create the rounded corner of the buffered shape. A higher number of points generates a smoother buffer corner. Default: `10` |

For our example, we will use the expression `bufferFeature(_centroid , .5, "Miles", 10)` to create our new `_buffer` column.

To view the buffers on the map, navigate to the **Layers** tab, click **Add New Layer**, then create a new **Polygon** layer. Use your new buffer layer as the source to generate the polygons.

Creating this layer allows viewers to visually interpret which neighborhoods have parks that fall within half a mile of their geometric center.

## Spatial Join

Spatial join combines attributes from one dataset to another based on their spatial relationship. To perform spatial join, specify geometry columns in both datasets and select the type of spatial join you want to perform. Once the operation is complete, you will receive a new joined dataset.

On a dataset with a geometry column, click **⋮ More Options >> Spatial Join** to open the spatial join configuration panel.

### Example

In this example, we will run a spatial join operation on `Parks - Chicago Park District Park Boundaries` (selecting the `_geojson` column) and `Boundaries - Neighborhoods` (selecting the `_buffer` column).

Select any columns to include in the spatial join along with an aggregation option, then click **Run** to generate spatial join results. If something does not look right, change any parameters then click **Rerun**.

In this case, we will include the `acres` column, aggregating by `sum`. Since we are interested in the amount of parkland within 0.5 miles of the neighborhoods, we will use the `Overlap` spatial join operation.

Once the results look good, click **Confirm**.

### Visualizing Spatial Joins

Upon completing the spatial join, a new `Polygon` layer may appear showing any GeoJSON features included in the join.

Using the neighborhood polygon boundaries, we can use the aggregated `sum - acres` column to visualize how much parkland is available within a 0.5-mile radius of a neighborhood's centroid.

## Spatial Merge (Spatial Dissolve)

Use spatial merge with `geojson` or `h3` data to dissolve borders or otherwise combine geometries based on a common field. When merging `geojson` data, all polygons, LineStrings, Points, and other features will be merged. When merging `h3` data, all `h3` indexes that share the field will be merged.

Follow these steps to complete a spatial merge:

1. Start a new group by operation, then select a field shared by the geometries you wish to merge.
2. Choose any other columns you wish to include in the group-by operation.
3. Select the `Merge` aggregation option for your `polygon` or `h3` column.

Upon completing the group by operation, a new dataset will appear with your merged spatial objects.

### Example

Spatial Merge from Foursquare on Vimeo

In the above example, a dataset containing election data is loaded into Studio. This election data has the column `_geojson`, which contains GeoJSON `MultiPolygon` objects representing congressional districts, as well as a column `PARTY_LEADING`, indicating which party controls the congressional district.

To view the number of congressional districts controlled by an individual party, we run a group by operation on the `STATENAME` field. Then, we choose the `Merge` aggregation option on our `_geojson` column and `Mode` on our `PARTY_LEADING` column.

Upon completing the merge, a new layer is automatically generated with the new `Merge _geojson` column. We can quickly configure this layer to display the data calculated by the group by operation. In this case, we alter the color scale to have just two steps, then select the new `Mode PARTY_LEADING` column to fill the polygons. This map now shows the party that controls the most congressional districts in each US state.
