# _*_ coding: utf-8 _*_ #
# @Time: 2020/6/1 11:40
# @Author: XiongChao


from osgeo import ogr, osr
import math


def transform_to_utm(blh_dataset, source_srs):
    lon = blh_dataset[0][0]
    utm_zone = 31 + math.floor(lon / 6.0)
    utm_srs = osr.SpatialReference()
    utm_srs.SetUTM(utm_zone, 1)
    src_to_utm_trans = osr.CreateCoordinateTransformation(source_srs, utm_srs)
    utm_dataset = []
    for blh_point in blh_dataset:
        temp_point = blh_point
        utm_point = list(src_to_utm_trans.TransformPoint(temp_point[0], temp_point[1]))
        utm_point[2] = blh_point[2]
        utm_dataset.append(utm_point)
    return utm_dataset, utm_srs


def transform_to_src(utm_dataset, source_srs, utm_srs):
    utm_to_src_trans = osr.CreateCoordinateTransformation(utm_srs, source_srs)
    src_dataset = []
    for utm_point in utm_dataset:
        temp_point = utm_point
        src_point = list(utm_to_src_trans.TransformPoint(temp_point[0], temp_point[1]))
        src_point[2] = utm_point[2]
        src_dataset.append(src_point)
    return src_dataset


def create_line_geometry(dataset):
    geo = ogr.Geometry(ogr.wkbLineStringZM)
    for i in range(len(dataset)):
        geo.SetPoint(i, dataset[i][0], dataset[i][1], dataset[i][2])
    return geo


def create_point_geometry(point):
    geo = ogr.Geometry(ogr.wkbPointZM)
    geo.SetPoint(0, point[0], point[1], point[2])
    return geo