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

Append shapefiles without creating a copy in memory #7

Closed gertcuykens closed 6 years ago

gertcuykens commented 7 years ago

Hi Jonas, as far as I understand, we can open a shapefile or create one but not append to one

shape, err := shp.Open("static-segments/segments.shp")
shape, err := shp.Create("new-static-segments/segments.shp", shp.MULTIPOINT)

Can we have a shape, err := shp.Append("static-segments/segments.shp", shp.MULTIPOINT) ?

Thanks

fawick commented 6 years ago

Good idea IMHO.

But why the shape type parameter for shp.Append()? Wouldn't the shape type be given by the existing file already? Or is your idea to have Append check and return an error if the type in the call to Append and the one in segments.shp do not match?

gertcuykens commented 6 years ago

yep you are 100% correct, sorry forgot about that :D

So it would be

shape, err := shp.Append("static-segments/segments.shp")

No type checking just the same Error handling as shp.Open does

fawick commented 6 years ago

@gertcuykens, @tdilo I invite you to test and review https://github.com/fawick/go-shp/tree/addAppend

gertcuykens commented 6 years ago

Any specific reason not to recycle err instead of er?

    er := &errReader{Reader: shp}
    w.bbox.MinX = readFloat64(er)
    w.bbox.MinY = readFloat64(er)
    w.bbox.MaxX = readFloat64(er)
    w.bbox.MaxY = readFloat64(er)
    if er.e != nil {
        return nil, fmt.Errorf("cannot read bounding box: %v", err)
    }
fawick commented 6 years ago

The reason is that readFloat64 does not return an error by itself. The errReader allow to check whether should could actually be read from in these four calls.to readFloat64.

On November 17, 2017 9:41:27 PM GMT+01:00, Gert Cuykens notifications@github.com wrote:

Any specific reason not to recycle err instead of er?

  er := &errReader{Reader: shp}
  w.bbox.MinX = readFloat64(er)
  w.bbox.MinY = readFloat64(er)
  w.bbox.MaxX = readFloat64(er)
  w.bbox.MaxY = readFloat64(er)
  if er.e != nil {
      return nil, fmt.Errorf("cannot read bounding box: %v", err)
  }

-- You are receiving this because you commented. Reply to this email directly or view it on GitHub: https://github.com/jonas-p/go-shp/issues/7#issuecomment-345359791

gertcuykens commented 6 years ago

Ok i see maybe the last line should be

return nil, fmt.Errorf("cannot read bounding box: %v", er.e)

then ?

fawick commented 6 years ago

Yes, you're absolutely right, thank you, good catch! Fixed.