-
Notifications
You must be signed in to change notification settings - Fork 68
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.idea |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package shp | ||
|
||
import ( | ||
"bytes" | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
|
@@ -30,6 +31,8 @@ type Reader struct { | |
dbfNumRecords int32 | ||
dbfHeaderLength int16 | ||
dbfRecordLength int16 | ||
|
||
memoryData *memoryShapeData | ||
} | ||
|
||
type readSeekCloser interface { | ||
|
@@ -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) | ||
|
@@ -53,6 +75,17 @@ func Open(filename string) (*Reader, error) { | |
return s, nil | ||
} | ||
|
||
func OpenFromMemory(shpFileData, dbfFileData []byte) (*Reader, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
s.readHeaders() | ||
return s, nil | ||
} | ||
|
||
// BBox returns the bounding box of the shapefile. | ||
func (r *Reader) BBox() Box { | ||
return r.bbox | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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?