56
56
from collections import namedtuple
57
57
from sys import exit , stdout , argv as sys_argv
58
58
from os import path , unlink , makedirs
59
- from math import pi , tan , log , exp , atan , ceil , log10
59
+ from math import pi , tan , log , exp , atan , ceil , log10 , floor
60
60
from multiprocessing import cpu_count , Pool , Process , Queue
61
61
from optparse import OptionParser , OptionGroup
62
+ from re import sub
62
63
63
64
try :
64
65
from osgeo import gdal , osr
@@ -911,6 +912,8 @@ def __init__(self, arguments):
911
912
912
913
elif self .options .resampling == 'lanczos' :
913
914
self .resampling = gdal .GRA_Lanczos
915
+
916
+ self .error_threshold = 0.125 # error threshold --> use same value as in gdalwarp
914
917
915
918
# User specified zoom levels
916
919
self .tminz = None
@@ -927,6 +930,18 @@ def __init__(self, arguments):
927
930
928
931
# KML generation
929
932
self .kml = self .options .kml
933
+
934
+ #Output Format
935
+ if self .options .output_format == 'JPEG' :
936
+ self .tiledriver = 'JPEG'
937
+ self .tileext = 'jpg'
938
+
939
+ elif self .options .output_format == 'PNG' :
940
+ self .tiledriver = 'PNG'
941
+ self .tileext = 'png'
942
+
943
+ else :
944
+ self .error ("Output formats allowed are PNG and JPEG" )
930
945
931
946
# Output the results
932
947
@@ -988,6 +1003,7 @@ def optparse_init(self):
988
1003
default = cpu_count (),
989
1004
help =
990
1005
'Number of concurrent processes (defaults to the number of cores in the system)' )
1006
+ p .add_option ("-f" , "--format" , dest = "output_format" , help = "Image format for output tiles. Just PNG and JPEG allowed. PNG is selected by default" )
991
1007
p .add_option ("-v" ,
992
1008
"--verbose" ,
993
1009
action = "store_true" ,
@@ -1047,6 +1063,7 @@ def optparse_init(self):
1047
1063
help =
1048
1064
"Yahoo Application ID from http://developer.yahoo.com/wsregapp/" )
1049
1065
p .add_option_group (g )
1066
+
1050
1067
1051
1068
# TODO: MapFile + TileIndexes per zoom level for efficient MapServer WMS
1052
1069
#g = OptionGroup(p, "WMS MapServer metadata", "Options for generated mapfile and tileindexes for MapServer")
@@ -1211,6 +1228,7 @@ def open_input(self):
1211
1228
# Correction of AutoCreateWarpedVRT for NODATA values
1212
1229
if self .in_nodata != []:
1213
1230
tempfilename = mktemp ('-gdal2tiles.vrt' )
1231
+
1214
1232
self .out_ds .GetDriver ().CreateCopy (tempfilename ,
1215
1233
self .out_ds )
1216
1234
# open as a text file
@@ -1277,11 +1295,15 @@ def open_input(self):
1277
1295
s = s .replace ("""</WorkingDataType>""" ,
1278
1296
"""</WorkingDataType>
1279
1297
<Option name="INIT_DEST">0</Option>""" )
1298
+ s = sub (r"<BlockXSize>\d+</BlockXSize>" , "<BlockXSize>{0}</BlockXSize>" .format (self .tilesize ), s )
1299
+ s = sub (r"<BlockYSize>\d+</BlockYSize>" , "<BlockYSize>{0}</BlockYSize>" .format (self .tilesize ), s )
1300
+
1280
1301
# save the corrected VRT
1281
1302
open (tempfilename , "w" ).write (s )
1282
1303
# open by GDAL as self.out_ds
1283
1304
self .out_ds = gdal .Open (tempfilename
1284
1305
) #, gdal.GA_ReadOnly)
1306
+
1285
1307
# delete the temporary file
1286
1308
unlink (tempfilename )
1287
1309
@@ -1775,6 +1797,11 @@ def generate_base_tiles(self, cpu):
1775
1797
self .out_drv .CreateCopy (tilefilename , dstile , strict = 0 )
1776
1798
1777
1799
del dstile
1800
+
1801
+ #Remove JPEG aux.xml sidecars
1802
+ sidecar = tilefilename + ".aux.xml"
1803
+ if (path .exists (sidecar )):
1804
+ unlink (sidecar )
1778
1805
1779
1806
# Do not create KML, we dont use it and it takes up valuable processing time
1780
1807
# Create a KML file for this tile.
@@ -1824,6 +1851,10 @@ def generate_overview_tiles(self, cpu, tz):
1824
1851
) #, "( TileMapService: z / x / y )"
1825
1852
1826
1853
if self .options .resume and path .exists (tilefilename ):
1854
+ #Remove JPEG aux.xml sidecars
1855
+ sidecar = tilefilename + ".aux.xml"
1856
+ if (path .exists (sidecar )):
1857
+ unlink (sidecar )
1827
1858
if self .options .verbose :
1828
1859
print ("Tile generation skiped because of --resume" )
1829
1860
else :
@@ -1834,7 +1865,11 @@ def generate_overview_tiles(self, cpu, tz):
1834
1865
# Create directories for the tile
1835
1866
if not path .exists (path .dirname (tilefilename )):
1836
1867
makedirs (path .dirname (tilefilename ))
1837
-
1868
+
1869
+ # TODO: improve that
1870
+ if self .out_drv .ShortName == 'JPEG' and tilebands == 4 :
1871
+ tilebands = 3
1872
+
1838
1873
dsquery = self .mem_drv .Create ('' , 2 * self .tilesize , 2 *
1839
1874
self .tilesize , tilebands )
1840
1875
# TODO: fill the null value
@@ -1895,6 +1930,11 @@ def generate_overview_tiles(self, cpu, tz):
1895
1930
# f = open( path.join(self.output, '%d/%d/%d.kml' % (tz, tx, ty)), 'w')
1896
1931
# f.write( self.generate_kml( tx, ty, tz, children ) )
1897
1932
# f.close()
1933
+
1934
+ #Remove JPEG aux.xml sidecars
1935
+ sidecar = tilefilename + ".aux.xml"
1936
+ if (path .exists (sidecar )):
1937
+ unlink (sidecar )
1898
1938
1899
1939
if not self .options .verbose :
1900
1940
#queue.put(tcount)
@@ -1985,7 +2025,8 @@ def scale_query_to_tile(self, dsquery, dstile, tilefilename=''):
1985
2025
dsquery .SetGeoTransform ((0.0 , tilesize / float (querysize ), 0.0 ,
1986
2026
0.0 , 0.0 , tilesize / float (querysize )))
1987
2027
dstile .SetGeoTransform ((0.0 , 1.0 , 0.0 , 0.0 , 0.0 , 1.0 ))
1988
-
2028
+
2029
+
1989
2030
res = gdal .ReprojectImage (dsquery , dstile , None , None ,
1990
2031
self .resampling )
1991
2032
if res != 0 :
0 commit comments