shapefile

package module
v0.0.0-...-1f28478 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2019 License: MIT Imports: 5 Imported by: 0

README

Go Shapefile Reader

Build Status GoDoc

Pure Go implementation for reading of ESRI Shapefiles as specified by the ESRI Shapfile Technical Description. I created this as part of a project that ingests GIS data provided by the National Hurricane Center as I was unable to find similar libraries that didn't need signficant modification.

Use

Importing
import "github.com/kellegous/shapefile"

There are also subpackages for reading the SHP and DBF formats, the imports for those are github.com/kellegous/shapefile/shp and github.com/kellegous/shapefile/dbf respectively.

Examples

Reading the records in a shapfile (shp and dbf)
sr, err := os.Open("data.shp")
if err != nil {
    panic(err)
}
defer sr.Close()

dr, err := os.Open("data.dbf")
if err != nil {
    panic(err)
}
defer dr.Close()

r, err := shapefile.NewReader(sr, shapefile.WithDBF(dr))
if err != nil {
    panic(err)
}

for {
    rec, err := r.Next()
    if err == io.EOF {
        break
    } else if err != nil {
        panic(err)
    }

    switch s := rec.Shape.(type) {
    case *shp.Point:
        fmt.Printf("{X:%0.2f, Y:%.2f}", s.X, s.Y)
        // ...
    }

    for i, field := range r.Fields() {
        fmt.Printf("%s = %s\n", field.Name, rec.Attr(i))
    }
}
Reading shapfile contained in zip archive
    s, err := os.Stat("al062018_5day_035.zip")
    if err != nil {
        panic(err)
    }

    r, err := os.Open("al062018_5day_035.zip")
    if err != nil {
        panic(err)
    }
    defer r.Close()

    zr, err := zip.NewReader(r, s.Size())
    if err != nil {
        panic(err)
    }

    sr, err := shapefile.NewReaderFromZip(zr, "al062018-035_5day_pts")
    if err != nil {
        panic(err)
    }
    defer sr.Close()
    
    for {
        rec, err := sr.Next()
        if err == io.EOF {
            break
        } else if err != nil {
            panic(err)
        }

        switch s := rec.Shape.(type) {
        case *shp.Point:
            fmt.Printf("{X:%0.2f, Y:%.2f}", s.X, s.Y)
            // ...
        }

        for i, field := range r.Fields() {
            fmt.Printf("%s = %s\n", field.Name, rec.Attr(i))
        }
    }

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(r *Reader) error

Option allows for options to be specified when calling NewReader.

func WithDBF

func WithDBF(r io.Reader) Option

WithDBF allows the specification of a dbf file that contains the associated attributes for each of the shp file records.

type ReadCloser

type ReadCloser struct {
	*Reader
	io.Closer
}

ReadCloser provides a Reader that also has associated resources that must be closed by the caller.

func NewReaderFromZip

func NewReaderFromZip(r *zip.Reader, name string) (*ReadCloser, error)

NewReaderFromZip is a convenience utility for the common task of opening a shapefile and its associated attribute database from within a zip archive. The name provided should not include a file extension. For instance, if you with to open the archived file pts.shp (which also implies opening pts.dbf if it is present), name should be provided as "pts".

type Reader

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

Reader provides sequential access to the records in the shapefile.

func NewReader

func NewReader(r io.Reader, opts ...Option) (*Reader, error)

NewReader creates a new Reader that provides sequential access to the records in a shp file structure. Optionally, a dbf io.Reader can be provided that will also provide access to the attributes that correspond to each shp record.

func (*Reader) BBox

func (r *Reader) BBox() *shp.BBox

BBox returns the bounding box that was declared in the file header of the .shp structure.

func (*Reader) FieldCount

func (r *Reader) FieldCount() int

FieldCount returns the number of fields.

func (*Reader) Fields

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

Fields returns the fields that were declared in the dbf header structure. If no dbf was provided when the Reader was created, this will return nil.

func (*Reader) Next

func (r *Reader) Next() (*Record, error)

Next reads the next record from the underlying readers. When the end of the shp reader is reached, this will return io.EOF. If a dbf reader is provided that has fewer records than the given shp reader, io.ErrUnexpectedEOF will be returned after the last record in the db reader.

func (*Reader) ShapeType

func (r *Reader) ShapeType() shp.ShapeType

ShapeType returns the shape type that was declared in the file header of the .shp structure.

type Record

type Record struct {
	Shape shp.Shape
	*dbf.Record
}

Record is record that combines the record taken from the specified shp file and also, optionally, adds metadata attributes if a dbf file was specified.

func (*Record) Attrs

func (r *Record) Attrs() []string

Attrs returns all the attributes for this record as a slice of strings.

Directories

Path Synopsis
cmd
poc

Jump to

Keyboard shortcuts

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