-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlueMarbleUtils.py
338 lines (221 loc) · 11.3 KB
/
BlueMarbleUtils.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import math
import os.path
from vipsCC import *
from PIL import Image
__author__ = 'Daniel Caldwell'
__license__ = 'MIT'
__copyright__ = 'Daniel Caldwell'
class BlueMarbleUrls:
s_base_url = 'ftp://neoftp.sci.gsfc.nasa.gov/bluemarble/bmng/world_500m/'
def __init__(self, month, has_topo, has_bathy ):
self.m_has_topo = has_topo
self.m_has_bathy = has_bathy
self.m_month = month
def GenerateUrl(self, Quadrant ):
url = self.s_base_url
url += 'world.'
if ( self.m_has_topo):
url += 'topo.'
if ( self.m_has_bathy ):
url += 'bathy.'
url += '2004'
url += '%(month)02d' % { 'month': self.m_month}
url += '.3x21600x21600.'
url += Quadrant
url += '.png'
return url
def A1(self):
return self.GenerateUrl('A1')
def A2(self):
return self.GenerateUrl('A2')
def B1(self):
return self.GenerateUrl('B1')
def B2(self):
return self.GenerateUrl('B2')
def C1(self):
return self.GenerateUrl('C1')
def C2(self):
return self.GenerateUrl('C2')
def D1(self):
return self.GenerateUrl('D1')
def D2(self):
return self.GenerateUrl('D2')
def HorizontalQuadrantToOrigin( HorizontalQuadrant):
if ( HorizontalQuadrant == "A" ):
return -180
if ( HorizontalQuadrant == "B"):
return -90
if ( HorizontalQuadrant == "C"):
return 0
if ( HorizontalQuadrant == "D"):
return 90
def VerticalQuadrantToOrigin( VerticalQuadrant ):
if ( VerticalQuadrant == "1"):
return 0
else:
return -90
# Calculate the pixels given the lon lat values
def CalculatePixel( lon, lat, HorizontalQuadrant, VerticalQuadrant, height=21600, width=21600):
# get the origins and shift them to 0-180 for vertical and 0-360 for horizontal
horizontalOrigin = HorizontalQuadrantToOrigin(HorizontalQuadrant) + 180
verticalOrigin = VerticalQuadrantToOrigin(VerticalQuadrant) + 90
targetLon = lon + 180
targetLat = lat + 90
# subtract the origin to get where it would be in that tile
targetLon = targetLon - horizontalOrigin
targetLat = targetLat - verticalOrigin
# print "Adjusted Lon/Lat = ( %s, %s )" % (targetLon, targetLat)
# cross multiply and divide
x = (21600 * targetLon ) / 90
# change lat scale from (-90 to 90) to (0 - 180)
y = 21600 - (21600 * targetLat ) / 90
# print "Calculated Pixel for ( %s, %s ) == ( %s, %s )" % (lon, lat, x, y)
x = int(x)
y = int(y)
# print "Image Pixel for ( %s, %s ) == ( %s, %s )" % (lon, lat, x, y)
return (x, y)
def CalculateDegreesForTile( xTile, yTile, zoomLevel ):
# number of tiles vertical or horizontal, not total for the whole zoom level.
numberOfTiles = 2.0 ** zoomLevel
# lon_deg = xTile / n * 360.0 - 180.0
# lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * yTile / n)))
# lat_deg = math.degrees(lat_rad)
minLat = math.degrees( math.atan( math.sinh( math.pi * ( 1 - 2 * (yTile + 1) / numberOfTiles ))) )
maxLat = math.degrees( math.atan( math.sinh( math.pi * ( 1 - 2 * yTile / numberOfTiles ))) )
# to calculate the degrees, we find where the tile is long the x axis from 0 -> nNumberOfTiles
# as a percentage. (0.5 from 0 - 256 would be 128)
# multiply that by 360 to get where it would be on a world between 0 and 360
# subtract 180 as our degree coordinates are between -180 and 180
minLon = (xTile / numberOfTiles) * 360.0 - 180.0
maxLon = (xTile + 1) / numberOfTiles * 360.0 - 180.0
return (minLat, minLon, maxLat, maxLon)
def LongitudeWithinQuadrant( HorizontalQuadrant, lon ):
if ( lon >= -180 and lon <= -90 and HorizontalQuadrant == "A" ):
return True
if ( lon >= -90 and lon <= 0 and HorizontalQuadrant == "B"):
return True
if ( lon >= 0 and lon <= 90 and HorizontalQuadrant == "C" ):
return True
if ( lon >= 90 and lon <= 180 and HorizontalQuadrant == "D" ):
return True
return False
def LatitudeWithinQuadrant( VerticalQuadrant, lat ):
if ( lat <= 90 and lat >= 0 and VerticalQuadrant == "1" ):
return True
if ( lat <= 0 and lat >= -90 and VerticalQuadrant == "2" ):
return True
return False
def IsWithinTile( HorizontalQuadrant, VerticalQuadrant, lon, lat ):
if ( LongitudeWithinQuadrant ( HorizontalQuadrant, lon ) and
LatitudeWithinQuadrant ( VerticalQuadrant, lat )):
return True
else:
return False
def cutTiles(filePath, destinationDirectory, zoomLevel, HorizontalQuadrant, VerticalQuadrant ):
if( HorizontalQuadrant != "A" and HorizontalQuadrant != "B" and HorizontalQuadrant != "C" and HorizontalQuadrant != "D"):
print ("invalid hemisphere")
return
if ( VerticalQuadrant != "1" and VerticalQuadrant != "2"):
print ("invalid horizontal quadrant")
return
currentImage = VImage.VImage(filePath)
# 64 is the number of tiles at 90 degrees
# for zoomLevel in range (5,10):
tileCount = 2 ** zoomLevel
verticalTileCount = tileCount
horizontalTileCount = tileCount
for verticalTileIndex in range( 0, verticalTileCount):
for horizontalTileIndex in range (0, horizontalTileCount ):
# latitude is vertical and longitude is horizontal... :p
minLat, minLon, maxLat, maxLon = CalculateDegreesForTile(horizontalTileIndex, verticalTileIndex, zoomLevel )
if not ( IsWithinTile( HorizontalQuadrant, VerticalQuadrant, minLon, minLat ) and \
IsWithinTile(HorizontalQuadrant, VerticalQuadrant, maxLon, maxLat ) ) :
# print "Tile ( %s, %s ) from ( %s, %s ) to ( %s, %s ) is not within Quadrant ( %s, %s )" % (horizontalTileIndex, verticalTileIndex, minLon, minLat, maxLon, maxLat, HorizontalQuadrant, VerticalQuadrant)
continue
print "Calculated Lon, Lat for Tile (%s, %s) = (%s, %s) to (%s, %s)" % (horizontalTileIndex, verticalTileIndex, minLon, minLat, maxLon, maxLat )
minPixelX, minPixelY = CalculatePixel( minLon, minLat, HorizontalQuadrant, VerticalQuadrant )
maxPixelX, maxPixelY = CalculatePixel( maxLon, maxLat, HorizontalQuadrant, VerticalQuadrant )
print "Calculated Pixel Bounds for Tile ( %s, %s ) = ( %s, %s ) to ( %s, %s )" % (horizontalTileIndex, verticalTileIndex, minPixelX, minPixelY, maxPixelX, maxPixelY)
realTileX = horizontalTileIndex
realTileY = verticalTileIndex
if ( HorizontalQuadrant == "A"):
# do nothing, the coordinates are already in tile A1
i=0
elif ( HorizontalQuadrant == "B"):
realTileX += (horizontalTileCount * 1)
elif ( HorizontalQuadrant == "C"):
realTileX += (horizontalTileCount * 2)
elif ( HorizontalQuadrant == "D"):
realTileX += (horizontalTileCount * 3)
if ( VerticalQuadrant == "1"):
# do nothing, the coordinates are already in tile A1
i=0
elif( VerticalQuadrant == "2"):
realTileY += (verticalTileCount * 1)
# print " Extracting Image for tile (%s, %s) from area (%s, %s) - (%s, %s)" % (realTileX, realTileY, minPixelX, minPixelY, maxPixelX, maxPixelY )
# print " Extracted Tile Will be: ( %s, %s )" % ( maxPixelX - minPixelX, maxPixelY - minPixelY)
# extract the sub image
height = minPixelY - maxPixelY
width = maxPixelX - minPixelX
print "Extracting Image at ( %s, %s ) with dimensions ( %s, %s ) " % (minPixelX, maxPixelY, width, height)
# use min x and max y as coordinate system is inverted...
extractedImage = currentImage.extract_area(minPixelX, maxPixelY, width, height )
print "Extracted Image Size: ( %s, %s )" % ( extractedImage.Xsize(), extractedImage.Ysize() )
scaleHeight = 256.0 / height
scaleWidth = 256.0 / width
print "Scaling Image for tile by (%s, %s)" % (scaleWidth, scaleHeight)
# scale to web mercator's 256x256
scaledImage = extractedImage.resize_linear( 256, 256 )
print "Saving tile as tile ( %s, %s )" % (horizontalTileIndex, verticalTileIndex)
newDirPath = "./%s/%s/%s" % ( destinationDirectory, zoomLevel, horizontalTileIndex)
if not os.path.exists(newDirPath):
os.makedirs(newDirPath)
newFilePath = "%s/%s.png" % (newDirPath, verticalTileIndex)
scaledImage.write(newFilePath)
print ""
def mergeToZoomLevel(tileCacheRootDirectory, sourceZoomLevel, targetZoomLevel ):
sourceDirectory = tileCacheRootDirectory + "/" + str(sourceZoomLevel)
if not (os.path.exists(sourceDirectory)):
print "Source Directory : %s doesn't exist" % ( sourceDirectory )
return
if not (int(sourceZoomLevel) > int(targetZoomLevel)):
print "Target Zoom Level: %s , must be less than Source Zoom Level: %s " % sourceZoomLevel, targetZoomLevel
sourceTileEdgeCount = 2 ** int(sourceZoomLevel);
targetTileEdgeCount = 2 ** int(targetZoomLevel)
# number of photos to merge to get to the lower zoom level
# distance of 1 = 2 x 2 = 4
# distance of 2 = 4 x 4 = 16
# distance of 3 = 8 x 8 = 64
# distance of n = 2^n
mergeCountEdge = 2 ** (int(sourceZoomLevel) - int(targetZoomLevel))
mergeCountTotal = mergeCountEdge * mergeCountEdge
for targetTileIndexX in range ( 0, targetTileEdgeCount ):
for targetTileIndexY in range(0, targetTileEdgeCount ):
print "Creating Image for Tile ( %s, %s, %s )" % (targetZoomLevel, targetTileIndexX, targetTileIndexY)
sourceStartTileX = mergeCountEdge * targetTileIndexX
sourceStartTileY = mergeCountEdge * targetTileIndexY
sourceEndTileX = sourceStartTileX + mergeCountEdge
sourceEndTileY = sourceStartTileY + mergeCountEdge
newImage = Image.new("RGB", (mergeCountEdge * 256, mergeCountEdge * 256))
xCount = 0
yCount = 0
for sourceTileIndexX in range ( sourceStartTileX, sourceEndTileX ):
for sourceTileIndexY in range ( sourceStartTileY, sourceEndTileY ):
imagePath = sourceDirectory + "/" + str(sourceTileIndexX) + "/" + str(sourceTileIndexY) + ".png"
currentSourceImage = Image.open(imagePath)
currentPixelX = xCount * 256
currentPixelY = yCount * 256
newImage.paste( currentSourceImage, (currentPixelX, currentPixelY) )
yCount = yCount + 1
xCount = xCount + 1
yCount = 0
newImage = newImage.resize( (256, 256), Image.BICUBIC )
targetFileDirectory = tileCacheRootDirectory + "/" + str(targetZoomLevel) + "/" + str(targetTileIndexX)
if not ( os.path.exists(targetFileDirectory)):
os.makedirs( targetFileDirectory )
targetFilePath = "./" + targetFileDirectory + "/" + str(targetTileIndexY) + ".png"
try :
# newImage.save( "test.png", "png" )
newImage.save( targetFilePath, "png" )
except IOError as err:
print(str(err))