def _get_box(self, req: gws.IWebRequest, p: GetBoxParams) -> gws.BytesResponse: layer = req.require_layer(p.layerUid) content = None mri = gws.MapRenderInput( ) extra_params = {} if p.layers: extra_params['layers'] = p.layers view = gws.gis.render.map_view_from_bbox( crs=gws.gis.crs.get(p.crs) or layer.map.crs, bbox=p.bbox, size=(p.width, p.height, units.PX), dpi=units.OGC_SCREEN_PPI, rotation=0 ) ts = gws.time_start(f'RENDER_BOX layer={p.layerUid} view={view!r}') try: content = layer.render_box(view, extra_params) except: gws.log.exception() gws.time_end(ts) return self._image_response(content)
def render_map_bbox_from_layer_caps_list(self, rd: Request, lcs: t.List[LayerCaps], bounds: gws.Bounds) -> gws.ContentResponse: try: px_width = int(rd.req.param('width')) px_height = int(rd.req.param('height')) except: raise gws.base.web.error.BadRequest() if not bounds or not px_width or not px_height: raise gws.base.web.error.BadRequest() mri = gws.MapRenderInput( background_color=0 if rd.req.param('transparent', '').lower() == 'false' else None, bbox=bounds.extent, crs=bounds.crs, out_size=(px_width, px_height, units.PX), planes=[ gws.MapRenderInputPlane(type='image_layer', layer=lc.layer) for lc in lcs ] ) mro = gws.gis.render.render_map(mri) if mro.planes and mro.planes[0].image: content = mro.planes[0].image.to_bytes() else: content = gws.lib.image.PIXEL_PNG8 return gws.ContentResponse(mime=gws.lib.mime.PNG, content=content)
def render(self, tri, notify=None): mp = tri.maps[0] mri = gws.MapRenderInput( background_color=mp.background_color, bbox=mp.bbox, center=mp.center, crs=tri.crs, dpi=tri.dpi, out_size=self.page_size, planes=mp.planes, rotation=mp.rotation, scale=mp.scale, ) notify = notify or (lambda a, b=None: None) notify('begin_print') notify('begin_page') notify('begin_map') mro = gws.gis.render.render_map(mri, notify) html = gws.gis.render.output_to_html_string(mro, wrap='fixed') notify('end_map') notify('end_page') notify('finalize_print') if not tri.out_mime or tri.out_mime == gws.lib.mime.HTML: notify('end_print') return gws.ContentResponse(mime=gws.lib.mime.HTML, content=html) if tri.out_mime == gws.lib.mime.PDF: res_path = gws.tempname('map.pdf') gws.lib.html2.render_to_pdf( html, out_path=res_path, page_size=self.page_size, ) notify('end_print') return gws.ContentResponse(path=res_path) if tri.out_mime == gws.lib.mime.PNG: res_path = gws.tempname('map.png') gws.lib.html2.render_to_png( html, out_path=res_path, page_size=self.page_size, ) notify('end_print') return gws.ContentResponse(path=res_path) raise gws.Error(f'invalid output mime: {tri.out_mime!r}')
def _prepare_map(self, rim: gws.TemplateRenderInputMap, opts) -> gws.MapRenderInput: # these are mandatory width = int(opts['width']) height = int(opts['height']) return gws.MapRenderInput( background_color=rim.background_color, bbox=opts.get('bbox', rim.bbox), center=opts.get('center', rim.center), crs=self.tri.crs, dpi=self.tri.dpi, out_size=(width, height, units.MM), planes=rim.planes, rotation=opts.get('scale', rim.rotation), scale=opts.get('scale', rim.scale), )
def handle_gettile(self, rd: core.Request): try: matrix_set_uid = rd.req.param('TILEMATRIXSET') matrix_uid = rd.req.param('TILEMATRIX') row = int(rd.req.param('TILEROW')) col = int(rd.req.param('TILECOL')) except Exception: raise gws.base.web.error.BadRequest() lcs = self.layer_caps_list_from_request(rd, ['layer'], self.SCOPE_LEAF) if not lcs: raise gws.base.web.error.NotFound() bounds = self._bounds_for_tile(matrix_set_uid, matrix_uid, row, col) if not bounds: raise gws.base.web.error.BadRequest() # crs = rd.project.map.crs # bbox = gws.gis.extent.transform(bbox, tm_crs, crs) mri = gws.MapRenderInput(background_color=None, bbox=bounds.extent, crs=bounds.crs, out_size=(256, 256, units.PX), planes=[ gws.MapRenderInputPlane( type='image_layer', layer=lc.layer) for lc in lcs ]) mro = gws.gis.render.render_map(mri) if mro.planes and mro.planes[0].image: content = mro.planes[0].image.to_bytes() else: content = gws.lib.image.PIXEL_PNG8 if self.root.application.developer_option('ows.annotate_wmts'): img = gws.lib.image.from_bytes(content) e = bounds.extent text = f"{matrix_uid} {row} {col}\n{e[0]}\n{e[1]}\n{e[2]}\n{e[3]}" content = img.add_text(text, x=10, y=10).add_box().to_bytes() return gws.ContentResponse(mime='image/png', content=content)
def _render_map(self, tri: gws.TemplateRenderInput, notify, out_path): if not tri.maps: return mp = tri.maps[0] mri = gws.MapRenderInput( background_color=mp.background_color, bbox=mp.bbox, center=mp.center, crs=tri.crs, dpi=tri.dpi, out_size=self.map_size, planes=mp.planes, rotation=mp.rotation, scale=mp.scale, ) mro = gws.gis.render.render_map(mri, notify) html = gws.gis.render.output_to_html_string(mro) # create an empty page with the map positioned at the right place # @TODO the position is a couple mm off # for better results, the map pos/size in qgis must be integer x, y, _ = self.map_position w, h, _ = self.map_size css = f""" position: fixed; left: {int(x)}mm; top: {int(y)}mm; width: {int(w)}mm; height: {int(h)}mm; """ html = f"<div style='{css}'>{html}</div>" gws.lib.html2.render_to_pdf(html, out_path, self.page_size) return mro