Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Grids and Segmentations

Authors
Affiliations
TU Wien
TU Wien
TU Wien
Binder

As you already learned in this cookbook, raw spatial data, such as irregular street segments and scattered intersection points, cannot be fed directly into most standard Machine Learning algorithms. To make this data meaningful for ML models, we need to transform it into a structured format. In this exercise we will do this by dividing the area of interest into regular cells using segmentation and computing aggregate statistics for each of those cells. This creates a standardized matrix of features, made up of our grid cells, that ML models can easily process.

So our next processing step will be to create two different grids for the area inside our bounding box:

  • A hexagonal grid with a side length of 250m

  • A square grid with a side length of 500m

What is Segmentation and why do we need it?

Segmentation refers to dividing a large geographic area into smaller, regularly shaped cells, typically squares or hexagons.

We need segmentation because raw spatial data doesn’t work well with most Machine Learning models. In order to be useful for the ML model, we need to pre-process the data in a way it can easily understand. By placing a grid over the area, we can group (aggregate) the data inside each cell. For example, we can count the number of intersections within each cell. This turns messy spatial data into a clean table where each cell is one row and the calculated values are features.

Square grids are simple and easy to work with, while hexagonal grids often give a more balanced view of neighboring areas. Therefore, the choice of grid type can influence the results which should be taken into consideration when choosing a certain grid.

Another important factor is the grid size. Using different grid sizes (like our 250m hexagons and 500m squares) helps capture patterns at different levels, and while finer grids will catch more details, coarser grids will be better for displaying general trends in the data.

Imports and Configuration

Now we load both feature tables from GitLab LFS (with a local cache): OSM intersections (intersections.csv) and GTFS stops (gtfs_stops.csv).

Loaded 8155 OSM intersections and 395 GTFS stops

Grids

In the next cell, we set up our two different grid. As already mentioned, those are:

  • A hexagonal grid with a side length of 250m

  • A square grid with a side length of 500m

Now we can plot the two grids we just created on the map to have a look at them and see how they divide the area.

Loading...
Loading...

Aggregation

Now that we have created our grids, we bring them together with both point layers from feature engineering.

We need to figure out which points fall within which grid cell. This process is called a “Spatial Join”. Once we know which points belong to which cell, we can summarize the data for that specific area.

For every cell we will calculate:

From OSM intersections

  • Density: total number of intersections inside the cell

  • Average irregularity: average “skewness” (delta) of those intersections

From GTFS stops

  • Stop density: number of stops inside the cell

  • Average route connectivity: mean route_count of stops in the cell

Since we want the same calculation for hexagonal and rectangular grids, we define small helpers and apply them to both grids.

Visualisation

We plot the aggregated values onto the hexagonal and rectangular grids. For each view we show the OSM metric and the matching GTFS metric on the same grid type, so you can compare street-network structure and transit coverage side by side.

We also keep the raw OSM intersection points available as an overlay on the choropleths.

Density on the hexagonal grid

Intersection density (OSM) and stop density (GTFS) on the 250 m hexagons.

Loading...
Loading...

Density on the rectangular grid

The same comparison on 500 m squares.

Loading...
Loading...

Connectivity / irregularity on the hexagonal grid

OSM average intersection irregularity (delta) next to GTFS average routes per stop.

Loading...
Loading...

Connectivity / irregularity on the rectangular grid

Loading...
Loading...

Summary

In this notebook you learned:

  • What Segmentation is and why it is necessary

  • Types of grids and how to create them

  • How to spatial-join and aggregate OSM and GTFS point features onto the same grids

  • How to compare those aggregates side by side on Folium choropleths