How to turn vector elevation lines into a grid — and build it from Lego

Milan Janosov

Towards Data Science

Vector and raster data are the two main types of spatial data structures. Vector data is great for storing exact locations and shapes, such as points, lines, and polygons. In contrast, raster data models spatial features using a grid of pixels, each storing particular values. Different data sources and applications yield different data structures; however, when conducting advanced spatial analytics, we often need to make these two different types meet. In this article, I will give an example of that — how to turn vector data, in this case, elevation lines, into a raster of grid cells. Additionally, I show how this can be visualized by matching each raster grid cell to a small Lego brick.

All images created by the author.

As a data source, I used the open data provided by the Budapest Open Data Atlas containing the elevation layers of the city. After downloading the spatial data file, let’s have a look at it using GeoPandas:

import geopandas as gpd

gdf = gpd.read_file(‘bpelev.json’)

print(len(gdf))
gdf.head(5)

The output of this cell: