Geospatial Data Representation: The GeoJSON Format

Martín González

2022-03-07 18:16:12
Reading Time: 3 minutes

Everyday, huge amounts of data are transmitted between thousands of services. To support this exchange, many standards have been invented that provide guidance on how to handle and transmit these huge volumes of data. One of these data-interchange formats, easily readable for humans and parsable for machines, is the very well-known JSON (JavaScript Object Notation). The JSON format, briefly summed up, consists of key-value pairs that can contain any kind of information as long as it can be typed with a keyboard, from text to numbers and sets of data. These values can also be more key-value pairs and so on.

Geospatial data is a kind of special data related to a physical part of a map. In order to treat geospatial data, a wide range of formats can be used (Shapefile, KML/KMZ, GML or WKT among many others). GeoJSON, as it name implies, is JSON data with a specific structure specifically designed to treat geospatial information. In this article you will learn the basics of this standard and how to use it.

The GeoJSON format

At its core, a GeoJSON is simply a JSON, as it consists of a list of key-value pairs. However, it must follow certain structure. The snippets below provide an example (Note that the file extension is different, this is because GitHub automatically renders geojson files to show its data on a map):

Which rendered, provides the following output:

Let’s break down the different key attributes in the example above.

Type

This property indicates whether the data consists of a single element (“Feature”) or a set of elements (“FeatureCollection”). We’ll go further into this later.

Geometry

The geometry attribute indicates what type of geometry is being represented by the GeoJSON. It is made up of two properties: “type”, which indicates the type of geometry being defined, and “coordinates”, which consist of the numerical description of the points that define the geometry. Since the example above consists of a single point, there are just two elements: latitude and longitude. GeoJSON supports the following geometries:

  • Point: Consists of single point in space.
  • LineString: Represents a line in a map.
  • Polygon: Displays a polygonal shape.
  • MultiPoint: Set of points.
  • MultiLineString: Set of linestrings.
  • MultiPolygon: Set of polygons.
  • GeometryCollection: Set of mixed geometries (points, linestrings & polygons).

For further examples on different geometries, check out the GeoJSON Specification.

Properties

The properties attribute is made up of metadata about the geospatial data being represented. It can be anything as long as the data is representable with textual characters (from a simple name to the population being contained in a polygon or even the longitude of a linestring in the real world).

What if I want to represent spatial data with different properties?

Don’t worry, the GeoJSON standard has you covered. Instead of defining a geometry of type GeometryCollection, with sets of different spatial data, the standard provides the main type called FeatureCollection. This, unlike Feature, enables you to define any spatial data with different properties. This is achieved through two properties: type, which has already been explained, and features, which consists of an array of single GeoJSONs of type Feature. (Yes, this means that you can’t have a FeatureCollection inside of another one).

Viewing and Saving GeoJSON

GeoJSON has many other features that haven’t been mentioned in this article. However, you can try and play with it yourself in websites like https://geojson.io/, which provides a built-in map that reflects the data provided. Also, as shown above. Here’s an example with the GeoJSON shown above:

The previous FeatureCollection shown in geojson.io. Source: https://geojson.io

As for saving the data, at the end of the day, a GeoJSON consists of a plain text file with the format described. When saving it, the name of the file must end with .geojson. Also, since a geoJSON file is really just a JSON, it could be saved with a .json extension too.