-
Notifications
You must be signed in to change notification settings - Fork 12
/
gdal2tiles.py
2202 lines (1819 loc) · 94.2 KB
/
gdal2tiles.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#******************************************************************************
# $Id: gdal2tiles.py 19288 2010-04-02 18:36:17Z rouault $
#
# Project: Google Summer of Code 2007, 2008 (http://code.google.com/soc/)
# Support: BRGM (http://www.brgm.fr)
# Purpose: Convert a raster into TMS (Tile Map Service) tiles in a directory.
# - generate Google Earth metadata (KML SuperOverlay)
# - generate simple HTML viewer based on Google Maps and OpenLayers
# - support of global tiles (Spherical Mercator) for compatibility
# with interactive web maps a la Google Maps
# Author: Klokan Petr Pridal, klokan at klokan dot cz
# Web: http://www.klokan.cz/projects/gdal2tiles/
# GUI: http://www.maptiler.org/
#
###############################################################################
# Copyright (c) 2008, Klokan Petr Pridal
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#******************************************************************************
import sys
try:
from osgeo import gdal
from osgeo import osr
except:
import gdal
print('You are using "old gen" bindings. gdal2tiles needs "new gen" bindings.')
sys.exit(1)
import os
import math
try:
from PIL import Image
import numpy
import osgeo.gdal_array as gdalarray
except:
# 'antialias' resampling is not available
pass
__version__ = "$Id: gdal2tiles.py 19288 2010-04-02 18:36:17Z rouault $"
resampling_list = ('average','near','bilinear','cubic','cubicspline','lanczos','antialias')
profile_list = ('mercator','geodetic','raster') #,'zoomify')
webviewer_list = ('all','google','openlayers','none')
# =============================================================================
# =============================================================================
# =============================================================================
__doc__globalmaptiles = """
globalmaptiles.py
Global Map Tiles as defined in Tile Map Service (TMS) Profiles
==============================================================
Functions necessary for generation of global tiles used on the web.
It contains classes implementing coordinate conversions for:
- GlobalMercator (based on EPSG:900913 = EPSG:3785)
for Google Maps, Yahoo Maps, Microsoft Maps compatible tiles
- GlobalGeodetic (based on EPSG:4326)
for OpenLayers Base Map and Google Earth compatible tiles
More info at:
http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
http://wiki.osgeo.org/wiki/WMS_Tiling_Client_Recommendation
http://msdn.microsoft.com/en-us/library/bb259689.aspx
http://code.google.com/apis/maps/documentation/overlays.html#Google_Maps_Coordinates
Created by Klokan Petr Pridal on 2008-07-03.
Google Summer of Code 2008, project GDAL2Tiles for OSGEO.
In case you use this class in your product, translate it to another language
or find it usefull for your project please let me know.
My email: klokan at klokan dot cz.
I would like to know where it was used.
Class is available under the open-source GDAL license (www.gdal.org).
"""
MAXZOOMLEVEL = 32
TILESIZE = 256
class GlobalMercator(object):
"""
TMS Global Mercator Profile
---------------------------
Functions necessary for generation of tiles in Spherical Mercator projection,
EPSG:900913 (EPSG:gOOglE, Google Maps Global Mercator), EPSG:3785, OSGEO:41001.
Such tiles are compatible with Google Maps, Microsoft Virtual Earth, Yahoo Maps,
UK Ordnance Survey OpenSpace API, ...
and you can overlay them on top of base maps of those web mapping applications.
Pixel and tile coordinates are in TMS notation (origin [0,0] in bottom-left).
What coordinate conversions do we need for TMS Global Mercator tiles::
LatLon <-> Meters <-> Pixels <-> Tile
WGS84 coordinates Spherical Mercator Pixels in pyramid Tiles in pyramid
lat/lon XY in metres XY pixels Z zoom XYZ from TMS
EPSG:4326 EPSG:900913
.----. --------- -- TMS
/ \ <-> | | <-> /----/ <-> Google
\ / | | /--------/ QuadTree
----- --------- /------------/
KML, public WebMapService Web Clients TileMapService
What is the coordinate extent of Earth in EPSG:900913?
[-20037508.342789244, -20037508.342789244, 20037508.342789244, 20037508.342789244]
Constant 20037508.342789244 comes from the circumference of the Earth in meters,
which is 40 thousand kilometers, the coordinate origin is in the middle of extent.
In fact you can calculate the constant as: 2 * math.pi * 6378137 / 2.0
$ echo 180 85 | gdaltransform -s_srs EPSG:4326 -t_srs EPSG:900913
Polar areas with abs(latitude) bigger then 85.05112878 are clipped off.
What are zoom level constants (pixels/meter) for pyramid with EPSG:900913?
whole region is on top of pyramid (zoom=0) covered by 256x256 pixels tile,
every lower zoom level resolution is always divided by two
initialResolution = 20037508.342789244 * 2 / 256 = 156543.03392804062
What is the difference between TMS and Google Maps/QuadTree tile name convention?
The tile raster itself is the same (equal extent, projection, pixel size),
there is just different identification of the same raster tile.
Tiles in TMS are counted from [0,0] in the bottom-left corner, id is XYZ.
Google placed the origin [0,0] to the top-left corner, reference is XYZ.
Microsoft is referencing tiles by a QuadTree name, defined on the website:
http://msdn2.microsoft.com/en-us/library/bb259689.aspx
The lat/lon coordinates are using WGS84 datum, yeh?
Yes, all lat/lon we are mentioning should use WGS84 Geodetic Datum.
Well, the web clients like Google Maps are projecting those coordinates by
Spherical Mercator, so in fact lat/lon coordinates on sphere are treated as if
the were on the WGS84 ellipsoid.
From MSDN documentation:
To simplify the calculations, we use the spherical form of projection, not
the ellipsoidal form. Since the projection is used only for map display,
and not for displaying numeric coordinates, we don't need the extra precision
of an ellipsoidal projection. The spherical projection causes approximately
0.33 percent scale distortion in the Y direction, which is not visually noticable.
How do I create a raster in EPSG:900913 and convert coordinates with PROJ.4?
You can use standard GIS tools like gdalwarp, cs2cs or gdaltransform.
All of the tools supports -t_srs 'epsg:900913'.
For other GIS programs check the exact definition of the projection:
More info at http://spatialreference.org/ref/user/google-projection/
The same projection is degined as EPSG:3785. WKT definition is in the official
EPSG database.
Proj4 Text:
+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0
+k=1.0 +units=m +nadgrids=@null +no_defs
Human readable WKT format of EPGS:900913:
PROJCS["Google Maps Global Mercator",
GEOGCS["WGS 84",
DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,
AUTHORITY["EPSG","7030"]],
AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],
UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]],
PROJECTION["Mercator_1SP"],
PARAMETER["central_meridian",0],
PARAMETER["scale_factor",1],
PARAMETER["false_easting",0],
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]]]
"""
def __init__(self, tileSize=256):
"Initialize the TMS Global Mercator pyramid"
self.tileSize = tileSize
self.initialResolution = 2 * math.pi * 6378137 / self.tileSize
# 156543.03392804062 for tileSize 256 pixels
self.originShift = 2 * math.pi * 6378137 / 2.0
# 20037508.342789244
def LatLonToMeters(self, lat, lon ):
"Converts given lat/lon in WGS84 Datum to XY in Spherical Mercator EPSG:900913"
mx = lon * self.originShift / 180.0
my = math.log( math.tan((90 + lat) * math.pi / 360.0 )) / (math.pi / 180.0)
my = my * self.originShift / 180.0
return mx, my
def MetersToLatLon(self, mx, my ):
"Converts XY point from Spherical Mercator EPSG:900913 to lat/lon in WGS84 Datum"
lon = (mx / self.originShift) * 180.0
lat = (my / self.originShift) * 180.0
lat = 180 / math.pi * (2 * math.atan( math.exp( lat * math.pi / 180.0)) - math.pi / 2.0)
return lat, lon
def PixelsToMeters(self, px, py, zoom):
"Converts pixel coordinates in given zoom level of pyramid to EPSG:900913"
res = self.Resolution( zoom )
mx = px * res - self.originShift
my = py * res - self.originShift
return mx, my
def MetersToPixels(self, mx, my, zoom):
"Converts EPSG:900913 to pyramid pixel coordinates in given zoom level"
res = self.Resolution( zoom )
px = (mx + self.originShift) / res
py = (my + self.originShift) / res
return px, py
def PixelsToTile(self, px, py):
"Returns a tile covering region in given pixel coordinates"
tx = int( math.ceil( px / float(self.tileSize) ) - 1 )
ty = int( math.ceil( py / float(self.tileSize) ) - 1 )
return tx, ty
def PixelsToRaster(self, px, py, zoom):
"Move the origin of pixel coordinates to top-left corner"
mapSize = self.tileSize << zoom
return px, mapSize - py
def MetersToTile(self, mx, my, zoom):
"Returns tile for given mercator coordinates"
px, py = self.MetersToPixels( mx, my, zoom)
return self.PixelsToTile( px, py)
def TileBounds(self, tx, ty, zoom):
"Returns bounds of the given tile in EPSG:900913 coordinates"
minx, miny = self.PixelsToMeters( tx*self.tileSize, ty*self.tileSize, zoom )
maxx, maxy = self.PixelsToMeters( (tx+1)*self.tileSize, (ty+1)*self.tileSize, zoom )
return ( minx, miny, maxx, maxy )
def TileLatLonBounds(self, tx, ty, zoom ):
"Returns bounds of the given tile in latutude/longitude using WGS84 datum"
bounds = self.TileBounds( tx, ty, zoom)
minLat, minLon = self.MetersToLatLon(bounds[0], bounds[1])
maxLat, maxLon = self.MetersToLatLon(bounds[2], bounds[3])
return ( minLat, minLon, maxLat, maxLon )
def Resolution(self, zoom ):
"Resolution (meters/pixel) for given zoom level (measured at Equator)"
# return (2 * math.pi * 6378137) / (self.tileSize * 2**zoom)
return self.initialResolution / (2**zoom)
def ZoomForPixelSize(self, pixelSize ):
"Maximal scaledown zoom of the pyramid closest to the pixelSize."
for i in range(MAXZOOMLEVEL):
if pixelSize > self.Resolution(i):
if i!=0:
return i-1
else:
return 0 # We don't want to scale up
def GoogleTile(self, tx, ty, zoom):
"Converts TMS tile coordinates to Google Tile coordinates"
# coordinate origin is moved from bottom-left to top-left corner of the extent
return tx, (2**zoom - 1) - ty
def QuadTree(self, tx, ty, zoom ):
"Converts TMS tile coordinates to Microsoft QuadTree"
quadKey = ""
ty = (2**zoom - 1) - ty
for i in range(zoom, 0, -1):
digit = 0
mask = 1 << (i-1)
if (tx & mask) != 0:
digit += 1
if (ty & mask) != 0:
digit += 2
quadKey += str(digit)
return quadKey
#---------------------
class GlobalGeodetic(object):
"""
TMS Global Geodetic Profile
---------------------------
Functions necessary for generation of global tiles in Plate Carre projection,
EPSG:4326, "unprojected profile".
Such tiles are compatible with Google Earth (as any other EPSG:4326 rasters)
and you can overlay the tiles on top of OpenLayers base map.
Pixel and tile coordinates are in TMS notation (origin [0,0] in bottom-left).
What coordinate conversions do we need for TMS Global Geodetic tiles?
Global Geodetic tiles are using geodetic coordinates (latitude,longitude)
directly as planar coordinates XY (it is also called Unprojected or Plate
Carre). We need only scaling to pixel pyramid and cutting to tiles.
Pyramid has on top level two tiles, so it is not square but rectangle.
Area [-180,-90,180,90] is scaled to 512x256 pixels.
TMS has coordinate origin (for pixels and tiles) in bottom-left corner.
Rasters are in EPSG:4326 and therefore are compatible with Google Earth.
LatLon <-> Pixels <-> Tiles
WGS84 coordinates Pixels in pyramid Tiles in pyramid
lat/lon XY pixels Z zoom XYZ from TMS
EPSG:4326
.----. ----
/ \ <-> /--------/ <-> TMS
\ / /--------------/
----- /--------------------/
WMS, KML Web Clients, Google Earth TileMapService
"""
def __init__(self, tileSize = 256):
self.tileSize = tileSize
def LatLonToPixels(self, lat, lon, zoom):
"Converts lat/lon to pixel coordinates in given zoom of the EPSG:4326 pyramid"
res = 180.0 / self.tileSize / 2**zoom
px = (180 + lat) / res
py = (90 + lon) / res
return px, py
def PixelsToTile(self, px, py):
"Returns coordinates of the tile covering region in pixel coordinates"
tx = int( math.ceil( px / float(self.tileSize) ) - 1 )
ty = int( math.ceil( py / float(self.tileSize) ) - 1 )
return tx, ty
def LatLonToTile(self, lat, lon, zoom):
"Returns the tile for zoom which covers given lat/lon coordinates"
px, py = self.LatLonToPixels( lat, lon, zoom)
return self.PixelsToTile(px,py)
def Resolution(self, zoom ):
"Resolution (arc/pixel) for given zoom level (measured at Equator)"
return 180.0 / self.tileSize / 2**zoom
#return 180 / float( 1 << (8+zoom) )
def ZoomForPixelSize(self, pixelSize ):
"Maximal scaledown zoom of the pyramid closest to the pixelSize."
for i in range(MAXZOOMLEVEL):
if pixelSize > self.Resolution(i):
if i!=0:
return i-1
else:
return 0 # We don't want to scale up
def TileBounds(self, tx, ty, zoom):
"Returns bounds of the given tile"
res = 180.0 / self.tileSize / 2**zoom
return (
tx*self.tileSize*res - 180,
ty*self.tileSize*res - 90,
(tx+1)*self.tileSize*res - 180,
(ty+1)*self.tileSize*res - 90
)
def TileLatLonBounds(self, tx, ty, zoom):
"Returns bounds of the given tile in the SWNE form"
b = self.TileBounds(tx, ty, zoom)
return (b[1],b[0],b[3],b[2])
class Profile(object):
def __init__(self):
self.mercator=GlobalMercator()
self.geodetic=GlobalGeodetic()
self.tileswne=None
class Tile(object):
def __init__(self):
self.tminz=None
self.tmaxz=None
self.tminmax=None
self.tsize=None
self.nativezoom=None
self.ominx=None
self.omaxx=None
self.ominy=None
self.omaxy=None
class OutData(object):
def __init__(self):
self.out_ds=None
self.alphaband=None
self.dataBandsCount=None
self.out_gt=None
def def_in_srs(s_srs,input):
in_ds=gdal.Open(input, gdal.GA_ReadOnly)
in_srs = None
if s_srs:
in_srs = osr.SpatialReference()
in_srs.SetFromUserInput(s_srs)
in_srs_wkt = in_srs.ExportToWkt()
else:
in_srs_wkt = in_ds.GetProjection()
if not in_srs_wkt and in_ds.GetGCPCount() != 0:
in_srs_wkt = in_ds.GetGCPProjection()
if in_srs_wkt:
in_srs = osr.SpatialReference()
in_srs.ImportFromWkt(in_srs_wkt)
return in_srs,in_srs_wkt
def def_out_srs(profile, in_srs):
out_srs = osr.SpatialReference()
if profile == 'mercator':
out_srs.ImportFromEPSG(900913)
elif profile == 'geodetic':
out_srs.ImportFromEPSG(4326)
else:
out_srs = in_srs
return out_srs
def init_drv(tiledriver):
out_drv = gdal.GetDriverByName(tiledriver)
mem_drv = gdal.GetDriverByName('MEM')
return out_drv,mem_drv
def init_out_data(s_srs,input,profile,verbose,in_nodata):
in_srs,in_srs_wkt=def_in_srs(s_srs,input)
out_srs=def_out_srs(profile,in_srs)
in_ds=gdal.Open(input, gdal.GA_ReadOnly)
out_data = OutData()
out_data.out_ds = in_ds
if profile in ('mercator', 'geodetic'):
out_ds = None
if (in_ds.GetGeoTransform() == (0.0, 1.0, 0.0, 0.0, 0.0, 1.0)) and (in_ds.GetGCPCount() == 0):
error("There is no georeference - neither affine transformation (worldfile) nor GCPs. You can generate only 'raster' profile tiles.", "Either gdal2tiles with parameter -p 'raster' or use another GIS software for georeference e.g. gdal_transform -gcp / -a_ullr / -a_srs")
if in_srs:
if (in_srs.ExportToProj4() != out_srs.ExportToProj4()) or (in_ds.GetGCPCount() != 0):
out_ds = image_warping(in_ds,in_srs_wkt,out_srs,verbose,in_nodata)
else:
error("Input file has unknown SRS.", "Use --s_srs ESPG:xyz (or similar) to provide source reference system.")
if out_ds is not None:
out_data.out_ds = out_ds
if verbose:
print "Projected file:", "tiles.vrt", "( %sP x %sL - %s bands)" % (out_ds.RasterXSize, out_ds.RasterYSize, out_ds.RasterCount)
#
# Here we should have a raster (out_ds) in the correct Spatial Reference system
#
# Get alpha band (either directly or from NODATA value)
out_data.alphaband = out_data.out_ds.GetRasterBand(1).GetMaskBand()
if (out_data.alphaband.GetMaskFlags() & gdal.GMF_ALPHA) or out_data.out_ds.RasterCount == 4 or out_data.out_ds.RasterCount == 2: # TODO: Better test for alpha band in the dataset
out_data.dataBandsCount = out_data.out_ds.RasterCount - 1
else:
out_data.dataBandsCount = out_data.out_ds.RasterCount
# Read the georeference
out_data.out_gt = out_data.out_ds.GetGeoTransform()
return out_data
def correct_ACW_nodata(in_nodata,verbose, out_ds):
import tempfile
tempfilename = tempfile.mktemp('-gdal2tiles.vrt')
out_ds.GetDriver().CreateCopy(tempfilename, out_ds) # open as a text file
s = open(tempfilename).read() # Add the warping options
s = s.replace("""<GDALWarpOptions>""", """<GDALWarpOptions>
<Option name="INIT_DEST">NO_DATA</Option>
<Option name="UNIFIED_SRC_NODATA">YES</Option>""")
# replace BandMapping tag for NODATA bands....
for i in range(len(in_nodata)):
s = s.replace("""<BandMapping src="%i" dst="%i"/>""" % ((i + 1), (i + 1)),
"""<BandMapping src="%i" dst="%i">
<SrcNoDataReal>%i</SrcNoDataReal>
<SrcNoDataImag>0</SrcNoDataImag>
<DstNoDataReal>%i</DstNoDataReal>
<DstNoDataImag>0</DstNoDataImag>
</BandMapping>""" % ((i + 1), (i + 1), in_nodata[i], in_nodata[i])) # Or rewrite to white by: , 255 ))
# save the corrected VRT
open(tempfilename, "w").write(s) # open by GDAL as out_ds
out_ds = gdal.Open(tempfilename) #, gdal.GA_ReadOnly)
# delete the temporary file
os.unlink(tempfilename) # set NODATA_VALUE metadata
out_ds.SetMetadataItem('NODATA_VALUES', '%i %i %i' % (in_nodata[0], in_nodata[1], in_nodata[2]))
if verbose:
print "Modified warping result saved into 'tiles1.vrt'"
open("tiles1.vrt", "w").write(s)
return out_ds
def correct_ACW_mono_rgb(verbose, out_ds):
import tempfile
tempfilename = tempfile.mktemp('-gdal2tiles.vrt')
out_ds.GetDriver().CreateCopy(tempfilename, out_ds) # open as a text file
s = open(tempfilename).read() # Add the warping options
s = s.replace("""<BlockXSize>""",
"""<VRTRasterBand dataType="Byte" band="%i" subClass="VRTWarpedRasterBand">
<ColorInterp>Alpha</ColorInterp>
</VRTRasterBand>
<BlockXSize>""" % (out_ds.RasterCount + 1))
s = s.replace("""</GDALWarpOptions>""",
"""<DstAlphaBand>%i</DstAlphaBand>
</GDALWarpOptions>""" % (out_ds.RasterCount + 1))
s = s.replace("""</WorkingDataType>""", """</WorkingDataType>
<Option name="INIT_DEST">0</Option>""")
# save the corrected VRT
open(tempfilename, "w").write(s) # open by GDAL as out_ds
out_ds = gdal.Open(tempfilename) #, gdal.GA_ReadOnly)
# delete the temporary file
os.unlink(tempfilename)
if verbose:
print "Modified -dstalpha warping result saved into 'tiles1.vrt'"
open("tiles1.vrt", "w").write(s)
return out_ds
def image_warping(in_ds,in_srs_wkt,out_srs,verbose,in_nodata):
# Generation of VRT dataset in tile projection, default 'nearest neighbour' warping
out_ds = gdal.AutoCreateWarpedVRT(in_ds, in_srs_wkt, out_srs.ExportToWkt())
# TODO: HIGH PRIORITY: Correction of AutoCreateWarpedVRT according the max zoomlevel for correct direct warping!!!
if verbose:
print "Warping of the raster by AutoCreateWarpedVRT (result saved into 'tiles.vrt')"
out_ds.GetDriver().CreateCopy("tiles.vrt", out_ds)
# Note: self.in_srs and self.in_srs_wkt contain still the non-warped reference system!!!
# Correction of AutoCreateWarpedVRT for NODATA values
if in_nodata != []:
out_ds = correct_ACW_nodata(in_nodata,verbose, out_ds)
# Correction of AutoCreateWarpedVRT for Mono (1 band) and RGB (3 bands) files without NODATA:
# equivalent of gdalwarp -dstalpha
if in_nodata == [] and out_ds.RasterCount in [1, 3]:
out_ds = correct_ACW_mono_rgb(verbose,out_ds)
s = '''
'''
return out_ds
class Configuration (object):
def create_tile(self):
tile = Tile()
# User specified zoom levels
if self.options.zoom:
minmax = self.options.zoom.split('-', 1)
minmax.extend([''])
min_, max_ = minmax[:2]
tile.tminz = int(min_)
if max:
tile.tmaxz = int(max_)
else:
tile.tmaxz = int(min_)
return tile
def __init__(self,arguments):
self.stopped = False
self.input = None
self.output = None
# Tile format
self.tiledriver = 'PNG'
self.tileext = 'png'
self.querysize_c = 4 * TILESIZE
# RUN THE ARGUMENT PARSER:
parser=self.optparse_init()
self.options, args = parser.parse_args(arguments)
if not args:
error("No input file specified")
# POSTPROCESSING OF PARSED ARGUMENTS:
# Workaround for old versions of GDAL
try:
if (self.options.verbose and self.options.resampling == 'near') or gdal.TermProgress_nocb:
pass
except:
error("This version of GDAL is not supported. Please upgrade to 1.6+.")
# Test output directory, if it doesn't exist
if os.path.isdir(args[-1]) or ( len(args) > 1 and not os.path.exists(args[-1])):
self.output = args[-1]
args = args[:-1]
# More files on the input not directly supported yet
if (len(args) > 1):
error("Processing of several input files is not supported.",
"""Please first use a tool like gdal_vrtmerge.py or gdal_merge.py on the files:
gdal_vrtmerge.py -o merged.vrt %s""" % " ".join(args))
# TODO: Call functions from gdal_vrtmerge.py directly
self.input = args[0]
# Default values for not given options
if not self.output:
# Directory with input filename without extension in actual directory
self.output = os.path.splitext(os.path.basename( self.input ))[0]
if not self.options.title:
self.options.title = os.path.basename( self.input )
if self.options.url and not self.options.url.endswith('/'):
self.options.url += '/'
if self.options.url:
self.options.url += os.path.basename( self.output ) + '/'
self.resampling = None
self.init_resampling()
# KML generation
self.kml = self.options.kml
# Output the results
if self.options.verbose:
print("Options:", self.options)
print("Input:", self.input)
print("Output:", self.output)
print("Cache: %s MB" % (gdal.GetCacheMax() / 1024 / 1024))
print('')
def optparse_init(self):
"""Prepare the option parser for input (argv)"""
from optparse import OptionParser, OptionGroup
usage = "Usage: %prog [options] input_file(s) [output]"
p = OptionParser(usage, version="%prog "+ __version__)
p.add_option("-p", "--profile", dest='profile', type='choice', choices=profile_list,
help="Tile cutting profile (%s) - default 'mercator' (Google Maps compatible)" % ",".join(profile_list))
p.add_option("-r", "--resampling", dest="resampling", type='choice', choices=resampling_list,
help="Resampling method (%s) - default 'average'" % ",".join(resampling_list))
p.add_option('-s', '--s_srs', dest="s_srs", metavar="SRS",
help="The spatial reference system used for the source input data")
p.add_option('-z', '--zoom', dest="zoom",
help="Zoom levels to render (format:'2-5' or '10').")
p.add_option('-e', '--resume', dest="resume", action="store_true",
help="Resume mode. Generate only missing files.")
p.add_option('-a', '--srcnodata', dest="srcnodata", metavar="NODATA",
help="NODATA transparency value to assign to the input data")
p.add_option("-v", "--verbose",
action="store_true", dest="verbose",
help="Print status messages to stdout")
# KML options
g = OptionGroup(p, "KML (Google Earth) options", "Options for generated Google Earth SuperOverlay metadata")
g.add_option("-k", "--force-kml", dest='kml', action="store_true",
help="Generate KML for Google Earth - default for 'geodetic' profile and 'raster' in EPSG:4326. For a dataset with different projection use with caution!")
g.add_option("-n", "--no-kml", dest='kml', action="store_false",
help="Avoid automatic generation of KML files for EPSG:4326")
g.add_option("-u", "--url", dest='url',
help="URL address where the generated tiles are going to be published")
p.add_option_group(g)
# HTML options
g = OptionGroup(p, "Web viewer options", "Options for generated HTML viewers a la Google Maps")
g.add_option("-w", "--webviewer", dest='webviewer', type='choice', choices=webviewer_list,
help="Web viewer to generate (%s) - default 'all'" % ",".join(webviewer_list))
g.add_option("-t", "--title", dest='title',
help="Title of the map")
g.add_option("-c", "--copyright", dest='copyright',
help="Copyright for the map")
g.add_option("-g", "--googlekey", dest='googlekey',
help="Google Maps API key from http://code.google.com/apis/maps/signup.html")
g.add_option("-y", "--yahookey", dest='yahookey',
help="Yahoo Application ID from http://developer.yahoo.com/wsregapp/")
p.add_option_group(g)
# TODO: MapFile + TileIndexes per zoom level for efficient MapServer WMS
#g = OptionGroup(p, "WMS MapServer metadata", "Options for generated mapfile and tileindexes for MapServer")
#g.add_option("-i", "--tileindex", dest='wms', action="store_true"
# help="Generate tileindex and mapfile for MapServer (WMS)")
# p.add_option_group(g)
p.set_defaults(verbose=False, profile="mercator", kml=False, url='',
webviewer='all', copyright='', resampling='average', resume=False,
googlekey='INSERT_YOUR_KEY_HERE', yahookey='INSERT_YOUR_YAHOO_APP_ID_HERE')
return p
def init_resampling(self):
if self.options.resampling == 'average':
try:
if gdal.RegenerateOverview:
pass
except:
error("'average' resampling algorithm is not available.", "Please use -r 'near' argument or upgrade to newer version of GDAL.")
elif self.options.resampling == 'antialias':
try:
if numpy:
pass
except:
error("'antialias' resampling algorithm is not available.", "Install PIL (Python Imaging Library) and numpy.")
elif self.options.resampling == 'near':
self.resampling = gdal.GRA_NearestNeighbour
self.querysize_c = TILESIZE
elif self.options.resampling == 'bilinear':
self.resampling = gdal.GRA_Bilinear
self.querysize_c = TILESIZE * 2
elif self.options.resampling == 'cubic':
self.resampling = gdal.GRA_Cubic
elif self.options.resampling == 'cubicspline':
self.resampling = gdal.GRA_CubicSpline
elif self.options.resampling == 'lanczos':
self.resampling = gdal.GRA_Lanczos
def open_input(self,tile):
"""Initialization of the input raster, reprojection if necessary"""
gdal.AllRegister()
# Initialize necessary GDAL drivers
self.out_drv,self.mem_drv=init_drv(self.tiledriver)
if not self.out_drv:
raise Exception("The '%s' driver was not found, is it available in this GDAL build?", self.tiledriver)
if not self.mem_drv:
raise Exception("The 'MEM' driver was not found, is it available in this GDAL build?")
# Open the input file
if self.input:
self.in_ds = gdal.Open(self.input, gdal.GA_ReadOnly)
else:
raise Exception("No input file was specified")
if self.options.verbose:
print("Input file:", "( %sP x %sL - %s bands)" % (self.in_ds.RasterXSize, self.in_ds.RasterYSize, self.in_ds.RasterCount))
if not self.in_ds:
# Note: GDAL prints the ERROR message too
error("It is not possible to open the input file '%s'." % self.input )
# Read metadata from the input file
if self.in_ds.RasterCount == 0:
error( "Input file '%s' has no raster band" % self.input )
if self.in_ds.GetRasterBand(1).GetRasterColorTable():
# TODO: Process directly paletted dataset by generating VRT in memory
error( "Please convert this file to RGB/RGBA and run gdal2tiles on the result.",
"""From paletted file you can create RGBA file (temp.vrt) by:
gdal_translate -of vrt -expand rgba %s temp.vrt
then run:
gdal2tiles temp.vrt""" % self.input )
# Get NODATA value
self.in_nodata = []
for i in range(1, self.in_ds.RasterCount+1):
if self.in_ds.GetRasterBand(i).GetNoDataValue() != None:
self.in_nodata.append( self.in_ds.GetRasterBand(i).GetNoDataValue() )
if self.options.srcnodata:
nds = list(map( float, self.options.srcnodata.split(',')))
if len(nds) < self.in_ds.RasterCount:
self.in_nodata = (nds * self.in_ds.RasterCount)[:self.in_ds.RasterCount]
else:
self.in_nodata = nds
if self.options.verbose:
print("NODATA: %s" % self.in_nodata)
#
# Here we should have RGBA input dataset opened in self.in_ds
#
if self.options.verbose:
print("Preprocessed file:", "( %sP x %sL - %s bands)" % (self.in_ds.RasterXSize, self.in_ds.RasterYSize, self.in_ds.RasterCount))
# Spatial Reference System of the input raster
self.in_srs,self.in_srs_wkt = def_in_srs(self.options.s_srs,self.input)
#elif self.options.profile != 'raster':
# error("There is no spatial reference system info included in the input file.","You should run gdal2tiles with --s_srs EPSG:XXXX or similar.")
# Spatial Reference System of tiles
self.out_srs=def_out_srs(self.options.profile,self.in_srs)
# Are the reference systems the same? Reproject if necessary.
out_data = init_out_data(self.options.s_srs,self.input,self.options.profile,self.options.verbose,self.in_nodata)
#originX, originY = out_data.out_gt[0], out_data.out_gt[3]
#pixelSize = out_data.out_gt[1] # = out_data.out_gt[5]
# Test the size of the pixel
# MAPTILER - COMMENTED
#if out_data.out_gt[1] != (-1 * out_data.out_gt[5]) and self.options.profile != 'raster':
# TODO: Process corectly coordinates with are have swichted Y axis (display in OpenLayers too)
#error("Size of the pixel in the output differ for X and Y axes.")
# Report error in case rotation/skew is in geotransform (possible only in 'raster' profile)
if (out_data.out_gt[2], out_data.out_gt[4]) != (0,0):
error("Georeference of the raster contains rotation or skew. Such raster is not supported. Please use gdalwarp first.")
# TODO: Do the warping in this case automaticaly
# KML test
isepsg4326 = False
srs4326 = osr.SpatialReference()
srs4326.ImportFromEPSG(4326)
if self.out_srs and srs4326.ExportToProj4() == self.out_srs.ExportToProj4():
self.kml = True
isepsg4326 = True
if self.options.verbose:
print("KML autotest OK!")
#
# Here we expect: pixel is square, no rotation on the raster
#
# Output Bounds - coordinates in the output SRS
tile.ominx = out_data.out_gt[0]
tile.omaxx = out_data.out_gt[0]+out_data.out_ds.RasterXSize*out_data.out_gt[1]
tile.omaxy = out_data.out_gt[3]
tile.ominy = out_data.out_gt[3]-out_data.out_ds.RasterYSize*out_data.out_gt[1]
# Note: maybe round(x, 14) to avoid the gdal_translate behaviour, when 0 becomes -1e-15
if self.options.verbose:
print("Bounds (output srs):", round(tile.ominx, 13), tile.ominy, tile.omaxx, tile.omaxy)
#
# Calculating ranges for tiles in different zoom levels
#
profile=self.tile_range(out_data,tile,srs4326,isepsg4326)
return out_data, profile
def tile_range(self,out_data,tile,srs4326,isepsg4326):
profile = Profile()
if self.options.profile == 'mercator':
self.tile_range_mercator(profile,out_data,tile)
if self.options.profile == 'geodetic':
self.tile_range_geodetic(profile,out_data,tile)
if self.options.profile == 'raster':
self.tile_range_raster(out_data,tile)
# Function which generates SWNE in LatLong for given tile
if self.kml and self.in_srs_wkt:
ct = osr.CoordinateTransformation(self.in_srs, srs4326)
def rastertileswne(x,y,z):
pixelsizex = (2**(self.tmaxz-z) * out_data.out_gt[1]) # X-pixel size in level
pixelsizey = (2**(self.tmaxz-z) * out_data.out_gt[1]) # Y-pixel size in level (usually -1*pixelsizex)
west = out_data.out_gt[0] + x*TILESIZE*pixelsizex
east = west + TILESIZE*pixelsizex
south = tile.ominy + y*TILESIZE*pixelsizex
north = south + TILESIZE*pixelsizex
if not isepsg4326:
# Transformation to EPSG:4326 (WGS84 datum)
west, south = ct.TransformPoint(west, south)[:2]
east, north = ct.TransformPoint(east, north)[:2]
return south, west, north, east
profile.tileswne = rastertileswne
else:
profile.tileswne = lambda x, y, z: (0,0,0,0)
return profile
def tile_range_raster(self,out_data,tile):
log2 = lambda x:math.log10(x) / math.log10(2) # log2 (base 2 logarithm)
tile.nativezoom = int(max(math.ceil(log2(out_data.out_ds.RasterXSize / float(TILESIZE))), math.ceil(log2(out_data.out_ds.RasterYSize / float(TILESIZE)))))
if self.options.verbose:
print "Native zoom of the raster:", tile.nativezoom
# Get the minimal zoom level (whole raster in one tile)
if tile.tminz == None:
tile.tminz = 0
# Get the maximal zoom level (native resolution of the raster)
if tile.tmaxz == None:
tile.tmaxz = tile.nativezoom
# Generate table with min max tile coordinates for all zoomlevels
tile.tminmax = list(range(0, tile.tmaxz + 1))
tile.tsize = list(range(0, tile.tmaxz + 1))
for tz in range(0, tile.tmaxz + 1):
tsize = 2.0 ** (tile.nativezoom - tz) * TILESIZE
tminx, tminy = 0, 0
tmaxx = int(math.ceil(out_data.out_ds.RasterXSize / tsize)) - 1
tmaxy = int(math.ceil(out_data.out_ds.RasterYSize / tsize)) - 1
tile.tsize[tz] = math.ceil(tsize)
tile.tminmax[tz] = tminx, tminy, tmaxx, tmaxy
def tile_range_geodetic(self,profile,out_data,tile):
# from globalmaptiles.py
# Function which generates SWNE in LatLong for given tile
profile.tileswne = profile.geodetic.TileLatLonBounds
# Generate table with min max tile coordinates for all zoomlevels
tile.tminmax = list(range(0, 32))
for tz in range(0, 32):
tminx, tminy = profile.geodetic.LatLonToTile(tile.ominx, tile.ominy, tz)
tmaxx, tmaxy = profile.geodetic.LatLonToTile(tile.omaxx, tile.omaxy, tz) # crop tiles extending world limits (+-180,+-90)
tminx, tminy = max(0, tminx), max(0, tminy)
tmaxx, tmaxy = min(2 ** (tz + 1) - 1, tmaxx), min(2 ** tz - 1, tmaxy)
tile.tminmax[tz] = tminx, tminy, tmaxx, tmaxy
# TODO: Maps crossing 180E (Alaska?)
# Get the maximal zoom level (closest possible zoom level up on the resolution of raster)
if tile.tminz == None:
tile.tminz = profile.geodetic.ZoomForPixelSize(out_data.out_gt[1] * max(out_data.out_ds.RasterXSize, out_data.out_ds.RasterYSize) / float(TILESIZE))
# Get the maximal zoom level (closest possible zoom level up on the resolution of raster)
if tile.tmaxz == None:
tile.tmaxz = profile.geodetic.ZoomForPixelSize(out_data.out_gt[1])
if self.options.verbose:
print "Bounds (latlong):", tile.ominx, tile.ominy, tile.omaxx, tile.omaxy
def tile_range_mercator(self,profile,out_data,tile):
# from globalmaptiles.py
# Function which generates SWNE in LatLong for given tile
profile.tileswne = profile.mercator.TileLatLonBounds
# Generate table with min max tile coordinates for all zoomlevels
tile.tminmax = list(range(0, 32))
for tz in range(0, 32):
tminx, tminy = profile.mercator.MetersToTile(tile.ominx, tile.ominy, tz)
tmaxx, tmaxy = profile.mercator.MetersToTile(tile.omaxx, tile.omaxy, tz) # crop tiles extending world limits (+-180,+-90)
tminx, tminy = max(0, tminx), max(0, tminy)
tmaxx, tmaxy = min(2 ** tz - 1, tmaxx), min(2 ** tz - 1, tmaxy)
tile.tminmax[tz] = tminx, tminy, tmaxx, tmaxy
# TODO: Maps crossing 180E (Alaska?)
# Get the minimal zoom level (map covers area equivalent to one tile)
if tile.tminz == None:
tile.tminz = profile.mercator.ZoomForPixelSize(out_data.out_gt[1] * max(out_data.out_ds.RasterXSize, out_data.out_ds.RasterYSize) / float(TILESIZE))
# Get the maximal zoom level (closest possible zoom level up on the resolution of raster)
if tile.tmaxz == None:
tile.tmaxz = profile.mercator.ZoomForPixelSize(out_data.out_gt[1])
if self.options.verbose:
print "Bounds (latlong):", profile.mercator.MetersToLatLon(tile.ominx, tile.ominy), profile.mercator.MetersToLatLon(tile.omaxx, tile.omaxy)
print 'MinZoomLevel:', self.tminz
print "MaxZoomLevel:", self.tmaxz, "(", profile.mercator.Resolution(self.tmaxz), ")"
# =============================================================================
def error(msg, details = "" ):
"""Print an error message and stop the processing"""
if details:
sys.stderr.write(msg+ "\n\n" + details)
else:
sys.stderr.write(msg)
sys.exit(0)