gdal2tiles.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. """
  2. ***************************************************************************
  3. gdal2tiles.py
  4. ---------------------
  5. Date : January 2016
  6. Copyright : (C) 2016 by Médéric Ribreux
  7. Email : mederic dot ribreux at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'January 2016'
  19. __copyright__ = '(C) 2016, Médéric Ribreux'
  20. import os
  21. from PyQt5.QtGui import QIcon
  22. from qgis.core import (QgsProcessingAlgorithm,
  23. QgsProcessingException,
  24. QgsProcessingParameterDefinition,
  25. QgsProcessingParameterRasterLayer,
  26. QgsProcessingParameterCrs,
  27. QgsProcessingParameterEnum,
  28. QgsProcessingParameterString,
  29. QgsProcessingParameterNumber,
  30. QgsProcessingParameterBoolean,
  31. QgsProcessingParameterFolderDestination)
  32. from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
  33. from processing.algs.gdal.GdalUtils import GdalUtils
  34. from processing.tools.system import isWindows
  35. pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
  36. class gdal2tiles(GdalAlgorithm):
  37. INPUT = 'INPUT'
  38. PROFILE = 'PROFILE'
  39. RESAMPLING = 'RESAMPLING'
  40. ZOOM = 'ZOOM'
  41. SOURCE_CRS = 'SOURCE_CRS'
  42. NODATA = 'NODATA'
  43. KML = 'KML'
  44. NO_KML = 'NO_KML'
  45. URL = 'URL'
  46. VIEWER = 'VIEWER'
  47. TITLE = 'TITLE'
  48. COPYRIGHT = 'COPYRIGHT'
  49. GOOGLE_KEY = 'GOOGLE_KEY'
  50. BING_KEY = 'BING_KEY'
  51. RESUME = 'RESUME'
  52. OUTPUT = 'OUTPUT'
  53. def __init__(self):
  54. super().__init__()
  55. def initAlgorithm(self, config=None):
  56. self.profiles = ((self.tr('Mercator'), 'mercator'),
  57. (self.tr('Geodetic'), 'geodetic'),
  58. (self.tr('Raster'), 'raster'))
  59. self.methods = ((self.tr('Average'), 'average'),
  60. (self.tr('Nearest Neighbour'), 'near'),
  61. (self.tr('Bilinear (2x2 Kernel)'), 'bilinear'),
  62. (self.tr('Cubic (4x4 Kernel)'), 'cubic'),
  63. (self.tr('Cubic B-Spline (4x4 Kernel)'), 'cubicspline'),
  64. (self.tr('Lanczos (6x6 Kernel)'), 'lanczos'),
  65. (self.tr('Antialias'), 'antialias'))
  66. self.viewers = ((self.tr('All'), 'all'),
  67. (self.tr('GoogleMaps'), 'google'),
  68. (self.tr('OpenLayers'), 'openlayers'),
  69. (self.tr('Leaflet'), 'leaflet'),
  70. (self.tr('None'), 'none'))
  71. self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT, self.tr('Input layer')))
  72. self.addParameter(QgsProcessingParameterEnum(self.PROFILE,
  73. self.tr('Tile cutting profile'),
  74. options=[i[0] for i in self.profiles],
  75. allowMultiple=False,
  76. defaultValue=0))
  77. self.addParameter(QgsProcessingParameterString(self.ZOOM,
  78. self.tr('Zoom levels to render'),
  79. defaultValue='',
  80. optional=True))
  81. self.addParameter(QgsProcessingParameterEnum(self.VIEWER,
  82. self.tr('Web viewer to generate'),
  83. options=[i[0] for i in self.viewers],
  84. allowMultiple=False,
  85. defaultValue=0))
  86. self.addParameter(QgsProcessingParameterString(self.TITLE,
  87. self.tr('Title of the map'),
  88. optional=True))
  89. self.addParameter(QgsProcessingParameterString(self.COPYRIGHT,
  90. self.tr('Copyright of the map'),
  91. optional=True))
  92. params = [
  93. QgsProcessingParameterEnum(self.RESAMPLING,
  94. self.tr('Resampling method'),
  95. options=[i[0] for i in self.methods],
  96. allowMultiple=False,
  97. defaultValue=0),
  98. QgsProcessingParameterCrs(self.SOURCE_CRS,
  99. self.tr('The spatial reference system used for the source input data'),
  100. optional=True),
  101. QgsProcessingParameterNumber(self.NODATA,
  102. self.tr('Transparency value to assign to the input data'),
  103. type=QgsProcessingParameterNumber.Double,
  104. defaultValue=0,
  105. optional=True),
  106. QgsProcessingParameterString(self.URL,
  107. self.tr('URL address where the generated tiles are going to be published'),
  108. optional=True),
  109. QgsProcessingParameterString(self.GOOGLE_KEY,
  110. self.tr('Google Maps API key (http://code.google.com/apis/maps/signup.html)'),
  111. optional=True),
  112. QgsProcessingParameterString(self.BING_KEY,
  113. self.tr('Bing Maps API key (https://www.bingmapsportal.com/)'),
  114. optional=True),
  115. QgsProcessingParameterBoolean(self.RESUME,
  116. self.tr('Generate only missing files'),
  117. defaultValue=False),
  118. QgsProcessingParameterBoolean(self.KML,
  119. self.tr('Generate KML for Google Earth'),
  120. defaultValue=False),
  121. QgsProcessingParameterBoolean(self.NO_KML,
  122. self.tr('Avoid automatic generation of KML files for EPSG:4326'),
  123. defaultValue=False)
  124. ]
  125. for param in params:
  126. param.setFlags(param.flags() | QgsProcessingParameterDefinition.FlagAdvanced)
  127. self.addParameter(param)
  128. self.addParameter(QgsProcessingParameterFolderDestination(self.OUTPUT,
  129. self.tr('Output directory')))
  130. def name(self):
  131. return 'gdal2tiles'
  132. def displayName(self):
  133. return self.tr('栅格切片')
  134. def icon(self):
  135. return QIcon(os.path.join(pluginPath, 'images', 'dbms', 'totile2.png'))
  136. def group(self):
  137. return self.tr('切片工具')
  138. def groupId(self):
  139. return 'cachetools'
  140. def commandName(self):
  141. return 'gdal2tiles'
  142. def flags(self):
  143. return super().flags() | QgsProcessingAlgorithm.FlagDisplayNameIsLiteral
  144. def getConsoleCommands(self, parameters, context, feedback, executing=True):
  145. arguments = [
  146. '-p',
  147. self.profiles[self.parameterAsEnum(parameters, self.PROFILE, context)][1],
  148. ]
  149. zoom = self.parameterAsString(parameters, self.ZOOM, context)
  150. if zoom:
  151. arguments.append('-z')
  152. arguments.append(str(zoom))
  153. arguments.append('-w')
  154. arguments.append(self.viewers[self.parameterAsEnum(parameters, self.VIEWER, context)][1])
  155. title = self.parameterAsString(parameters, self.TITLE, context)
  156. if title:
  157. arguments.append('-t')
  158. arguments.append(title)
  159. copying = self.parameterAsString(parameters, self.COPYRIGHT, context)
  160. if copying:
  161. arguments.append('-c')
  162. arguments.append(copying)
  163. arguments.append('-r')
  164. arguments.append(self.methods[self.parameterAsEnum(parameters, self.RESAMPLING, context)][1])
  165. crs = self.parameterAsCrs(parameters, self.SOURCE_CRS, context)
  166. if crs.isValid():
  167. arguments.append('-s')
  168. arguments.append(GdalUtils.gdal_crs_string(crs))
  169. if self.NODATA in parameters and parameters[self.NODATA] is not None:
  170. nodata = self.parameterAsDouble(parameters, self.NODATA, context)
  171. arguments.append('-a')
  172. arguments.append(str(nodata))
  173. url = self.parameterAsString(parameters, self.URL, context)
  174. if url:
  175. arguments.append('-u')
  176. arguments.append(url)
  177. key = self.parameterAsString(parameters, self.GOOGLE_KEY, context)
  178. if key:
  179. arguments.append('-g')
  180. arguments.append(key)
  181. key = self.parameterAsString(parameters, self.BING_KEY, context)
  182. if key:
  183. arguments.append('-b')
  184. arguments.append(key)
  185. if self.parameterAsBoolean(parameters, self.RESUME, context):
  186. arguments.append('-e')
  187. if self.parameterAsBoolean(parameters, self.KML, context):
  188. arguments.append('-k')
  189. if self.parameterAsBoolean(parameters, self.NO_KML, context):
  190. arguments.append('-n')
  191. inLayer = self.parameterAsRasterLayer(parameters, self.INPUT, context)
  192. if inLayer is None:
  193. raise QgsProcessingException(self.invalidRasterError(parameters, self.INPUT))
  194. arguments.append(inLayer.source())
  195. arguments.append(self.parameterAsString(parameters, self.OUTPUT, context))
  196. return [self.commandName() + ('.bat' if isWindows() else '.py'), GdalUtils.escapeAndJoin(arguments)]