示例#1
0
func coverBoundingBoxLongs(topLeftLat, topLeftLon, bottomRightLat, bottomRightLon float64, length int) Coverage {
	actualWidthDegreesPerHash := widthDegrees(length)
	actualHeightDegreesPerHash := heightDegrees(length)
	diff := longitudeDiff(bottomRightLon, topLeftLon)
	maxLon := topLeftLon + diff
	var hashes []string

	for lat := bottomRightLat; lat <= topLeftLat; lat += actualHeightDegreesPerHash {
		for lon := topLeftLon; lon <= maxLon; lon += actualWidthDegreesPerHash {
			geohash_str := geohash.Encode(lat, lon, length)
			hashes = append(hashes, geohash_str)
		}
	}
	// ensure have the borders covered
	for lat := bottomRightLat; lat <= topLeftLat; lat += actualHeightDegreesPerHash {
		geohash_str := geohash.Encode(lat, maxLon, length)
		hashes = append(hashes, geohash_str)
	}
	for lon := topLeftLon; lon <= maxLon; lon += actualWidthDegreesPerHash {
		geohash_str := geohash.Encode(topLeftLat, lon, length)
		hashes = append(hashes, geohash_str)
	}
	// ensure that the topRight corner is covered
	geohash_str := geohash.Encode(topLeftLat, maxLon, length)
	hashes = append(hashes, geohash_str)

	return Coverage{hashes, len(hashes)}
}
示例#2
0
func processLatLon(latLon string) (string, error) {
	latLonSplit := strings.Split(latLon, ",")
	if len(latLonSplit) != 2 {
		return "", fmt.Errorf("'%s'' is not a valid location (lat,lon)", latLon)
	}

	lat, err := strconv.ParseFloat(latLonSplit[0], 64)
	if err != nil {
		return "", err
	}

	lon, err := strconv.ParseFloat(latLonSplit[1], 64)
	if err != nil {
		return "", err
	}

	var gh string
	if flagPrecision > 0 {
		gh = geohash.Encode(lat, lon, flagPrecision)
	} else {
		gh = geohash.EncodeAuto(lat, lon)
	}
	return gh, nil
}
示例#3
0
func display_regions(lat, lng string) {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	geohash_4 := []string{"tdr0", "tdr1", "tdr2", "tdr3", "tdr4", "tdsr", "tdst", "tdsv", "tdsw", "tdsx", "tdsy", "tdsz", "tdu2", "tdu3", "tdu8", "tdu9", "tdub", "te5t", "te5v", "te5w", "te5x", "te5y", "te5z", "te78", "te79", "te7b", "te7c", "te7d", "te7e", "te7f", "te7g", "te7s", "te7t", "te7u", "te7v", "te7w", "te7x", "te7y", "te7z", "tee8", "tehj", "tehn", "tehp", "tek0", "tek1", "tek5", "tekh", "tekj", "tepd", "tepe", "tepf", "tepg", "teps", "tg5h", "tg5j", "tg5k", "tgf2", "tgf3", "tgf4", "tgf5", "tgf6", "tgf7", "tgf8", "tgf9", "tgfc", "tgfd", "tgfe", "tgff", "tgvt", "tgvu", "tgvv", "tgvw", "tgvx", "tgvy", "tgvz", "tgyh", "tgyj", "tgym", "tgyn", "tgyp", "tgyq", "tgyr", "tgyx", "tgyz", "thrj", "thrm", "thrn", "thrp", "thrq", "ts5s", "ts5t", "ts5u", "ts5v", "ts5y", "ts5z", "ts7b", "ts7c", "tshj", "tshn", "tshp", "tsk0", "tsmy", "tsmz", "tsqn", "tsqp", "tsqq", "tsqr", "tstb", "tstc", "tstf", "tsw0", "tsw1", "tsw2", "tsw3", "tsw4", "tsyx", "tsyz", "tszp", "tszr", "ttjq", "ttjr", "ttjt", "ttjv", "ttjw", "ttjx", "ttjy", "ttm0", "ttm1", "ttm2", "ttn8", "ttn9", "ttnc", "ttnd", "ttne", "ttnf", "ttng", "ttnj", "ttnm", "ttnn", "ttns", "ttp0", "ttp1", "ttp2", "ttp4", "tuj8", "tuj9", "tujb", "tujc", "tujf", "tun0", "tun1", "tun2", "tun3", "tun4", "tun8"}
	geohash_5 := []string{"tdusb", "tdusc", "tdusf", "tdut0", "tdut1", "tdut2", "tdut3", "tdut4", "thrjh", "thrjj", "thrjk", "thrjm", "thrjn", "thrjs", "thrjt", "thrju", "thrjv", "thrnh", "thrnj", "thrnk", "ttnct", "ttncu", "ttncv", "ttncw", "ttncy", "ttnfh", "ttnfj", "tuqbb", "tuqbc", "tuqc0"}

	latitude, _ := strconv.ParseFloat(lat, 64)
	longitude, _ := strconv.ParseFloat(lng, 64)
	geohash_str7 := geohash.Encode(longitude, latitude, 7)
	geohash_str6 := geohash.Encode(longitude, latitude, 6)
	pipeline := client.Pipeline()
	pipeline.LRange(geohash_str6, 0, -1)
	pipeline.LRange(geohash_str7, 0, -1)
	cmds, err := pipeline.Exec()
	if err != nil {
		fmt.Println(err)
	}

	var results []Result

	for _, cmd := range cmds {
		result := fmt.Sprintf("%s", cmd)
		redis_values := strings.Split(result, "&&")

		set := make(map[string]string)
		var feature_types []string
		var uuids []string
		var url_names []string
		var names []string
		var curpolygons []string

		// fmt.Println(stringInSlice(geohash_str6[0:4],geohash_4))
		if stringInSlice(geohash_str6[0:4], geohash_4) {
			result_cmd := client.LRange(geohash_str6[0:4], 0, -1)
			result_4 := fmt.Sprintf("%s", result_cmd)
			redis_values_4 := strings.Split(result_4, "&&")
			redis_values = append(redis_values, redis_values_4...)
		}

		if stringInSlice(geohash_str6[0:5], geohash_5) {
			result_cmd := client.LRange(geohash_str6[0:5], 0, -1)
			result_5 := fmt.Sprintf("%s", result_cmd)
			redis_values_5 := strings.Split(result_5, "&&")
			redis_values = append(redis_values, redis_values_5...)
		}

		for _, redis_value := range redis_values {
			index := strings.Index(redis_value, "[")
			if index != -1 {
				redis_value = redis_value[index+1 : len(redis_value)]
			}
			if len(redis_value) > 10 {
				value_array := strings.Split(redis_value, "::")
				md5 := md5.New()
				io.WriteString(md5, value_array[2])
				calc_md5 := GetMD5Hash(value_array[2])
				set[calc_md5] = value_array[2] + "--" + value_array[1] + "::" + value_array[3] + "::" + value_array[4] + "::" + value_array[0]
			} else {
				// fmt.Println("Geohash Not Found")
			}
		}

		for _, v := range set {
			set_values := strings.Split(v, "--")
			hash_polygon := set_values[0]
			hash_values := set_values[1]
			var begin, end int
			for i := 0; i < len(hash_polygon); i++ {
				if hash_polygon[i] == '(' {
					begin = i
				} else if '0' <= hash_polygon[i] && hash_polygon[i] <= '9' {
					break
				}
			}

			for i := len(hash_polygon) - 1; i >= 0; i-- {
				if hash_polygon[i] == ')' {
					end = i
				} else if '0' <= hash_polygon[i] && hash_polygon[i] <= '9' {
					break
				}
			}
			curpolygons = append(curpolygons, hash_polygon[begin+1:end])
			hash_values_array := strings.Split(hash_values, "::")
			feature_types = append(feature_types, hash_values_array[0])
			url_names = append(url_names, hash_values_array[1])
			names = append(names, hash_values_array[2])
			uuids = append(uuids, hash_values_array[3])

		}

		cur_point := geo.NewPoint(latitude, longitude)

		for index, curpolygon := range curpolygons {
			var points []string
			points = strings.Split(curpolygon, ",")
			polygon := ConstructPolygonFromPoints(points, false)
			if polygon.Contains(cur_point) {
				results = append(results, Result{names[index], url_names[index], uuids[index], feature_types[index], latitude, longitude})
			}
		}
	}
	fmt.Println(results)
}
示例#4
0
func queryDBRent() string {
	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	db := setupRentDB()
	sql_string2 := `select id,ST_X(coordinates), ST_Y(coordinates) from rent_flats limit 10000`
	defer db.Close()
	db.Exec("set search_path to public, postgis;")
	rows, err := db.Raw(sql_string2).Rows()
	fmt.Println(err)

	db2 := setupDB()
	defer db2.Close()
	db2.Exec("set search_path to public, postgis;")

	match := 0
	not_match := 0
	// total := 0
	var geohash_str7, geohash_str6 string
	var coordinate_x, coordinate_y string
	var flatid int
	var geohashes [MAX_HASH_VARIANT][]string
	var flatids []int
	var lons []float64
	var lats []float64
	for rows.Next() {
		rows.Scan(&flatid, &coordinate_y, &coordinate_x)
		lon, _ := strconv.ParseFloat(coordinate_x, 64)
		lat, _ := strconv.ParseFloat(coordinate_y, 64)
		geohash_str7 = geohash.Encode(lon, lat, 7)
		geohash_str6 = geohash.Encode(lon, lat, 6)
		lats = append(lats, lat)
		lons = append(lons, lon)
		flatids = append(flatids, flatid)
		geohashes[0] = append(geohashes[0], geohash_str7)
		geohashes[1] = append(geohashes[1], geohash_str6)
		// fmt.Println("BeforeGEOHASH",lon,lat,geohash_str)
	}
	fmt.Println("Geohashing")
	// geohashes = append(geohashes,"")
	// for index,geohash := range geohashes[0] {
	// 	ret := client.LRange(geohash,0,-1)
	// 	if len(ret.Val()) == 0 {
	// 		fmt.Println("TESTINGGEOHASH===>",geohash[0],lons[index],lats[index],flatids[index])
	// 		not_match = not_match + 1
	// 	} else {
	// 		match = match + 1
	// 	}
	// 	// fmt.Println("TESTINGGEOHASH===>",geohash,ret,ret.Val())
	// }

	// fmt.Println(geohashes)

	pipeline := client.Pipeline()
	for i := 0; i < MAX_HASH_VARIANT; i++ {
		for _, geohash := range geohashes[i] {
			pipeline.LRange(geohash, 0, -1)
		}
	}
	var mod_value = len(geohashes[0])
	fmt.Println(mod_value, geohashes[0][0])
	// pipeline := client.Pipeline()
	// for _, geohash := range geohashes {
	// 	pipeline.LRange(geohash,0,-1)
	// }
	cmds, err := pipeline.Exec()
	// fmt.Println("LENGTH==>",len(cmds))
	var ctr = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
	var uuids [MAX_HASH_VARIANT][]string
	var names [MAX_HASH_VARIANT][]string
	// fmt.Println("Chutiyaapmistake",mod_value)
	for index, _ := range cmds {
		if index > mod_value/MAX_HASH_VARIANT {
			break
		}

		var polygons [MAX_HASH_VARIANT][]string
		var feature_types [MAX_HASH_VARIANT][]string
		var curuuids [MAX_HASH_VARIANT][]string
		var url_names [MAX_HASH_VARIANT][]string
		var names [MAX_HASH_VARIANT][]string
		flag := 0
		// fmt.Println("INDEX ==> ",index)
		for mod := 0; mod < MAX_HASH_VARIANT; mod++ {
			// fmt.Println(((mod_value*mod)+index))
			// fmt.Println(cmds[((mod_value*mod)+index)])
			cmd := cmds[(mod_value*mod)+index]
			a := fmt.Sprintf("%b", cmd)
			b := strings.Split(a, "%!b(string=")
			ctr[len(b)] = ctr[len(b)] + 1
			set := make(map[string]string)
			for k := 3; k < len(b); k++ {
				uuid := b[k][0:20]
				curuuids[mod] = append(curuuids[mod], uuid)
				uuids[mod] = append(uuids[mod], uuid)
				c := strings.Split(b[k], "::")
				// fmt.Println(c)
				calc_md5 := fmt.Sprintf("%x", md5.Sum(([]byte)(c[2])))
				set[calc_md5] = c[2]
				feature_types[mod] = append(feature_types[mod], c[1])
				polygons[mod] = append(polygons[mod], c[2])
				url_names[mod] = append(url_names[mod], c[3])
				names[mod] = append(names[mod], c[4])
			}
			// fmt.Println("CHECK SIZES ===> ", len(polygons[0]),len(polygons[1]),len(names[0]),len(names[1]))
			// fmt.Println("1")
			var curpolygons []string
			for _, v := range polygons[mod] {
				var begin, end int
				for i := 0; i < len(v); i++ {
					if v[i] == '(' {
						begin = i
					} else if '0' <= v[i] && v[i] <= '9' {
						break
					}
				}

				for i := len(v) - 1; i >= 0; i-- {
					if v[i] == ')' {
						end = i
					} else if '0' <= v[i] && v[i] <= '9' {
						break
					}
				}

				curpolygons = append(curpolygons, v[begin+1:end])
			}
			// fmt.Println("2")
			// fmt.Println("FUCK YOU")
			cur_point := geo.NewPoint(lats[index], lons[index])
			url := fmt.Sprintf("https://rails.housing.com//api/v2/rent/%d", flatids[index])
			// fmt.Println("URLHIT==>",url)
			content, _ := getContent(url)
			// fmt.Println("API-CONTENT=====>",string(content))
			stringcontent := string(content)
			stringa := "display_regions"
			stringurl := "display_regions_urls"
			stringuuid := "display_regions_uuids"
			// idx:= get_match(stringcontent, stringa)
			// fmt.Println("FOUND INDEXES===>",idx)
			display_regions_index := -1
			display_regions_urls_index := -1
			display_regions_uuids_index := -1
			display_regions_index = strings.Index(stringcontent, stringa)
			display_regions_urls_index = strings.Index(stringcontent, stringurl)
			display_regions_uuids_index = strings.Index(stringcontent, stringuuid)

			fmt.Println(display_regions_index, display_regions_urls_index, display_regions_uuids_index)
			// fmt.Println("LENGTH of current_polygon ===> ",len(curpolygons))
			for i := 0; i < len(curpolygons); i++ {
				var points []string
				points = strings.Split(curpolygons[i], ",")
				polygon := ConstructPolygonFromPoints(points, false)
				if polygon.Contains(cur_point) {
					fmt.Println(Result{names[mod][i], url_names[mod][i], uuids[mod][i], feature_types[mod][i], lats[index], lons[index]})
					// fmt.Println("YOLO")
					// fmt.Println("INDEXES ISSUE=====>", mod,index,names,len(names[0]),len(names[1]))
					// fmt.Println("OUTPUT ==> ",strings.Index(string(content), names[mod][i]))
					// fmt.Println("INDEXES ISSUE=====>", mod,index,names)
					if strings.Index(string(content), names[mod][i]) != -1 {
						// fmt.Println("FOUND====>",names[mod][i])
						flag = 1
					}
				}
			}
			// fmt.Println("CHeck HERE")
		}
		if flag == 0 {
			not_match = not_match + 1
		} else {
			match = match + 1
		}
	}
	fmt.Println("Match and Not Match")
	fmt.Println(match, not_match, len(geohashes))
	// fmt.Println
	fmt.Println("Length of UUID")
	fmt.Println(len(names))

	return "Computed Everything"
}
示例#5
0
func display_regions_bulk(lats, lngs []string) {

	geohash_4 := []string{"tdr0", "tdr1", "tdr2", "tdr3", "tdr4", "tdsr", "tdst", "tdsv", "tdsw", "tdsx", "tdsy", "tdsz", "tdu2", "tdu3", "tdu8", "tdu9", "tdub", "te5z", "te7b", "te7c", "te7f", "tehp", "tek0", "tek1", "tepd", "tepe", "tepf", "tepg", "teps", "tg5h", "tg5j", "tg5k", "tgvt", "tgvu", "tgvv", "tgvw", "tgvx", "tgvy", "tgvz", "tgyh", "tgyj", "tgym", "tgyn", "tgyp", "tgyq", "tgyr", "tgyx", "tgyz", "thrj", "thrm", "thrn", "thrp", "thrq", "tsmy", "tsmz", "tsqn", "tsqp", "tsqq", "tsqr", "tstb", "tstc", "tstf", "tsw0", "tsw1", "tsw2", "tsw3", "tsw4", "ttjq", "ttjr", "ttjt", "ttjv", "ttjw", "ttjx", "ttjy", "ttm0", "ttm1", "ttm2", "ttnd", "ttne", "ttnf", "ttng", "ttnj", "ttnm", "ttnn", "ttns", "tuj8", "tuj9", "tujb", "tujc", "tujf", "tun0", "tun1", "tun2", "tun3", "tun4", "tun8"}
	geohash_5 := []string{"tdusb", "tdusc", "tdusf", "tdut0", "tdut1", "tdut2", "tdut3", "tdut4", "thrjh", "thrjj", "thrjk", "thrjm", "thrjn", "thrjs", "thrjt", "thrju", "thrjv", "thrnh", "thrnj", "thrnk", "ttnct", "ttncu", "ttncv", "ttncw", "ttncy", "ttnfh", "ttnfj", "tuqbb", "tuqbc", "tuqc0"}

	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})

	var latitudes []float64
	var longitudes []float64
	var geohash6 []string
	var results [][]Result
	pipeline := client.Pipeline()

	for i := 0; i < len(lats); i++ {
		latitude, _ := strconv.ParseFloat(lats[i], 64)
		longitude, _ := strconv.ParseFloat(lngs[i], 64)

		latitudes = append(latitudes, latitude)
		longitudes = append(longitudes, longitude)

		geohash_str6 := geohash.Encode(longitude, latitude, 6)
		geohash_str7 := geohash.Encode(longitude, latitude, 7)

		geohash6 = append(geohash6, geohash_str6)

		pipeline.LRange(geohash_str6, 0, -1)
		pipeline.LRange(geohash_str7, 0, -1)
	}

	cmds, err := pipeline.Exec()
	if err != nil {
		fmt.Println(err)
	}
	LENGTH := 2 * (len(latitudes))

	for index := 0; index < LENGTH; {
		var current_results []Result
		jindex := index
		for ; jindex < min(index+2, LENGTH); jindex++ {
			result := fmt.Sprintf("%s", cmds[jindex])
			redis_values := strings.Split(result, "&&")

			set := make(map[string]string)
			var feature_types []string
			var uuids []string
			var url_names []string
			var names []string
			var curpolygons []string

			if jindex%2 == 0 && stringInSlice(geohash6[jindex/2][0:4], geohash_4) {
				result_cmd := client.LRange(geohash6[jindex/2][0:4], 0, -1)
				result_4 := fmt.Sprintf("%s", result_cmd)
				redis_values_4 := strings.Split(result_4, "&&")
				redis_values = append(redis_values, redis_values_4...)
			}

			if jindex%2 == 0 && stringInSlice(geohash6[jindex/2][0:5], geohash_5) {
				result_cmd := client.LRange(geohash6[jindex/2][0:5], 0, -1)
				result_5 := fmt.Sprintf("%s", result_cmd)
				redis_values_5 := strings.Split(result_5, "&&")
				redis_values = append(redis_values, redis_values_5...)
			}

			for _, redis_value := range redis_values {
				index := strings.Index(redis_value, "[")
				if index != -1 {
					redis_value = redis_value[index+1 : len(redis_value)]
				}
				if len(redis_value) > 10 {
					value_array := strings.Split(redis_value, "::")
					// fmt.Println("Value_Array",value_array)
					md5 := md5.New()
					io.WriteString(md5, value_array[2])
					calc_md5 := GetMD5Hash(value_array[2])
					set[calc_md5] = value_array[2] + "--" + value_array[1] + "::" + value_array[3] + "::" + value_array[4] + "::" + value_array[0]
				} else {
					// fmt.Println("Geohash Not Found")
				}
			}

			for _, v := range set {
				set_values := strings.Split(v, "--")
				hash_polygon := set_values[0]
				hash_values := set_values[1]
				var begin, end int
				for i := 0; i < len(hash_polygon); i++ {
					if hash_polygon[i] == '(' {
						begin = i
					} else if '0' <= hash_polygon[i] && hash_polygon[i] <= '9' {
						break
					}
				}

				for i := len(hash_polygon) - 1; i >= 0; i-- {
					if hash_polygon[i] == ')' {
						end = i
					} else if '0' <= hash_polygon[i] && hash_polygon[i] <= '9' {
						break
					}
				}
				curpolygons = append(curpolygons, hash_polygon[begin+1:end])
				hash_values_array := strings.Split(hash_values, "::")
				feature_types = append(feature_types, hash_values_array[0])
				url_names = append(url_names, hash_values_array[1])
				names = append(names, hash_values_array[2])
				uuids = append(uuids, hash_values_array[3])

			}

			cur_point := geo.NewPoint(latitudes[jindex/2], longitudes[jindex/2])
			for index, curpolygon := range curpolygons {
				var points []string
				points = strings.Split(curpolygon, ",")
				polygon := ConstructPolygonFromPoints(points, uuids[index] == "3b399sdfa9baf4cc8c30e90")
				// fmt.Println("QUERY====>",cur_point,names[index],uuids[index])
				// if uuids[index] == "df1b0d8128f30883c4e5" {
				// // 	fmt.Println("POLYGON======>",curpolygon)
				// }
				if polygon.Contains(cur_point) {
					//If intersection is equal to TRUE
					// fmt.Println("FOUND===>",Result{names[index],url_names[index],uuids[index],feature_types[index],latitudes[jindex/2],longitudes[jindex/2]})
					current_results = append(current_results, Result{names[index], url_names[index], uuids[index], feature_types[index], latitudes[jindex/2], longitudes[jindex/2]})
				} else {
					// IF intersection is equal to FALSE
					// current_results = append(current_results,Result{"Not found this ->" + names[index],"Not found","Not found","Not found",latitudes[jindex/2],longitudes[jindex/2]})
				}
			}
		}
		results = append(results, current_results)
		index = jindex
	}
	fmt.Println(results)
}