Python 读取TIF文件,并根据鼠标点击获取经纬度

import cv2
import numpy as np
import os
from osgeo import gdal
from pyproj import Proj,Transformer, transform


res_dir = "/home/ktxx/uav_camp/output"
dir_list = os.listdir(res_dir)
dir_list.sort()
# print(dir_list[-1])
#tif图片路径
# img_path = '/home/ktxx/uav_camp/output/24-06-25_14-54-13大疆智图解算/mosaicing/ortho/ortho.tif'
# img_path = '/home/ktxx/uav_camp/output/24-06-26_15-43-57-4k/mosaicing/ortho/ortho.tif'
# img_path = '/home/ktxx/uav_camp/output/24-06-26_10-28-12抽帧航线/mosaicing/ortho/ortho.tif'
img_path = os.path.join(res_dir, dir_list[-1]) + "/mosaicing/ortho/ortho.tif"
print(img_path)
img = cv2.imread(img_path)
dataset = gdal.Open(img_path)  # 打开tif
geo_information = dataset.GetGeoTransform()
# 获取投影信息
projection = dataset.GetProjection()
print(projection)
col = dataset.RasterXSize
row = dataset.RasterYSize  
band = dataset.RasterCount
dem = dataset.GetRasterBand(1).ReadAsArray()
# print(dem)
count = 0


def cgcs2000_to_wgs84(x, y):
    global count
    count += 1
    # 定义CGCS2000坐标点
    cgcs2000_x = geo_information[0] + x * geo_information[1] + y * geo_information[2]
    cgcs2000_y = geo_information[3] + x * geo_information[4] + y * geo_information[5]
    # 转换到WGS84
    transformer = Transformer.from_crs("EPSG:32650", "EPSG:4326", always_xy=True)  # 4548 4326
    # transformer = Transformer.from_crs("EPSG:3857", "EPSG:4326", always_xy=True)  # 4548 4326
    # transformer = Transformer.from_crs("EPSG:4548", "EPSG:4326", always_xy=True)
    # 转换坐标
    lon, lat = transformer.transform(cgcs2000_x, cgcs2000_y)
    print(str(count) + ": " + f"UTM坐标:({cgcs2000_x}, {cgcs2000_y})")
    print(str(count) + ": " + f"转换后的WGS84坐标:经度 {lon}, 纬度 {lat}")
    return lon, lat
    # return cgcs2000_x, cgcs2000_y


def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        xy = "%d,%d" % (x, y)
        lon, lat = cgcs2000_to_wgs84(x, y)
        ttt = str('%.7f'%lon) + "," + str('%.7f'%lat)
        cv2.circle(img, (x, y), 1, (0, 0, 255), thickness=3)
        cv2.putText(img, ttt, (int(x), int(y)), cv2.FONT_HERSHEY_PLAIN, 1.0, (0, 0, 255), thickness=1)
        cv2.imshow("show", img)


cv2.namedWindow("show", 0)
cv2.resizeWindow("show", 1500, 1500)
cv2.setMouseCallback("show", on_EVENT_LBUTTONDOWN)
cv2.imshow("show", img)

k = cv2.waitKey(0) # waitkey代表读取键盘的输入,括号里的数字代表等待多长时间,单位ms。 0代表一直等待
if k == 27:     # 键盘上Esc键的键值
   cv2.destroyAllWindows() 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值