shp

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: BSD-2-Clause Imports: 11 Imported by: 11

Documentation

Overview

Package shp decodes and encodes shapefiles to and from geometry objects. Z and M data in the shapefile geometry is ignored.

Example

This example shows how to read in information from a shapefile, write it out again, then read it in a second time to ensure that it was properly written.

type record struct {
	// The geometry data will be stored here.
	geom.Polygon

	// The "value" attribute will be stored here. It would also work to
	// just name this field "Value".
	Val float64 `shp:"value"`
}

d, err := NewDecoder("testdata/triangles.shp")
if err != nil {
	panic(err)
}

e, err := NewEncoder("testdata/testout.shp", record{})
if err != nil {
	panic(err)
}

for {
	var rec record
	// Decode a record from the input file.
	if !d.DecodeRow(&rec) {
		break
	}
	// Encode the record to the output file
	if err = e.Encode(rec); err != nil {
		panic(err)
	}
	fmt.Printf("polygon area %.3g, value %g\n", rec.Polygon.Area(), rec.Val)
}
// Check to see if any errors occured during decoding.
if err = d.Error(); err != nil {
	panic(err)
}
d.Close()
e.Close()

const testFile = "testdata/testout"
// Read the data back in from the output file.
d, err = NewDecoder(testFile + ".shp")
if err != nil {
	panic(err)
}
for {
	var rec record
	// Decode a record from the input file.
	if !d.DecodeRow(&rec) {
		break
	}
	fmt.Printf("polygon area %.3g, value %g\n", rec.Polygon.Area(), rec.Val)
}
// Check to see if any errors occured during decoding.
if err = d.Error(); err != nil {
	panic(err)
}
d.Close()

os.Remove(testFile + ".shp")
os.Remove(testFile + ".shx")
os.Remove(testFile + ".dbf")
Output:

polygon area 2.3, value 6
polygon area 2.17, value 1
polygon area 2.3, value 6
polygon area 2.17, value 1

Index

Examples

Constants

This section is empty.

Variables

View Source
var FixOrientation = false

FixOrientation specifies whether to automatically check and fix the orientation of polygons imported from shapefiles.

Functions

This section is empty.

Types

type Decoder

type Decoder struct {
	shp.Reader
	// contains filtered or unexported fields
}

Decoder is a wrapper around the github.com/jonas-p/go-shp shapefile reader.

func NewDecoder

func NewDecoder(filename string) (*Decoder, error)

NewDecoder creates a new Decoder.

func (*Decoder) Close

func (r *Decoder) Close()

Close closes the underlying Reader.

func (*Decoder) DecodeRow

func (r *Decoder) DecodeRow(rec interface{}) bool

DecodeRow decodes a shapefile row into a struct. The input value rec must be a pointer to a struct. The function will attempt to match the struct fields to shapefile data. It will read the shape data into any struct fields that implement the geom.Geom interface. It will read attribute data into any struct fields whose `shp` tag or field names that match an attribute name in the shapefile (case insensitive). Only exported fields will be matched, and all matched fields must be of either string, int, or float64 types. The return value is true if there are still more records to be read from the shapefile. Be sure to call r.Error() after reading is finished to check for any errors that may have occured.

func (*Decoder) DecodeRowFields

func (r *Decoder) DecodeRowFields(fieldNames ...string) (
	g geom.Geom, fields map[string]string, more bool)

DecodeRowFields decodes a shapefile row, returning the row geometry (g), the values of the specified fields (fields), and whether there are still more records to be read from the shapefile (more).

func (Decoder) Error

func (r Decoder) Error() error

Error returns any errors that have been encountered while decoding a shapfile.

func (*Decoder) SR

func (r *Decoder) SR() (*proj.SR, error)

SR reads the shapefile spatial reference from the corresponding .prj file.

type Encoder

type Encoder struct {
	shp.Writer
	// contains filtered or unexported fields
}

Encoder is a wrapper around the github.com/jonas-p/go-shp shapefile reader.

func NewEncoder

func NewEncoder(filename string, archetype interface{}) (*Encoder, error)

NewEncoder creates a new encoder using the path to the output shapefile and a data archetype which is a struct whose fields will become the fields in the output shapefile. The archetype struct must also contain a field that holds a concrete geometry type by which to set the shape type in the output shapefile.

func NewEncoderFromFields

func NewEncoderFromFields(filename string, t shp.ShapeType,
	fields ...shp.Field) (*Encoder, error)

NewEncoderFromFields creates a new Encoder from a given file name, geometry type, and data field names.

func (*Encoder) Close

func (e *Encoder) Close()

Close closes the underlying Writer.

func (*Encoder) Encode

func (e *Encoder) Encode(d interface{}) error

Encode encodes the data in a struct as a shapefile record. d must be of the same type as the archetype struct that was used to initialize the encoder.

func (*Encoder) EncodeFields

func (e *Encoder) EncodeFields(g geom.Geom, vals ...interface{}) error

EncodeFields encodes the geometry 'g' and 'vals' values as a shapefile record. The number of values should be the same as the number of fields the shapefile was created with.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL