shp

package module
Version: v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2017 License: MIT Imports: 12 Imported by: 0

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

Documentation

Index

Constants

View Source
const (
	NULL        ShapeType = 0
	POINT                 = 1
	POLYLINE              = 3
	POLYGON               = 5
	MULTIPOINT            = 8
	POINTZ                = 11
	POLYLINEZ             = 13
	POLYGONZ              = 15
	MULTIPOINTZ           = 18
	POINTM                = 21
	POLYLINEM             = 23
	POLYGONM              = 25
	MULTIPOINTM           = 28
	MULTIPATCH            = 31
)

These are the possible shape types.

Variables

This section is empty.

Functions

func AttributeCount

func AttributeCount(sr SequentialReader) int

AttributeCount returns the number of fields of the database.

func Attributes

func Attributes(sr SequentialReader) []string

Attributes returns all attributes of the shape that sr was last advanced to.

func ShapesInZip

func ShapesInZip(zipFilePath string) ([]string, error)

ShapesInZip returns a string-slice with the names (i.e. relatives paths in archive file tree) of all shapes that are in the ZIP archive at zipFilePath.

Types

type Box

type Box struct {
	MinX, MinY, MaxX, MaxY float64
}

Box structure made up from four coordinates. This type is used to represent bounding boxes

func BBoxFromPoints

func BBoxFromPoints(points []Point) (box Box)

BBoxFromPoints returns the bounding box calculated from points.

func (*Box) Extend

func (b *Box) Extend(box Box)

Extend extends the box with coordinates from the provided box. This method calls Box.ExtendWithPoint twice with {MinX, MinY} and {MaxX, MaxY}

func (*Box) ExtendWithPoint

func (b *Box) ExtendWithPoint(p Point)

ExtendWithPoint extends box with coordinates from point if they are outside the range of the current box.

type Field

type Field struct {
	Name      [11]byte
	Fieldtype byte
	Addr      [4]byte // not used
	Size      uint8
	Precision uint8
	Padding   [14]byte
}

Field representation of a field object in the DBF file

func DateField

func DateField(name string) Field

DateField feturns a Field that can be used in SetFields to initialize the DBF file. Used to store Date strings formatted as YYYYMMDD. Data wise this is the same as a StringField with length 8.

func FloatField

func FloatField(name string, length uint8, precision uint8) Field

FloatField returns a Field that can be used in SetFields to initialize the DBF file. Used to store floating points with precision in the DBF.

func NumberField

func NumberField(name string, length uint8) Field

NumberField returns a Field that can be used in SetFields to initialize the DBF file.

func StringField

func StringField(name string, length uint8) Field

StringField returns a Field that can be used in SetFields to initialize the DBF file.

func (Field) String

func (f Field) String() string

Returns a string representation of the Field. Currently this only returns field name.

type MultiPatch

type MultiPatch struct {
	Box       Box
	NumParts  int32
	NumPoints int32
	Parts     []int32
	PartTypes []int32
	Points    []Point
	ZRange    [2]float64
	ZArray    []float64
	MRange    [2]float64
	MArray    []float64
}

MultiPatch consists of a number of surfaces patches. Each surface path descries a surface. The surface patches of a MultiPatch are referred to as its parts, and the type of part controls how the order of vertices of an MultiPatch part is interpreted.

func (MultiPatch) BBox

func (p MultiPatch) BBox() Box

BBox returns the bounding box of the MultiPatch feature

type MultiPoint

type MultiPoint struct {
	Box       Box
	NumPoints int32
	Points    []Point
}

MultiPoint is the shape that consists of multiple points.

func (MultiPoint) BBox

func (p MultiPoint) BBox() Box

BBox returns the bounding box of the MultiPoint feature

type MultiPointM

type MultiPointM struct {
	Box       Box
	NumPoints int32
	Points    []Point
	MRange    [2]float64
	MArray    []float64
}

MultiPointM is the collection of multiple points with measures.

func (MultiPointM) BBox

func (p MultiPointM) BBox() Box

BBox eturns the bounding box of the MultiPointM feature

type MultiPointZ

type MultiPointZ struct {
	Box       Box
	NumPoints int32
	Points    []Point
	ZRange    [2]float64
	ZArray    []float64
	MRange    [2]float64
	MArray    []float64
}

MultiPointZ consists of one ore more PointZ.

func (MultiPointZ) BBox

func (p MultiPointZ) BBox() Box

BBox eturns the bounding box of the MultiPointZ feature.

type Null

type Null struct {
}

Null is an empty shape.

func (Null) BBox

func (n Null) BBox() Box

BBox Returns an empty BBox at the geometry origin.

type Point

type Point struct {
	X, Y float64
}

Point is the shape that consists of single a geometry point.

