GeoJSON and Go

Exploring GeoJSON with Go and its lack of generics

fires

GeoJSON is an open format for geographical data and is used in a wide range of software products and libraries. If we want to build a data source for mapping, having our API provide GeoJSON is beneficial as, most of the time, it can be directly used in any client-side product that is accessing data.

Golang is a strictly typed language, which means that expressing the data model of GeoJSON can be challenging, as the data structure will change depending on what geometry type you want to represent. However, there is a way to deal with this well, which will be demonstrated here.

GeoJSON describes geometries as Points, Lines or Polygons. There can also be collections of these, called MultiPoint, MultiLine, and MultiPolygon.

A Point is a pair of coordinates, or [2]float64

A Line, or a MultiPoint, is a list of points, or [][2]float64

A MultiLine is a list of Lines, or [][][2]float64

A polygon is also a list of lines, but with the additional requirement that each line must end at its beginning, so it is technically a ring, rather than a line. The first ring in the list is the outer part of the polygon, with the following rings being cut-outs of that original shape. Polygon can be expressed in Go as [][][2]float64

A MultiPolygon is simply a list of Polygons, or [][][][2]float64

Here is how you would describe these types in Go:

go
1
type GeoJSONPoint struct {
2
Type string `json:"type"`
3
Coordinates [2]float64 `json:"coordinates"`
4
}
5
6
// line or multipoint
7
type GeoJSONLine struct {
8
Type string `json:"type"`
9
Coordinates [][2]float64 `json:"coordinates"`
10
}
11
12
// polygon or multiline
13
type GeoJSONPolygon struct {
14
Type string `json:"type"`
15
Coordinates [][][2]float64 `json:"coordinates"`
16
}
17
18
type GeoJSONMultiPolygon struct {
19
Type string `json:"type"`
20
Coordinates [][][][2]float64 `json:"coordinates"`
21
}
22

The Type variable should always be set to the geometry type: "Point", "Line", "Polygon", "MultiPoint", "MultiLine", or "MultiPolygon"

GeoJSON can also define a "Geometry Collection" which is a list of any one of the above geometry types. So you could have a list with several Points, and a MultiPolygon, or any other combination.

This creates a problem in Go, as we don't have generics. We cannot just define a struct with a list of generic "geometries". This doesn't mean we can't work with GeoJSON. We just have to get creative.

The one thing structs all have in common is that they can be serialised as JSON. This means we can use json.RawMessage to create a generic list:

go
1
type GeoJSONGeometryCollection struct {
2
Type string `json:"type"` // will always be "GeometryCollection"
3
Geometries []json.RawMessage `json:"geometries"`
4
}
5

So how do we unmarshal this? We'll need to add this type first:

go
1
type GeoJSONGenericGeometry struct {
2
Type string `json:"type"`
3
Coordinates json.RawMessage `json:"coordinates"`
4
}
5

Now we have list of geometries as JSON, but we don't know what type they are until we unmarshal each one of them. So we need a generic geometry type to unmarshal those into first, so we can get the Type variable. Once we have that type variable, we can unmarshal it again into the actual type.

go
1
// Assume the variable geoJSON is a []byte with valid geojson
2
geoJSONGeometryCollection := GeoJSONGeometryCollection{}
3
json.Unmarshal(geoJSON, &geoJSONGeometryCollection)
4
5
for _, geometryJSON := range geoJSONGeometryCollection.Geometries {
6
generic := GeoJSONGenericGeometry{}
7
json.Unmarshal(geometryJSON, &generic)
8
switch generic.Type {
9
10
case "MultiPolygon":
11
multiPolygon := GeoJSONMultiPolygon{}
12
json.Unmarshal(geometryJSON, &multiPolygon)
13
// You can now use the multiPolygon
14
15
case "Polygon", "MultiLine":
16
polygon := GeoJSONPolygon{}
17
json.Unmarshal(geometryJSON, &polygon)
18
// You can now use the polygon
19
20
case "Line", "MultiPoint":
21
line := GeoJSONLine{}
22
json.Unmarshal(geometryJSON, &line)
23
// You can now use the line
24
25
case "Point":
26
point := GeoJSONPoint{}
27
json.Unmarshal(geometryJSON, &point)
28
// You can now use the point
29
30
default:
31
return nil, fmt.Errorf("invalid datatype")
32
}
33
}
34

Once you have the data in the correct structs, you are free to convert your GeoJSON into any other format you need, or do your own logic and predictions on the data.