Spatial Data: The Structure of Geospatial Information
Before we can map our data, we need to understand that geographic information is more than just a table of numbers: it’s a representation of our three-dimensional world projected onto a flat screen. Understanding the principles of how we digitalize physical space is the key to avoiding distorted shapes and misaligned points.
In this lesson, we’ll explore the fundamentals of spatial data structuresusing the sf package. We’ll look at how different geometric shapes are organized, decipher the metadata that gives coordinates their identity, and examine the trade-offs involved in flattening our planet for the digital canvas.
🗺️ Where We Live, But in 2D
In standard data visualization, we map variables to x and y positions on a grid. In spatial visualization, we do the same, but with a crucial difference: our "data points" are not always points, but may represent complex shapes that exist on a curved surface.
To represent our three-dimensional world on a two-dimensional canvas, we most commonly use a vector model . While some spatial data is stored as a grid of pixels (known as raster data), we focus on the vector approach. This model organizes coordinates into specific geometric structures that allow us to map physical boundaries and locations onto a digital 2D grid.
🧱 The Building Blocks
Within the vector model, most spatial datasets you’ll encounter are built from one of three fundamental geometric structures. These are the "Building Blocks" of any digital vector map:
No matter which of these, every shape can essentially be presented by coordinates. Even the most complex border is just a list of numbers organized by these rules:
- Point:
(X, Y)⇒ a single coordinate - Line:
((X1, Y1), (X2, Y2), ...)⇒ a sequence of points - Polygon:
((X1, Y1), ..., (X1, Y1))⇒ a sequence forming a ring
Things often get more complex when we deal with Multipart Geometries as a geographic entity rarely fits into a single simple shape.
🧩 Complex Shapes, Simple Features
A country like Greece or the United States isn't one single polygon. It is a collection of mainlands and various islands that all belong to the same data entry.
In a digital map, we treat these as a single "feature". This ensures that when you look for "Greece" in a dataset, you aren't just getting one island, but the entire country as a unified piece of information.
This is the core of modern spatial data: instead of managing separate lists for your data and your shapes, the standard workflow treats geographic boundaries just like any other variable.
It’s a standard rectangular, two-dimensional table where every row is a geographic feature (like a single location, a river, or a city), and one special column holds the precise instructions on how to draw it.
This approach actually allows you to mix types in a single table. You could have a point (location), a line (river), and a polygon (city) all living in the same dataset, though in practice, we often keep them in separate datasets for clarity to maintain thematic focus and make styling each layer easier.
This "Table with one special column" model isn't just a clever idea: it is a formal international standard called Simple Features .
This standard defines how digital geometries should be stored and described so that different software can "talk" to each other. Whether you are using a GeoJSON file in a web browser, a PostGIS database, or a Shapefile in a GIS, the data follows the same geometric hierarchy.
You might hear spatial data referred to by many names depending on where it is stored, and it can be a bit overwhelming at first. So let's introduce the main file formats briefly:
- GeoJSON: A text-based format popular for web maps and APIs (it looks a lot like a nested R list).
- GeoPackage (.gpkg): The modern, open-source successor to the old "Shapefile" (.shp), much faster and easier to share as a single file.
- Shapefile (.shp): The "grandfather" of spatial formats. While it is still everywhere, it’s actually a collection of at least three different files (
.shp,.shx,.dbf) that must stay together to work. - PostGIS: A powerful spatial extension for SQL databases used by professional cartographers and analysts.
- KML/KMZ: The format used by Google Earth. While great for visualizing points in a browser, it can be a bit "messy" to clean and process for statistical analysis.
And to decode a bit more of the jargon: if someone mentions a "Web Feature Service" (abbreviated as "WFS" or sometimes "WMS"), that person is not actually talking about static files, but a way to provide information. Think of them as a "live stream" of spatial data that you connect to via a URL rather than downloading a file.
The good news? They all "look and feel" the same once you bring them into R.
Ready to enroll? 🚀
Enroll now to unlock instant, lifetime access to every lesson, and become the ggplot2 expert your company needs!
Already enrolled? Login