-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtile2gps.py
165 lines (137 loc) · 5.11 KB
/
tile2gps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding:utf-8
import numpy as np
import math
import os
import argparse
import sys
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Transfor a Latitude and longitude coordinates')
parser.add_argument('--txt_dir', dest='txt_name', help='detectron result txt file name',
default='/home/data/merged/724/test_tree.txt', type=str)
parser.add_argument('--result_file_name', dest='file_name', help='lat and lon result file name',
default='/home/data/merged/724/GPSData.txt', type=str)
parser.add_argument('--out_npy', dest='out_npy', help='lat and lon result file name',
default='/home/data/merged/724/list.npy', type=str)
parser.add_argument('--npy_file_path', dest='npy_file_path', help='merger pic gen npy file path',
default='/home/data/merged/724/npy/', type=str)
# if len(sys.argv) == 1:
# parser.print_help()
# sys.exit(1)
args = parser.parse_args()
return args
def num2deg(xtile, ytile, xpixel, ypixel, zoom):
"""
#output every pixel deg in tile
:param xtile:
:param ytile:
:param xpixel: 是瓦片的目录
:param ypixel: 是目录下的文件
:param zoom:
:return:
"""
n = 2.0 ** zoom
# 经度
lon_deg = (((xtile + (xpixel / 256)) / n) * 360.0) - 180.0
# 纬度
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * (ytile + (ypixel / 256)) / n)))
lat_deg = math.degrees(lat_rad)
return lat_deg, lon_deg
def split_txt(txt_name):
"""
#读取生成的检测文件
:param txt_name:
:return:
"""
source_file = open(txt_name)
lines = source_file.readlines()
img_names = {}
for line in lines:
staff = line.split()
# 照片名称
name = staff[0]
# 置信度
conf = staff[1]
# point1 点位置
x0 = staff[2]
y0 = staff[3]
# point2的点位置
x1 = staff[4]
y1 = staff[5]
if not name in img_names.keys():
img_names[name] = [x0 + " " + y0 + " " + x1 + " " + y1]
else:
img_names[name].append(x0 + " " + y0 + " " + x1 + " " + y1)
return img_names
def gen_gps(txt_name, npy_file_path, zoom):
"""
:param txt_name:
:param zoom:
:return:
"""
img_names = split_txt(txt_name)
list = []
# idx是照片名称,value是idx下所有的点列表
for idx, value in img_names.items():
tile = np.load(os.path.join(npy_file_path, idx + ".npy"), allow_pickle=True)
for line in value:
box = line.split()
# 每个框的大小W和H
Wieth = int(np.round(float(box[3]))) - int(np.round(float(box[1])))
Hight = int(np.round(float(box[2]))) - int(np.round(float(box[0])))
# 取出每一个框的中心点坐标(x,y)
xpixel = (int(np.round(float(box[0]))) + int(np.round(float(box[2])))) / 2
ypixel = (int(np.round(float(box[1]))) + int(np.round(float(box[3])))) / 2
# 像素点所在2048图片的位置在(x行y列)
x = int(xpixel / 256)
y = int(ypixel / 256)
# 相对于256图片的像素
xpixel = xpixel - (x * 256)
ypixel = ypixel - (y * 256)
ypixel = 256 - ypixel
# 读取保存的瓦片编号文件的x行y列就是瓦片的编号
tile_coord = tile[x][y]
try:
xtile = int(tile_coord[0])
ytile = int(tile_coord[1])
except Exception as e:
print(x, y, tile_coord, idx, line)
continue
# 每个像素坐标转换成经纬度
lat_deg, lon_deg = num2deg(xtile, ytile, xpixel, ypixel, zoom)
lat_deg = abs(lat_deg)
lon_deg = abs(lon_deg)
list.append([xtile, ytile, zoom, lon_deg, lat_deg, Wieth, Hight, idx])
return list
def _tiletogps():
lst = []
for idx in range(1, 91):
tile = np.load(os.path.join(args.npy_file_path, str(idx).zfill(6) + ".npy"))
for x in range(8):
for y in range(8):
tile_coord = tile[x][y]
xtile = tile_coord[0]
ytile = tile_coord[1]
lat_deg, lon_deg = num2deg(xtile, ytile, 0, 0, zoom=21)
lst.append([xtile, ytile, 21, abs(lat_deg), abs(lon_deg), 0, 0])
return lst
def gen_gps_npy(conf):
lst = gen_gps(conf.get('result_txt_path'), conf.get('merge_npy_path'), conf.get('z_tile'))
np.save(conf.get('result_lat_lon_npy_path'), lst)
if __name__ == '__main__':
num2deg(1718533, 1202941, 0, 0, 21)
if __name__ == '__main__2':
args = parse_args()
print('Called with args:')
print(args)
lst = gen_gps(args.txt_name, 21)
# lst = tiletogps()
with open(os.path.join(args.file_name), 'w') as f:
for i in lst:
f.write('{} {} {} {} {} {} {}\n'.format(i[0], i[1], i[2], i[3], i[4], i[5], i[6]))
np.save(args.out_npy, lst)
# tmp = num2deg(859267, 601480, 0, 0, 20)
# print(tmp)
print("----------------")