Skip to content
This repository was archived by the owner on Jun 9, 2018. It is now read-only.

Commit 24963d2

Browse files
authoredOct 26, 2016
Merge pull request #31 from GitHubRGI/whatnick-patch-1
changes pushed from gitlab
2 parents 08ced90 + 9417ae1 commit 24963d2

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed
 

‎Tiling/gdal2tiles_parallel.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@
5656
from collections import namedtuple
5757
from sys import exit, stdout, argv as sys_argv
5858
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
6060
from multiprocessing import cpu_count, Pool, Process, Queue
6161
from optparse import OptionParser, OptionGroup
62+
from re import sub
6263

6364
try:
6465
from osgeo import gdal, osr
@@ -911,6 +912,8 @@ def __init__(self, arguments):
911912

912913
elif self.options.resampling == 'lanczos':
913914
self.resampling = gdal.GRA_Lanczos
915+
916+
self.error_threshold = 0.125 # error threshold --> use same value as in gdalwarp
914917

915918
# User specified zoom levels
916919
self.tminz = None
@@ -927,6 +930,18 @@ def __init__(self, arguments):
927930

928931
# KML generation
929932
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")
930945

931946
# Output the results
932947

@@ -988,6 +1003,7 @@ def optparse_init(self):
9881003
default=cpu_count(),
9891004
help=
9901005
'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")
9911007
p.add_option("-v",
9921008
"--verbose",
9931009
action="store_true",
@@ -1047,6 +1063,7 @@ def optparse_init(self):
10471063
help=
10481064
"Yahoo Application ID from http://developer.yahoo.com/wsregapp/")
10491065
p.add_option_group(g)
1066+
10501067

10511068
# TODO: MapFile + TileIndexes per zoom level for efficient MapServer WMS
10521069
#g = OptionGroup(p, "WMS MapServer metadata", "Options for generated mapfile and tileindexes for MapServer")
@@ -1211,6 +1228,7 @@ def open_input(self):
12111228
# Correction of AutoCreateWarpedVRT for NODATA values
12121229
if self.in_nodata != []:
12131230
tempfilename = mktemp('-gdal2tiles.vrt')
1231+
12141232
self.out_ds.GetDriver().CreateCopy(tempfilename,
12151233
self.out_ds)
12161234
# open as a text file
@@ -1277,11 +1295,15 @@ def open_input(self):
12771295
s = s.replace("""</WorkingDataType>""",
12781296
"""</WorkingDataType>
12791297
<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+
12801301
# save the corrected VRT
12811302
open(tempfilename, "w").write(s)
12821303
# open by GDAL as self.out_ds
12831304
self.out_ds = gdal.Open(tempfilename
12841305
) #, gdal.GA_ReadOnly)
1306+
12851307
# delete the temporary file
12861308
unlink(tempfilename)
12871309

@@ -1775,6 +1797,11 @@ def generate_base_tiles(self, cpu):
17751797
self.out_drv.CreateCopy(tilefilename, dstile, strict=0)
17761798

17771799
del dstile
1800+
1801+
#Remove JPEG aux.xml sidecars
1802+
sidecar = tilefilename+".aux.xml"
1803+
if(path.exists(sidecar)):
1804+
unlink(sidecar)
17781805

17791806
# Do not create KML, we dont use it and it takes up valuable processing time
17801807
# Create a KML file for this tile.
@@ -1824,6 +1851,10 @@ def generate_overview_tiles(self, cpu, tz):
18241851
) #, "( TileMapService: z / x / y )"
18251852

18261853
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)
18271858
if self.options.verbose:
18281859
print("Tile generation skiped because of --resume")
18291860
else:
@@ -1834,7 +1865,11 @@ def generate_overview_tiles(self, cpu, tz):
18341865
# Create directories for the tile
18351866
if not path.exists(path.dirname(tilefilename)):
18361867
makedirs(path.dirname(tilefilename))
1837-
1868+
1869+
# TODO: improve that
1870+
if self.out_drv.ShortName == 'JPEG' and tilebands == 4:
1871+
tilebands = 3
1872+
18381873
dsquery = self.mem_drv.Create('', 2 * self.tilesize, 2 *
18391874
self.tilesize, tilebands)
18401875
# TODO: fill the null value
@@ -1895,6 +1930,11 @@ def generate_overview_tiles(self, cpu, tz):
18951930
# f = open( path.join(self.output, '%d/%d/%d.kml' % (tz, tx, ty)), 'w')
18961931
# f.write( self.generate_kml( tx, ty, tz, children ) )
18971932
# f.close()
1933+
1934+
#Remove JPEG aux.xml sidecars
1935+
sidecar = tilefilename+".aux.xml"
1936+
if(path.exists(sidecar)):
1937+
unlink(sidecar)
18981938

18991939
if not self.options.verbose:
19001940
#queue.put(tcount)
@@ -1985,7 +2025,8 @@ def scale_query_to_tile(self, dsquery, dstile, tilefilename=''):
19852025
dsquery.SetGeoTransform((0.0, tilesize / float(querysize), 0.0,
19862026
0.0, 0.0, tilesize / float(querysize)))
19872027
dstile.SetGeoTransform((0.0, 1.0, 0.0, 0.0, 0.0, 1.0))
1988-
2028+
2029+
19892030
res = gdal.ReprojectImage(dsquery, dstile, None, None,
19902031
self.resampling)
19912032
if res != 0:

0 commit comments

Comments
 (0)