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.
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.
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.
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:
For further examples on different geometries, check out the GeoJSON Specification.
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).
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).
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:
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.