In the last chapter you learned about points, markers, lines and polygons. You can now draw these on a leaflet map and know when which object is the right one. You can distinguish layer groups[^] and feature groups[^] from each other and you know what to look for when displaying a leaflet map on a small display. In addition, you can now react appropriately to a mouse click or other event.

The next chapter is about the format GeoJson[^] and how to handle data well even in large quantities.

GeoJSON is an open format that makes it easy to describe geographic data. It follows a specification - namely the Simple Feature Access Specification[^]. For the description of the geodata GeoJSON uses the JavaScript Object Notation (JSON)[^].

The term Simple Feature Access Specification hides a specification of the Open Geospatial Consortium (OGC)[^]. This specification contains a generally valid description for geodata and their geometries. The fact that the specification is universally valid means that this data can be exchanged easily. The OGC is a non-profit organisation that aims to develop universally valid standards for geodata.

In this chapter we will …

First, we look at why GeoJSON was built. Then we compare the individual GeoJSON elements with the objects Leaflet provides. And last but not least, we try out the methods Leaflet provides specifically for processing GeoJSON data.

The history of GeoJSON

GeoJSON[^] extended JSON[^]. Before JSON was established as a data format, there was the extensible markup language XML[^].

Whenever something new is created, there is a reason for it!

XML was published in 1998[^] to allow data to be exchanged between machines without the need for post-processing by humans. This became more and more important in times of the internet. Why did JSON - and later GeoJSON - arise besides XML?

Why was the GeoJSON format introduced?

What was the motivation for the development of the GeoJSON format? Obviously, there were situations where XML was not optimal for data exchange. Why is this the case, and what are the advantages of JSON or GeoJSON? First of all, all three formats offer the following:

  • All three formats can be read and understood by a human.
  • All three formats are hierarchically structured. This means that values can be represented within other values.
  • All three formats are relatively easy to learn.
  • All three formats can be analyzed and used by many programming languages.
  • All three formats are interchangeable over the Hypertext Transfer Protocol (HTTP), i.e., via the Internet.

Let's take a closer look at each format in this chapter to see what advantages the JSON format has over XML and what possibilities the GeoJSON format offers when working with geodata[^].


XML describes the structure of data. With the help of tags a meaning is given to the data - a semantics[^]. Because of XML's tag system, small data sets often become blown up and thus confusing. In addition, the addressing of individual tags in an XML file is sometimes quite complicated.


JSON is basically nothing more than the specification of a syntax[^]. No meaning is given to the data, rather it is a pure syntactic structure. Because JSON structures data, objects can be easily defined from that data. JSON is under constant development. In December 1999, the first JSON format specification was adopted. Currently, there are two different but similar standards in terms of content, [RFC 8259[^] and ECMA-404[^].

The main advantage of JSON over XML is its ease of use. Since JSON represents valid JavaScript by itself, it can be called directly and thus converted into a JavaScript object. The individual properties of these objects are accessed via the attributes. In the next chapter, we will include a file containing GeoJSON objects. Just by including it, it is possible to access the GeoJSON objects inside. In comparison, an XML file must first be parsed with an XML parser! Another advantage of JSON is that no end tag is required. Mainly for this reason JSON is more compact and is read and executed faster.

Data from a GeoJSON file located in a GitHub repository is automatically displayed on an interactive map when the file is clicked in the repository. Github[^] creates these maps using Leaflet. For example, check out the world.geo.json[^] repository to see for yourself.

A small example illustrates that XML requires more characters than JSON to describe the same object. An object encoded with 95 characters in XML requires 73 characters in JSON. For one object, this difference is marginal. Usually, a large number of objects are represented digitally. For a large number of objects, this difference can be very important. In the following, you will first see the XML snippet which consists of 95 characters.


The same object can be described with 73 characters in JSON format.

joomlers“: {

And why after JSON even GeoJSON?

Geodata could be described and processed in JSON. What is the advantage of the special Geo-JSON format? GeoJSON is JSON - but optimized for geodata. GeoJSON gives the geodata semantics once again - i.e. a meaning. Now you are probably wondering what this back and forth is all about. Does it make sense? Yes, it makes sense: GeoJSON only picks out the positive features of the JSON and XML formats.

GeoJSON describes points, lines, and polygons and does well with these shapes in a coordinate system. In the previous chapter we saw that working with geodata is basically nothing different. GeoJSON has become a very popular data format for many geospatial information systems. In this book, I mention GeoJSON specifically because Leaflet is also very good at handling data in GeoJSON format. Here, we'll start by taking a closer look at GeoJSON objects. If you'd prefer to get practical right away, it's best to keep scrolling. In the next part, you will learn how to display and edit GeoJSON elements on your map.

To put it in time chronology: GeoJSON is still quite young:

The formal specification of the GeoJSON format can be found at[^] online.

Explore GeoJSON

You now know that you can use GeoJSON to encode many geographic data structures in a machine-readable language. A GeoJSON object can represent a simple geometry, for example a point, a line or a polygon. In addition, you can give a geometry a meaning. You can assign properties to each element. For example, you can give a line the property 'type=street' or 'name=church street'. In this case, you create a GeoJSON object of type Feature[^]. If you want to group multiple feature objects together, you can group them into a collection of features. For this, there is a GeoJSON type called FeatureCollection[^]. Understanding these concepts has many advantages. It also helps you understand how to work with geospatial data in general. This is because the basic concepts applied in GeoJSON have been a part of geoinformation systems[^] for many years.

Let's start from the beginning:

A geometry

A geometry[^] is a shape. All shapes in GeoJSON are described using one or more coordinates. A coordinate is called position in GeoJSON.

GeoJSON does support the geometry types

  1. Point,
  2. LineString,
  3. Polygon,
  4. MultiPoint,
  5. MultiLineString, und
  6. MultiPolygon

– and each of these geometry types contains positions.


The most important element when working with geodata is the definition of the point on the Earth. The point on the earth is the one to which the geodata is assigned. We know this data also under the name coordinate. In the chapter about the coordinate system I have already written quite a lot about coordinates on the earth. Here again briefly: A coordinate is a number combination. Each number of a coordinate is for a dimension. We'll limit ourselves to two dimensions in this book, namely the geographic longitude and the geographic latitude. GeoJSON supports three dimensions - in addition to longitude and latitude, you can also specify the Elevation on Earth.

With the global navigation satellite system (GPS)[^] a fourth dimension is relevant. Besides longitude, latitude and altitude, the time matters.

The coordinates are formatted in GeoJSON in decimal format. The first number is the geographical longitude and the second number is the geographical latitude. Concretely, a position in GeoJSON looks like this:

;[longitude, latitude, altitude]


;[50.254, 7.5847, 324.1]