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 ¶
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 ¶
NewDecoder creates a new Decoder.
func (*Decoder) DecodeRow ¶
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).
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 ¶
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 ¶
NewEncoderFromFields creates a new Encoder from a given file name, geometry type, and data field names.