Geographic Information Systems (GIS) is a technology used to capture, store, manipulate, analyze, and present geographical data. Go is a programming language that is gaining popularity because of its simplicity, performance, and concurrency. In this tutorial, we will explore how to work with GIS data in Go programming.


Prerequisites

Before we get started, you need to have Go programming language installed on your computer. You also need to have a basic understanding of Go programming and GIS concepts.


Working with Shapefiles

Shapefiles are one of the most common formats used to store GIS data. In Go, there are several libraries that can be used to read and write shapefiles. One of the popular libraries is "github.com/jonas-p/go-shp". Here's an example of how to read a shapefile using this library:

package main

import (
	"fmt"
	"github.com/jonas-p/go-shp"
)

func main() {
	// Open a shapefile for reading
	shape, err := shp.Open("path/to/shapefile.shp")
	if err != nil {
		panic(err)
	}
	defer shape.Close()

	// Get the fields
	fields := shape.Fields()

	// Loop through each record
	for shape.Next() {
		// Get the shape
		n, p := shape.Shape()

		// Loop through each field
		for k, f := range fields {
			val := shape.ReadAttribute(n, k)
			fmt.Printf("%s: %v\n", f, val)
		}

		// Do something with the shape
		fmt.Println(p.BBox())
	}
}

In this example, we use the shp.Open function to open a shapefile for reading. We then use the shape.Next function to loop through each record in the shapefile. For each record, we get the shape and loop through each field to read its value using the shape.ReadAttribute function.


Working with GeoJSON

GeoJSON is another popular format used to store GIS data. In Go, there are several libraries that can be used to read and write GeoJSON. One of the popular libraries is "github.com/paulmach/orb". Here's an example of how to read a GeoJSON file using this library:

package main

import (
	"fmt"
	"io/ioutil"
	"github.com/paulmach/orb/geojson"
)

func main() {
	// Read the GeoJSON file
	data, err := ioutil.ReadFile("path/to/geojson.json")
	if err != nil {
		panic(err)
	}

	// Parse the GeoJSON
	fc, err := geojson.UnmarshalFeatureCollection(data)
	if err != nil {
		panic(err)
	}

	// Loop through each feature
	for _, f := range fc.Features {
		// Do something with the feature
		fmt.Println(f.Geometry.Bound())
	}
}

In this example, we use the ioutil.ReadFile function to read the GeoJSON file. We then use the geojson.UnmarshalFeatureCollection function to parse the GeoJSON into a feature collection. We then loop through each feature and do something with its geometry.


Conclusion

In this tutorial, we explored how to work with GIS data in Go programming. We covered how to read shapefiles and GeoJSON files using popular libraries. With this knowledge, you can now start building GIS applications in Go.