mirror of
https://github.com/dolthub/dolt.git
synced 2026-04-28 04:28:53 -05:00
Create shared 'geo' Noms types package
First cut at creating a shared Noms types package, by creating 'geotypes.noms' to define Geoposition and Georectangle. These are shared by quad_tree and sfcrime_importer initially, but will also by used by sfcrime_search and the stuff that works with photos. quad_tree used to add methods to the generated Geoposition and Georectangle types. This doesn't work anymore, since these types are now generated as part of a separate Go package. Fortunately, these methods are easily re-cast as pure functions. Towards #409
This commit is contained in:
+212
@@ -0,0 +1,212 @@
|
||||
// This file was generated by nomdl/codegen.
|
||||
|
||||
package sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
var __sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef = __sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_Ref()
|
||||
|
||||
// This function builds up a Noms value that describes the type
|
||||
// package implemented by this file and registers it with the global
|
||||
// type package definition cache.
|
||||
func __sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_Ref() ref.Ref {
|
||||
p := types.PackageDef{
|
||||
Types: types.ListOfTypeRefDef{
|
||||
|
||||
types.MakeStructTypeRef("Geoposition",
|
||||
[]types.Field{
|
||||
types.Field{"Latitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
types.Field{"Longitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("Georectangle",
|
||||
[]types.Field{
|
||||
types.Field{"TopLeft", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
types.Field{"BottomRight", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
},
|
||||
}.New()
|
||||
return types.RegisterPackage(&p)
|
||||
}
|
||||
|
||||
// Geoposition
|
||||
|
||||
type Geoposition struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewGeoposition() Geoposition {
|
||||
return Geoposition{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(0),
|
||||
types.NewString("Longitude"), types.Float32(0),
|
||||
)}
|
||||
}
|
||||
|
||||
type GeopositionDef struct {
|
||||
Latitude float32
|
||||
Longitude float32
|
||||
}
|
||||
|
||||
func (def GeopositionDef) New() Geoposition {
|
||||
return Geoposition{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(def.Latitude),
|
||||
types.NewString("Longitude"), types.Float32(def.Longitude),
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Geoposition) Def() (d GeopositionDef) {
|
||||
d.Latitude = float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
d.Longitude = float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForGeoposition = types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 0)
|
||||
|
||||
func (m Geoposition) TypeRef() types.TypeRef {
|
||||
return __typeRefForGeoposition
|
||||
}
|
||||
|
||||
func init() {
|
||||
types.RegisterFromValFunction(__typeRefForGeoposition, func(v types.Value) types.NomsValue {
|
||||
return GeopositionFromVal(v)
|
||||
})
|
||||
}
|
||||
|
||||
func GeopositionFromVal(val types.Value) Geoposition {
|
||||
// TODO: Validate here
|
||||
return Geoposition{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (s Geoposition) NomsValue() types.Value {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s Geoposition) Equals(other types.Value) bool {
|
||||
if other, ok := other.(Geoposition); ok {
|
||||
return s.m.Equals(other.m)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Geoposition) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s Geoposition) Chunks() (futures []types.Future) {
|
||||
futures = append(futures, s.TypeRef().Chunks()...)
|
||||
futures = append(futures, s.m.Chunks()...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Geoposition) Latitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLatitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Latitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
func (s Geoposition) Longitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLongitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Longitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
// Georectangle
|
||||
|
||||
type Georectangle struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewGeorectangle() Georectangle {
|
||||
return Georectangle{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 1),
|
||||
types.NewString("TopLeft"), NewGeoposition().NomsValue(),
|
||||
types.NewString("BottomRight"), NewGeoposition().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
type GeorectangleDef struct {
|
||||
TopLeft GeopositionDef
|
||||
BottomRight GeopositionDef
|
||||
}
|
||||
|
||||
func (def GeorectangleDef) New() Georectangle {
|
||||
return Georectangle{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 1),
|
||||
types.NewString("TopLeft"), def.TopLeft.New().NomsValue(),
|
||||
types.NewString("BottomRight"), def.BottomRight.New().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Georectangle) Def() (d GeorectangleDef) {
|
||||
d.TopLeft = GeopositionFromVal(s.m.Get(types.NewString("TopLeft"))).Def()
|
||||
d.BottomRight = GeopositionFromVal(s.m.Get(types.NewString("BottomRight"))).Def()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForGeorectangle = types.MakeTypeRef(__sha1_3bfd4da1c27a6472279b96d731b47e58e8832deePackageInFile_sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee_CachedRef, 1)
|
||||
|
||||
func (m Georectangle) TypeRef() types.TypeRef {
|
||||
return __typeRefForGeorectangle
|
||||
}
|
||||
|
||||
func init() {
|
||||
types.RegisterFromValFunction(__typeRefForGeorectangle, func(v types.Value) types.NomsValue {
|
||||
return GeorectangleFromVal(v)
|
||||
})
|
||||
}
|
||||
|
||||
func GeorectangleFromVal(val types.Value) Georectangle {
|
||||
// TODO: Validate here
|
||||
return Georectangle{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (s Georectangle) NomsValue() types.Value {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s Georectangle) Equals(other types.Value) bool {
|
||||
if other, ok := other.(Georectangle); ok {
|
||||
return s.m.Equals(other.m)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Georectangle) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s Georectangle) Chunks() (futures []types.Future) {
|
||||
futures = append(futures, s.TypeRef().Chunks()...)
|
||||
futures = append(futures, s.m.Chunks()...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Georectangle) TopLeft() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("TopLeft")))
|
||||
}
|
||||
|
||||
func (s Georectangle) SetTopLeft(val Geoposition) Georectangle {
|
||||
return Georectangle{s.m.Set(types.NewString("TopLeft"), val.NomsValue())}
|
||||
}
|
||||
|
||||
func (s Georectangle) BottomRight() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("BottomRight")))
|
||||
}
|
||||
|
||||
func (s Georectangle) SetBottomRight(val Geoposition) Georectangle {
|
||||
return Georectangle{s.m.Set(types.NewString("BottomRight"), val.NomsValue())}
|
||||
}
|
||||
@@ -1,134 +0,0 @@
|
||||
package main
|
||||
|
||||
import "math"
|
||||
|
||||
const (
|
||||
// EarthRadius the Earth's radius is about 6,371km according to Wikipedia
|
||||
EarthRadius = 6371
|
||||
)
|
||||
|
||||
// Latitude64 casts the Latitude value to a float64 for convenience.
|
||||
func (p GeopositionDef) Latitude64() float64 {
|
||||
return float64(p.Latitude)
|
||||
}
|
||||
|
||||
// Longitude64 casts the Longitude value to a float64 for convenience.
|
||||
func (p GeopositionDef) Longitude64() float64 {
|
||||
return float64(p.Longitude)
|
||||
}
|
||||
|
||||
// TopLeftOf returns true if it is above and to the left of "o"
|
||||
func (p GeopositionDef) TopLeftOf(o GeopositionDef) bool {
|
||||
return p.Latitude > o.Latitude && p.Longitude < o.Longitude
|
||||
}
|
||||
|
||||
// BelowRightOf returns true if it's below and to the right of "o"
|
||||
func (p GeopositionDef) BelowRightOf(o GeopositionDef) bool {
|
||||
return p.Latitude < o.Latitude && p.Longitude > o.Longitude
|
||||
}
|
||||
|
||||
// TopLeftOrSameOf returns true if it is above and to the left of "o"
|
||||
func (p GeopositionDef) TopLeftOrSameOf(o GeopositionDef) bool {
|
||||
return p.Latitude >= o.Latitude && p.Longitude <= o.Longitude
|
||||
}
|
||||
|
||||
// BelowRightOrSameOf returns true if it's below and to the right of "o"
|
||||
func (p GeopositionDef) BelowRightOrSameOf(o GeopositionDef) bool {
|
||||
return p.Latitude <= o.Latitude && p.Longitude >= o.Longitude
|
||||
}
|
||||
|
||||
// PointAtDistanceAndBearing returns a Point populated with the lat and lng coordinates
|
||||
// by transposing the origin point the passed in distance (in kilometers)
|
||||
// by the passed in compass bearing (in degrees).
|
||||
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
|
||||
func (p *GeopositionDef) PointAtDistanceAndBearing(dist float64, bearing float64) *GeopositionDef {
|
||||
dr := dist / EarthRadius
|
||||
|
||||
bearing = (bearing * (math.Pi / 180.0))
|
||||
|
||||
lat1 := (float64(p.Latitude64()) * (math.Pi / 180.0))
|
||||
lng1 := (float64(p.Longitude64()) * (math.Pi / 180.0))
|
||||
|
||||
lat2Part1 := math.Sin(lat1) * math.Cos(dr)
|
||||
lat2Part2 := math.Cos(lat1) * math.Sin(dr) * math.Cos(bearing)
|
||||
|
||||
lat2 := math.Asin(lat2Part1 + lat2Part2)
|
||||
|
||||
lng2Part1 := math.Sin(bearing) * math.Sin(dr) * math.Cos(lat1)
|
||||
lng2Part2 := math.Cos(dr) - (math.Sin(lat1) * math.Sin(lat2))
|
||||
|
||||
lng2 := lng1 + math.Atan2(lng2Part1, lng2Part2)
|
||||
lng2 = math.Mod((lng2+3*math.Pi), (2*math.Pi)) - math.Pi
|
||||
|
||||
lat2 = lat2 * (180.0 / math.Pi)
|
||||
lng2 = lng2 * (180.0 / math.Pi)
|
||||
|
||||
return &GeopositionDef{Latitude: float32(lat2), Longitude: float32(lng2)}
|
||||
}
|
||||
|
||||
// DistanceTo calculates the Haversine distance between two points in kilometers.
|
||||
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
|
||||
func (p GeopositionDef) DistanceTo(o GeopositionDef) float32 {
|
||||
dLat := (o.Latitude64() - p.Latitude64()) * (math.Pi / 180.0)
|
||||
dLon := (o.Longitude64() - p.Longitude64()) * (math.Pi / 180.0)
|
||||
|
||||
lat1 := p.Latitude64() * (math.Pi / 180.0)
|
||||
lat2 := o.Latitude64() * (math.Pi / 180.0)
|
||||
|
||||
a1 := math.Sin(dLat/2) * math.Sin(dLat/2)
|
||||
a2 := math.Sin(dLon/2) * math.Sin(dLon/2) * math.Cos(lat1) * math.Cos(lat2)
|
||||
|
||||
a := a1 + a2
|
||||
|
||||
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
||||
|
||||
return float32(EarthRadius * c)
|
||||
}
|
||||
|
||||
// BoundingRectangle calculates a rectangle whose sides east/west and north/south.
|
||||
// The center of the rectangle is "p" and each side is "2 * radius" in length.
|
||||
// A radius value of 1 equals 1 kilometer.
|
||||
func (p GeopositionDef) BoundingRectangle(radius float64) GeorectangleDef {
|
||||
northPoint := p.PointAtDistanceAndBearing(radius, 0)
|
||||
eastPoint := p.PointAtDistanceAndBearing(radius, 90)
|
||||
southPoint := p.PointAtDistanceAndBearing(radius, 180)
|
||||
westPoint := p.PointAtDistanceAndBearing(radius, 270)
|
||||
|
||||
return GeorectangleDef{
|
||||
TopLeft: GeopositionDef{northPoint.Latitude, westPoint.Longitude},
|
||||
BottomRight: GeopositionDef{southPoint.Latitude, eastPoint.Longitude},
|
||||
}
|
||||
}
|
||||
|
||||
// IntersectsRect returns true if "o" intersects r
|
||||
func (r GeorectangleDef) IntersectsRect(o GeorectangleDef) bool {
|
||||
return r.TopLeft.TopLeftOf(o.BottomRight) && r.BottomRight.BelowRightOf(o.TopLeft)
|
||||
}
|
||||
|
||||
// ContainsRect returns true if "o" is within r
|
||||
func (r GeorectangleDef) ContainsRect(o GeorectangleDef) bool {
|
||||
return r.TopLeft.TopLeftOrSameOf(o.TopLeft) && r.BottomRight.BelowRightOrSameOf(o.BottomRight)
|
||||
}
|
||||
|
||||
// ContainsPoint returns true if p is contained in r
|
||||
func (r GeorectangleDef) ContainsPoint(p GeopositionDef) bool {
|
||||
return r.TopLeft.TopLeftOrSameOf(p) && r.BottomRight.BelowRightOf(p)
|
||||
}
|
||||
|
||||
// Split creates one new rectangle for each quadrant in "r"
|
||||
func (r GeorectangleDef) Split() (tlRect, blRect, trRect, brRect GeorectangleDef) {
|
||||
// fmt.Println("Splitting:", r)
|
||||
maxLat := float32(r.TopLeft.Latitude)
|
||||
minLon := float32(r.TopLeft.Longitude)
|
||||
minLat := float32(r.BottomRight.Latitude)
|
||||
maxLon := float32(r.BottomRight.Longitude)
|
||||
midLat := ((maxLat - minLat) / 2) + minLat
|
||||
midLon := ((maxLon - minLon) / 2) + minLon
|
||||
|
||||
tlRect = GeorectangleDef{GeopositionDef{maxLat, minLon}, GeopositionDef{midLat, midLon}}
|
||||
blRect = GeorectangleDef{GeopositionDef{midLat, minLon}, GeopositionDef{minLat, midLon}}
|
||||
trRect = GeorectangleDef{GeopositionDef{maxLat, midLon}, GeopositionDef{midLat, maxLon}}
|
||||
brRect = GeorectangleDef{GeopositionDef{midLat, midLon}, GeopositionDef{minLat, maxLon}}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -1,96 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPoint(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
p1 := GeopositionDef{5, 5}
|
||||
p2 := GeopositionDef{3, 7}
|
||||
p3 := GeopositionDef{10, 7}
|
||||
p4 := GeopositionDef{10, 12}
|
||||
|
||||
assert.True(p1.TopLeftOf(p2))
|
||||
assert.False(p2.TopLeftOf(p1))
|
||||
assert.True(p2.BelowRightOf(p1))
|
||||
assert.False(p1.BelowRightOf(p2))
|
||||
assert.False(p1.TopLeftOf(p1))
|
||||
assert.False(p2.BelowRightOf(p2))
|
||||
|
||||
assert.True(p3.TopLeftOrSameOf(p2))
|
||||
assert.True(p3.TopLeftOrSameOf(p3))
|
||||
assert.False(p4.TopLeftOrSameOf(p3))
|
||||
assert.True(p2.BelowRightOrSameOf(p3))
|
||||
assert.True(p3.BelowRightOrSameOf(p3))
|
||||
assert.False(p3.BelowRightOrSameOf(p4))
|
||||
assert.True(p3.TopLeftOrSameOf(p4))
|
||||
}
|
||||
|
||||
func TestRectangle(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
tl1 := GeopositionDef{10, 5}
|
||||
br1 := GeopositionDef{5, 10}
|
||||
r1 := GeorectangleDef{tl1, br1}
|
||||
|
||||
tl2 := GeopositionDef{9, 7}
|
||||
br2 := GeopositionDef{7, 9}
|
||||
r2 := GeorectangleDef{tl2, br2}
|
||||
|
||||
tl3 := GeopositionDef{30, 20}
|
||||
br3 := GeopositionDef{20, 30}
|
||||
r3 := GeorectangleDef{tl3, br3}
|
||||
|
||||
assert.True(tl1.TopLeftOf(br2))
|
||||
|
||||
assert.True(r1.IntersectsRect(r2))
|
||||
assert.True(r2.IntersectsRect(r1))
|
||||
assert.True(r2.IntersectsRect(r2))
|
||||
|
||||
assert.True(r1.ContainsRect(r2))
|
||||
assert.False(r2.ContainsRect(r1))
|
||||
assert.True(r2.ContainsRect(r2))
|
||||
|
||||
assert.False(r1.IntersectsRect(r3))
|
||||
assert.False(r1.ContainsRect(r3))
|
||||
|
||||
assert.True(r1.ContainsPoint(tl2))
|
||||
assert.False(r1.ContainsPoint(br3))
|
||||
|
||||
tlRect, blRect, trRect, brRect := r3.Split()
|
||||
tl0 := GeopositionDef{30, 20}
|
||||
tm0 := GeopositionDef{30, 25}
|
||||
lm0 := GeopositionDef{25, 20}
|
||||
mm0 := GeopositionDef{25, 25}
|
||||
rm0 := GeopositionDef{25, 30}
|
||||
bm0 := GeopositionDef{20, 25}
|
||||
br0 := GeopositionDef{20, 30}
|
||||
|
||||
topLeft := GeorectangleDef{tl0, mm0}
|
||||
bottomLeft := GeorectangleDef{lm0, bm0}
|
||||
topRight := GeorectangleDef{tm0, rm0}
|
||||
bottomRight := GeorectangleDef{mm0, br0}
|
||||
|
||||
assert.True(tlRect == topLeft)
|
||||
assert.True(blRect == bottomLeft)
|
||||
assert.True(trRect == topRight)
|
||||
assert.True(brRect == bottomRight)
|
||||
}
|
||||
|
||||
func TestDistances(t *testing.T) {
|
||||
p1 := GeopositionDef{37.83, -122.52}
|
||||
p2 := GeopositionDef{37.70, -122.36}
|
||||
|
||||
km1 := p1.DistanceTo(p2)
|
||||
fmt.Printf("distance from %v to %v: %f\n", p1, p2, km1)
|
||||
km2 := p2.DistanceTo(p1)
|
||||
fmt.Printf("distance from %v to %v: %f\n", p2, p1, km2)
|
||||
assert.Equal(t, km1, km2)
|
||||
|
||||
point := GeopositionDef{37.7644008, -122.4511607}
|
||||
kms := 0.9
|
||||
boundingRect := point.BoundingRectangle(kms)
|
||||
fmt.Printf("Point: %v, distance: %f kms, Bounding %v", point, kms, boundingRect)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
geo "github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/datas"
|
||||
"github.com/attic-labs/noms/dataset"
|
||||
@@ -43,7 +44,10 @@ func main() {
|
||||
log.Fatalf("Invalid inputRef: %v", *inputRefStr)
|
||||
}
|
||||
|
||||
gr := GeorectangleDef{GeopositionDef{37.83, -122.52}, GeopositionDef{37.70, -122.36}}
|
||||
gr := geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: 37.83, Longitude: -122.52},
|
||||
BottomRight: geo.GeopositionDef{Latitude: 37.70, Longitude: -122.36},
|
||||
}
|
||||
qtRoot := QuadTreeDef{
|
||||
Nodes: ListOfNodeDef{},
|
||||
Tiles: MapOfStringToQuadTreeDef{},
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
geo "github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
"github.com/attic-labs/noms/clients/util"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/types"
|
||||
@@ -26,26 +27,26 @@ var (
|
||||
)
|
||||
|
||||
// Query returns a slice of Nodes that are a maximum of "kilometers" away from "p"
|
||||
func (qt *QuadTree) Query(p GeopositionDef, kilometers float64) (GeorectangleDef, []Node) {
|
||||
r := p.BoundingRectangle(kilometers)
|
||||
func (qt *QuadTree) Query(p geo.GeopositionDef, kilometers float64) (geo.GeorectangleDef, []Node) {
|
||||
r := util.BoundingRectangle(p, kilometers)
|
||||
nodes := qt.Search(r, p, float32(kilometers))
|
||||
return r, nodes
|
||||
}
|
||||
|
||||
// Search returns a slice of Nodes that are within "r" and a maximum of "kilometers" away from "p"
|
||||
func (qt *QuadTree) Search(r GeorectangleDef, p GeopositionDef, kilometers float32) []Node {
|
||||
func (qt *QuadTree) Search(r geo.GeorectangleDef, p geo.GeopositionDef, kilometers float32) []Node {
|
||||
nodes := []Node{}
|
||||
if qt.Tiles().Len() > 0 {
|
||||
for _, q := range quadrants {
|
||||
tile := qt.Tiles().Get(q)
|
||||
if tile.Georectangle().Def().IntersectsRect(r) {
|
||||
if util.IntersectsRect(tile.Georectangle().Def(), r) {
|
||||
tnodes := tile.Search(r, p, kilometers)
|
||||
nodes = append(nodes, tnodes...)
|
||||
}
|
||||
}
|
||||
} else if qt.Nodes().Len() > 0 {
|
||||
qt.Nodes().Iter(func(n Node, i uint64) bool {
|
||||
if p.DistanceTo(n.Geoposition().Def()) < kilometers {
|
||||
if util.DistanceTo(p, n.Geoposition().Def()) < kilometers {
|
||||
nodes = append(nodes, n)
|
||||
}
|
||||
return false
|
||||
@@ -56,25 +57,25 @@ func (qt *QuadTree) Search(r GeorectangleDef, p GeopositionDef, kilometers float
|
||||
}
|
||||
|
||||
// Query returns a slice of NodeDefs that are a maximum of "kilometers" away from "p"
|
||||
func (qt *QuadTreeDef) Query(p GeopositionDef, kilometers float64) ListOfNodeDef {
|
||||
r := p.BoundingRectangle(kilometers)
|
||||
func (qt *QuadTreeDef) Query(p geo.GeopositionDef, kilometers float64) ListOfNodeDef {
|
||||
r := util.BoundingRectangle(p, kilometers)
|
||||
return qt.Search(r, p, float32(kilometers))
|
||||
}
|
||||
|
||||
// Search returns a slice of NodeDefs that are within "r" and a maximum of "kilometers" away from "p"
|
||||
func (qt QuadTreeDef) Search(r GeorectangleDef, p GeopositionDef, kilometers float32) ListOfNodeDef {
|
||||
func (qt QuadTreeDef) Search(r geo.GeorectangleDef, p geo.GeopositionDef, kilometers float32) ListOfNodeDef {
|
||||
nodes := ListOfNodeDef{}
|
||||
if qt.hasTiles() {
|
||||
for _, q := range quadrants {
|
||||
tile := qt.Tiles[q]
|
||||
if tile.Georectangle.IntersectsRect(r) {
|
||||
if util.IntersectsRect(tile.Georectangle, r) {
|
||||
tnodes := tile.Search(r, p, kilometers)
|
||||
nodes = append(nodes, tnodes...)
|
||||
}
|
||||
}
|
||||
} else if len(qt.Nodes) > 0 {
|
||||
for _, n := range qt.Nodes {
|
||||
if p.DistanceTo(n.Geoposition) < kilometers {
|
||||
if util.DistanceTo(p, n.Geoposition) < kilometers {
|
||||
nodes = append(nodes, n)
|
||||
}
|
||||
}
|
||||
@@ -157,16 +158,16 @@ func (qt *QuadTreeDef) split() {
|
||||
qt.Nodes = ListOfNodeDef{}
|
||||
}
|
||||
|
||||
func (qt *QuadTreeDef) tileContaining(p GeopositionDef) (quadrant string, tile QuadTreeDef) {
|
||||
func (qt *QuadTreeDef) tileContaining(p geo.GeopositionDef) (quadrant string, tile QuadTreeDef) {
|
||||
d.Chk.True(qt.hasTiles(), "tileContaining method called on QuadTree node with no tiles")
|
||||
|
||||
if qt.Tiles[tl].Georectangle.ContainsPoint(p) {
|
||||
if util.ContainsPoint(qt.Tiles[tl].Georectangle, p) {
|
||||
quadrant, tile = tl, qt.Tiles[tl]
|
||||
} else if qt.Tiles[bl].Georectangle.ContainsPoint(p) {
|
||||
} else if util.ContainsPoint(qt.Tiles[bl].Georectangle, p) {
|
||||
quadrant, tile = bl, qt.Tiles[bl]
|
||||
} else if qt.Tiles[tr].Georectangle.ContainsPoint(p) {
|
||||
} else if util.ContainsPoint(qt.Tiles[tr].Georectangle, p) {
|
||||
quadrant, tile = tr, qt.Tiles[tr]
|
||||
} else if qt.Tiles[br].Georectangle.ContainsPoint(p) {
|
||||
} else if util.ContainsPoint(qt.Tiles[br].Georectangle, p) {
|
||||
quadrant, tile = br, qt.Tiles[br]
|
||||
}
|
||||
|
||||
@@ -174,7 +175,7 @@ func (qt *QuadTreeDef) tileContaining(p GeopositionDef) (quadrant string, tile Q
|
||||
}
|
||||
|
||||
// CreateNewQuadTreeDef is a convenience method for creating a new QuadTreeDef.
|
||||
func CreateNewQuadTreeDef(depth uint8, path string, rect GeorectangleDef) QuadTreeDef {
|
||||
func CreateNewQuadTreeDef(depth uint8, path string, rect geo.GeorectangleDef) QuadTreeDef {
|
||||
nodes := make(ListOfNodeDef, 0, maxNodes)
|
||||
qt := QuadTreeDef{
|
||||
Nodes: nodes,
|
||||
@@ -190,7 +191,7 @@ func CreateNewQuadTreeDef(depth uint8, path string, rect GeorectangleDef) QuadTr
|
||||
// makeChildren() handles the dirty work of splitting up the Georectangle in qt
|
||||
// into 4 quadrants
|
||||
func (qt *QuadTreeDef) makeChildren() {
|
||||
tlRect, blRect, trRect, brRect := qt.Georectangle.Split()
|
||||
tlRect, blRect, trRect, brRect := util.Split(qt.Georectangle)
|
||||
qt.Tiles[tl] = CreateNewQuadTreeDef(qt.Depth+1, qt.Path+"a", tlRect)
|
||||
qt.Tiles[bl] = CreateNewQuadTreeDef(qt.Depth+1, qt.Path+"b", blRect)
|
||||
qt.Tiles[tr] = CreateNewQuadTreeDef(qt.Depth+1, qt.Path+"c", trRect)
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package main
|
||||
|
||||
//go:generate go run ../../nomdl/codegen/codegen.go
|
||||
//go:generate go run ../../nomdl/codegen/codegen.go -deps-dir=../gen -ldb=../gen/pkgdb -package-ds=NomsTypePackages
|
||||
|
||||
+38
-227
@@ -4,6 +4,7 @@ package main
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
@@ -17,46 +18,32 @@ func __mainPackageInFile_types_Ref() ref.Ref {
|
||||
p := types.PackageDef{
|
||||
Types: types.ListOfTypeRefDef{
|
||||
|
||||
types.MakeStructTypeRef("Geoposition",
|
||||
[]types.Field{
|
||||
types.Field{"Latitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
types.Field{"Longitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("Georectangle",
|
||||
[]types.Field{
|
||||
types.Field{"TopLeft", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
types.Field{"BottomRight", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("Node",
|
||||
[]types.Field{
|
||||
types.Field{"Geoposition", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
types.Field{"Geoposition", types.MakeTypeRef(ref.Parse("sha1-3bfd4da1c27a6472279b96d731b47e58e8832dee"), 0), false},
|
||||
types.Field{"Reference", types.MakeCompoundTypeRef("", types.RefKind, types.MakePrimitiveTypeRef(types.ValueKind)), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("QuadTree",
|
||||
[]types.Field{
|
||||
types.Field{"Nodes", types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(ref.Ref{}, 2)), false},
|
||||
types.Field{"Tiles", types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeTypeRef(ref.Ref{}, 3)), false},
|
||||
types.Field{"Nodes", types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(ref.Ref{}, 0)), false},
|
||||
types.Field{"Tiles", types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeTypeRef(ref.Ref{}, 1)), false},
|
||||
types.Field{"Depth", types.MakePrimitiveTypeRef(types.UInt8Kind), false},
|
||||
types.Field{"NumDescendents", types.MakePrimitiveTypeRef(types.UInt32Kind), false},
|
||||
types.Field{"Path", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
types.Field{"Georectangle", types.MakeTypeRef(ref.Ref{}, 1), false},
|
||||
types.Field{"Georectangle", types.MakeTypeRef(ref.Parse("sha1-3bfd4da1c27a6472279b96d731b47e58e8832dee"), 1), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("SQuadTree",
|
||||
[]types.Field{
|
||||
types.Field{"Nodes", types.MakeCompoundTypeRef("", types.ListKind, types.MakeCompoundTypeRef("", types.RefKind, types.MakePrimitiveTypeRef(types.ValueKind))), false},
|
||||
types.Field{"Tiles", types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(ref.Ref{}, 4))), false},
|
||||
types.Field{"Tiles", types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(ref.Ref{}, 2))), false},
|
||||
types.Field{"Depth", types.MakePrimitiveTypeRef(types.UInt8Kind), false},
|
||||
types.Field{"NumDescendents", types.MakePrimitiveTypeRef(types.UInt32Kind), false},
|
||||
types.Field{"Path", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
types.Field{"Georectangle", types.MakeTypeRef(ref.Ref{}, 1), false},
|
||||
types.Field{"Georectangle", types.MakeTypeRef(ref.Parse("sha1-3bfd4da1c27a6472279b96d731b47e58e8832dee"), 1), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
@@ -65,182 +52,6 @@ func __mainPackageInFile_types_Ref() ref.Ref {
|
||||
return types.RegisterPackage(&p)
|
||||
}
|
||||
|
||||
// Geoposition
|
||||
|
||||
type Geoposition struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewGeoposition() Geoposition {
|
||||
return Geoposition{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(0),
|
||||
types.NewString("Longitude"), types.Float32(0),
|
||||
)}
|
||||
}
|
||||
|
||||
type GeopositionDef struct {
|
||||
Latitude float32
|
||||
Longitude float32
|
||||
}
|
||||
|
||||
func (def GeopositionDef) New() Geoposition {
|
||||
return Geoposition{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(def.Latitude),
|
||||
types.NewString("Longitude"), types.Float32(def.Longitude),
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Geoposition) Def() (d GeopositionDef) {
|
||||
d.Latitude = float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
d.Longitude = float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForGeoposition = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0)
|
||||
|
||||
func (m Geoposition) TypeRef() types.TypeRef {
|
||||
return __typeRefForGeoposition
|
||||
}
|
||||
|
||||
func init() {
|
||||
types.RegisterFromValFunction(__typeRefForGeoposition, func(v types.Value) types.NomsValue {
|
||||
return GeopositionFromVal(v)
|
||||
})
|
||||
}
|
||||
|
||||
func GeopositionFromVal(val types.Value) Geoposition {
|
||||
// TODO: Validate here
|
||||
return Geoposition{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (s Geoposition) NomsValue() types.Value {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s Geoposition) Equals(other types.Value) bool {
|
||||
if other, ok := other.(Geoposition); ok {
|
||||
return s.m.Equals(other.m)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Geoposition) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s Geoposition) Chunks() (futures []types.Future) {
|
||||
futures = append(futures, s.TypeRef().Chunks()...)
|
||||
futures = append(futures, s.m.Chunks()...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Geoposition) Latitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLatitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Latitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
func (s Geoposition) Longitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLongitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Longitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
// Georectangle
|
||||
|
||||
type Georectangle struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewGeorectangle() Georectangle {
|
||||
return Georectangle{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("TopLeft"), NewGeoposition().NomsValue(),
|
||||
types.NewString("BottomRight"), NewGeoposition().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
type GeorectangleDef struct {
|
||||
TopLeft GeopositionDef
|
||||
BottomRight GeopositionDef
|
||||
}
|
||||
|
||||
func (def GeorectangleDef) New() Georectangle {
|
||||
return Georectangle{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("TopLeft"), def.TopLeft.New().NomsValue(),
|
||||
types.NewString("BottomRight"), def.BottomRight.New().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Georectangle) Def() (d GeorectangleDef) {
|
||||
d.TopLeft = GeopositionFromVal(s.m.Get(types.NewString("TopLeft"))).Def()
|
||||
d.BottomRight = GeopositionFromVal(s.m.Get(types.NewString("BottomRight"))).Def()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForGeorectangle = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1)
|
||||
|
||||
func (m Georectangle) TypeRef() types.TypeRef {
|
||||
return __typeRefForGeorectangle
|
||||
}
|
||||
|
||||
func init() {
|
||||
types.RegisterFromValFunction(__typeRefForGeorectangle, func(v types.Value) types.NomsValue {
|
||||
return GeorectangleFromVal(v)
|
||||
})
|
||||
}
|
||||
|
||||
func GeorectangleFromVal(val types.Value) Georectangle {
|
||||
// TODO: Validate here
|
||||
return Georectangle{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (s Georectangle) NomsValue() types.Value {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s Georectangle) Equals(other types.Value) bool {
|
||||
if other, ok := other.(Georectangle); ok {
|
||||
return s.m.Equals(other.m)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Georectangle) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s Georectangle) Chunks() (futures []types.Future) {
|
||||
futures = append(futures, s.TypeRef().Chunks()...)
|
||||
futures = append(futures, s.m.Chunks()...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Georectangle) TopLeft() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("TopLeft")))
|
||||
}
|
||||
|
||||
func (s Georectangle) SetTopLeft(val Geoposition) Georectangle {
|
||||
return Georectangle{s.m.Set(types.NewString("TopLeft"), val.NomsValue())}
|
||||
}
|
||||
|
||||
func (s Georectangle) BottomRight() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("BottomRight")))
|
||||
}
|
||||
|
||||
func (s Georectangle) SetBottomRight(val Geoposition) Georectangle {
|
||||
return Georectangle{s.m.Set(types.NewString("BottomRight"), val.NomsValue())}
|
||||
}
|
||||
|
||||
// Node
|
||||
|
||||
type Node struct {
|
||||
@@ -249,33 +60,33 @@ type Node struct {
|
||||
|
||||
func NewNode() Node {
|
||||
return Node{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2),
|
||||
types.NewString("Geoposition"), NewGeoposition().NomsValue(),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Geoposition"), sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.NewGeoposition().NomsValue(),
|
||||
types.NewString("Reference"), types.Ref{R: ref.Ref{}},
|
||||
)}
|
||||
}
|
||||
|
||||
type NodeDef struct {
|
||||
Geoposition GeopositionDef
|
||||
Geoposition sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionDef
|
||||
Reference ref.Ref
|
||||
}
|
||||
|
||||
func (def NodeDef) New() Node {
|
||||
return Node{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Geoposition"), def.Geoposition.New().NomsValue(),
|
||||
types.NewString("Reference"), types.Ref{R: def.Reference},
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Node) Def() (d NodeDef) {
|
||||
d.Geoposition = GeopositionFromVal(s.m.Get(types.NewString("Geoposition"))).Def()
|
||||
d.Geoposition = sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionFromVal(s.m.Get(types.NewString("Geoposition"))).Def()
|
||||
d.Reference = s.m.Get(types.NewString("Reference")).Ref()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForNode = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2)
|
||||
var __typeRefForNode = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0)
|
||||
|
||||
func (m Node) TypeRef() types.TypeRef {
|
||||
return __typeRefForNode
|
||||
@@ -313,11 +124,11 @@ func (s Node) Chunks() (futures []types.Future) {
|
||||
return
|
||||
}
|
||||
|
||||
func (s Node) Geoposition() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("Geoposition")))
|
||||
func (s Node) Geoposition() sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Geoposition {
|
||||
return sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionFromVal(s.m.Get(types.NewString("Geoposition")))
|
||||
}
|
||||
|
||||
func (s Node) SetGeoposition(val Geoposition) Node {
|
||||
func (s Node) SetGeoposition(val sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Geoposition) Node {
|
||||
return Node{s.m.Set(types.NewString("Geoposition"), val.NomsValue())}
|
||||
}
|
||||
|
||||
@@ -337,13 +148,13 @@ type QuadTree struct {
|
||||
|
||||
func NewQuadTree() QuadTree {
|
||||
return QuadTree{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 3),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("Nodes"), types.NewList(),
|
||||
types.NewString("Tiles"), types.NewMap(),
|
||||
types.NewString("Depth"), types.UInt8(0),
|
||||
types.NewString("NumDescendents"), types.UInt32(0),
|
||||
types.NewString("Path"), types.NewString(""),
|
||||
types.NewString("Georectangle"), NewGeorectangle().NomsValue(),
|
||||
types.NewString("Georectangle"), sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.NewGeorectangle().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
@@ -353,13 +164,13 @@ type QuadTreeDef struct {
|
||||
Depth uint8
|
||||
NumDescendents uint32
|
||||
Path string
|
||||
Georectangle GeorectangleDef
|
||||
Georectangle sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleDef
|
||||
}
|
||||
|
||||
func (def QuadTreeDef) New() QuadTree {
|
||||
return QuadTree{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 3),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("Nodes"), def.Nodes.New().NomsValue(),
|
||||
types.NewString("Tiles"), def.Tiles.New().NomsValue(),
|
||||
types.NewString("Depth"), types.UInt8(def.Depth),
|
||||
@@ -375,11 +186,11 @@ func (s QuadTree) Def() (d QuadTreeDef) {
|
||||
d.Depth = uint8(s.m.Get(types.NewString("Depth")).(types.UInt8))
|
||||
d.NumDescendents = uint32(s.m.Get(types.NewString("NumDescendents")).(types.UInt32))
|
||||
d.Path = s.m.Get(types.NewString("Path")).(types.String).String()
|
||||
d.Georectangle = GeorectangleFromVal(s.m.Get(types.NewString("Georectangle"))).Def()
|
||||
d.Georectangle = sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleFromVal(s.m.Get(types.NewString("Georectangle"))).Def()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForQuadTree = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 3)
|
||||
var __typeRefForQuadTree = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1)
|
||||
|
||||
func (m QuadTree) TypeRef() types.TypeRef {
|
||||
return __typeRefForQuadTree
|
||||
@@ -457,11 +268,11 @@ func (s QuadTree) SetPath(val string) QuadTree {
|
||||
return QuadTree{s.m.Set(types.NewString("Path"), types.NewString(val))}
|
||||
}
|
||||
|
||||
func (s QuadTree) Georectangle() Georectangle {
|
||||
return GeorectangleFromVal(s.m.Get(types.NewString("Georectangle")))
|
||||
func (s QuadTree) Georectangle() sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Georectangle {
|
||||
return sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleFromVal(s.m.Get(types.NewString("Georectangle")))
|
||||
}
|
||||
|
||||
func (s QuadTree) SetGeorectangle(val Georectangle) QuadTree {
|
||||
func (s QuadTree) SetGeorectangle(val sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Georectangle) QuadTree {
|
||||
return QuadTree{s.m.Set(types.NewString("Georectangle"), val.NomsValue())}
|
||||
}
|
||||
|
||||
@@ -473,13 +284,13 @@ type SQuadTree struct {
|
||||
|
||||
func NewSQuadTree() SQuadTree {
|
||||
return SQuadTree{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 4),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2),
|
||||
types.NewString("Nodes"), types.NewList(),
|
||||
types.NewString("Tiles"), types.NewMap(),
|
||||
types.NewString("Depth"), types.UInt8(0),
|
||||
types.NewString("NumDescendents"), types.UInt32(0),
|
||||
types.NewString("Path"), types.NewString(""),
|
||||
types.NewString("Georectangle"), NewGeorectangle().NomsValue(),
|
||||
types.NewString("Georectangle"), sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.NewGeorectangle().NomsValue(),
|
||||
)}
|
||||
}
|
||||
|
||||
@@ -489,13 +300,13 @@ type SQuadTreeDef struct {
|
||||
Depth uint8
|
||||
NumDescendents uint32
|
||||
Path string
|
||||
Georectangle GeorectangleDef
|
||||
Georectangle sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleDef
|
||||
}
|
||||
|
||||
func (def SQuadTreeDef) New() SQuadTree {
|
||||
return SQuadTree{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 4),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2),
|
||||
types.NewString("Nodes"), def.Nodes.New().NomsValue(),
|
||||
types.NewString("Tiles"), def.Tiles.New().NomsValue(),
|
||||
types.NewString("Depth"), types.UInt8(def.Depth),
|
||||
@@ -511,11 +322,11 @@ func (s SQuadTree) Def() (d SQuadTreeDef) {
|
||||
d.Depth = uint8(s.m.Get(types.NewString("Depth")).(types.UInt8))
|
||||
d.NumDescendents = uint32(s.m.Get(types.NewString("NumDescendents")).(types.UInt32))
|
||||
d.Path = s.m.Get(types.NewString("Path")).(types.String).String()
|
||||
d.Georectangle = GeorectangleFromVal(s.m.Get(types.NewString("Georectangle"))).Def()
|
||||
d.Georectangle = sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleFromVal(s.m.Get(types.NewString("Georectangle"))).Def()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForSQuadTree = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 4)
|
||||
var __typeRefForSQuadTree = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2)
|
||||
|
||||
func (m SQuadTree) TypeRef() types.TypeRef {
|
||||
return __typeRefForSQuadTree
|
||||
@@ -593,11 +404,11 @@ func (s SQuadTree) SetPath(val string) SQuadTree {
|
||||
return SQuadTree{s.m.Set(types.NewString("Path"), types.NewString(val))}
|
||||
}
|
||||
|
||||
func (s SQuadTree) Georectangle() Georectangle {
|
||||
return GeorectangleFromVal(s.m.Get(types.NewString("Georectangle")))
|
||||
func (s SQuadTree) Georectangle() sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Georectangle {
|
||||
return sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeorectangleFromVal(s.m.Get(types.NewString("Georectangle")))
|
||||
}
|
||||
|
||||
func (s SQuadTree) SetGeorectangle(val Georectangle) SQuadTree {
|
||||
func (s SQuadTree) SetGeorectangle(val sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Georectangle) SQuadTree {
|
||||
return SQuadTree{s.m.Set(types.NewString("Georectangle"), val.NomsValue())}
|
||||
}
|
||||
|
||||
@@ -719,7 +530,7 @@ func (m ListOfNode) TypeRef() types.TypeRef {
|
||||
}
|
||||
|
||||
func init() {
|
||||
__typeRefForListOfNode = types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2))
|
||||
__typeRefForListOfNode = types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0))
|
||||
types.RegisterFromValFunction(__typeRefForListOfNode, func(v types.Value) types.NomsValue {
|
||||
return ListOfNodeFromVal(v)
|
||||
})
|
||||
@@ -860,7 +671,7 @@ func (m MapOfStringToQuadTree) TypeRef() types.TypeRef {
|
||||
}
|
||||
|
||||
func init() {
|
||||
__typeRefForMapOfStringToQuadTree = types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 3))
|
||||
__typeRefForMapOfStringToQuadTree = types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1))
|
||||
types.RegisterFromValFunction(__typeRefForMapOfStringToQuadTree, func(v types.Value) types.NomsValue {
|
||||
return MapOfStringToQuadTreeFromVal(v)
|
||||
})
|
||||
@@ -1131,7 +942,7 @@ func (m MapOfStringToRefOfSQuadTree) TypeRef() types.TypeRef {
|
||||
}
|
||||
|
||||
func init() {
|
||||
__typeRefForMapOfStringToRefOfSQuadTree = types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 4)))
|
||||
__typeRefForMapOfStringToRefOfSQuadTree = types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2)))
|
||||
types.RegisterFromValFunction(__typeRefForMapOfStringToRefOfSQuadTree, func(v types.Value) types.NomsValue {
|
||||
return MapOfStringToRefOfSQuadTreeFromVal(v)
|
||||
})
|
||||
@@ -1240,7 +1051,7 @@ func (m RefOfSQuadTree) TypeRef() types.TypeRef {
|
||||
}
|
||||
|
||||
func init() {
|
||||
__typeRefForRefOfSQuadTree = types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 4))
|
||||
__typeRefForRefOfSQuadTree = types.MakeCompoundTypeRef("", types.RefKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 2))
|
||||
types.RegisterFromValFunction(__typeRefForRefOfSQuadTree, func(v types.Value) types.NomsValue {
|
||||
return RefOfSQuadTreeFromVal(v)
|
||||
})
|
||||
|
||||
@@ -1,15 +1,7 @@
|
||||
struct Geoposition {
|
||||
Latitude: Float32
|
||||
Longitude: Float32
|
||||
}
|
||||
|
||||
struct Georectangle {
|
||||
TopLeft: Geoposition
|
||||
BottomRight: Geoposition
|
||||
}
|
||||
alias Geo = import "../util/geotypes.noms"
|
||||
|
||||
struct Node {
|
||||
Geoposition: Geoposition
|
||||
Geoposition: Geo.Geoposition
|
||||
Reference: Ref(Value)
|
||||
}
|
||||
|
||||
@@ -19,7 +11,7 @@ struct QuadTree {
|
||||
Depth: UInt8
|
||||
NumDescendents: UInt32
|
||||
Path: String
|
||||
Georectangle: Georectangle
|
||||
Georectangle: Geo.Georectangle
|
||||
}
|
||||
|
||||
struct SQuadTree {
|
||||
@@ -28,5 +20,5 @@ struct SQuadTree {
|
||||
Depth: UInt8
|
||||
NumDescendents: UInt32
|
||||
Path: String
|
||||
Georectangle: Georectangle
|
||||
Georectangle: Geo.Georectangle
|
||||
}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package main
|
||||
|
||||
//go:generate go run ../../nomdl/codegen/codegen.go
|
||||
//go:generate go run ../../nomdl/codegen/codegen.go -deps-dir=../gen -ldb=../gen/pkgdb -package-ds=NomsTypePackages
|
||||
|
||||
@@ -4,11 +4,6 @@ import (
|
||||
"encoding/csv"
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
"github.com/attic-labs/noms/clients/util"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/dataset"
|
||||
"github.com/attic-labs/noms/types"
|
||||
"math"
|
||||
"os"
|
||||
"runtime"
|
||||
@@ -16,6 +11,13 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/attic-labs/noms/chunks"
|
||||
geo "github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
"github.com/attic-labs/noms/clients/util"
|
||||
"github.com/attic-labs/noms/d"
|
||||
"github.com/attic-labs/noms/dataset"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
|
||||
// data can be obtained using:
|
||||
@@ -105,7 +107,7 @@ func main() {
|
||||
id, _ := strconv.ParseInt(r[0], 10, 64)
|
||||
lon64, _ := strconv.ParseFloat(r[9], 32)
|
||||
lat64, _ := strconv.ParseFloat(r[10], 32)
|
||||
geopos := GeopositionDef{Latitude: float32(lat64), Longitude: float32(lon64)}
|
||||
geopos := geo.GeopositionDef{Latitude: float32(lat64), Longitude: float32(lon64)}
|
||||
incident := IncidentDef{
|
||||
ID: id,
|
||||
Category: r[1],
|
||||
@@ -204,10 +206,6 @@ func max(x, y float32) float32 {
|
||||
return float32(math.Max(float64(x), float64(y)))
|
||||
}
|
||||
|
||||
func (p Geoposition) String() string {
|
||||
return fmt.Sprintf("Geoposition(lat: %f, lon: %f", p.Latitude(), p.Longitude())
|
||||
}
|
||||
|
||||
func toFixedFloor(num float32, precision int) float32 {
|
||||
output := math.Pow(10, float64(precision))
|
||||
return float32(math.Floor(float64(num)*output) / output)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
"github.com/attic-labs/noms/ref"
|
||||
"github.com/attic-labs/noms/types"
|
||||
)
|
||||
@@ -16,13 +17,6 @@ func __mainPackageInFile_types_Ref() ref.Ref {
|
||||
p := types.PackageDef{
|
||||
Types: types.ListOfTypeRefDef{
|
||||
|
||||
types.MakeStructTypeRef("Geoposition",
|
||||
[]types.Field{
|
||||
types.Field{"Latitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
types.Field{"Longitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
|
||||
},
|
||||
types.Choices{},
|
||||
),
|
||||
types.MakeStructTypeRef("Incident",
|
||||
[]types.Field{
|
||||
types.Field{"ID", types.MakePrimitiveTypeRef(types.Int64Kind), false},
|
||||
@@ -34,7 +28,7 @@ func __mainPackageInFile_types_Ref() ref.Ref {
|
||||
types.Field{"PdDistrict", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
types.Field{"Resolution", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
types.Field{"Address", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
types.Field{"Geoposition", types.MakeTypeRef(ref.Ref{}, 0), false},
|
||||
types.Field{"Geoposition", types.MakeTypeRef(ref.Parse("sha1-3bfd4da1c27a6472279b96d731b47e58e8832dee"), 0), false},
|
||||
types.Field{"PdID", types.MakePrimitiveTypeRef(types.StringKind), false},
|
||||
},
|
||||
types.Choices{},
|
||||
@@ -44,94 +38,6 @@ func __mainPackageInFile_types_Ref() ref.Ref {
|
||||
return types.RegisterPackage(&p)
|
||||
}
|
||||
|
||||
// Geoposition
|
||||
|
||||
type Geoposition struct {
|
||||
m types.Map
|
||||
}
|
||||
|
||||
func NewGeoposition() Geoposition {
|
||||
return Geoposition{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(0),
|
||||
types.NewString("Longitude"), types.Float32(0),
|
||||
)}
|
||||
}
|
||||
|
||||
type GeopositionDef struct {
|
||||
Latitude float32
|
||||
Longitude float32
|
||||
}
|
||||
|
||||
func (def GeopositionDef) New() Geoposition {
|
||||
return Geoposition{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("Latitude"), types.Float32(def.Latitude),
|
||||
types.NewString("Longitude"), types.Float32(def.Longitude),
|
||||
)}
|
||||
}
|
||||
|
||||
func (s Geoposition) Def() (d GeopositionDef) {
|
||||
d.Latitude = float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
d.Longitude = float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForGeoposition = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0)
|
||||
|
||||
func (m Geoposition) TypeRef() types.TypeRef {
|
||||
return __typeRefForGeoposition
|
||||
}
|
||||
|
||||
func init() {
|
||||
types.RegisterFromValFunction(__typeRefForGeoposition, func(v types.Value) types.NomsValue {
|
||||
return GeopositionFromVal(v)
|
||||
})
|
||||
}
|
||||
|
||||
func GeopositionFromVal(val types.Value) Geoposition {
|
||||
// TODO: Validate here
|
||||
return Geoposition{val.(types.Map)}
|
||||
}
|
||||
|
||||
func (s Geoposition) NomsValue() types.Value {
|
||||
return s.m
|
||||
}
|
||||
|
||||
func (s Geoposition) Equals(other types.Value) bool {
|
||||
if other, ok := other.(Geoposition); ok {
|
||||
return s.m.Equals(other.m)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s Geoposition) Ref() ref.Ref {
|
||||
return s.m.Ref()
|
||||
}
|
||||
|
||||
func (s Geoposition) Chunks() (futures []types.Future) {
|
||||
futures = append(futures, s.TypeRef().Chunks()...)
|
||||
futures = append(futures, s.m.Chunks()...)
|
||||
return
|
||||
}
|
||||
|
||||
func (s Geoposition) Latitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Latitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLatitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Latitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
func (s Geoposition) Longitude() float32 {
|
||||
return float32(s.m.Get(types.NewString("Longitude")).(types.Float32))
|
||||
}
|
||||
|
||||
func (s Geoposition) SetLongitude(val float32) Geoposition {
|
||||
return Geoposition{s.m.Set(types.NewString("Longitude"), types.Float32(val))}
|
||||
}
|
||||
|
||||
// Incident
|
||||
|
||||
type Incident struct {
|
||||
@@ -140,7 +46,7 @@ type Incident struct {
|
||||
|
||||
func NewIncident() Incident {
|
||||
return Incident{types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("ID"), types.Int64(0),
|
||||
types.NewString("Category"), types.NewString(""),
|
||||
types.NewString("Description"), types.NewString(""),
|
||||
@@ -150,7 +56,7 @@ func NewIncident() Incident {
|
||||
types.NewString("PdDistrict"), types.NewString(""),
|
||||
types.NewString("Resolution"), types.NewString(""),
|
||||
types.NewString("Address"), types.NewString(""),
|
||||
types.NewString("Geoposition"), NewGeoposition().NomsValue(),
|
||||
types.NewString("Geoposition"), sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.NewGeoposition().NomsValue(),
|
||||
types.NewString("PdID"), types.NewString(""),
|
||||
)}
|
||||
}
|
||||
@@ -165,14 +71,14 @@ type IncidentDef struct {
|
||||
PdDistrict string
|
||||
Resolution string
|
||||
Address string
|
||||
Geoposition GeopositionDef
|
||||
Geoposition sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionDef
|
||||
PdID string
|
||||
}
|
||||
|
||||
func (def IncidentDef) New() Incident {
|
||||
return Incident{
|
||||
types.NewMap(
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1),
|
||||
types.NewString("$type"), types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0),
|
||||
types.NewString("ID"), types.Int64(def.ID),
|
||||
types.NewString("Category"), types.NewString(def.Category),
|
||||
types.NewString("Description"), types.NewString(def.Description),
|
||||
@@ -197,12 +103,12 @@ func (s Incident) Def() (d IncidentDef) {
|
||||
d.PdDistrict = s.m.Get(types.NewString("PdDistrict")).(types.String).String()
|
||||
d.Resolution = s.m.Get(types.NewString("Resolution")).(types.String).String()
|
||||
d.Address = s.m.Get(types.NewString("Address")).(types.String).String()
|
||||
d.Geoposition = GeopositionFromVal(s.m.Get(types.NewString("Geoposition"))).Def()
|
||||
d.Geoposition = sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionFromVal(s.m.Get(types.NewString("Geoposition"))).Def()
|
||||
d.PdID = s.m.Get(types.NewString("PdID")).(types.String).String()
|
||||
return
|
||||
}
|
||||
|
||||
var __typeRefForIncident = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1)
|
||||
var __typeRefForIncident = types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0)
|
||||
|
||||
func (m Incident) TypeRef() types.TypeRef {
|
||||
return __typeRefForIncident
|
||||
@@ -312,11 +218,11 @@ func (s Incident) SetAddress(val string) Incident {
|
||||
return Incident{s.m.Set(types.NewString("Address"), types.NewString(val))}
|
||||
}
|
||||
|
||||
func (s Incident) Geoposition() Geoposition {
|
||||
return GeopositionFromVal(s.m.Get(types.NewString("Geoposition")))
|
||||
func (s Incident) Geoposition() sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Geoposition {
|
||||
return sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.GeopositionFromVal(s.m.Get(types.NewString("Geoposition")))
|
||||
}
|
||||
|
||||
func (s Incident) SetGeoposition(val Geoposition) Incident {
|
||||
func (s Incident) SetGeoposition(val sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee.Geoposition) Incident {
|
||||
return Incident{s.m.Set(types.NewString("Geoposition"), val.NomsValue())}
|
||||
}
|
||||
|
||||
@@ -390,7 +296,7 @@ func (m ListOfIncident) TypeRef() types.TypeRef {
|
||||
}
|
||||
|
||||
func init() {
|
||||
__typeRefForListOfIncident = types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 1))
|
||||
__typeRefForListOfIncident = types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef(__mainPackageInFile_types_CachedRef, 0))
|
||||
types.RegisterFromValFunction(__typeRefForListOfIncident, func(v types.Value) types.NomsValue {
|
||||
return ListOfIncidentFromVal(v)
|
||||
})
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
struct Geoposition {
|
||||
Latitude: Float32
|
||||
Longitude: Float32
|
||||
}
|
||||
alias geo = import "../util/geotypes.noms"
|
||||
|
||||
struct Incident {
|
||||
ID: Int64
|
||||
@@ -13,7 +10,7 @@ struct Incident {
|
||||
PdDistrict: String
|
||||
Resolution: String
|
||||
Address: String
|
||||
Geoposition: Geoposition
|
||||
Geoposition: geo.Geoposition
|
||||
PdID: String
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
geo "github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
)
|
||||
|
||||
const (
|
||||
// EarthRadius the Earth's radius is about 6,371km according to Wikipedia
|
||||
EarthRadius = 6371
|
||||
)
|
||||
|
||||
// Latitude64 casts the Latitude value to a float64 for convenience.
|
||||
func Latitude64(p geo.GeopositionDef) float64 {
|
||||
return float64(p.Latitude)
|
||||
}
|
||||
|
||||
// Longitude64 casts the Longitude value to a float64 for convenience.
|
||||
func Longitude64(p geo.GeopositionDef) float64 {
|
||||
return float64(p.Longitude)
|
||||
}
|
||||
|
||||
// TopLeftOf returns true if it is above and to the left of "o"
|
||||
func TopLeftOf(p geo.GeopositionDef, o geo.GeopositionDef) bool {
|
||||
return p.Latitude > o.Latitude && p.Longitude < o.Longitude
|
||||
}
|
||||
|
||||
// BelowRightOf returns true if it's below and to the right of "o"
|
||||
func BelowRightOf(p geo.GeopositionDef, o geo.GeopositionDef) bool {
|
||||
return p.Latitude < o.Latitude && p.Longitude > o.Longitude
|
||||
}
|
||||
|
||||
// TopLeftOrSameOf returns true if it is above and to the left of "o"
|
||||
func TopLeftOrSameOf(p geo.GeopositionDef, o geo.GeopositionDef) bool {
|
||||
return p.Latitude >= o.Latitude && p.Longitude <= o.Longitude
|
||||
}
|
||||
|
||||
// BelowRightOrSameOf returns true if it's below and to the right of "o"
|
||||
func BelowRightOrSameOf(p geo.GeopositionDef, o geo.GeopositionDef) bool {
|
||||
return p.Latitude <= o.Latitude && p.Longitude >= o.Longitude
|
||||
}
|
||||
|
||||
// PointAtDistanceAndBearing returns a Point populated with the lat and lng coordinates
|
||||
// by transposing the origin point the passed in distance (in kilometers)
|
||||
// by the passed in compass bearing (in degrees).
|
||||
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
|
||||
func PointAtDistanceAndBearing(p geo.GeopositionDef, dist float64, bearing float64) *geo.GeopositionDef {
|
||||
dr := dist / EarthRadius
|
||||
|
||||
bearing = bearing * (math.Pi / 180.0)
|
||||
|
||||
lat1 := Latitude64(p) * (math.Pi / 180.0)
|
||||
lng1 := Longitude64(p) * (math.Pi / 180.0)
|
||||
|
||||
lat2Part1 := math.Sin(lat1) * math.Cos(dr)
|
||||
lat2Part2 := math.Cos(lat1) * math.Sin(dr) * math.Cos(bearing)
|
||||
|
||||
lat2 := math.Asin(lat2Part1 + lat2Part2)
|
||||
|
||||
lng2Part1 := math.Sin(bearing) * math.Sin(dr) * math.Cos(lat1)
|
||||
lng2Part2 := math.Cos(dr) - (math.Sin(lat1) * math.Sin(lat2))
|
||||
|
||||
lng2 := lng1 + math.Atan2(lng2Part1, lng2Part2)
|
||||
lng2 = math.Mod((lng2+3*math.Pi), (2*math.Pi)) - math.Pi
|
||||
|
||||
lat2 = lat2 * (180.0 / math.Pi)
|
||||
lng2 = lng2 * (180.0 / math.Pi)
|
||||
|
||||
return &geo.GeopositionDef{Latitude: float32(lat2), Longitude: float32(lng2)}
|
||||
}
|
||||
|
||||
// DistanceTo calculates the Haversine distance between two points in kilometers.
|
||||
// Original Implementation from: http://www.movable-type.co.uk/scripts/latlong.html
|
||||
func DistanceTo(p geo.GeopositionDef, o geo.GeopositionDef) float32 {
|
||||
dLat := (Latitude64(o) - Latitude64(p)) * (math.Pi / 180.0)
|
||||
dLon := (Longitude64(o) - Longitude64(p)) * (math.Pi / 180.0)
|
||||
|
||||
lat1 := Latitude64(p) * (math.Pi / 180.0)
|
||||
lat2 := Latitude64(o) * (math.Pi / 180.0)
|
||||
|
||||
a1 := math.Sin(dLat/2) * math.Sin(dLat/2)
|
||||
a2 := math.Sin(dLon/2) * math.Sin(dLon/2) * math.Cos(lat1) * math.Cos(lat2)
|
||||
|
||||
a := a1 + a2
|
||||
|
||||
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
|
||||
|
||||
return float32(EarthRadius * c)
|
||||
}
|
||||
|
||||
// BoundingRectangle calculates a rectangle whose sides east/west and north/south.
|
||||
// The center of the rectangle is "p" and each side is "2 * radius" in length.
|
||||
// A radius value of 1 equals 1 kilometer.
|
||||
func BoundingRectangle(p geo.GeopositionDef, radius float64) geo.GeorectangleDef {
|
||||
northPoint := PointAtDistanceAndBearing(p, radius, 0)
|
||||
eastPoint := PointAtDistanceAndBearing(p, radius, 90)
|
||||
southPoint := PointAtDistanceAndBearing(p, radius, 180)
|
||||
westPoint := PointAtDistanceAndBearing(p, radius, 270)
|
||||
|
||||
return geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: northPoint.Latitude, Longitude: westPoint.Longitude},
|
||||
BottomRight: geo.GeopositionDef{Latitude: southPoint.Latitude, Longitude: eastPoint.Longitude},
|
||||
}
|
||||
}
|
||||
|
||||
// IntersectsRect returns true if "o" intersects r
|
||||
func IntersectsRect(r geo.GeorectangleDef, o geo.GeorectangleDef) bool {
|
||||
return TopLeftOf(r.TopLeft, o.BottomRight) && BelowRightOf(r.BottomRight, o.TopLeft)
|
||||
}
|
||||
|
||||
// ContainsRect returns true if "o" is within r
|
||||
func ContainsRect(r geo.GeorectangleDef, o geo.GeorectangleDef) bool {
|
||||
return TopLeftOrSameOf(r.TopLeft, o.TopLeft) && BelowRightOrSameOf(r.BottomRight, o.BottomRight)
|
||||
}
|
||||
|
||||
// ContainsPoint returns true if p is contained in r
|
||||
func ContainsPoint(r geo.GeorectangleDef, p geo.GeopositionDef) bool {
|
||||
return TopLeftOrSameOf(r.TopLeft, p) && BelowRightOf(r.BottomRight, p)
|
||||
}
|
||||
|
||||
// Split creates one new rectangle for each quadrant in "r"
|
||||
func Split(r geo.GeorectangleDef) (tlRect, blRect, trRect, brRect geo.GeorectangleDef) {
|
||||
maxLat := r.TopLeft.Latitude
|
||||
minLon := r.TopLeft.Longitude
|
||||
minLat := r.BottomRight.Latitude
|
||||
maxLon := r.BottomRight.Longitude
|
||||
midLat := ((maxLat - minLat) / 2) + minLat
|
||||
midLon := ((maxLon - minLon) / 2) + minLon
|
||||
|
||||
tlRect = geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: maxLat, Longitude: minLon},
|
||||
BottomRight: geo.GeopositionDef{Latitude: midLat, Longitude: midLon},
|
||||
}
|
||||
blRect = geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: midLat, Longitude: minLon},
|
||||
BottomRight: geo.GeopositionDef{Latitude: minLat, Longitude: midLon},
|
||||
}
|
||||
trRect = geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: maxLat, Longitude: midLon},
|
||||
BottomRight: geo.GeopositionDef{Latitude: midLat, Longitude: maxLon},
|
||||
}
|
||||
brRect = geo.GeorectangleDef{
|
||||
TopLeft: geo.GeopositionDef{Latitude: midLat, Longitude: midLon},
|
||||
BottomRight: geo.GeopositionDef{Latitude: minLat, Longitude: maxLon},
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/attic-labs/noms/Godeps/_workspace/src/github.com/stretchr/testify/assert"
|
||||
geo "github.com/attic-labs/noms/clients/gen/sha1_3bfd4da1c27a6472279b96d731b47e58e8832dee"
|
||||
)
|
||||
|
||||
func TestPoint(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
p1 := geo.GeopositionDef{5, 5}
|
||||
p2 := geo.GeopositionDef{3, 7}
|
||||
p3 := geo.GeopositionDef{10, 7}
|
||||
p4 := geo.GeopositionDef{10, 12}
|
||||
|
||||
assert.True(TopLeftOf(p1, p2))
|
||||
assert.False(TopLeftOf(p2, p1))
|
||||
assert.True(BelowRightOf(p2, p1))
|
||||
assert.False(BelowRightOf(p1, p2))
|
||||
assert.False(TopLeftOf(p1, p1))
|
||||
assert.False(BelowRightOf(p2, p2))
|
||||
|
||||
assert.True(TopLeftOrSameOf(p3, p2))
|
||||
assert.True(TopLeftOrSameOf(p3, p3))
|
||||
assert.False(TopLeftOrSameOf(p4, p3))
|
||||
assert.True(BelowRightOrSameOf(p2, p3))
|
||||
assert.True(BelowRightOrSameOf(p3, p3))
|
||||
assert.False(BelowRightOrSameOf(p3, p4))
|
||||
assert.True(TopLeftOrSameOf(p3, p4))
|
||||
}
|
||||
|
||||
func TestRectangle(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
tl1 := geo.GeopositionDef{10, 5}
|
||||
br1 := geo.GeopositionDef{5, 10}
|
||||
r1 := geo.GeorectangleDef{tl1, br1}
|
||||
|
||||
tl2 := geo.GeopositionDef{9, 7}
|
||||
br2 := geo.GeopositionDef{7, 9}
|
||||
r2 := geo.GeorectangleDef{tl2, br2}
|
||||
|
||||
tl3 := geo.GeopositionDef{30, 20}
|
||||
br3 := geo.GeopositionDef{20, 30}
|
||||
r3 := geo.GeorectangleDef{tl3, br3}
|
||||
|
||||
assert.True(TopLeftOf(tl1, br2))
|
||||
|
||||
assert.True(IntersectsRect(r1, r2))
|
||||
assert.True(IntersectsRect(r2, r1))
|
||||
assert.True(IntersectsRect(r2, r2))
|
||||
|
||||
assert.True(ContainsRect(r1, r2))
|
||||
assert.False(ContainsRect(r2, r1))
|
||||
assert.True(ContainsRect(r2, r2))
|
||||
|
||||
assert.False(IntersectsRect(r1, r3))
|
||||
assert.False(ContainsRect(r1, r3))
|
||||
|
||||
assert.True(ContainsPoint(r1, tl2))
|
||||
assert.False(ContainsPoint(r1, br3))
|
||||
|
||||
tlRect, blRect, trRect, brRect := Split(r3)
|
||||
tl0 := geo.GeopositionDef{30, 20}
|
||||
tm0 := geo.GeopositionDef{30, 25}
|
||||
lm0 := geo.GeopositionDef{25, 20}
|
||||
mm0 := geo.GeopositionDef{25, 25}
|
||||
rm0 := geo.GeopositionDef{25, 30}
|
||||
bm0 := geo.GeopositionDef{20, 25}
|
||||
br0 := geo.GeopositionDef{20, 30}
|
||||
|
||||
topLeft := geo.GeorectangleDef{tl0, mm0}
|
||||
bottomLeft := geo.GeorectangleDef{lm0, bm0}
|
||||
topRight := geo.GeorectangleDef{tm0, rm0}
|
||||
bottomRight := geo.GeorectangleDef{mm0, br0}
|
||||
|
||||
assert.True(tlRect == topLeft)
|
||||
assert.True(blRect == bottomLeft)
|
||||
assert.True(trRect == topRight)
|
||||
assert.True(brRect == bottomRight)
|
||||
}
|
||||
|
||||
func TestDistances(t *testing.T) {
|
||||
p1 := geo.GeopositionDef{37.83, -122.52}
|
||||
p2 := geo.GeopositionDef{37.70, -122.36}
|
||||
|
||||
km1 := DistanceTo(p1, p2)
|
||||
fmt.Printf("distance from %v to %v: %f\n", p1, p2, km1)
|
||||
km2 := DistanceTo(p2, p1)
|
||||
fmt.Printf("distance from %v to %v: %f\n", p2, p1, km2)
|
||||
assert.Equal(t, km1, km2)
|
||||
|
||||
point := geo.GeopositionDef{37.7644008, -122.4511607}
|
||||
kms := 0.9
|
||||
boundingRect := BoundingRectangle(point, kms)
|
||||
fmt.Printf("Point: %v, distance: %f kms, Bounding %v", point, kms, boundingRect)
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
struct Geoposition {
|
||||
Latitude: Float32
|
||||
Longitude: Float32
|
||||
}
|
||||
|
||||
struct Georectangle {
|
||||
TopLeft: Geoposition
|
||||
BottomRight: Geoposition
|
||||
}
|
||||
@@ -4,8 +4,6 @@ package test
|
||||
|
||||
//go:generate go run ../codegen.go -deps-dir=gen -ldb=/tmp/depGenTest -package-ds=testDeps -in=../testDeps/leafDep/leafDep.noms -out=../testDeps/leafDep/leafDep.go
|
||||
|
||||
//go:generate go run ../codegen.go -deps-dir=gen -ldb=/tmp/depGenTest -package-ds=testDeps -in=../testDeps/dep.noms -out=../testDeps/dep.go
|
||||
|
||||
//go:generate go run ../codegen.go -deps-dir=gen -ldb=/tmp/depGenTest -package-ds=testDeps
|
||||
|
||||
//go:generate rm -rf /tmp/depGenTest
|
||||
|
||||
Reference in New Issue
Block a user