generated from moja-global/Import-Me
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbound_to_blocks.py
145 lines (102 loc) · 3.22 KB
/
bound_to_blocks.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
import fiona
import math
import json
def convertToLngOrigin(value):
return (value + 180.0)
def convertToLatOrigin(value):
return (-value + 90.0)
def convOtoI(o, partSize):
k0Plus = 0.000000001
return int((o / partSize) + k0Plus)
def generateTileBlockCellID(lat, lng):
tilelonSize = 1.0
tilelatSize = 1.0
tilecols = 360.0
blocklonSize = 0.1
blocklatSize = 0.1
blockcols = 10.0
oLon = convertToLngOrigin(lng)
oLat = convertToLatOrigin(lat)
tileX = (convOtoI(oLon, tilelonSize))
tileY = (convOtoI(oLat, tilelatSize))
blockX = convOtoI(oLon-(tileX*tilelonSize), blocklonSize)
blockY = convOtoI(oLat-(tileY*tilelatSize), blocklatSize)
tileIdx = tileY*tilecols + tileX
blockIdx = float(blockY)*blockcols + float(blockX)
return int(tileIdx), int(blockIdx)
#!/usr/bin/python
import sys, getopt
def main(argv):
inputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('test.py -i <inputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('bound_to_blocks.py -i <inputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
print ('Input file is: ', inputfile)
src = fiona.open(inputfile)
bounds = src.bounds
print("feature bounds:", src.bounds)
lon = bounds[0]
lat = bounds[1]
left_down = generateTileBlockCellID(lat, lon)
print("left_down:", left_down)
lon = bounds[2]
lat = bounds[3]
right_above = generateTileBlockCellID(lat, lon)
print("right_above:",right_above)
lon = bounds[0]
lat = bounds[3]
left_above = generateTileBlockCellID(lat, lon)
print("left_above:",left_above)
lon = bounds[2]
lat = bounds[1]
right_down = generateTileBlockCellID(lat, lon)
print("right_down:",right_down)
mintile = left_above[0]
minblock = left_above[1]
maxtile = right_down[0]
maxblock = right_down[1]
tile = mintile
block = minblock
blocklist = [{"tile":tile, "block":block}]
print("start block:", tile, block)
tmp_right_block_bound = right_above[1]
tmp_left_block_bound = minblock
tmp_left_tile_bound = mintile
tmp_right_tile_bound = right_above[0]
while not (tile == maxtile and block == maxblock):
if not (block == tmp_right_block_bound and tile == tmp_right_tile_bound):
if block % 10 == 9:
tile +=1
block -= 9
blocklist.append({"tile":tile, "block":block})
else:
block +=1
blocklist.append({"tile":tile, "block":block})
else:
tmp_left_tile_bound = tmp_left_tile_bound + 360
tmp_right_tile_bound = tmp_right_tile_bound + 360
if math.floor(tmp_left_block_bound/10) == 9:
tmp_left_block_bound = tmp_left_block_bound - 90
else:
tmp_left_block_bound = tmp_left_block_bound + 10
tile = tmp_left_tile_bound
block = tmp_left_block_bound
blocklist.append({"tile":tile, "block":block})
if math.floor(tmp_right_block_bound/10) == 9:
tmp_right_block_bound = tmp_right_block_bound - 90
else:
tmp_right_block_bound = tmp_right_block_bound + 10
print("end block:", tile, block)
print("blocklist: ", blocklist)
with open('blocklist.json', 'w',encoding='utf-8') as f:
json.dump(blocklist,f, ensure_ascii=False, indent=4)
if __name__ == "__main__":
main(sys.argv[1:])