Categorygithub.com/jonas-p/go-shp
modulepackage
0.1.1
Repository: https://github.com/jonas-p/go-shp.git
Documentation: pkg.go.dev

# README

go-shp

Build Status Build status Go Report Card Codevov

Go library for reading and writing ESRI Shapefiles. This is a pure Golang implementation based on the ESRI Shapefile technical description.

Usage

Installation

go get github.com/jonas-p/go-shp

Importing

import "github.com/jonas-p/go-shp"

Examples

Reading a shapefile

// open a shapefile for reading
shape, err := shp.Open("points.shp")
if err != nil { log.Fatal(err) } 
defer shape.Close()
	
// fields from the attribute table (DBF)
fields := shape.Fields()
	
// loop through all features in the shapefile
for shape.Next() {
	n, p := shape.Shape()
	
	// print feature
	fmt.Println(reflect.TypeOf(p).Elem(), p.BBox())
	
	// print attributes
	for k, f := range fields {
		val := shape.ReadAttribute(n, k)
		fmt.Printf("\t%v: %v\n", f, val)
	}
	fmt.Println()
}

Creating a shapefile

// points to write
points := []shp.Point{
	shp.Point{10.0, 10.0},
	shp.Point{10.0, 15.0},
	shp.Point{15.0, 15.0},
	shp.Point{15.0, 10.0},
}
	
// fields to write
fields := []shp.Field{
	// String attribute field with length 25
	shp.StringField("NAME", 25),
}
	
// create and open a shapefile for writing points
shape, err := shp.Create("points.shp", shp.POINT)
if err != nil { log.Fatal(err) }
defer shape.Close()
	
// setup fields for attributes
shape.SetFields(fields)
	
// write points and attributes
for n, point := range points {
	shape.Write(&point)
	
	// write attribute for object n for field 0 (NAME)
	shape.WriteAttribute(n, 0, "Point " + strconv.Itoa(n + 1))
}

Resources

# Functions

Append returns a Writer pointer that will append to the given shapefile and the first error that was encounted during creation of that Writer.
AttributeCount returns the number of fields of the database.
Attributes returns all attributes of the shape that sr was last advanced to.
BBoxFromPoints returns the bounding box calculated from points.
Create returns a point to new Writer and the first error that was encountered.
DateField feturns a Field that can be used in SetFields to initialize the DBF file.
FloatField returns a Field that can be used in SetFields to initialize the DBF file.
NewPolyLine returns a pointer a new PolyLine created with the provided points.
NumberField returns a Field that can be used in SetFields to initialize the DBF file.
Open opens a Shapefile for reading.
OpenShapeFromZip opens a shape file that is contained in a ZIP archive.
OpenZip opens a ZIP file that contains a single shapefile.
SequentialReaderFromExt returns a new SequentialReader that interprets shp as a source of shapes whose attributes can be retrieved from dbf.
ShapesInZip returns a string-slice with the names (i.e.
StringField returns a Field that can be used in SetFields to initialize the DBF file.

# Constants

These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.
These are the possible shape types.

# Structs

Box structure made up from four coordinates.
Field representation of a field object in the DBF file.
MultiPatch consists of a number of surfaces patches.
MultiPoint is the shape that consists of multiple points.
MultiPointM is the collection of multiple points with measures.
MultiPointZ consists of one ore more PointZ.
Null is an empty shape.
Point is the shape that consists of single a geometry point.
PointM is a point with a measure.
PointZ is a triplet of double precision coordinates plus a measure.
PolyLine is a shape type that consists of an ordered set of vertices that consists of one or more parts.
PolyLineM is the polyline in which each point also has a measure.
PolyLineZ is a shape which consists of one or more parts.
Reader provides a interface for reading Shapefiles.
Writer is the type that is used to write a new shapefile.
ZipReader provides an interface for reading Shapefiles that are compressed in a ZIP archive.

# Interfaces

SequentialReader is the interface that allows reading shapes and attributes one after another.
Shape interface.

# Type aliases

Polygon is identical to the PolyLine struct.
PolygonM structure is identical to the PolyLineZ structure.
PolygonZ structure is identical to the PolyLineZ structure.
ShapeType is a identifier for the the type of shapes.