def _init_proj(): global _proj_initalized if not _proj_initalized and 'proj_data_dir' in base_config().srs: proj_data_dir = base_config().srs['proj_data_dir'] log_system.info('loading proj data from %s', proj_data_dir) set_datapath(proj_data_dir) _proj_initalized = True
def img_to_buf(img, image_opts): defaults = {} if image_opts.mode and img.mode[0] == "I" and img.mode != image_opts.mode: img = img.convert(image_opts.mode) if image_opts.colors is None and base_config().image.paletted and image_opts.format.endswith("png"): # force 255 colors for png with globals.image.paletted image_opts = image_opts.copy() image_opts.colors = 255 if image_opts.colors: quantizer = None if "quantizer" in image_opts.encoding_options: quantizer = image_opts.encoding_options["quantizer"] if image_opts.transparent: img = quantize(img, colors=image_opts.colors, alpha=True, defaults=defaults, quantizer=quantizer) else: img = quantize(img, colors=image_opts.colors, quantizer=quantizer) if hasattr(Image, "RLE"): defaults["compress_type"] = Image.RLE format = filter_format(image_opts.format.ext) buf = StringIO() if format == "jpeg": img = img.convert("RGB") if "jpeg_quality" in image_opts.encoding_options: defaults["quality"] = image_opts.encoding_options["jpeg_quality"] else: defaults["quality"] = base_config().image.jpeg_quality img.save(buf, format, **defaults) buf.seek(0) return buf
def test_defaults(self): with TempFiles() as tmp: with open(tmp[0], 'wb') as f: f.write(TestDefaultsLoading.defaults_yaml) load_base_config(config_file=tmp[0], clear_existing=True) assert base_config().biz == 'foobar' assert base_config().wiz == 'foobar' assert base_config().foo.bar.ham == 2 assert base_config().foo.bar.eggs == 4 assert not hasattr(base_config(), 'wms')
def img_to_buf(img, image_opts): defaults = {} image_opts = image_opts.copy() # convert I or L images to target mode if image_opts.mode and img.mode[0] in ('I', 'L') and img.mode != image_opts.mode: img = img.convert(image_opts.mode) if (image_opts.colors is None and base_config().image.paletted and image_opts.format.endswith('png')): # force 255 colors for png with globals.image.paletted image_opts.colors = 255 format = filter_format(image_opts.format.ext) if format == 'mixed': if img_has_transparency(img): format = 'png' else: format = 'jpeg' image_opts.colors = None image_opts.transparent = False # quantize if colors is set, but not if we already have a paletted image if image_opts.colors and not (img.mode == 'P' and len(img.getpalette()) == image_opts.colors*3): quantizer = None if 'quantizer' in image_opts.encoding_options: quantizer = image_opts.encoding_options['quantizer'] if image_opts.transparent: img = quantize(img, colors=image_opts.colors, alpha=True, defaults=defaults, quantizer=quantizer) else: img = quantize(img, colors=image_opts.colors, quantizer=quantizer) if hasattr(Image, 'RLE'): defaults['compress_type'] = Image.RLE buf = BytesIO() if format == 'jpeg': img = img.convert('RGB') if 'jpeg_quality' in image_opts.encoding_options: defaults['quality'] = image_opts.encoding_options['jpeg_quality'] else: defaults['quality'] = base_config().image.jpeg_quality # unsupported transparency tuple can still be in non-RGB img.infos # see: https://github.com/python-pillow/Pillow/pull/2633 if format == 'png' and img.mode != 'RGB' and 'transparency' in img.info and isinstance(img.info['transparency'], tuple): del img.info['transparency'] img.save(buf, format, **defaults) buf.seek(0) return buf
def test_base_config(self): # test that all concurrent have access to their # local base_config from mapproxy.config import base_config from mapproxy.config import local_base_config from copy import deepcopy # make two separate base_configs conf1 = deepcopy(base_config()) conf1.conf = 1 conf2 = deepcopy(base_config()) conf2.conf = 2 base_config().bar = 'baz' # run test in parallel, check1 and check2 should interleave # each with their local conf error_occured = False def check1(x): global error_occured if base_config().conf != 1 or 'bar' in base_config(): error_occured = True def check2(x): global error_occured if base_config().conf != 2 or 'bar' in base_config(): error_occured = True assert 'bar' in base_config() def test1(): with local_base_config(conf1): pool1 = ThreadPool(5) list(pool1.imap(check1, range(200))) def test2(): with local_base_config(conf2): pool2 = ThreadPool(5) list(pool2.imap(check2, range(200))) t1 = threading.Thread(target=test1) t2 = threading.Thread(target=test2) t1.start() t2.start() t1.join() t2.join() assert not error_occured assert 'bar' in base_config()
def test_user_srs_definitions(self): user_yaml = b""" srs: axis_order_ne: ['EPSG:9999'] """ with TempFiles() as tmp: with open(tmp[0], 'wb') as f: f.write(user_yaml) load_base_config(config_file=tmp[0]) assert 'EPSG:9999' in base_config().srs.axis_order_ne assert 'EPSG:9999' not in base_config().srs.axis_order_en #defaults still there assert 'EPSG:31468' in base_config().srs.axis_order_ne assert 'CRS:84' in base_config().srs.axis_order_en
def font_file(font_name): font_dir = base_config().image.font_dir if font_dir: abspath(font_dir) else: font_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'fonts') font_name = font_name.replace(' ', '') path = os.path.join(font_dir, font_name + '.ttf') return path
def font_file(font_name): font_dir = base_config().image.font_dir font_name = font_name.replace(' ', '') if font_dir: abspath(font_dir) path = os.path.join(font_dir, font_name + '.ttf') else: path = pkg_resources.resource_filename(__name__, 'fonts/' + font_name + '.ttf') return path
def img_to_buf(img, image_opts): defaults = {} image_opts = image_opts.copy() if image_opts.mode and img.mode[0] == 'I' and img.mode != image_opts.mode: img = img.convert(image_opts.mode) if (image_opts.colors is None and base_config().image.paletted and image_opts.format.endswith('png')): # force 255 colors for png with globals.image.paletted image_opts.colors = 255 format = filter_format(image_opts.format.ext) if format == 'mixed': if img_has_transparency(img): format = 'png' else: format = 'jpeg' image_opts.colors = None image_opts.transparent = False if image_opts.colors: quantizer = None if 'quantizer' in image_opts.encoding_options: quantizer = image_opts.encoding_options['quantizer'] if image_opts.transparent: img = quantize(img, colors=image_opts.colors, alpha=True, defaults=defaults, quantizer=quantizer) else: img = quantize(img, colors=image_opts.colors, quantizer=quantizer) if hasattr(Image, 'RLE'): defaults['compress_type'] = Image.RLE buf = StringIO() if format == 'jpeg': img = img.convert('RGB') if 'jpeg_quality' in image_opts.encoding_options: defaults['quality'] = image_opts.encoding_options['jpeg_quality'] else: defaults['quality'] = base_config().image.jpeg_quality img.save(buf, format, **defaults) buf.seek(0) return buf
def __init__(self, task, worker_class, size=2, dry_run=False, progress_logger=None): self.tiles_queue = queue_class(size) self.task = task self.dry_run = dry_run self.procs = [] self.progress_logger = progress_logger conf = base_config() for _ in xrange(size): worker = worker_class(self.task, self.tiles_queue, conf) worker.start() self.procs.append(worker)
def is_axis_order_ne(self): """ Returns `True` if the axis order is North, then East (i.e. y/x or lat/lon). >>> SRS(4326).is_axis_order_ne True >>> SRS('CRS:84').is_axis_order_ne False >>> SRS(31468).is_axis_order_ne True >>> SRS(31463).is_axis_order_ne False >>> SRS(25831).is_axis_order_ne False """ if self.srs_code in base_config().srs.axis_order_ne: return True if self.srs_code in base_config().srs.axis_order_en: return False if self.is_latlong: return True return False
def test_defaults_overwrite(self): with TempFiles(2) as tmp: with open(tmp[0], 'wb') as f: f.write(TestDefaultsLoading.defaults_yaml) with open(tmp[1], 'wb') as f: f.write(b""" baz: [9, 2, 1, 4] biz: 'barfoo' foo: bar: eggs: 5 """) load_base_config(config_file=tmp[0], clear_existing=True) load_base_config(config_file=tmp[1]) assert base_config().biz == 'barfoo' assert base_config().wiz == 'foobar' assert base_config().baz == [9, 2, 1, 4] assert base_config().foo.bar.ham == 2 assert base_config().foo.bar.eggs == 5 assert not hasattr(base_config(), 'wms')
def setup(self): srs._proj_initalized = False srs._srs_cache = {} base_config().srs.proj_data_dir = os.path.dirname(__file__)
def __init__(self, size=100): self.size = size self.base_config = base_config()
def __init__(self, task_queue, result_queue): threading.Thread.__init__(self) self.task_queue = task_queue self.result_queue = result_queue self.base_config = base_config()
def img_to_buf(img, image_opts, georef=None): defaults = {} image_opts = image_opts.copy() # convert I or L images to target mode if image_opts.mode and img.mode[0] in ( 'I', 'L') and img.mode != image_opts.mode: img = img.convert(image_opts.mode) if (image_opts.colors is None and base_config().image.paletted and image_opts.format.endswith('png')): # force 255 colors for png with globals.image.paletted image_opts.colors = 255 format = filter_format(image_opts.format.ext) if format == 'mixed': if img_has_transparency(img): format = 'png' else: format = 'jpeg' image_opts.colors = None image_opts.transparent = False # quantize if colors is set, but not if we already have a paletted image if image_opts.colors and not (img.mode == 'P' and len(img.getpalette()) == image_opts.colors * 3): quantizer = None if 'quantizer' in image_opts.encoding_options: quantizer = image_opts.encoding_options['quantizer'] if image_opts.transparent: img = quantize(img, colors=image_opts.colors, alpha=True, defaults=defaults, quantizer=quantizer) else: img = quantize(img, colors=image_opts.colors, quantizer=quantizer) if hasattr(Image, 'RLE'): defaults['compress_type'] = Image.RLE buf = BytesIO() if format == 'jpeg': img = img.convert('RGB') if 'jpeg_quality' in image_opts.encoding_options: defaults['quality'] = image_opts.encoding_options['jpeg_quality'] else: defaults['quality'] = base_config().image.jpeg_quality elif format == 'tiff': if georef: tags = georef.tiff_tags(img.size) defaults['tiffinfo'] = tags if 'tiff_compression' in image_opts.encoding_options: defaults['compression'] = image_opts.encoding_options[ 'tiff_compression'] if defaults['compression'] == 'jpeg': if 'jpeg_quality' in image_opts.encoding_options: defaults['quality'] = image_opts.encoding_options[ 'jpeg_quality'] # unsupported transparency tuple can still be in non-RGB img.infos # see: https://github.com/python-pillow/Pillow/pull/2633 if format == 'png' and img.mode != 'RGB' and 'transparency' in img.info and isinstance( img.info['transparency'], tuple): del img.info['transparency'] img.save(buf, format, **defaults) buf.seek(0) return buf
def check1(x): global error_occured if base_config().conf != 1 or 'bar' in base_config(): error_occured = True
def teardown(self): srs._proj_initalized = False srs._srs_cache = {} base_config().srs.proj_data_dir = None
def check2(x): global error_occured if base_config().conf != 2 or 'bar' in base_config(): error_occured = True