jonas-p / go-shp Goto Github PK
View Code? Open in Web Editor NEWGo library for reading and writing ESRI Shapefiles. Pure Golang implementation based on the ESRI Shapefile technical description.
License: MIT License
Go library for reading and writing ESRI Shapefiles. Pure Golang implementation based on the ESRI Shapefile technical description.
License: MIT License
Hi everyone,
how can i read shapefile with the geom is multipolygon and convert to array list polygon
thank you
source code : w.dbf, err = os.Create(w.filename + "dbf")
can be modified w.dbf, err = os.Create(w.filename + ".dbf")
(idea originally by @tdilo)
At the moment OpenZip
assumes that the shapefile lies in the root of the archived filetree and has the same basename as the archive file.
The idea is to lift the restriction by scanning through the archive and using the relative path and basename of the first found shapefile.
It's possible to know the geometry type of the shapes read from a zip file? There is a technical reason for the missing of GeomtryType attribute in the SequentialReader interface?
go-shp should start making releases that according to Semantic Versioning so that dep can be used more naturally.
I suggested go-shp
to be used as a geometry provider in the vector tile server Tegola here: go-spatial/tegola#160
They were open to the idea and suggested that go-shp
implemented a spatial index. This issue is meant to keep track of that.
I am going to tackle these over the next couple of days.
Edit: Added Goreportcard
In a shp file that I have we have type ""type": called multipolygon.
"Feature","geometry":{"type":"MultiPolygon","coordinates":"
What is the best way to read this? When I do *shp.Polygon I am only able to retrieve one of the polygons and its bbx.
I use this lib to read the shapefile.DBF and store it as a map Attribute. when I try to get the Value of I had stored, I get nil,I use value,ok := Attribute["OBJECTID"]
,it return nil,false
.I store it like this:
for indFields, v := range fields {
featureAttributes[v.String()] = shpreader.Attribute(indFields)
}
And when I do it like this:
func (f Feature) GetAttrByName(name string) string {
for k, v := range f.Attributes {
if k == name {
return v
}
}
mylog.Error.Printf("获取%s的值失败", name)
return ""
}
it can work again.just slightly perplexed.Why is that?
Out of the box TestZipReader doens't seem to be working
--- FAIL: TestZipReader (0.01s)
zipreader_test.go:75: Testing zipped reading for test_files/polyline
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polyline read was wrong. Wanted 2, got 1.
zipreader_test.go:75: Testing zipped reading for test_files/multipoint
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/multipoint read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/polylinez
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polylinez read was wrong. Wanted 2, got 1.
zipreader_test.go:75: Testing zipped reading for test_files/multipatch
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/multipatch read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/polygonz
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polygonz read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/polylinem
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polylinem read was wrong. Wanted 2, got 1.
zipreader_test.go:75: Testing zipped reading for test_files/polygonm
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polygonm read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/point
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/point read was wrong. Wanted 3, got 2.
zipreader_test.go:75: Testing zipped reading for test_files/pointz
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/pointz read was wrong. Wanted 3, got 2.
zipreader_test.go:75: Testing zipped reading for test_files/multipointz
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/multipointz read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/multipointm
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/multipointm read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/polygon
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/polygon read was wrong. Wanted 1, got 0.
zipreader_test.go:75: Testing zipped reading for test_files/pointm
zipreader_test.go:64: Error when iterating over the shapes: Error while reading next shape: EOF
reader_test.go:220: Number of shapes for test_files/pointm read was wrong. Wanted 3, got 2.
FAIL
exit status 1
FAIL github.com/gertcuykens/go-shp 0.029s
%```
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
@jonas-p are you still planning on maintaining this? if not some people in the community might want to fork it and address some the issues and prs.
I usually encounter shapefiles that come as a ZIP which contains the .shp/.shx/.dbf. It would be great to have the possibility of iterating over the shapes and the database without uncompressing the ZIP beforehand.
This might prove to be non-trivial, as the io.ReadCloser
that is returned by zip.File.Open
does not support seeking. It might be okay for the shapes, as they are read sequentially, anyway, but the random-access to the DBF will not be possible.
As the typical use case might be iterating over all shapes and their individual attributes, one might restrict the access to the attributes to the ones belonging to the last read shape.
I imagine something like a type ZipReader
that has func (z *ZipReader) Next()
and func (z *ZipReader) ShapeFields() (row int, shape Shape, fieldValues []string)
which can be used in a loop in the same way the methods of shp.Reader
are.
Internally Next()
would read the .shp one shape and the .dbf one row at a time, with the latter internally buffered as a plain []byte
-slice which is later interpreted in the same way by shp.ZipReader.ShapeFields
as shp.Reader.ReadAttribute
does with the single attributes now.
For example, if an attribute value is "Galápagos Province", ReadAttribute
returns "Gal\303\241pagos Province".
layer, err := shp.Create("layer/test.shp", shp.POLYLINE)
works but doing
layer, err := shp.Create("layer/test", shp.POLYLINE)` instead results in something unexpected:
teshp
teshx
tedbf
In the example in README.md
, there is the code
// loop through all features in the shapefile
for shape.Next() {
but in (r *Reader) Next()
you return false
if the file hits EOF, meaning the last item in the file will not be read.
r.shape.read(er)
if er.e != nil {
r.err = fmt.Errorf("Error while reading next shape: %v", er.e)
return false
}
I know some behaviour changed around EOF as error between go versions a while back (there was very briefly a similar issue in grpc) so I presume this has snuck in that way.
Is there a way to use this package and convert the shp files into JSON?
Thanks
dbf file get written as shapedbf
instead of shape.dbf
when doing shp.Create("shape.shp", shp.POLYLINE)
all other files seem ok
shape, _ := shp.OpenZip(tmpFile.Name())
defer shape.Close()
for shape.Next() {
_, p := shape.Shape()
if point, ok := p.(*shp.Polygon); ok {
fmt.Println(point.Points)
}
}
Why do I have to write it as p(shp.Polygon)? I want to be able to write p.Points() directly.
It seems like I have to spend time understanding the internal structure and designing according to what I need
when Next move ptr to end, but no function to move ptn to begin, readHeader write ReadHeader works
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
I think the writer should also have a method to return the final BBox like the Reader does
func (r *Reader) BBox() Box {
return r.bbox
}
I use this library to navigate through a shape file containing a road network (made out of polylines). After the r.Next() call the function Shape() returns the row number -1. This results into endless loops and makes the library useless for any file reading. Are there already known situations this habit appeared? Do you have any solutions?
The road network has been created from a filtered OSM-file using QGIS.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.