tidwall / geojson Goto Github PK
View Code? Open in Web Editor NEWGeoJSON for Go. Used by Tile38
License: MIT License
GeoJSON for Go. Used by Tile38
License: MIT License
It would be really great to make geojson.Object
Marshaler again. :) We're using it (since the package was part of tile38) as a field of other types, which we often need to marshal to JSON. We now moved from tile38/geojson to the standalone one and took me a while to find out that geojson.Object is no longer a Marshaler.
geometry.Poly{}.Valid() check only if the Exterior and the holes are valid Series, not if they are a valid polygon.
Therefore Polygons with missing Points, intersecting edges etc. are not detected.
Is this intended or just not necessary till now?
package main
import (
"github.com/tidwall/geojson"
"github.com/tidwall/geojson/geometry"
)
func main() {
polygon := geometry.NewPoly([]geometry.Point{
{
8.88242492198438,
47.85972404073712,
},
{
6.003023836249525,
47.467582223811604,
},
{
10.561998072355692,
45.71873204889732,
},
{
6.738413586888328,
48.35187757503576,
},
{
8.88242492198438,
47.85972404073712,
},
}, nil, nil)
print(polygon.Valid()) //Expected to be false
geojsonPolygon := geojson.NewPolygon(polygon)
print(geojsonPolygon.Valid()) //Expected to be false
geojsonPolygonFeature := geojson.NewFeature(geojsonPolygon, "")
print(geojsonPolygonFeature.Valid()) //Expected to be False
geojsonPolygonFeatureJson := geojsonPolygonFeature.JSON()
print(geojsonPolygonFeatureJson)
}
Apparently, Feature
object is supposed to have both geometry
and properties
, according to the spec: https://tools.ietf.org/html/rfc7946#section-3.2
Currently, feature JSON is lacking properties, which makes it invalid. This makes the returned JSON unusable in e.g. geojson.io
The proposed fix is in #9
I was playing around with Tile38 and noticed a little difference in the distance value.
Tile38 uses 6371000m as earthRadius = 6371e3
geo.go#L12
Another one uses 6376500m GeoCoordinate.cs#L297
I don't know which one is more accurate.
Thanks for the awesome work.
Hello,
I noticed a strange behavior wrt the ring intersection : https://play.golang.org/p/NIXpnLvZTWG
package main
import (
"fmt"
"github.com/tidwall/geojson/geometry"
)
func main() {
exterior := make([]geometry.Point, 5)
exterior[0] = geometry.Point{X: 0, Y: 0}
exterior[1] = geometry.Point{X: 6000, Y: 0}
exterior[2] = geometry.Point{X: 10, Y: 10.1}
exterior[3] = geometry.Point{X: 0, Y: 10}
exterior[4] = geometry.Point{X: 0, Y: 0}
polygon := geometry.NewPoly(exterior, nil, nil)
rectangle := geometry.Rect{
Min: geometry.Point{X: 2560, Y: 0},
Max: geometry.Point{X: 5120, Y: 2560},
}
flagContains := polygon.ContainsRect(rectangle)
flagIntersects := rectangle.IntersectsPoly(polygon)
flagIntersects2 := polygon.IntersectsRect(rectangle)
fmt.Printf("intersects %v, contains %v, intersects2 %v", flagIntersects, flagContains, flagIntersects2)
}
Yields: False,False,False.
Expected output: True, False, True.
Thank you ! Thomas
When running go get -u github.com/tidwall/geojson@latest
I receive the following error:
github.com/tidwall/[email protected]: reading github.com/tidwall/lotsa/go.mod at revision v1.0.2: git ls-remote -q origin in /Users/user/go/pkg/mod/cache/vcs/d11311936b4f8995a8390df6960b70488ff2ad2b6842ea20a38c94116dd7efe0: exit status 128:
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
By the look of it, the repository github.com/tidwall/lotsa
needs to be converted to a Golang module.
Moved from: tidwall/tile38#330
Hi,
I'm using your library to calculate if a point is within the polygon defined by a Uber H3 res0. Example code:
package main
import (
"fmt"
"github.com/tidwall/geojson/geometry"
"github.com/uber/h3-go/v4"
)
func main() {
res0 := h3.Cell(h3.IndexFromString("8003fffffffffff"))
boundary := make([]geometry.Point, 0)
fmt.Println("boundary: ")
for _, point := range res0.Boundary() {
point := geometry.Point{Y: point.Lat, X: point.Lng}
boundary = append(boundary, point)
fmt.Println(point)
}
poly := geometry.NewPoly(boundary, nil, nil)
fmt.Println("point:")
point := geometry.Point{Y: res0.LatLng().Lat, X: res0.LatLng().Lng}
fmt.Println("poly contains point")
fmt.Println(poly.ContainsPoint(point))
}
Some H3Cells are transmeridian such as this example. ContainsPoint return false
while actually the point is the centerpoint (res0.LatLng())
of the polygon. I tried wrapping the longitudes to a 0 - 360 degrees system, but this also doesn't work because the point isn't valid anymore.
Hi, thanks a lot for this fantastic package!
I have a question about the DefaultIndexOptions
which defined use QuadTree:
Lines 39 to 42 in ad814f6
I try to setup a benchmark in CI, here is a sample which shows RTree based index seems faster:
=== RUN TestBigArizona
az/none 1582 points created in 106.201µs using 27344 bytes
az/none/in 100,000 ops over 4 threads in 399ms, 250,372/sec, 3994 ns/op
az/none/on 100,000 ops over 4 threads in 197ms, 508,399/sec, 1966 ns/op
az/none/out 100,000 ops over 4 threads in 403ms, 247,900/sec, 4033 ns/op
az/none/rnd 100,000 ops over 4 threads in 434ms, 230,199/sec, 4344 ns/op
az/quad 1582 points created in 336.003µs using 32232 bytes
az/quad/in 100,000 ops over 4 threads in 38ms, 2,615,503/sec, 382 ns/op
az/quad/on 100,000 ops over 4 threads in 17ms, 5,775,986/sec, 173 ns/op
az/quad/out 100,000 ops over 4 threads in 30ms, 3,289,053/sec, 304 ns/op
az/quad/rnd 100,000 ops over 4 threads in 46ms, 2,196,806/sec, 455 ns/op
az/rtre 1582 points created in 874.808µs using 39656 bytes
az/rtre/in 100,000 ops over 4 threads in 25ms, 4,021,162/sec, 248 ns/op
az/rtre/on 100,000 ops over 4 threads in 8ms, 12,413,302/sec, 80 ns/op
az/rtre/out 100,000 ops over 4 threads in 24ms, 4,147,342/sec, 241 ns/op
az/rtre/rnd 100,000 ops over 4 threads in 32ms, 3,130,243/sec, 319 ns/op
--- PASS: TestBigArizona (1.69s)
=== RUN TestBigTexas
tx/none 12478 points created in 276.703µs using 204880 bytes
tx/none/in 100,000 ops over 4 threads in 4655ms, 21,484/sec, 46545 ns/op
tx/none/on 100,000 ops over 4 threads in 2098ms, 47,672/sec, 20976 ns/op
tx/none/out 100,000 ops over 4 threads in 4550ms, 21,977/sec, 45501 ns/op
tx/none/rnd 100,000 ops over 4 threads in 4758ms, 21,019/sec, 47575 ns/op
tx/quad 12478 points created in 3.578431ms using 245864 bytes
tx/quad/in 100,000 ops over 4 threads in 116ms, 863,551/sec, 1158 ns/op
tx/quad/on 100,000 ops over 4 threads in 131ms, 760,742/sec, 1314 ns/op
tx/quad/out 100,000 ops over 4 threads in 113ms, 886,719/sec, 1127 ns/op
tx/quad/rnd 100,000 ops over 4 threads in 115ms, 869,634/sec, 1149 ns/op
tx/rtre 12478 points created in 8.743577ms using 295016 bytes
tx/rtre/in 100,000 ops over 4 threads in 60ms, 1,662,062/sec, 601 ns/op
tx/rtre/on 100,000 ops over 4 threads in 71ms, 1,405,447/sec, 711 ns/op
tx/rtre/out 100,000 ops over 4 threads in 58ms, 1,720,619/sec, 581 ns/op
tx/rtre/rnd 100,000 ops over 4 threads in 61ms, 1,631,339/sec, 612 ns/op
--- PASS: TestBigTexas (16.82s)
So default option use QuadTree because it used less memory than RTree and not too slower compared with RTree and use QuadTree could reduce index time when series created?
Hi 👋
I want to load a geojson with a feature collection and iterate over the features with the ForEach
message that the FeatureCollection
provides.
func main() {
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}
fc, err := geojson.Parse(string(file), nil)
if err != nil {
return err
}
fc.ForEach(func(o geojson.Object) bool {
if o.Empty() {
return true
}
fmt.Println(o)
return true
})
}
>> {"type":"Polygon","coordinates":[[[13.107376098632812,52.37769505233968],[13.699264526367188,52.37769505233968],[13.699264526367188,52.63973017532399],[13.107376098632812,52.63973017532399],[13.107376098632812,52.37769505233968]]]}
If I run this I would expect to iterate over the an array of Feature
and not Polygon
.
Is this intentional and I have to rethink my approach?
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.