func (Point) BBox

func (p Point) BBox() Box

BBox returns the bounding box of the Point feature, i.e. an empty area at the point location itself.

type PointM

type PointM struct {
	X float64
	Y float64
	M float64
}

PointM is a point with a measure.

func (PointM) BBox

func (p PointM) BBox() Box

BBox returns the bounding box of the PointM feature which is a zero-sized area at the X- and Y-coordinates of the point.

type PointZ

type PointZ struct {
	X float64
	Y float64
	Z float64
	M float64
}

PointZ is a triplet of double precision coordinates plus a measure.

func (PointZ) BBox

func (p PointZ) BBox() Box

BBox eturns the bounding box of the PointZ feature which is an zero-sized area at the X and Y coordinates of the feature.

type PolyLine

type PolyLine struct {
	Box
	NumParts  int32
	NumPoints int32
	Parts     []int32
	Points    []Point
}

PolyLine is a shape type that consists of an ordered set of vertices that consists of one or more parts. A part is a connected sequence of two ore more points. Parts may or may not be connected to another and may or may not intersect each other.

func NewPolyLine

func NewPolyLine(parts [][]Point) *PolyLine

NewPolyLine returns a pointer a new PolyLine created with the provided points. The inner slice should be the points that the parent part consists of.

func (PolyLine) BBox

func (p PolyLine) BBox() Box

BBox returns the bounding box of the PolyLine feature

type PolyLineM

type PolyLineM struct {
	Box       Box
	NumParts  int32
	NumPoints int32
	Parts     []int32
	Points    []Point
	MRange    [2]float64
	MArray    []float64
}

PolyLineM is the polyline in which each point also has a measure.

func (PolyLineM) BBox

func (p PolyLineM) BBox() Box

BBox returns the bounding box of the PolyLineM feature.

type PolyLineZ

type PolyLineZ struct {
	Box       Box
	NumParts  int32
	NumPoints int32
	Parts     []int32
	Points    []Point
	ZRange    [2]float64
	ZArray    []float64
	MRange    [2]float64
	MArray    []float64
}

PolyLineZ is a shape which consists of one or more parts. A part is a connected sequence of two or more points. Parts may or may not be connected and may or may not intersect one another.

func (PolyLineZ) BBox

func (p PolyLineZ) BBox() Box

BBox eturns the bounding box of the PolyLineZ feature.

type Polygon

type Polygon PolyLine

Polygon is identical to the PolyLine struct. However the parts must form rings that may not intersect.

func (Polygon) BBox

func (p Polygon) BBox() Box

BBox returns the bounding box of the Polygon feature

type PolygonM

type PolygonM PolyLineZ

PolygonM structure is identical to the PolyLineZ structure.

func (PolygonM) BBox

func (p PolygonM) BBox() Box

BBox returns the bounding box of the PolygonM feature.

type PolygonZ

type PolygonZ PolyLineZ

PolygonZ structure is identical to the PolyLineZ structure.

func (PolygonZ) BBox

func (p PolygonZ) BBox() Box

BBox returns the bounding box of the PolygonZ feature

type Reader

type Reader struct {
	GeometryType ShapeType
	// contains filtered or unexported fields
}

Reader provides a interface for reading Shapefiles. Calls to the Next method will iterate through the objects in the Shapefile. After a call to Next the object will be available through the Shape method.

func Open

func Open(filename string) (*Reader, error)

Open opens a Shapefile for reading.

func (*Reader) Attribute

func (r *Reader) Attribute(n int) string

Attribute returns value of the n-th attribute of the most recent feature that was read by a call to Next.

func (*Reader) AttributeCount

func (r *Reader) AttributeCount() int

AttributeCount returns number of records in the DBF table.

func (*Reader) BBox

func (r *Reader) BBox() Box

BBox returns the bounding box of the shapefile.

func (*Reader) Close

func (r *Reader) Close() error

Close closes the Shapefile.

func (*Reader) Err

func (r *Reader) Err() error

Err returns the last non-EOF error encountered.

func (*Reader) Fields

func (r *Reader) Fields() []Field

Fields returns a slice of Fields that are present in the DBF table.

func (*Reader) Next

func (r *Reader) Next() bool

Next reads in the next Shape in the Shapefile, which will then be available through the Shape method. It returns false when the reader has reached the end of the file or encounters an error.

func (*Reader) ReadAttribute

func (r *Reader) ReadAttribute(row int, field int) string

ReadAttribute returns the attribute value at row for field in the DBF table as a string. Both values starts at 0.

func (*Reader) Shape

func (r *Reader) Shape() (int, Shape)

Shape returns the most recent feature that was read by a call to Next. It returns two values, the int is the object index starting from zero in the shapefile which can be used as row in ReadAttribute, and the Shape is the object.

