package coryCommon import ( "fmt" "github.com/golang/geo/s2" toolTurf "github.com/tiger1103/gfast/v3/api/v1/common/tool/turf" "github.com/tomchavakis/geojson/geometry" "github.com/tomchavakis/turf-go" ) // DetailedMap shp文件数据 type DetailedMap struct { Positions []struct { Lng float64 `json:"lng"` Lat float64 `json:"lat"` Alt float64 `json:"alt"` } `json:"positions"` Width string `json:"width"` Color string `json:"color"` Alpha string `json:"alpha"` Name string `json:"name"` Property string `json:"property"` TxtMemo string `json:"TxtMemo"` ShapeLeng string `json:"Shape_Leng"` ShapeArea string `json:"Shape_Area"` Range struct { MinX float64 `json:"min_x"` MinY float64 `json:"min_y"` MaxX float64 `json:"max_x"` MaxY float64 `json:"max_y"` } `json:"range"` } // RectangularFrameRange 是否在矩形框范围内,是否在打卡范围内 func RectangularFrameRange(dataInfo DetailedMap, locationLng float64, locationLat float64) (flag bool) { //1、组装数据 84坐标 polygon := [][]float64{} for _, data := range dataInfo.Positions { polygon = append(polygon, []float64{data.Lng, data.Lat}) } //3、判断位置 distance := toolTurf.BooleanPointInPolygon([]float64{locationLng, locationLat}, polygon) if distance { fmt.Println("点在矩形框内") return true } else { fmt.Println("点在矩形框外") return false } } // FEIQI_RectangularFrameRange 是否在矩形框范围内,是否在打卡范围内 !!!!!!!!!!!!!!!!!有问题 func FEIQI_RectangularFrameRange(dataInfo DetailedMap, locationLng float64, locationLat float64) (flag bool) { //1、组装数据 var pl geometry.Polygon var ls []geometry.LineString var pt []geometry.Point for _, data := range dataInfo.Positions { wgs84 := LatLng{Latitude: data.Lat, Longitude: data.Lng} t1 := WGS84ToEPSG900913(wgs84) var p geometry.Point p.Lng = t1.Latitude p.Lat = t1.Longitude pt = append(pt, p) } var lsTwo geometry.LineString lsTwo.Coordinates = pt ls = append(ls, lsTwo) pl.Coordinates = append(pl.Coordinates, lsTwo) //2、当前人所在位置 locationLng, locationLat = GCJ02toWGS84(locationLng, locationLat) wgs84 := LatLng{Latitude: locationLng, Longitude: locationLat} t1 := WGS84ToEPSG900913(wgs84) myPoint := geometry.Point{ Lng: t1.Longitude, Lat: t1.Latitude, } //3、判断myPoint是否在pl框内 distance, _ := turf.PointInPolygon(myPoint, pl) if distance { fmt.Println("点在矩形框内") return true } else { fmt.Println("点在矩形框外") return false } } type LatLng struct { Latitude float64 Longitude float64 } // WGS84ToEPSG900913 将WGS84坐标转换为EPSG-900913坐标 func WGS84ToEPSG900913(wgs84 LatLng) LatLng { // 将WGS84坐标转换为s2.LatLng ll := s2.LatLngFromDegrees(wgs84.Latitude, wgs84.Longitude) // 创建s2.Point p := s2.PointFromLatLng(ll) // 计算EPSG-900913坐标 //epsgX, epsgY := p.Normalize() normalize := p.Normalize() // 将坐标范围映射到EPSG-900913的范围 epsgX := normalize.X * 20037508.34 / 180.0 epsgY := normalize.Y * 20037508.34 / 180.0 return LatLng{Latitude: epsgY, Longitude: epsgX} }