geom: github.com/ctessum/geom/encoding/shp Index | Examples | Files

package shp

import "github.com/ctessum/geom/encoding/shp"

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

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.

Code:

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

Package Files

shp.go shp2geom.go

Variables

var FixOrientation = false

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

type Decoder Uses

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 Uses

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

NewDecoder creates a new Decoder.

func (*Decoder) Close Uses

func (r *Decoder) Close()

Close closes the underlying Reader.

func (*Decoder) DecodeRow Uses

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 Uses

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 Uses

func (r Decoder) Error() error

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

func (*Decoder) SR Uses

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

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

type Encoder Uses

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 Uses

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 Uses

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 Uses

func (e *Encoder) Close()

Close closes the underlying Writer.

func (*Encoder) Encode Uses

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 Uses

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.

Package shp imports 11 packages (graph) and is imported by 6 packages. Updated 2019-07-02. Refresh now. Tools for package owners.