func First(fun func(s string) bool, step int, vals ...string) string { var l = len(vals) var reverse = step < 0 for i := coreutil.Ifi(reverse, l-1, 0); coreutil.Ifb(reverse, i >= 0, i < l); i += step { if fun(vals[i]) { return vals[i] } } return "" }
func Distance(s1, s2 string) int { var cost, min1, min2, min3, i, j int var d = make([][]int, len(s1)+1) for i = 0; i < len(d); i++ { d[i] = make([]int, len(s2)+1) d[i][0] = i } for i = 0; i < len(d[0]); i++ { d[0][i] = i } for i = 1; i < len(d); i++ { for j = 1; j < len(d[0]); j++ { cost = coreutil.Ifi(s1[i-1] == s2[j-1], 0, 1) min1 = d[i-1][j] + 1 min2 = d[i][j-1] + 1 min3 = d[i-1][j-1] + cost d[i][j] = int(math.Min(math.Min(float64(min1), float64(min2)), float64(min3))) } } return d[len(s1)][len(s2)] }
func createElevs(targetDir string) { type sourceRec struct { dirPath string filePath string fileTime time.Time byteOrder binary.ByteOrder fileLen int64 } var srcBasePath = "/media/hdx/elev_raw/" var force = []string{ /*"N64W024"*/ } var initSourceRec = func(dirRelPath string) *sourceRec { return &sourceRec{path.Join(srcBasePath, dirRelPath), "", time.Time{}, binary.LittleEndian, 0} } var fexts = []string{".hgt", ".hgt.le", ".HGT", ".HGT.LE"} var srcRecs = map[string]*sourceRec{"": initSourceRec(""), "bathy": initSourceRec("bathy/hgt"), "srtm": initSourceRec("srtm/hgt"), "rmw": initSourceRec("rmw3"), "vfp": initSourceRec("vfp")} var lola numutil.Dvec2 var fp, fbName, skey, nkey string var hasBathy, hasElev bool var srec *sourceRec var newest, srtmTime time.Time var pngMakers = 0 var pngChan = make(chan bool) var makePng = func(outFilePath string, elevFile *sourceRec, bathyFilePath string) { var ps, ps1, psx = 1200, 1201, 1200 * 2400 var bathyLandVal int16 = 0 var bathyMapping = []int16{-4500, -2500, -9500, -6500, -10500, -3500, bathyLandVal, -5500, -7500, -200, -8500, -1500, -750} var pngSize = image.Rect(0, 0, ps, ps) var px, py int var bv, ev, fv int16 var srcFile, bathFile, pngFile *os.File var err error var pngImage *image.NRGBA fmt.Println(outFilePath) if pngFile, err = os.Create(outFilePath); err != nil { fmt.Println("PANIC", err) panic(err) } if bathFile, err = os.Open(bathyFilePath); err != nil { bathFile = nil } if srcFile, err = os.Open(elevFile.filePath); err != nil { srcFile = nil } if (bathFile != nil) || (srcFile != nil) { pngImage = image.NewNRGBA(pngSize) for py = 0; py < pngSize.Max.Y; py++ { for px = 0; px < pngSize.Max.X; px++ { if (bathFile == nil) || !fileutil.ReadFromBinary(bathFile, int64((math.Floor(float64(py)/40)*30)+math.Floor(float64(px)/40))*2, binary.LittleEndian, &bv) { bv = bathyLandVal } else { bv = bathyMapping[bv] } if (srcFile == nil) || !fileutil.ReadFromBinary(srcFile, (int64((py*coreutil.Ifi(int(elevFile.fileLen) == psx, ps, ps1))+px)*2) /*+ coreutil.Ifl(int(elevFile.fileLen) == psx, 0, 2402)*/, elevFile.byteOrder, &ev) { ev = -32768 } fv = coreutil.Ifs(bv == bathyLandVal, ev, coreutil.Ifs((ev != 0) && (ev > bv), ev, bv)) pngImage.Set(px, py, color.NRGBA{0, byte(fv), byte(fv >> 8), 255}) } } if bathFile != nil { bathFile.Close() } if srcFile != nil { srcFile.Close() } err = png.Encode(pngFile, pngImage) if err != nil { fmt.Println("PANIC", err) panic(err) } } pngFile.Close() pngChan <- true } var goMakePng = func(outFilePath string, elevFile *sourceRec, bathyFilePath string) { for pngMakers >= 8 { <-pngChan pngMakers-- } pngMakers++ go makePng(outFilePath, elevFile, bathyFilePath) } srtmTime = time.Date(2008, 10, 15, 12, 30, 30, 500, time.Local) for lola.Y = geoutil.LatMin; lola.Y < geoutil.LatMax; lola.Y++ { for lola.X = geoutil.LonMin; lola.X < geoutil.LonMax; lola.X++ { fbName = geoutil.LoLaFileName(lola.X, lola.Y) if (len(force) == 0) || (stringutil.InSliceAt(force, fbName) >= 0) { fp = path.Join(targetDir, fbName) for skey, srec = range srcRecs { if len(skey) > 0 { srec.filePath, srec.fileTime, srec.fileLen = fileutil.FileExistsPath(srec.dirPath, fbName, fexts, true, false) if skey == "srtm" { srec.fileTime = srtmTime } } } newest = time.Time{} nkey = "" for skey, srec = range srcRecs { if (len(skey) > 0) && (skey != "bathy") && (len(srec.filePath) > 0) && (srec.fileTime.After(newest)) { newest = srec.fileTime nkey = skey if strings.HasSuffix(strings.ToLower(srec.filePath), ".hgt.le") { srec.byteOrder = binary.LittleEndian } else { srec.byteOrder = binary.BigEndian } } } if hasBathy, hasElev = len(srcRecs["bathy"].filePath) > 0, len(nkey) > 0; hasBathy || hasElev { goMakePng(fp, srcRecs[nkey], srcRecs["bathy"].filePath) } else { fmt.Println("\tNO BATHY, NO ELEV!\n") } if (len(srcRecs["rmw"].filePath) > 0) && (len(srcRecs["srtm"].filePath) > 0) && (len(srcRecs["vfp"].filePath) > 0) { goMakePng(path.Join(targetDir+"_rsv", fbName+"."+nkey+".r.png"), srcRecs["rmw"], srcRecs["bathy"].filePath) goMakePng(path.Join(targetDir+"_rsv", fbName+"."+nkey+".s.png"), srcRecs["srtm"], srcRecs["bathy"].filePath) goMakePng(path.Join(targetDir+"_rsv", fbName+"."+nkey+".v.png"), srcRecs["vfp"], srcRecs["bathy"].filePath) } else if (len(srcRecs["rmw"].filePath) > 0) && (len(srcRecs["vfp"].filePath) > 0) { goMakePng(path.Join(targetDir+"_rv", fbName+"."+nkey+".r.png"), srcRecs["rmw"], srcRecs["bathy"].filePath) goMakePng(path.Join(targetDir+"_rv", fbName+"."+nkey+".v.png"), srcRecs["vfp"], srcRecs["bathy"].filePath) } else if (len(srcRecs["rmw"].filePath) > 0) && (len(srcRecs["srtm"].filePath) > 0) { goMakePng(path.Join(targetDir+"_rs", fbName+"."+nkey+".r.png"), srcRecs["rmw"], srcRecs["bathy"].filePath) goMakePng(path.Join(targetDir+"_rs", fbName+"."+nkey+".s.png"), srcRecs["srtm"], srcRecs["bathy"].filePath) } else if (len(srcRecs["vfp"].filePath) > 0) && (len(srcRecs["srtm"].filePath) > 0) { goMakePng(path.Join(targetDir+"_sv", fbName+"."+nkey+".v.png"), srcRecs["vfp"], srcRecs["bathy"].filePath) goMakePng(path.Join(targetDir+"_sv", fbName+"."+nkey+".s.png"), srcRecs["srtm"], srcRecs["bathy"].filePath) } } } } }
func createDbCollection(dbConn *mgo.Session, dbName string, sourceFilePath, collName string, skipFirst bool, hasGeoIndex bool, makeRec recMaker) { var fr *os.File var br *bufio.Reader var err error var rec []string var line []byte var isPrefix, isDvec2 bool var geoIndexDone = false var mr bson.M var ll numutil.Dvec2 var i, ri, rc = 0, 0, 0 fr, err = os.Open(sourceFilePath) if fr != nil { defer fr.Close() } if err != nil { panic(err) } if fr != nil { fmt.Println("Reading", sourceFilePath) br = bufio.NewReaderSize(fr, coreutil.Ifi(strings.HasSuffix(sourceFilePath, "allCountries.txt"), 1024*1024*1024, 1024*1024*72)) for rec = nil; err == nil; line, isPrefix, err = br.ReadLine() { if isPrefix || err != nil { fmt.Println(err) panic("readline") } else { rec = stringutil.Split(string(line), "\t") } if (rec != nil) && (len(rec) > 0) && !strings.HasPrefix(rec[0], "#") { if (i != 0) || !skipFirst { if mr = makeRec(i, rec); mr != nil { mrs[ri] = mr ri++ rc++ if hasGeoIndex { if ll, isDvec2 = mr["l"].(numutil.Dvec2); isDvec2 { mr["l"] = []float64{ll.X, ll.Y} if !geoIndexDone { dbutil.EnsureIndex(dbConn, dbName, collName, &mgo.Index{Key: []string{"@l"}, Bits: 32, Min: -180, Max: 181}) geoIndexDone = true } } } if (i % 250000) == 0 { fmt.Println("Read", i) } } } i++ } } for i = 0; i < rc; i = i + 4096 { if ri = i + 4096; ri > rc { ri = rc } dbutil.Insert(dbConn, dbName, collName, mrs[i:ri]...) if (i % (4096 * 50)) == 0 { fmt.Println("Insert", i) } } fmt.Println("Recs total: ", collName, rc) } }
func ConnectToLocal() (*mgo.Session, error) { return ConnectTo(ConnectUrl("localhost", coreutil.Ifi(IsHost, 4057, 5317))) }
func ToggleFullscreen() { // sdl.WM_ToggleFullScreen(glutil.Screen) ReinitVideo(coreutil.Ifi(FullScreen, SCREEN_DEFWIDTH, 0), coreutil.Ifi(FullScreen, SCREEN_DEFHEIGHT, 0), true, true) }