type SequentialReader

type SequentialReader interface {
	// Close() frees the resources allocated by the SequentialReader.
	io.Closer

	// Next() tries to advance the reading by one shape and one attribute row
	// and returns true if the read operation could be performed without any
	// error.
	Next() bool

	// Shape returns the index and the last read shape. If the SequentialReader
	// encountered any errors, nil is returned for the Shape.
	Shape() (int, Shape)

	// Attribute returns the value of the n-th attribute in the current row. If
	// the SequentialReader encountered any errors, the empty string is
	// returned.
	Attribute(n int) string

	// Fields returns the fields of the database. If the SequentialReader
	// encountered any errors, nil is returned.
	Fields() []Field

	// Err returns the last non-EOF error encountered.
	Err() error
}

SequentialReader is the interface that allows reading shapes and attributes one after another. It also embeds io.Closer.

func SequentialReaderFromExt

func SequentialReaderFromExt(shp, dbf io.ReadCloser) SequentialReader

SequentialReaderFromExt returns a new SequentialReader that interprets shp as a source of shapes whose attributes can be retrieved from dbf.

type Shape

type Shape interface {
	BBox() Box
	// contains filtered or unexported methods
}

Shape interface

type ShapeType

type ShapeType int32

ShapeType is a identifier for the the type of shapes.

type Writer

type Writer struct {
	GeometryType ShapeType
	// contains filtered or unexported fields
}

Writer is the type that is used to write a new shapefile.

func Append added in v0.1.1

func Append(filename string) (*Writer, error)

Append returns a Writer pointer that will append to the given shapefile and the first error that was encounted during creation of that Writer. The shapefile must have a valid index file.

func Create

func Create(filename string, t ShapeType) (*Writer, error)

Create returns a point to new Writer and the first error that was encountered. In case an error occurred the returned Writer point will be nil This also creates a corresponding SHX file. It is important to use Close() when done because that method writes all the headers for each file (SHP, SHX and DBF). If filename does not end on ".shp" already, it will be treated as the basename for the file and the ".shp" extension will be appended to that name.

func (*Writer) BBox

func (w *Writer) BBox() Box

BBox returns the bounding box of the Writer.

func (*Writer) Close

func (w *Writer) Close()

Close closes the Writer. This must be used at the end of the transaction because it writes the correct headers to the SHP/SHX and DBF files before closing.

func (*Writer) SetFields

func (w *Writer) SetFields(fields []Field) error

SetFields sets field values in the DBF. This initializes the DBF file and should be used prior to writing any attributes.

func (*Writer) Write

func (w *Writer) Write(shape Shape) int32

Write shape to the Shapefile. This also creates a record in the SHX file and DBF file (if it is initialized). Returns the index of the written object which can be used in WriteAttribute.

func (*Writer) WriteAttribute

func (w *Writer) WriteAttribute(row int, field int, value interface{}) error

WriteAttribute writes value for field into the given row in the DBF. Row number should be the same as the order the Shape was written to the Shapefile. The field value corresponds to the field in the slice used in SetFields.

type ZipReader

type ZipReader struct {
	// contains filtered or unexported fields
}

ZipReader provides an interface for reading Shapefiles that are compressed in a ZIP archive.

func OpenShapeFromZip

func OpenShapeFromZip(zipFilePath string, name string) (*ZipReader, error)

OpenShapeFromZip opens a shape file that is contained in a ZIP archive. The parameter name is name of the shape file. The name of the shapefile must be a relative path: it must not start with a drive letter (e.g. C:) or leading slash, and only forward slashes are allowed. These rules are the same as in https://golang.org/pkg/archive/zip/#FileHeader.

func OpenZip

func OpenZip(zipFilePath string) (*ZipReader, error)

OpenZip opens a ZIP file that contains a single shapefile.

func (*ZipReader) Attribute

func (zr *ZipReader) Attribute(n int) string

Attribute returns the n-th field of the last row that was read. If there were any errors before, the empty string is returned.

func (*ZipReader) Close

func (zr *ZipReader) Close() error

Close closes the ZipReader and frees the allocated resources.

func (*ZipReader) Err

func (zr *ZipReader) Err() error

Err returns the last non-EOF error that was encountered by this ZipReader.

func (*ZipReader) Fields

func (zr *ZipReader) Fields() []Field

Fields returns a slice of Fields that are present in the DBF table.

func (*ZipReader) Next

func (zr *ZipReader) Next() bool

Next reads the next shape in the shapefile and the next row in the DBF. Call Shape() and Attribute() to access the values.

func (*ZipReader) Shape

func (zr *ZipReader) Shape() (int, Shape)

Shape returns the shape that was last read as well as the current index.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL