def create_outputformat(mime_type, options, imagemode, basename, parameters): """ Returns a ``mapscript.outputFormatObj`` for the given format name and imagemode. """ reg_format = get_format_by_mime(mime_type) if not reg_format: raise RenderException( "Unsupported output format '%s'." % mime_type, "format" ) outputformat = ms.outputFormatObj(reg_format.driver, "custom") outputformat.name = reg_format.wcs10name outputformat.mimetype = reg_format.mimeType outputformat.extension = reg_format.defaultExt outputformat.imagemode = imagemode #for key, value in options: # outputformat.setOption(str(key), str(value)) if mime_type == "image/tiff": _apply_gtiff(outputformat, **parameters) filename = basename + reg_format.defaultExt outputformat.setOption("FILENAME", str(filename)) return outputformat
def create_outputformat(mime_type, options, imagemode, basename, parameters): """ Returns a ``mapscript.outputFormatObj`` for the given format name and imagemode. """ reg_format = get_format_by_mime(mime_type) if not reg_format: raise RenderException("Unsupported output format '%s'." % mime_type, "format") outputformat = ms.outputFormatObj(reg_format.driver, "custom") outputformat.name = reg_format.wcs10name outputformat.mimetype = reg_format.mimeType outputformat.extension = reg_format.defaultExt outputformat.imagemode = imagemode #for key, value in options: # outputformat.setOption(str(key), str(value)) if mime_type == "image/tiff": _apply_gtiff(outputformat, **parameters) filename = basename + reg_format.defaultExt outputformat.setOption("FILENAME", str(filename)) return outputformat
def get_all_outputformats(self, use_mime=True): outputformats = [] for frmt in self.get_wcs_formats(): of = ms.outputFormatObj(frmt.driver, "custom") of.name = frmt.mimeType if use_mime else frmt.wcs10name of.mimetype = frmt.mimeType of.extension = frmt.defaultExt outputformats.append(of) return outputformats
def create_outputformat(mime_type, options, imagemode, basename): """ Returns a ``mapscript.outputFormatObj`` for the given format name and imagemode. """ reg_format = get_format_by_mime(mime_type) if not reg_format: raise Exception("Unsupported output format '%s'." % frmt) outputformat = ms.outputFormatObj(reg_format.driver, "custom") outputformat.name = reg_format.wcs10name outputformat.mimetype = reg_format.mimeType outputformat.extension = reg_format.defaultExt outputformat.imagemode = imagemode for key, value in options: outputformat.setOption(str(key), str(value)) filename = basename + reg_format.defaultExt outputformat.setOption("FILENAME", str(filename)) return outputformat
def render_map(self, render_map): # TODO: get layer creators for each layer type in the map map_obj = ms.mapObj() if render_map.bgcolor: map_obj.imagecolor.setHex("#" + render_map.bgcolor.lower()) else: map_obj.imagecolor.setRGB(0, 0, 0) frmt = getFormatRegistry().getFormatByMIME(render_map.format) if not frmt: raise MapRenderError('No such format %r' % render_map.format, code='InvalidFormat', locator='format') outputformat_obj = ms.outputFormatObj(frmt.driver) outputformat_obj.transparent = (ms.MS_ON if render_map.transparent else ms.MS_OFF) outputformat_obj.mimetype = frmt.mimeType if frmt.defaultExt: if frmt.defaultExt.startswith('.'): extension = frmt.defaultExt[1:] else: extension = frmt.defaultExt outputformat_obj.extension = extension map_obj.setOutputFormat(outputformat_obj) # map_obj.setExtent(*render_map.bbox) map_obj.setSize(render_map.width, render_map.height) map_obj.setProjection(render_map.crs) map_obj.setConfigOption('MS_NONSQUARE', 'yes') layers_plus_factories = self._get_layers_plus_factories(render_map) layers_plus_factories_plus_data = [ (layer, factory, factory.create(map_obj, layer)) for layer, factory in layers_plus_factories ] # log the resulting map if logger.isEnabledFor(logging.DEBUG): with tempfile.NamedTemporaryFile() as f: map_obj.save(f.name) f.seek(0) logger.debug(f.read().decode('ascii')) try: # actually render the map image_obj = map_obj.draw() try: image_bytes = image_obj.getBytes() except Exception: tmp_name = '/vsimem/%s' % uuid4().hex image_obj.save(tmp_name, map_obj) with vsi.open(tmp_name) as f: image_bytes = f.read() vsi.unlink(tmp_name) extension = outputformat_obj.extension if extension: if len(render_map.layers) == 1: filename = '%s.%s' % (render_map.layers[0].name, extension) else: filename = 'map.%s' % extension else: filename = None return image_bytes, outputformat_obj.mimetype, filename finally: # disconnect for layer, factory, data in layers_plus_factories_plus_data: factory.destroy(map_obj, layer, data)