Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to load from memory #36

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
43 changes: 40 additions & 3 deletions reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package shp

import (
"bytes"
"encoding/binary"
"fmt"
"io"
Expand Down Expand Up @@ -30,6 +31,8 @@ type Reader struct {
dbfNumRecords int32
dbfHeaderLength int16
dbfRecordLength int16

memoryData *memoryShapeData
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy with adding this member variable to the struct.

Do you need to have random access? If not, have you considered https://godoc.org/github.com/jonas-p/go-shp#SequentialReaderFromExt for opening the byte slices?

}

type readSeekCloser interface {
Expand All @@ -38,6 +41,25 @@ type readSeekCloser interface {
io.Closer
}

type byteSeekCloser struct {
bytes.Reader
}

func (bsc *byteSeekCloser) Close() error {
return nil
}

func newBytesReader(data []byte) *byteSeekCloser {
return &byteSeekCloser{
Reader: *bytes.NewReader(data),
}
}

type memoryShapeData struct {
shpFileData []byte
dbfFileData []byte
}

// Open opens a Shapefile for reading.
func Open(filename string) (*Reader, error) {
ext := filepath.Ext(filename)
Expand All @@ -53,6 +75,17 @@ func Open(filename string) (*Reader, error) {
return s, nil
}

func OpenFromMemory(shpFileData, dbfFileData []byte) (*Reader, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exported function OpenFromMemory should have comment or be unexported

mD := &memoryShapeData{
shpFileData: shpFileData,
dbfFileData: dbfFileData,
}

s := &Reader{filename: "", shp: newBytesReader(mD.shpFileData), memoryData: mD}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have doubts about this, as well. Effectively, you are adding references to mD twice to s. I understand that you want to preserve the dbf data so that openDbf has something to open later, but the proposed design will overcomplicate things.

s.readHeaders()
return s, nil
}

// BBox returns the bounding box of the shapefile.
func (r *Reader) BBox() Box {
return r.bbox
Expand Down Expand Up @@ -194,9 +227,13 @@ func (r *Reader) openDbf() (err error) {
return
}

r.dbf, err = os.Open(r.filename + ".dbf")
if err != nil {
return
if r.memoryData != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO this is a bit of a code smell: Testing whether one of the members is holding data and then branching the logic from there.

r.dbf = newBytesReader(r.memoryData.dbfFileData)
} else {
r.dbf, err = os.Open(r.filename + ".dbf")
if err != nil {
return
}
}

// read header
Expand Down