美文网首页
golang geohash

golang geohash

作者: hehehehe | 来源:发表于2020-10-06 17:47 被阅读0次
package main

import (
    "fmt"
    "github.com/jonas-p/go-shp"
    "github.com/mmcloughlin/geohash"
    "github.com/twpayne/go-geom"
    "log"
    "strconv"
)

func main() {
    lat := 30.549608
    lon := 114.376971
    hash_base32 := geohash.EncodeWithPrecision(lat, lon, 8)
    fmt.Println(hash_base32)

    neighbors := geohash.Neighbors(hash_base32)
    hashs := append(neighbors, hash_base32)

    geomMap := make(map[string]*geom.Polygon, 9)
    for _, hash := range hashs {
        box := geohash.BoundingBox(hash)
        polygon, _ := geom.NewPolygon(geom.XY).SetCoords([][]geom.Coord{
            {
                {box.MaxLng, box.MaxLat},
                {box.MaxLng, box.MinLat},
                {box.MinLng, box.MinLat},
                {box.MinLng, box.MaxLat},
                {box.MaxLng, box.MaxLat},
            }})
        geomMap[hash] = polygon
    }
    polygonMap := map[string]*shp.PolyLine{}
    for key, multiPlygon := range geomMap {
        coordsMultiPolygon := multiPlygon.Coords()
        points := make([][]shp.Point, len(coordsMultiPolygon), len(coordsMultiPolygon))
        for index, coordsPolygon := range coordsMultiPolygon {
            points2 := make([]shp.Point, len(coordsPolygon), len(coordsPolygon))
            for j, coord := range coordsPolygon {
                x := coord.X()
                y := coord.Y()
                point := shp.Point{x, y}
                points2[j] = point
            }
            points[index]=points2
        }
        polygonTemp := shp.NewPolyLine(points)
        polygonMap[key] = polygonTemp
    }

    // points to write

    fields := []shp.Field{
        // String attribute field with length 25
        shp.StringField("base_32", 25),
        shp.StringField("binary", 50),
    }
    // create and open a shapefile for writing points
    shape, err := shp.Create("F:/pop2pop/polygons.shp", shp.POLYGON)
    if err != nil {
        log.Fatal(err)
    }
    defer shape.Close()

    // setup fields for attributes
    shape.SetFields(fields)

    // write points and attributes
    cursor := 0
    for key, polygon := range polygonMap {
        shape.Write(polygon)
        // write attribute for object n for field 0 (NAME)
        toInt, _ := geohash.ConvertStringToInt(key)
        binary := fmt.Sprintf("%b", toInt)
        shape.WriteAttribute(cursor, 0, key)
        shape.WriteAttribute(cursor, 1, binary)
        cursor++;
    }


    points2 := []shp.Point{
        shp.Point{10.0, 10.0},
        shp.Point{10.0, 15.0},
        shp.Point{15.0, 15.0},
        shp.Point{15.0, 10.0},
    }

    // fields to write
    fields2 := []shp.Field{
        // String attribute field with length 25
        shp.StringField("NAME", 25),
    }

    // create and open a shapefile for writing points
    shape2, err := shp.Create("F:/pop2pop/points.shp", shp.POINT)
    if err != nil { log.Fatal(err) }
    defer shape2.Close()

    // setup fields for attributes
    shape2.SetFields(fields2)

    // write points and attributes
    for n, point := range points2 {
        shape2.Write(&point)
        // write attribute for object n for field 0 (NAME)
        shape2.WriteAttribute(n, 0, "Point " + strconv.Itoa(n + 1))
    }
}

相关文章

  • golang geohash

  • Laravel-GeoHash LBS地理位置距离计算方法geo

    Laravel GeoHash Laravel GeoHash LBS地理位置距离计算方法geohash,将一个经...

  • hash

    防卡哈希函数 hash: 题目链接:GeoHash一·编码解码 GeoHash:

  • Redis geo地理位置模块

    业界比较通用的地理位置排序算法是GeoHash算法,Redis也使用了GeoHash算法。GeoHash算法将二维...

  • 学习资料汇总

    GeoHash核心原理解析 GeoHash算法学习讲解、解析及原理分析

  • 正面刚算法-geohash(一)简明介绍geohash

    通俗的了解geohash 1.从地图直观上看geohash geohash 打个比方就是把世界地图平铺填充到一个矩...

  • Redis原理及实践之GeoHash

    2. 地理位置距离排序算法(GeoHash) GeoHash算法思想GeoHash算法将二维的经纬度数据映射到一维...

  • ClickHouse最近点查询优化

    方案一     暴力全表扫描 方案二     使用geohash进行表分区,然后搜索最近的geohash,最后扫描...

  • GeoHash

    对9个base32进行排序 [wt3mf9qe, wt3mf9qg, wt3mf9qs, wt3mf9qt, wt...

  • geohash

    1、简介:GeoHash是一种地址编码方法。他能够把二维的空间经纬度数据编码成一个字符串2、实现:网上相关原理介绍...

网友评论

      本文标题:golang geohash

      本文链接:https://www.haomeiwen.com/subject/pvbtpktx.html