jonas-p / go-shp

Go library for reading and writing ESRI Shapefiles. Pure Golang implementation based on the ESRI Shapefile technical description.
MIT License
255 stars 67 forks source link

how to read a shape as a specific type #3

Closed rustyoz closed 8 years ago

rustyoz commented 8 years ago

similar to this:

    for shape.Next() {
        var p shp.PolyLine
        n, p := shape.Shape()

i would like to read each point of the resulting polyline

jonas-p commented 8 years ago

All points are stored in the instance variable Points, so you'll have to iterate through that.

Read more here http://godoc.org/github.com/jonas-p/go-shp#PolyLine

There's also a test that does exactly this

func test_PolyLine(t *testing.T, filename string, points [][]float64, shapes_num int) {
    shapes := getShapes(filename, t)
    if len(shapes) != shapes_num {
        t.Error("Number of shapes read was wrong.")
    }
    for n, s := range shapes {
        p, ok := s.(*PolyLine)
        if !ok {
            t.Fatal("Failed to type assert.")
        }
        for k, point := range p.Points {
            if !pointsEqual(points[n*3+k], []float64{point.X, point.Y}) {
                t.Error("Points did not match.")
            }
        }
    }
}