Merge pull request #362 from willhite/sfcrime-search

sfcrime_search utility.
This commit is contained in:
Dan Willhite
2015-09-28 17:31:39 -07:00
9 changed files with 1075 additions and 16 deletions
+10 -16
View File
@@ -20,7 +20,6 @@ var (
inputRefStr = flag.String("input-ref", "", "ref to list containing nodes")
outputDs = flag.String("output-ds", "", "dataset to store data in.")
quietFlag = flag.Bool("quiet", false, "suppress printing of progress statements")
commit = flag.Bool("commit", true, "commit the quadtree to nomsdb")
)
func main() {
@@ -92,22 +91,17 @@ func main() {
if !*quietFlag {
fmt.Printf("Nodes Appended: %d, elapsed time: %.2f secs\n", nodesAppended, secsSince(start))
qtRoot.Analyze()
fmt.Printf("Calling SaveToNoms(), elapsed time: %.2f secs\n", secsSince(start))
}
if *commit {
if !*quietFlag {
fmt.Printf("Calling SaveToNoms(), elapsed time: %.2f secs\n", secsSince(start))
}
nomsQtRoot := qtRoot.SaveToNoms(dataset.Store(), start)
if !*quietFlag {
fmt.Printf("Calling Commit(), elapsed time: %.2f secs\n", secsSince(start))
}
_, ok = dataset.Commit(nomsQtRoot.NomsValue())
d.Chk.True(ok, "Could not commit due to conflicting edit")
if !*quietFlag {
fmt.Printf("Commit completed, elapsed time: %.2f secs\n", time.Now().Sub(start).Seconds())
}
nomsQtRoot := qtRoot.SaveToNoms(dataset.Store(), start)
if !*quietFlag {
fmt.Printf("Calling Commit(), elapsed time: %.2f secs\n", secsSince(start))
}
fmt.Println("QuadTree ref:", dataset.Store().Root().String())
_, ok = dataset.Commit(nomsQtRoot.NomsValue())
d.Chk.True(ok, "Could not commit due to conflicting edit")
if !*quietFlag {
fmt.Printf("Commit completed, elapsed time: %.2f secs\n", time.Now().Sub(start).Seconds())
}
fmt.Println("QuadTree ref:", nomsQtRoot.NomsValue().Ref())
}
+1
View File
@@ -0,0 +1 @@
sfcrime_search
+124
View File
@@ -0,0 +1,124 @@
package main
import "math"
const (
// According to Wikipedia, the Earth's radius is about 6,371km
EARTH_RADIUS = 6371
)
func (p GeopositionDef) Latitude64() float64 {
return float64(p.Latitude)
}
func (p GeopositionDef) Longitude64() float64 {
return float64(p.Longitude)
}
// LessThan 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
}
// GreaterThan 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
}
// LessThanOrEqual 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
}
// GreaterThanOrEqual 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
}
// 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 / EARTH_RADIUS
bearing = (bearing * (math.Pi / 180.0))
lat1 := (float64(p.Latitude64()) * (math.Pi / 180.0))
lng1 := (float64(p.Longitude64()) * (math.Pi / 180.0))
lat2_part1 := math.Sin(lat1) * math.Cos(dr)
lat2_part2 := math.Cos(lat1) * math.Sin(dr) * math.Cos(bearing)
lat2 := math.Asin(lat2_part1 + lat2_part2)
lng2_part1 := math.Sin(bearing) * math.Sin(dr) * math.Cos(lat1)
lng2_part2 := math.Cos(dr) - (math.Sin(lat1) * math.Sin(lat2))
lng2 := lng1 + math.Atan2(lng2_part1, lng2_part2)
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)}
}
// 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(EARTH_RADIUS * c)
}
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) {
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
}
+96
View File
@@ -0,0 +1,96 @@
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)
}
+46
View File
@@ -0,0 +1,46 @@
package main
import (
// "fmt"
)
const (
maxNodes = 16
maxDepth = 10
tl = "TopLeft"
bl = "BottomLeft"
tr = "TopRight"
br = "BottomRight"
)
var (
quadrants = []string{tl, bl, tr, br}
)
func (qt *SQuadTree) Query(p GeopositionDef, kilometers float64) (GeorectangleDef, []Incident) {
r := p.BoundingRectangle(kilometers)
nodes := qt.Search(r, p, float32(kilometers))
return r, nodes
}
func (qt *SQuadTree) Search(r GeorectangleDef, p GeopositionDef, kilometers float32) []Incident {
nodes := []Incident{}
if qt.Tiles().Len() > 0 {
for _, q := range quadrants {
tile := qt.Tiles().Get(q)
if tile.Georectangle().Def().IntersectsRect(r) {
tnodes := tile.Search(r, p, kilometers)
nodes = append(nodes, tnodes...)
}
}
} else if qt.Nodes().Len() > 0 {
qt.Nodes().Iter(func(n Incident) bool {
if p.DistanceTo(n.Geoposition().Def()) < kilometers {
nodes = append(nodes, n)
}
return false
})
}
return nodes
}
+3
View File
@@ -0,0 +1,3 @@
package main
//go:generate go run ../../nomdl/codegen/codegen.go
+99
View File
@@ -0,0 +1,99 @@
package main
import (
"flag"
"fmt"
"log"
"time"
"github.com/attic-labs/noms/d"
"github.com/attic-labs/noms/datas"
"github.com/attic-labs/noms/ref"
"github.com/attic-labs/noms/types"
"net"
"net/http"
"sort"
"strings"
)
var (
datasFlags = datas.NewFlags()
quadTreeRefFlag = flag.String("quadtree-ref", "", "ref containing root of quadtree")
latFlag = flag.Float64("lat", 0.0, "latitude of point to search for crime instances")
lonFlag = flag.Float64("lon", 0.0, "longitude of point to search for crime instances")
distanceFlag = flag.Float64("distance", 0.5, "distince in kilometers from point to search for crime instances")
sqtRoot SQuadTree
)
const (
maxRequests = 8
searchPath = "/s/"
)
type httpServer struct {
port int
l *net.Listener
conns map[net.Conn]http.ConnState
writeLimit chan struct{}
}
func main() {
flag.Parse()
start := time.Now()
datastore, ok := datasFlags.CreateDataStore()
if !ok || *quadTreeRefFlag == "" {
flag.Usage()
return
}
defer datastore.Close()
var qtRef ref.Ref
err := d.Try(func() {
qtRef = ref.Parse(*quadTreeRefFlag)
})
if err != nil {
log.Fatalf("Invalid quadtree-ref: %v", *quadTreeRefFlag)
}
qtVal := types.ReadValue(qtRef, datastore)
sqtRoot = SQuadTreeFromVal(qtVal)
if *latFlag == 0.0 || *lonFlag == 0.0 {
flag.Usage()
return
}
gp := GeopositionDef{float32(*latFlag), float32(*lonFlag)}
if !sqtRoot.Georectangle().Def().ContainsPoint(gp) {
fmt.Printf("lat/lon: %+v is not within sf area: %+v\n", gp, sqtRoot.Georectangle().Def())
return
}
gr, incidents := sqtRoot.Query(gp, *distanceFlag)
fmt.Printf("bounding Rectangle: %+v, numIncidents: %d\n", gr, len(incidents))
var resDefs []IncidentDef
for _, incident := range incidents {
resDefs = append(resDefs, incident.Def())
}
sort.Sort(sort.Reverse(ByDate(resDefs)))
for _, n := range resDefs {
fmt.Printf("Incident, date: %s, category: %s, desc: %s, address: %s\n", n.Date, n.Category, n.Description, n.Address)
}
fmt.Printf("Done, elapsed time: %.2f secs\n", time.Now().Sub(start).Seconds())
}
type ByDate []IncidentDef
func (s ByDate) Len() int {
return len(s)
}
func (s ByDate) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByDate) Less(i, j int) bool {
i1 := strings.Split(s[i].Date, "/")
idate := fmt.Sprintf("%s/%s/%s", i1[2], i1[0], i1[1])
j1 := strings.Split(s[j].Date, "/")
jdate := fmt.Sprintf("%s/%s/%s", j1[2], j1[0], j1[1])
return idate < jdate
}
+670
View File
@@ -0,0 +1,670 @@
// This file was generated by nomdl/codegen.
package main
import (
"github.com/attic-labs/noms/ref"
"github.com/attic-labs/noms/types"
)
var __mainPackageInFile_types_CachedRef = __mainPackageInFile_types_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 __mainPackageInFile_types_Ref() types.Ref {
p := types.PackageDef{
Types: types.MapOfStringToTypeRefDef{
"Geoposition": __typeRefOfGeoposition(),
"Georectangle": __typeRefOfGeorectangle(),
"Incident": __typeRefOfIncident(),
"SQuadTree": __typeRefOfSQuadTree(),
},
}.New()
return types.Ref{R: types.RegisterPackage(&p)}
}
// Geoposition
type Geoposition struct {
m types.Map
}
func NewGeoposition() Geoposition {
return Geoposition{types.NewMap(
types.NewString("$name"), types.NewString("Geoposition"),
types.NewString("$type"), types.MakeTypeRef("Geoposition", __mainPackageInFile_types_CachedRef),
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("$name"), types.NewString("Geoposition"),
types.NewString("$type"), types.MakeTypeRef("Geoposition", __mainPackageInFile_types_CachedRef),
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
}
// Creates and returns a Noms Value that describes Geoposition.
func __typeRefOfGeoposition() types.TypeRef {
return types.MakeStructTypeRef("Geoposition",
[]types.Field{
types.Field{"Latitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
types.Field{"Longitude", types.MakePrimitiveTypeRef(types.Float32Kind), false},
},
types.Choices{},
)
}
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 Geoposition) bool {
return s.m.Equals(other.m)
}
func (s Geoposition) Ref() ref.Ref {
return s.m.Ref()
}
func (s Geoposition) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
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("$name"), types.NewString("Georectangle"),
types.NewString("$type"), types.MakeTypeRef("Georectangle", __mainPackageInFile_types_CachedRef),
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("$name"), types.NewString("Georectangle"),
types.NewString("$type"), types.MakeTypeRef("Georectangle", __mainPackageInFile_types_CachedRef),
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
}
// Creates and returns a Noms Value that describes Georectangle.
func __typeRefOfGeorectangle() types.TypeRef {
return types.MakeStructTypeRef("Georectangle",
[]types.Field{
types.Field{"TopLeft", types.MakeTypeRef("Geoposition", types.Ref{}), false},
types.Field{"BottomRight", types.MakeTypeRef("Geoposition", types.Ref{}), false},
},
types.Choices{},
)
}
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 Georectangle) bool {
return s.m.Equals(other.m)
}
func (s Georectangle) Ref() ref.Ref {
return s.m.Ref()
}
func (s Georectangle) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
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())}
}
// Incident
type Incident struct {
m types.Map
}
func NewIncident() Incident {
return Incident{types.NewMap(
types.NewString("$name"), types.NewString("Incident"),
types.NewString("$type"), types.MakeTypeRef("Incident", __mainPackageInFile_types_CachedRef),
types.NewString("Category"), types.NewString(""),
types.NewString("Description"), types.NewString(""),
types.NewString("Address"), types.NewString(""),
types.NewString("Date"), types.NewString(""),
types.NewString("Geoposition"), NewGeoposition().NomsValue(),
)}
}
type IncidentDef struct {
Category string
Description string
Address string
Date string
Geoposition GeopositionDef
}
func (def IncidentDef) New() Incident {
return Incident{
types.NewMap(
types.NewString("$name"), types.NewString("Incident"),
types.NewString("$type"), types.MakeTypeRef("Incident", __mainPackageInFile_types_CachedRef),
types.NewString("Category"), types.NewString(def.Category),
types.NewString("Description"), types.NewString(def.Description),
types.NewString("Address"), types.NewString(def.Address),
types.NewString("Date"), types.NewString(def.Date),
types.NewString("Geoposition"), def.Geoposition.New().NomsValue(),
)}
}
func (s Incident) Def() (d IncidentDef) {
d.Category = s.m.Get(types.NewString("Category")).(types.String).String()
d.Description = s.m.Get(types.NewString("Description")).(types.String).String()
d.Address = s.m.Get(types.NewString("Address")).(types.String).String()
d.Date = s.m.Get(types.NewString("Date")).(types.String).String()
d.Geoposition = GeopositionFromVal(s.m.Get(types.NewString("Geoposition"))).Def()
return
}
// Creates and returns a Noms Value that describes Incident.
func __typeRefOfIncident() types.TypeRef {
return types.MakeStructTypeRef("Incident",
[]types.Field{
types.Field{"Category", types.MakePrimitiveTypeRef(types.StringKind), false},
types.Field{"Description", types.MakePrimitiveTypeRef(types.StringKind), false},
types.Field{"Address", types.MakePrimitiveTypeRef(types.StringKind), false},
types.Field{"Date", types.MakePrimitiveTypeRef(types.StringKind), false},
types.Field{"Geoposition", types.MakeTypeRef("Geoposition", types.Ref{}), false},
},
types.Choices{},
)
}
func IncidentFromVal(val types.Value) Incident {
// TODO: Validate here
return Incident{val.(types.Map)}
}
func (s Incident) NomsValue() types.Value {
return s.m
}
func (s Incident) Equals(other Incident) bool {
return s.m.Equals(other.m)
}
func (s Incident) Ref() ref.Ref {
return s.m.Ref()
}
func (s Incident) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
func (s Incident) Category() string {
return s.m.Get(types.NewString("Category")).(types.String).String()
}
func (s Incident) SetCategory(val string) Incident {
return Incident{s.m.Set(types.NewString("Category"), types.NewString(val))}
}
func (s Incident) Description() string {
return s.m.Get(types.NewString("Description")).(types.String).String()
}
func (s Incident) SetDescription(val string) Incident {
return Incident{s.m.Set(types.NewString("Description"), types.NewString(val))}
}
func (s Incident) Address() string {
return s.m.Get(types.NewString("Address")).(types.String).String()
}
func (s Incident) SetAddress(val string) Incident {
return Incident{s.m.Set(types.NewString("Address"), types.NewString(val))}
}
func (s Incident) Date() string {
return s.m.Get(types.NewString("Date")).(types.String).String()
}
func (s Incident) SetDate(val string) Incident {
return Incident{s.m.Set(types.NewString("Date"), types.NewString(val))}
}
func (s Incident) Geoposition() Geoposition {
return GeopositionFromVal(s.m.Get(types.NewString("Geoposition")))
}
func (s Incident) SetGeoposition(val Geoposition) Incident {
return Incident{s.m.Set(types.NewString("Geoposition"), val.NomsValue())}
}
// SQuadTree
type SQuadTree struct {
m types.Map
}
func NewSQuadTree() SQuadTree {
return SQuadTree{types.NewMap(
types.NewString("$name"), types.NewString("SQuadTree"),
types.NewString("$type"), types.MakeTypeRef("SQuadTree", __mainPackageInFile_types_CachedRef),
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(),
)}
}
type SQuadTreeDef struct {
Nodes ListOfIncidentDef
Tiles MapOfStringToSQuadTreeDef
Depth uint8
NumDescendents uint32
Path string
Georectangle GeorectangleDef
}
func (def SQuadTreeDef) New() SQuadTree {
return SQuadTree{
types.NewMap(
types.NewString("$name"), types.NewString("SQuadTree"),
types.NewString("$type"), types.MakeTypeRef("SQuadTree", __mainPackageInFile_types_CachedRef),
types.NewString("Nodes"), def.Nodes.New().NomsValue(),
types.NewString("Tiles"), def.Tiles.New().NomsValue(),
types.NewString("Depth"), types.UInt8(def.Depth),
types.NewString("NumDescendents"), types.UInt32(def.NumDescendents),
types.NewString("Path"), types.NewString(def.Path),
types.NewString("Georectangle"), def.Georectangle.New().NomsValue(),
)}
}
func (s SQuadTree) Def() (d SQuadTreeDef) {
d.Nodes = ListOfIncidentFromVal(s.m.Get(types.NewString("Nodes"))).Def()
d.Tiles = MapOfStringToSQuadTreeFromVal(s.m.Get(types.NewString("Tiles"))).Def()
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()
return
}
// Creates and returns a Noms Value that describes SQuadTree.
func __typeRefOfSQuadTree() types.TypeRef {
return types.MakeStructTypeRef("SQuadTree",
[]types.Field{
types.Field{"Nodes", types.MakeCompoundTypeRef("", types.ListKind, types.MakeTypeRef("Incident", types.Ref{})), false},
types.Field{"Tiles", types.MakeCompoundTypeRef("", types.MapKind, types.MakePrimitiveTypeRef(types.StringKind), types.MakeTypeRef("SQuadTree", types.Ref{})), 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("Georectangle", types.Ref{}), false},
},
types.Choices{},
)
}
func SQuadTreeFromVal(val types.Value) SQuadTree {
// TODO: Validate here
return SQuadTree{val.(types.Map)}
}
func (s SQuadTree) NomsValue() types.Value {
return s.m
}
func (s SQuadTree) Equals(other SQuadTree) bool {
return s.m.Equals(other.m)
}
func (s SQuadTree) Ref() ref.Ref {
return s.m.Ref()
}
func (s SQuadTree) Type() types.TypeRef {
return s.m.Get(types.NewString("$type")).(types.TypeRef)
}
func (s SQuadTree) Nodes() ListOfIncident {
return ListOfIncidentFromVal(s.m.Get(types.NewString("Nodes")))
}
func (s SQuadTree) SetNodes(val ListOfIncident) SQuadTree {
return SQuadTree{s.m.Set(types.NewString("Nodes"), val.NomsValue())}
}
func (s SQuadTree) Tiles() MapOfStringToSQuadTree {
return MapOfStringToSQuadTreeFromVal(s.m.Get(types.NewString("Tiles")))
}
func (s SQuadTree) SetTiles(val MapOfStringToSQuadTree) SQuadTree {
return SQuadTree{s.m.Set(types.NewString("Tiles"), val.NomsValue())}
}
func (s SQuadTree) Depth() uint8 {
return uint8(s.m.Get(types.NewString("Depth")).(types.UInt8))
}
func (s SQuadTree) SetDepth(val uint8) SQuadTree {
return SQuadTree{s.m.Set(types.NewString("Depth"), types.UInt8(val))}
}
func (s SQuadTree) NumDescendents() uint32 {
return uint32(s.m.Get(types.NewString("NumDescendents")).(types.UInt32))
}
func (s SQuadTree) SetNumDescendents(val uint32) SQuadTree {
return SQuadTree{s.m.Set(types.NewString("NumDescendents"), types.UInt32(val))}
}
func (s SQuadTree) Path() string {
return s.m.Get(types.NewString("Path")).(types.String).String()
}
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) SetGeorectangle(val Georectangle) SQuadTree {
return SQuadTree{s.m.Set(types.NewString("Georectangle"), val.NomsValue())}
}
// ListOfIncident
type ListOfIncident struct {
l types.List
}
func NewListOfIncident() ListOfIncident {
return ListOfIncident{types.NewList()}
}
type ListOfIncidentDef []IncidentDef
func (def ListOfIncidentDef) New() ListOfIncident {
l := make([]types.Value, len(def))
for i, d := range def {
l[i] = d.New().NomsValue()
}
return ListOfIncident{types.NewList(l...)}
}
func (l ListOfIncident) Def() ListOfIncidentDef {
d := make([]IncidentDef, l.Len())
for i := uint64(0); i < l.Len(); i++ {
d[i] = IncidentFromVal(l.l.Get(i)).Def()
}
return d
}
func ListOfIncidentFromVal(val types.Value) ListOfIncident {
// TODO: Validate here
return ListOfIncident{val.(types.List)}
}
func (l ListOfIncident) NomsValue() types.Value {
return l.l
}
func (l ListOfIncident) Equals(p ListOfIncident) bool {
return l.l.Equals(p.l)
}
func (l ListOfIncident) Ref() ref.Ref {
return l.l.Ref()
}
func (l ListOfIncident) Len() uint64 {
return l.l.Len()
}
func (l ListOfIncident) Empty() bool {
return l.Len() == uint64(0)
}
func (l ListOfIncident) Get(i uint64) Incident {
return IncidentFromVal(l.l.Get(i))
}
func (l ListOfIncident) Slice(idx uint64, end uint64) ListOfIncident {
return ListOfIncident{l.l.Slice(idx, end)}
}
func (l ListOfIncident) Set(i uint64, val Incident) ListOfIncident {
return ListOfIncident{l.l.Set(i, val.NomsValue())}
}
func (l ListOfIncident) Append(v ...Incident) ListOfIncident {
return ListOfIncident{l.l.Append(l.fromElemSlice(v)...)}
}
func (l ListOfIncident) Insert(idx uint64, v ...Incident) ListOfIncident {
return ListOfIncident{l.l.Insert(idx, l.fromElemSlice(v)...)}
}
func (l ListOfIncident) Remove(idx uint64, end uint64) ListOfIncident {
return ListOfIncident{l.l.Remove(idx, end)}
}
func (l ListOfIncident) RemoveAt(idx uint64) ListOfIncident {
return ListOfIncident{(l.l.RemoveAt(idx))}
}
func (l ListOfIncident) fromElemSlice(p []Incident) []types.Value {
r := make([]types.Value, len(p))
for i, v := range p {
r[i] = v.NomsValue()
}
return r
}
type ListOfIncidentIterCallback func(v Incident) (stop bool)
func (l ListOfIncident) Iter(cb ListOfIncidentIterCallback) {
l.l.Iter(func(v types.Value) bool {
return cb(IncidentFromVal(v))
})
}
type ListOfIncidentIterAllCallback func(v Incident)
func (l ListOfIncident) IterAll(cb ListOfIncidentIterAllCallback) {
l.l.IterAll(func(v types.Value) {
cb(IncidentFromVal(v))
})
}
type ListOfIncidentFilterCallback func(v Incident) (keep bool)
func (l ListOfIncident) Filter(cb ListOfIncidentFilterCallback) ListOfIncident {
nl := NewListOfIncident()
l.IterAll(func(v Incident) {
if cb(v) {
nl = nl.Append(v)
}
})
return nl
}
// MapOfStringToSQuadTree
type MapOfStringToSQuadTree struct {
m types.Map
}
func NewMapOfStringToSQuadTree() MapOfStringToSQuadTree {
return MapOfStringToSQuadTree{types.NewMap()}
}
type MapOfStringToSQuadTreeDef map[string]SQuadTreeDef
func (def MapOfStringToSQuadTreeDef) New() MapOfStringToSQuadTree {
kv := make([]types.Value, 0, len(def)*2)
for k, v := range def {
kv = append(kv, types.NewString(k), v.New().NomsValue())
}
return MapOfStringToSQuadTree{types.NewMap(kv...)}
}
func (m MapOfStringToSQuadTree) Def() MapOfStringToSQuadTreeDef {
def := make(map[string]SQuadTreeDef)
m.m.Iter(func(k, v types.Value) bool {
def[k.(types.String).String()] = SQuadTreeFromVal(v).Def()
return false
})
return def
}
func MapOfStringToSQuadTreeFromVal(p types.Value) MapOfStringToSQuadTree {
// TODO: Validate here
return MapOfStringToSQuadTree{p.(types.Map)}
}
func (m MapOfStringToSQuadTree) NomsValue() types.Value {
return m.m
}
func (m MapOfStringToSQuadTree) Equals(p MapOfStringToSQuadTree) bool {
return m.m.Equals(p.m)
}
func (m MapOfStringToSQuadTree) Ref() ref.Ref {
return m.m.Ref()
}
func (m MapOfStringToSQuadTree) Empty() bool {
return m.m.Empty()
}
func (m MapOfStringToSQuadTree) Len() uint64 {
return m.m.Len()
}
func (m MapOfStringToSQuadTree) Has(p string) bool {
return m.m.Has(types.NewString(p))
}
func (m MapOfStringToSQuadTree) Get(p string) SQuadTree {
return SQuadTreeFromVal(m.m.Get(types.NewString(p)))
}
func (m MapOfStringToSQuadTree) Set(k string, v SQuadTree) MapOfStringToSQuadTree {
return MapOfStringToSQuadTree{m.m.Set(types.NewString(k), v.NomsValue())}
}
// TODO: Implement SetM?
func (m MapOfStringToSQuadTree) Remove(p string) MapOfStringToSQuadTree {
return MapOfStringToSQuadTree{m.m.Remove(types.NewString(p))}
}
type MapOfStringToSQuadTreeIterCallback func(k string, v SQuadTree) (stop bool)
func (m MapOfStringToSQuadTree) Iter(cb MapOfStringToSQuadTreeIterCallback) {
m.m.Iter(func(k, v types.Value) bool {
return cb(k.(types.String).String(), SQuadTreeFromVal(v))
})
}
type MapOfStringToSQuadTreeIterAllCallback func(k string, v SQuadTree)
func (m MapOfStringToSQuadTree) IterAll(cb MapOfStringToSQuadTreeIterAllCallback) {
m.m.IterAll(func(k, v types.Value) {
cb(k.(types.String).String(), SQuadTreeFromVal(v))
})
}
type MapOfStringToSQuadTreeFilterCallback func(k string, v SQuadTree) (keep bool)
func (m MapOfStringToSQuadTree) Filter(cb MapOfStringToSQuadTreeFilterCallback) MapOfStringToSQuadTree {
nm := NewMapOfStringToSQuadTree()
m.IterAll(func(k string, v SQuadTree) {
if cb(k, v) {
nm = nm.Set(k, v)
}
})
return nm
}
+26
View File
@@ -0,0 +1,26 @@
struct Geoposition {
Latitude: Float32
Longitude: Float32
}
struct Georectangle {
TopLeft: Geoposition
BottomRight: Geoposition
}
struct Incident {
Category: String
Description: String
Address: String
Date: String
Geoposition: Geoposition
}
struct SQuadTree {
Nodes: List(Incident)
Tiles: Map(String, SQuadTree)
Depth: UInt8
NumDescendents: UInt32
Path: String
Georectangle: Georectangle
}