wms_drv.py 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. """!
  2. @brief WMS, WMTS and NASA OnEarth drivers implemented in GRASS using GDAL Python bindings.
  3. List of classes:
  4. - wms_drv::WMSDrv
  5. - wms_drv::BaseRequestMgr
  6. - wms_drv::WMSRequestMgr
  7. - wms_drv::WMTSRequestMgr
  8. - wms_drv::OnEarthRequestMgr
  9. (C) 2012 by the GRASS Development Team
  10. This program is free software under the GNU General Public License
  11. (>=v2). Read the file COPYING that comes with GRASS for details.
  12. @author Stepan Turek <stepan.turek seznam.cz> (Mentor: Martin Landa)
  13. """
  14. import socket
  15. import grass.script as grass
  16. from time import sleep
  17. try:
  18. from osgeo import gdal
  19. from osgeo import gdalconst
  20. except:
  21. grass.fatal(_("Unable to load GDAL python bindings"))
  22. import numpy as Numeric
  23. Numeric.arrayrange = Numeric.arange
  24. from math import pi, floor
  25. from urllib2 import HTTPError
  26. from httplib import HTTPException
  27. try:
  28. from xml.etree.ElementTree import ParseError
  29. except ImportError: # < Python 2.7
  30. from xml.parsers.expat import ExpatError as ParseError
  31. from wms_base import WMSBase, GetSRSParamVal
  32. from wms_cap_parsers import WMTSCapabilitiesTree, OnEarthCapabilitiesTree
  33. class WMSDrv(WMSBase):
  34. def _download(self):
  35. """!Downloads data from WMS server using own driver
  36. @return temp_map with downloaded data
  37. """
  38. grass.message(_("Downloading data from WMS server..."))
  39. if "?" in self.params["url"]:
  40. self.params["url"] += "&"
  41. else:
  42. self.params["url"] += "?"
  43. if not self.params['capfile']:
  44. self.cap_file = self._fetchCapabilities(self.params)
  45. else:
  46. self.cap_file = self.params['capfile']
  47. # initialize correct manager according to chosen OGC service
  48. if self.params['driver'] == 'WMTS_GRASS':
  49. req_mgr = WMTSRequestMgr(self.params, self.bbox, self.region, self.proj_srs, self.cap_file)
  50. elif self.params['driver'] == 'WMS_GRASS':
  51. req_mgr = WMSRequestMgr(self.params, self.bbox, self.region, self.tile_size, self.proj_srs)
  52. elif self.params['driver'] == 'OnEarth_GRASS':
  53. req_mgr = OnEarthRequestMgr(self.params, self.bbox, self.region, self.proj_srs, self.cap_file)
  54. # get information about size in pixels and bounding box of raster, where all tiles will be joined
  55. map_region = req_mgr.GetMapRegion()
  56. init = True
  57. temp_map = None
  58. fetch_try = 0
  59. # iterate through all tiles and download them
  60. while True:
  61. if fetch_try == 0:
  62. # get url for request the tile and information for placing the tile into raster with other tiles
  63. tile = req_mgr.GetNextTile()
  64. # if last tile has been already downloaded
  65. if not tile:
  66. break
  67. # url for request the tile
  68. query_url = tile[0]
  69. # the tile size and offset in pixels for placing it into raster where tiles are joined
  70. tile_ref = tile[1]
  71. grass.debug(query_url, 2)
  72. try:
  73. wms_data = self._fetchDataFromServer(query_url, self.params['username'], self.params['password'])
  74. except (IOError, HTTPException) as e:
  75. if HTTPError == type(e) and e.code == 401:
  76. grass.fatal(_("Authorization failed to '%s' when fetching data.\n%s") % (self.params['url'], str(e)))
  77. else:
  78. grass.fatal(_("Unable to fetch data from: '%s'\n%s") % (self.params['url'], str(e)))
  79. temp_tile = self._tempfile()
  80. # download data into temporary file
  81. try:
  82. temp_tile_opened = open(temp_tile, 'wb')
  83. temp_tile_opened.write(wms_data.read())
  84. except IOError as e:
  85. # some servers are not happy with many subsequent requests for tiles done immediately,
  86. # if immediate request was unsuccessful, try to repeat the request after 5s and 30s breaks
  87. # TODO probably servers can return more kinds of errors related to this problem (not only 104)
  88. if socket.error == type(e) and e[0] == 104 and fetch_try < 2:
  89. fetch_try += 1
  90. if fetch_try == 1:
  91. sleep_time = 5
  92. elif fetch_try == 2:
  93. sleep_time = 30
  94. grass.warning(_("Server refused to send data for a tile.\nRequest will be repeated after %d s.") % sleep_time)
  95. sleep(sleep_time)
  96. continue
  97. else:
  98. grass.fatal(_("Unable to write data into tempfile.\n%s") % str(e))
  99. finally:
  100. temp_tile_opened.close()
  101. fetch_try = 0
  102. tile_dataset_info = gdal.Open(temp_tile, gdal.GA_ReadOnly)
  103. if tile_dataset_info is None:
  104. # print error xml returned from server
  105. try:
  106. error_xml_opened = open(temp_tile, 'rb')
  107. err_str = error_xml_opened.read()
  108. except IOError as e:
  109. grass.fatal(_("Unable to read data from tempfile.\n%s") % str(e))
  110. finally:
  111. error_xml_opened.close()
  112. if err_str is not None:
  113. grass.fatal(_("WMS server error: %s") % err_str)
  114. else:
  115. grass.fatal(_("WMS server unknown error") )
  116. temp_tile_pct2rgb = None
  117. if tile_dataset_info.RasterCount == 1 and \
  118. tile_dataset_info.GetRasterBand(1).GetRasterColorTable() is not None:
  119. # expansion of color table into bands
  120. temp_tile_pct2rgb = self._tempfile()
  121. tile_dataset = self._pct2rgb(temp_tile, temp_tile_pct2rgb)
  122. else:
  123. tile_dataset = tile_dataset_info
  124. # initialization of temp_map_dataset, where all tiles are merged
  125. if init:
  126. temp_map = self._tempfile()
  127. driver = gdal.GetDriverByName(self.gdal_drv_format)
  128. metadata = driver.GetMetadata()
  129. if not metadata.has_key(gdal.DCAP_CREATE) or \
  130. metadata[gdal.DCAP_CREATE] == 'NO':
  131. grass.fatal(_('Driver %s does not supports Create() method') % drv_format)
  132. self.temp_map_bands_num = tile_dataset.RasterCount
  133. temp_map_dataset = driver.Create(temp_map, map_region['cols'], map_region['rows'],
  134. self.temp_map_bands_num,
  135. tile_dataset.GetRasterBand(1).DataType)
  136. init = False
  137. # tile is written into temp_map
  138. tile_to_temp_map = tile_dataset.ReadRaster(0, 0, tile_ref['sizeX'], tile_ref['sizeY'],
  139. tile_ref['sizeX'], tile_ref['sizeY'])
  140. temp_map_dataset.WriteRaster(tile_ref['t_cols_offset'], tile_ref['t_rows_offset'],
  141. tile_ref['sizeX'], tile_ref['sizeY'], tile_to_temp_map)
  142. tile_dataset = None
  143. tile_dataset_info = None
  144. grass.try_remove(temp_tile)
  145. grass.try_remove(temp_tile_pct2rgb)
  146. if not temp_map:
  147. return temp_map
  148. # georeferencing and setting projection of temp_map
  149. projection = grass.read_command('g.proj',
  150. flags = 'wf',
  151. epsg =self.params['srs']).rstrip('\n')
  152. temp_map_dataset.SetProjection(projection)
  153. pixel_x_length = (map_region['maxx'] - map_region['minx']) / int(map_region['cols'])
  154. pixel_y_length = (map_region['miny'] - map_region['maxy']) / int(map_region['rows'])
  155. geo_transform = [map_region['minx'] , pixel_x_length , 0.0 , map_region['maxy'] , 0.0 , pixel_y_length]
  156. temp_map_dataset.SetGeoTransform(geo_transform )
  157. temp_map_dataset = None
  158. return temp_map
  159. def _pct2rgb(self, src_filename, dst_filename):
  160. """!Create new dataset with data in dst_filename with bands according to src_filename
  161. raster color table - modified code from gdal utility pct2rgb
  162. @return new dataset
  163. """
  164. out_bands = 4
  165. band_number = 1
  166. # open source file
  167. src_ds = gdal.Open(src_filename)
  168. if src_ds is None:
  169. grass.fatal(_('Unable to open %s ' % src_filename))
  170. src_band = src_ds.GetRasterBand(band_number)
  171. # Build color table
  172. lookup = [ Numeric.arrayrange(256),
  173. Numeric.arrayrange(256),
  174. Numeric.arrayrange(256),
  175. Numeric.ones(256)*255 ]
  176. ct = src_band.GetRasterColorTable()
  177. if ct is not None:
  178. for i in range(min(256,ct.GetCount())):
  179. entry = ct.GetColorEntry(i)
  180. for c in range(4):
  181. lookup[c][i] = entry[c]
  182. # create the working file
  183. gtiff_driver = gdal.GetDriverByName(self.gdal_drv_format)
  184. tif_ds = gtiff_driver.Create(dst_filename,
  185. src_ds.RasterXSize, src_ds.RasterYSize, out_bands)
  186. # do the processing one scanline at a time
  187. for iY in range(src_ds.RasterYSize):
  188. src_data = src_band.ReadAsArray(0,iY,src_ds.RasterXSize,1)
  189. for iBand in range(out_bands):
  190. band_lookup = lookup[iBand]
  191. dst_data = Numeric.take(band_lookup,src_data)
  192. tif_ds.GetRasterBand(iBand+1).WriteArray(dst_data,0,iY)
  193. return tif_ds
  194. class BaseRequestMgr:
  195. """!Base class for request managers.
  196. """
  197. def _computeRequestData(self, bbox, tl_corner, tile_span, tile_size, mat_num_bbox):
  198. """!Initialize data needed for iteration through tiles. Used by WMTS_GRASS and OnEarth_GRASS drivers.
  199. """
  200. epsilon = 1e-15
  201. # request data bbox specified in row and col number
  202. self.t_num_bbox = {}
  203. self.t_num_bbox['min_col'] = int(floor((bbox['minx'] - tl_corner['minx']) / tile_span['x'] + epsilon))
  204. self.t_num_bbox['max_col'] = int(floor((bbox['maxx'] - tl_corner['minx']) / tile_span['x'] - epsilon))
  205. self.t_num_bbox['min_row'] = int(floor((tl_corner['maxy'] - bbox['maxy']) / tile_span['y'] + epsilon))
  206. self.t_num_bbox['max_row'] = int(floor((tl_corner['maxy'] - bbox['miny']) / tile_span['y'] - epsilon))
  207. # Does required bbox intersects bbox of data available on server?
  208. self.intersects = False
  209. for col in ['min_col', 'max_col']:
  210. for row in ['min_row', 'max_row']:
  211. if (self.t_num_bbox['min_row'] <= self.t_num_bbox[row] and self.t_num_bbox[row] <= mat_num_bbox['max_row']) and \
  212. (self.t_num_bbox['min_col'] <= self.t_num_bbox[col] and self.t_num_bbox[col] <= mat_num_bbox['max_col']):
  213. self.intersects = True
  214. if not self.intersects:
  215. grass.warning(_('Region is out of server data extend.'))
  216. self.map_region = None
  217. return
  218. # crop request bbox to server data bbox extend
  219. if self.t_num_bbox['min_col'] < (mat_num_bbox['min_col']):
  220. self.t_num_bbox['min_col'] = int(mat_num_bbox['min_col'])
  221. if self.t_num_bbox['max_col'] > (mat_num_bbox['max_col']):
  222. self.t_num_bbox['max_col'] = int(mat_num_bbox['max_col'])
  223. if self.t_num_bbox['min_row'] < (mat_num_bbox['min_row']):
  224. self.t_num_bbox['min_row'] = int(mat_num_bbox['min_row'])
  225. if self.t_num_bbox['max_row'] > (mat_num_bbox['max_row']):
  226. self.t_num_bbox['max_row'] = int(mat_num_bbox['max_row'])
  227. num_tiles = (self.t_num_bbox['max_col'] - self.t_num_bbox['min_col'] + 1) * (self.t_num_bbox['max_row'] - self.t_num_bbox['min_row'] + 1)
  228. grass.message(_('Fetching %d tiles with %d x %d pixel size per tile...') % (num_tiles, tile_size['x'], tile_size['y']))
  229. # georeference of raster, where tiles will be merged
  230. self.map_region = {}
  231. self.map_region['minx'] = self.t_num_bbox['min_col'] * tile_span['x'] + tl_corner['minx']
  232. self.map_region['maxy'] = tl_corner['maxy'] - (self.t_num_bbox['min_row']) * tile_span['y']
  233. self.map_region['maxx'] = (self.t_num_bbox['max_col'] + 1) * tile_span['x'] + tl_corner['minx']
  234. self.map_region['miny'] = tl_corner['maxy'] - (self.t_num_bbox['max_row'] + 1) * tile_span['y']
  235. # size of raster, where tiles will be merged
  236. self.map_region['cols'] = int(tile_size['x'] * (self.t_num_bbox['max_col'] - self.t_num_bbox['min_col'] + 1))
  237. self.map_region['rows'] = int(tile_size['y'] * (self.t_num_bbox['max_row'] - self.t_num_bbox['min_row'] + 1))
  238. # hold information about current column and row during iteration
  239. self.i_col = self.t_num_bbox['min_col']
  240. self.i_row = self.t_num_bbox['min_row']
  241. # bbox for first tile request
  242. self.query_bbox = {
  243. 'minx' : tl_corner['minx'],
  244. 'maxy' : tl_corner['maxy'],
  245. 'maxx' : tl_corner['minx'] + tile_span['x'],
  246. 'miny' : tl_corner['maxy'] - tile_span['y'],
  247. }
  248. self.tile_ref = {
  249. 'sizeX' : tile_size['x'],
  250. 'sizeY' : tile_size['y']
  251. }
  252. def _isGeoProj(self, proj):
  253. """!Is it geographic projection?
  254. """
  255. if (proj.find("+proj=latlong") != -1 or \
  256. proj.find("+proj=longlat") != -1):
  257. return True
  258. return False
  259. class WMSRequestMgr(BaseRequestMgr):
  260. def __init__(self, params, bbox, region, tile_size, proj_srs, cap_file = None):
  261. """!Initialize data needed for iteration through tiles.
  262. """
  263. self.version = params['wms_version']
  264. self.srs_param = params['srs']
  265. proj = params['proj_name'] + "=" + GetSRSParamVal(params['srs'])
  266. self.url = params['url'] + ("SERVICE=WMS&REQUEST=GetMap&VERSION=%s&LAYERS=%s&WIDTH=%s&HEIGHT=%s&STYLES=%s&TRANSPARENT=%s" % \
  267. (params['wms_version'], params['layers'], tile_size['cols'], tile_size['rows'], params['styles'], \
  268. params['transparent']))
  269. if params['bgcolor']:
  270. self.url += "&BGCOLOR=" + params['bgcolor']
  271. self.url += "&" +proj+ "&" + "FORMAT=" + params['format']
  272. self.bbox = bbox
  273. self.proj_srs = proj_srs
  274. self.tile_rows = tile_size['rows']
  275. self.tile_cols = tile_size['cols']
  276. if params['urlparams'] != "":
  277. self.url += "&" + params['urlparams']
  278. cols = int(region['cols'])
  279. rows = int(region['rows'])
  280. # computes parameters of tiles
  281. self.num_tiles_x = cols / self.tile_cols
  282. self.last_tile_x_size = cols % self.tile_cols
  283. self.tile_length_x = float(self.tile_cols) / float(cols) * (self.bbox['maxx'] - self.bbox['minx'])
  284. self.last_tile_x = False
  285. if self.last_tile_x_size != 0:
  286. self.last_tile_x = True
  287. self.num_tiles_x = self.num_tiles_x + 1
  288. self.num_tiles_y = rows / self.tile_rows
  289. self.last_tile_y_size = rows % self.tile_rows
  290. self.tile_length_y = float(self.tile_rows) / float(rows) * (self.bbox['maxy'] - self.bbox['miny'])
  291. self.last_tile_y = False
  292. if self.last_tile_y_size != 0:
  293. self.last_tile_y = True
  294. self.num_tiles_y = self.num_tiles_y + 1
  295. self.tile_bbox = dict(self.bbox)
  296. self.tile_bbox['maxx'] = self.bbox['minx'] + self.tile_length_x
  297. self.i_x = 0
  298. self.i_y = 0
  299. self.map_region = self.bbox
  300. self.map_region['cols'] = cols
  301. self.map_region['rows'] = rows
  302. def GetMapRegion(self):
  303. """!Get size in pixels and bounding box of raster where all tiles will be merged.
  304. """
  305. return self.map_region
  306. def GetNextTile(self):
  307. """!Get url for tile request from server and information for merging the tile with other tiles
  308. """
  309. tile_ref = {}
  310. if self.i_x >= self.num_tiles_x:
  311. return None
  312. tile_ref['sizeX'] = self.tile_cols
  313. if self.i_x == self.num_tiles_x - 1 and self.last_tile_x:
  314. tile_ref['sizeX'] = self.last_tile_x_size
  315. # set bbox for tile (N, S)
  316. if self.i_y != 0:
  317. self.tile_bbox['miny'] -= self.tile_length_y
  318. self.tile_bbox['maxy'] -= self.tile_length_y
  319. else:
  320. self.tile_bbox['maxy'] = self.bbox['maxy']
  321. self.tile_bbox['miny'] = self.bbox['maxy'] - self.tile_length_y
  322. tile_ref['sizeY'] = self.tile_rows
  323. if self.i_y == self.num_tiles_y - 1 and self.last_tile_y:
  324. tile_ref['sizeY'] = self.last_tile_y_size
  325. query_bbox = self._getQueryBbox(self.tile_bbox, self.proj_srs, self.srs_param, self.version)
  326. query_url = self.url + "&" + "BBOX=%s,%s,%s,%s" % ( query_bbox['minx'], query_bbox['miny'], query_bbox['maxx'], query_bbox['maxy'])
  327. tile_ref['t_cols_offset'] = int(self.tile_cols * self.i_x)
  328. tile_ref['t_rows_offset'] = int(self.tile_rows * self.i_y)
  329. if self.i_y >= self.num_tiles_y - 1:
  330. self.i_y = 0
  331. self.i_x += 1
  332. # set bbox for next tile (E, W)
  333. self.tile_bbox['maxx'] += self.tile_length_x
  334. self.tile_bbox['minx'] += self.tile_length_x
  335. else:
  336. self.i_y += 1
  337. return query_url, tile_ref
  338. def _getQueryBbox(self, bbox, proj, srs_param, version):
  339. """!Creates query bbox (used in request URL)
  340. Mostly bbox is not modified but if WMS standard is 1.3.0 and
  341. projection is geographic, the bbox x and y are in most cases flipped.
  342. """
  343. # CRS:84 and CRS:83 are exception (CRS:83 and CRS:27 need to be tested)
  344. if srs_param in [84, 83] or version != '1.3.0':
  345. return bbox
  346. elif self._isGeoProj(proj):
  347. return self._flipBbox(bbox)
  348. return bbox
  349. def _flipBbox(self, bbox):
  350. """
  351. Flips bbox values between this keys:
  352. maxy -> maxx
  353. maxx -> maxy
  354. miny -> minx
  355. minx -> miny
  356. @return copy of bbox with flipped coordinates
  357. """
  358. temp_bbox = dict(bbox)
  359. new_bbox = {}
  360. new_bbox['maxy'] = temp_bbox['maxx']
  361. new_bbox['miny'] = temp_bbox['minx']
  362. new_bbox['maxx'] = temp_bbox['maxy']
  363. new_bbox['minx'] = temp_bbox['miny']
  364. return new_bbox
  365. class WMTSRequestMgr(BaseRequestMgr):
  366. def __init__(self, params, bbox, region, proj_srs, cap_file = None):
  367. """!Initializes data needed for iteration through tiles.
  368. """
  369. self.proj_srs = proj_srs
  370. self.meters_per_unit = None
  371. # constant defined in WMTS standard (in meters)
  372. self.pixel_size = 0.00028
  373. # parse capabilities file
  374. try:
  375. # checks all elements needed by this class,
  376. # invalid elements are removed
  377. cap_tree = WMTSCapabilitiesTree(cap_file)
  378. except ParseError as error:
  379. grass.fatal(_("Unable to parse tile service file.\n%s\n") % str(error))
  380. self.xml_ns = cap_tree.getxmlnshandler()
  381. root = cap_tree.getroot()
  382. # get layer tile matrix sets with required projection
  383. mat_sets = self._getMatSets(root, params['layers'], params['srs']) #[[TileMatrixSet, TileMatrixSetLink], ....]
  384. # TODO: what if more tile matrix sets have required srs (returned more than 1)?
  385. mat_set = mat_sets[0][0]
  386. mat_set_link = mat_sets[0][1]
  387. params['tile_matrix_set'] = mat_set.find(self.xml_ns.NsOws('Identifier')).text
  388. # find tile matrix with resolution closest and smaller to wanted resolution
  389. tile_mat = self._findTileMats(mat_set.findall(self.xml_ns.NsWmts('TileMatrix')), region, bbox)
  390. # get extend of data available on server expressed in max/min rows and cols of tile matrix
  391. mat_num_bbox = self._getMatSize(tile_mat, mat_set_link)
  392. # initialize data needed for iteration through tiles
  393. self._computeRequestData(tile_mat, params, bbox, mat_num_bbox)
  394. def GetMapRegion(self):
  395. """!Get size in pixels and bounding box of raster where all tiles will be merged.
  396. """
  397. return self.map_region
  398. def _getMatSets(self, root, layer_name, srs):
  399. """!Get matrix sets which are available for chosen layer and have required EPSG.
  400. """
  401. contents = root.find(self.xml_ns.NsWmts('Contents'))
  402. layers = contents.findall(self.xml_ns.NsWmts('Layer'))
  403. ch_layer = None
  404. for layer in layers:
  405. layer_id = layer.find(self.xml_ns.NsOws('Identifier')).text
  406. if layer_id == layer_name:
  407. ch_layer = layer
  408. break
  409. if ch_layer is None:
  410. grass.fatal(_("Layer '%s' was not found in capabilities file") % layer_name)
  411. mat_set_links = ch_layer.findall(self.xml_ns.NsWmts('TileMatrixSetLink'))
  412. suitable_mat_sets = []
  413. tileMatrixSets = contents.findall(self.xml_ns.NsWmts('TileMatrixSet'))
  414. for link in mat_set_links:
  415. mat_set_link_id = link.find(self.xml_ns.NsWmts('TileMatrixSet')).text
  416. for mat_set in tileMatrixSets:
  417. mat_set_id = mat_set.find(self.xml_ns.NsOws('Identifier')).text
  418. if mat_set_id != mat_set_link_id:
  419. continue
  420. mat_set_srs = mat_set.find(self.xml_ns.NsOws('SupportedCRS')).text
  421. if mat_set_srs.lower() == ("EPSG:"+ str(srs)).lower():
  422. suitable_mat_sets.append([mat_set, link])
  423. if not suitable_mat_sets:
  424. grass.fatal(_("Layer '%s' is not available with %s code.") % (layer_name, "EPSG:" + str(srs)))
  425. return suitable_mat_sets # [[TileMatrixSet, TileMatrixSetLink], ....]
  426. def _findTileMats(self, tile_mats, region, bbox):
  427. """!Find best tile matrix set for requested resolution.
  428. """
  429. scale_dens = []
  430. scale_dens.append((bbox['maxy'] - bbox['miny']) / region['rows'] * self._getMetersPerUnit() / self.pixel_size)
  431. scale_dens.append((bbox['maxx'] - bbox['minx']) / region['cols'] * self._getMetersPerUnit() / self.pixel_size)
  432. scale_den = min(scale_dens)
  433. first = True
  434. for t_mat in tile_mats:
  435. mat_scale_den = float(t_mat.find(self.xml_ns.NsWmts('ScaleDenominator')).text)
  436. if first:
  437. best_scale_den = mat_scale_den
  438. best_t_mat = t_mat
  439. first = False
  440. continue
  441. best_diff = best_scale_den - scale_den
  442. mat_diff = mat_scale_den - scale_den
  443. if (best_diff < mat_diff and mat_diff < 0) or \
  444. (best_diff > mat_diff and best_diff > 0):
  445. best_t_mat = t_mat
  446. best_scale_den = mat_scale_den
  447. return best_t_mat
  448. def _getMetersPerUnit(self):
  449. """!Get coefficient which allows to convert units of request projection into meters.
  450. """
  451. if self.meters_per_unit:
  452. return self.meters_per_unit
  453. # for geographic projection
  454. if self._isGeoProj(self.proj_srs):
  455. proj_params = self.proj_srs.split(' ')
  456. for param in proj_params:
  457. if '+a' in param:
  458. a = float(param.split('=')[1])
  459. break
  460. equator_perim = 2 * pi * a
  461. # meters per degree on equator
  462. self.meters_per_unit = equator_perim / 360
  463. # other units
  464. elif '+to_meter' in self.proj_srs:
  465. proj_params = self.proj_srs.split(' ')
  466. for param in proj_params:
  467. if '+to_meter' in param:
  468. self.meters_per_unit = 1/float(param.split('=')[1])
  469. break
  470. # coordinate system in meters
  471. else:
  472. self.meters_per_unit = 1
  473. return self.meters_per_unit
  474. def _getMatSize(self, tile_mat, mat_set_link):
  475. """!Get rows and cols extend of data available on server for chosen layer and tile matrix.
  476. """
  477. # general tile matrix size
  478. mat_num_bbox = {}
  479. mat_num_bbox['min_col'] = mat_num_bbox['min_row'] = 0
  480. mat_num_bbox['max_col'] = int(tile_mat.find(self.xml_ns.NsWmts('MatrixWidth')).text) - 1
  481. mat_num_bbox['max_row'] = int(tile_mat.find(self.xml_ns.NsWmts('MatrixHeight')).text) - 1
  482. # get extend restriction in TileMatrixSetLink for the tile matrix, if exists
  483. tile_mat_set_limits = mat_set_link.find((self.xml_ns.NsWmts('TileMatrixSetLimits')))
  484. if tile_mat_set_limits is None:
  485. return mat_num_bbox
  486. tile_mat_id = tile_mat.find(self.xml_ns.NsOws('Identifier')).text
  487. tile_mat_limits = tile_mat_set_limits.findall(self.xml_ns.NsWmts('TileMatrixLimits'))
  488. for limit in tile_mat_limits:
  489. limit_tile_mat = limit.find(self.xml_ns.NsWmts('TileMatrix'))
  490. limit_id = limit_tile_mat.text
  491. if limit_id == tile_mat_id:
  492. for i in [['min_row', 'MinTileRow'], ['max_row', 'MaxTileRow'], \
  493. ['min_col', 'MinTileCol'], ['max_col', 'MaxTileCol']]:
  494. i_tag = limit.find(self.xml_ns.NsWmts(i[1]))
  495. mat_num_bbox[i[0]] = int(i_tag.text)
  496. break
  497. return mat_num_bbox
  498. def _computeRequestData(self, tile_mat, params, bbox, mat_num_bbox):
  499. """!Initialize data needed for iteration through tiles.
  500. """
  501. scale_den = float(tile_mat.find(self.xml_ns.NsWmts('ScaleDenominator')).text)
  502. pixel_span = scale_den * self.pixel_size / self._getMetersPerUnit()
  503. tl_str = tile_mat.find(self.xml_ns.NsWmts('TopLeftCorner')).text.split(' ')
  504. tl_corner = {}
  505. tl_corner['minx'] = float(tl_str[0])
  506. tl_corner['maxy'] = float(tl_str[1])
  507. tile_span = {}
  508. self.tile_size = {}
  509. self.tile_size['x'] = int(tile_mat.find(self.xml_ns.NsWmts('TileWidth')).text)
  510. tile_span['x'] = pixel_span * self.tile_size['x']
  511. self.tile_size['y'] = int(tile_mat.find(self.xml_ns.NsWmts('TileHeight')).text)
  512. tile_span['y'] = pixel_span * self.tile_size['y']
  513. self.url = params['url'] + ("SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&" \
  514. "LAYER=%s&STYLE=%s&FORMAT=%s&TILEMATRIXSET=%s&TILEMATRIX=%s" % \
  515. (params['layers'], params['styles'], params['format'],
  516. params['tile_matrix_set'], tile_mat.find(self.xml_ns.NsOws('Identifier')).text ))
  517. BaseRequestMgr._computeRequestData(self, bbox, tl_corner, tile_span, self.tile_size, mat_num_bbox)
  518. def GetNextTile(self):
  519. """!Get url for tile request from server and information for merging the tile with other tiles.
  520. """
  521. if not self.intersects or self.i_col > self.t_num_bbox['max_col']:
  522. return None
  523. query_url = self.url + "&TILECOL=%i&TILEROW=%i" % (int(self.i_col), int(self.i_row))
  524. self.tile_ref['t_cols_offset'] = int(self.tile_size['x'] * (self.i_col - self.t_num_bbox['min_col']))
  525. self.tile_ref['t_rows_offset'] = int(self.tile_size['y'] * (self.i_row - self.t_num_bbox['min_row']))
  526. if self.i_row >= self.t_num_bbox['max_row']:
  527. self.i_row = self.t_num_bbox['min_row']
  528. self.i_col += 1
  529. else:
  530. self.i_row += 1
  531. return query_url, self.tile_ref
  532. class OnEarthRequestMgr(BaseRequestMgr):
  533. def __init__(self, params, bbox, region, proj_srs, tile_service):
  534. """!Initializes data needed for iteration through tiles.
  535. """
  536. try:
  537. # checks all elements needed by this class,
  538. # invalid elements are removed
  539. self.cap_tree = OnEarthCapabilitiesTree(tile_service)
  540. except ParseError as error:
  541. grass.fatal(_("Unable to parse tile service file.\n%s\n") % str(error))
  542. root = self.cap_tree.getroot()
  543. # parse tile service file and get needed data for making tile requests
  544. url, self.tile_span, t_patt_bbox, self.tile_size = self._parseTileService(root, bbox, region, params)
  545. self.url = url
  546. self.url[0] = params['url'] + url[0]
  547. # initialize data needed for iteration through tiles
  548. self._computeRequestData(bbox, t_patt_bbox, self.tile_span, self.tile_size)
  549. def GetMapRegion(self):
  550. """!Get size in pixels and bounding box of raster where all tiles will be merged.
  551. """
  552. return self.map_region
  553. def _parseTileService(self, root, bbox, region, params):
  554. """!Get data from tile service file
  555. """
  556. tiled_patterns = root.find('TiledPatterns')
  557. tile_groups = self._getAllTiledGroup(tiled_patterns)
  558. if not tile_groups:
  559. grass.fatal(_("Unable to parse tile service file. \n No tag '%s' was found.") % 'TiledGroup')
  560. req_group = None
  561. for group in tile_groups:
  562. name = group.find('Name')
  563. if name.text == params['layers']:
  564. req_group = group
  565. break
  566. if req_group is None:
  567. grass.fatal(_("Tiled group '%s' was not found in tile service file") % params['layers'])
  568. group_t_patts = req_group.findall('TilePattern')
  569. best_patt = self._parseTilePattern(group_t_patts, bbox, region)
  570. urls = best_patt.text.split('\n')
  571. if params['urlparams']:
  572. url = self._insTimeToTilePatternUrl(params['urlparams'], urls)
  573. else:
  574. url = urls[0]
  575. for u in urls:
  576. if not 'time=${' in u:
  577. url = u
  578. url, t_bbox, width, height = self.cap_tree.gettilepatternurldata(url)
  579. tile_span = {}
  580. tile_span['x'] = abs(t_bbox[0] - t_bbox[2])
  581. tile_span['y'] = abs(t_bbox[1] - t_bbox[3])
  582. tile_pattern_bbox = req_group.find('LatLonBoundingBox')
  583. t_patt_bbox = {}
  584. for s in ['minx', 'miny', 'maxx', 'maxy']:
  585. t_patt_bbox[s] = float(tile_pattern_bbox.get(s))
  586. tile_size = {}
  587. tile_size['x'] = width
  588. tile_size['y'] = height
  589. return url, tile_span, t_patt_bbox, tile_size
  590. def _getAllTiledGroup(self, parent, tiled_groups = None):
  591. """!Get all 'TileGroup' elements
  592. """
  593. if not tiled_groups:
  594. tiled_groups = []
  595. tiled_groups += parent.findall('TiledGroup')
  596. new_groups = parent.findall('TiledGroups')
  597. for group in new_groups:
  598. self._getAllTiledGroup(group, tiled_groups)
  599. return tiled_groups
  600. def _parseTilePattern(self, group_t_patts, bbox, region):
  601. """!Find best tile pattern for requested resolution.
  602. """
  603. res = {}
  604. res['y'] = (bbox['maxy'] - bbox['miny']) / region['rows']
  605. res['x'] = (bbox['maxx'] - bbox['minx']) / region['cols']
  606. if res['x'] < res['y']:
  607. comp_res = 'x'
  608. else:
  609. comp_res = 'y'
  610. t_res = {}
  611. best_patt = None
  612. for pattern in group_t_patts:
  613. url, t_bbox, width, height = self.cap_tree.gettilepatternurldata(pattern.text.split('\n')[0])
  614. t_res['x'] = abs(t_bbox[0] - t_bbox[2]) / width
  615. t_res['y'] = abs(t_bbox[1] - t_bbox[3]) / height
  616. if best_patt is None:
  617. best_res = t_res[comp_res]
  618. best_patt = pattern
  619. first = False
  620. continue
  621. best_diff = best_res - res[comp_res]
  622. tile_diff = t_res[comp_res] - res[comp_res]
  623. if (best_diff < tile_diff and tile_diff < 0) or \
  624. (best_diff > tile_diff and best_diff > 0):
  625. best_res = t_res[comp_res]
  626. best_patt = pattern
  627. return best_patt
  628. def _insTimeToTilePatternUrl(self, url_params, urls):
  629. """!Time can be variable in some urls in OnEarth TMS.
  630. Insert requested time from 'urlparams' into the variable if any url of urls contains the variable.
  631. """
  632. url = None
  633. not_sup_params = []
  634. url_params_list = url_params.split('&')
  635. for param in url_params_list:
  636. try:
  637. k, v = param.split('=')
  638. except ValueError:
  639. grass.warning(_("Wrong form of parameter '%s' in '%s'. \n \
  640. The parameter was ignored.") % (param, 'urlparams'))
  641. if k != 'time':
  642. not_sup_params.append(k)
  643. continue
  644. has_time_var = False
  645. for url in urls:
  646. url_p_idxs = self.geturlparamidxs(url, k)
  647. if not url_p_idxs:
  648. continue
  649. url_p_value = url[url_p_idxs[0] + len(k + '=') : url_p_idxs[1]]
  650. if url_p_value[:2] == '${' and \
  651. url_p_value[len(url_p_value) - 1] == '}':
  652. url = url[:url_p_idxs[0]] + param + url[url_p_idxs[1]:]
  653. has_time_var = True
  654. break
  655. if not has_time_var:
  656. grass.warning(_("Parameter '%s' in '%s' is not variable in tile pattern url.") % (k, 'urlparams'))
  657. if not_sup_params:
  658. grass.warning(_("%s driver supports only '%s' parameter in '%s'. Other parameters are ignored.") % \
  659. ('OnEarth GRASS', 'time', 'urlparams'))
  660. return url
  661. def _computeRequestData(self, bbox, t_patt_bbox, tile_span, tile_size):
  662. """!Initialize data needed for iteration through tiles.
  663. """
  664. epsilon = 1e-15
  665. mat_num_bbox = {}
  666. mat_num_bbox['min_row'] = mat_num_bbox['min_col'] = 0
  667. mat_num_bbox['max_row'] = floor((t_patt_bbox['maxy'] - t_patt_bbox['miny'])/ tile_span['y'] + epsilon)
  668. mat_num_bbox['max_col'] = floor((t_patt_bbox['maxx'] - t_patt_bbox['minx'])/ tile_span['x'] + epsilon)
  669. BaseRequestMgr._computeRequestData(self, bbox, t_patt_bbox, self.tile_span, self.tile_size, mat_num_bbox)
  670. def GetNextTile(self):
  671. """!Get url for tile request from server and information for merging the tile with other tiles
  672. """
  673. if self.i_col > self.t_num_bbox['max_col']:
  674. return None
  675. x_offset = self.tile_span['x'] * self.i_col
  676. y_offset = self.tile_span['y'] * self.i_row
  677. query_url = self.url[0] + "&" + "bbox=%s,%s,%s,%s" % (float(self.query_bbox['minx'] + x_offset),
  678. float(self.query_bbox['miny'] - y_offset),
  679. float(self.query_bbox['maxx'] + x_offset),
  680. float(self.query_bbox['maxy'] - y_offset)) + self.url[1]
  681. self.tile_ref['t_cols_offset'] = int(self.tile_size['y'] * (self.i_col - self.t_num_bbox['min_col']))
  682. self.tile_ref['t_rows_offset'] = int(self.tile_size['x'] * (self.i_row - self.t_num_bbox['min_row']))
  683. if self.i_row >= self.t_num_bbox['max_row']:
  684. self.i_row = self.t_num_bbox['min_row']
  685. self.i_col += 1
  686. else:
  687. self.i_row += 1
  688. return query_url, self.tile_ref