예제 #1
0
def test_paginate():

    char = '+'
    fill = '.'
    colormap = {
        '+': 'red',
        '.': 'black'
    }
    with fio.open(POLY_FILE) as src1, fio.open(POLY_FILE) as src2:
        for paginated_feat, feat in zip(
                gj2ascii.paginate(src1, char=char, fill=fill, colormap=colormap), src2):
            assert paginated_feat.strip() == gj2ascii.style(
                gj2ascii.render(feat, char=char, fill=fill), stylemap=colormap)
예제 #2
0
def test_style():

    array = [['0', '0', '0', '1', '0'], [' ', ' ', '2', '0', '1'],
             ['1', '1', '2', '1', '3']]
    colormap = {
        ' ': 'black',
        '0': 'blue',
        '1': 'yellow',
        '2': 'white',
        '3': 'red'
    }
    expected = []
    for row in array:
        o_row = []
        for char in row:
            color = gj2ascii.ANSI_COLORMAP[colormap[char]]
            o_row.append(color + char + ' ' + gj2ascii.core._ANSI_RESET)
        expected.append(''.join(o_row))
    expected = os.linesep.join(expected)
    assert expected == gj2ascii.style(gj2ascii.array2ascii(array),
                                      stylemap=colormap)
예제 #3
0
파일: test_core.py 프로젝트: nerik/gj2ascii
    def test_style(self):

        array = [['0', '0', '0', '1', '0'],
                 [' ', ' ', '2', '0', '1'],
                 ['1', '1', '2', '1', '3']]
        colormap = {
            ' ': 'black',
            '0': 'blue',
            '1': 'yellow',
            '2': 'white',
            '3': 'red'
        }
        expected = []
        for row in array:
            o_row = []
            for char in row:
                color = gj2ascii.ANSI_COLORMAP[colormap[char]]
                o_row.append(color + char + ' ' + gj2ascii.core._ANSI_RESET)
            expected.append(''.join(o_row))
        expected = os.linesep.join(expected)
        self.assertEqual(
            expected, gj2ascii.style(gj2ascii.array2ascii(array), stylemap=colormap))
예제 #4
0
#!/usr/bin/env python


"""
Render data with colors
"""


import fiona as fio
import gj2ascii


print("Render a single layer with colors")
with fio.open('sample-data/polygons.geojson') as src:
    rendered = gj2ascii.render(src, 40, char='+')
    print(gj2ascii.style(rendered, stylemap={'+': 'red'}))

print("")

print("Render multiple overlapping layers , apply colors, and zoom in on a bbox")
with fio.open('sample-data/polygons.geojson') as poly, \
        fio.open('sample-data/lines.geojson') as lines, \
        fio.open('sample-data/small-aoi-polygon-line.geojson') as bbox:
    layermap = [
        (poly, 'red'),
        (lines, 'blue')
    ]
    print(gj2ascii.style_multiple(layermap, 40, fill='yellow', bbox=bbox.bounds))
예제 #5
0
def main(infile, outfile, width, iterate, fill_map, char_map, all_touched, crs_def, no_prompt,
         properties, bbox, no_style):

    """
    Render spatial vector data as ASCII with colors and emoji.

    \b
    Multiple layers from multiple files can be rendered as characters, colors,
    or emoji.  When working with multiple layers they are rendered in order
    according to the painters algorithm.

    \b
    When working with multiple layers the layer specific arguments
    can be specified once to apply to all or multiple to apply specific conditions
    to specific layers.  In the latter case the arguments are applied in order so
    the second instance of an argument corresponds to the second layer.

    \b
    Examples:
    \b
        Render the entire layer across 40 text columms:
    \b
            $ gj2ascii ${INFILE} --width 40
    \b
        Read from stdin and fill all pixels that intersect a geometry:
    \b
            $ cat ${INFILE} | gj2ascii - \\
                --width 30 \\
                --all-touched
    \b
        Render individual features across 20 columns and display the attributes
        for two fields:
    \b
            $ gj2ascii ${INFILE} \\
                --properties ${PROP1},${PROP2}  \\
                --width 20 \\
                --iterate
    \b
        Render multiple layers.  The first layer is read from stdin and will be
        rendered as a single character, the second will be the character `*' but
        masked by the color blue, the third will be a randomly assigned character
        but masked by the color black, and the fourth will be the :thumbsup: emoji:
    \b
            $ cat ${SINGLE_LAYER_INFILE} | gj2ascii \\
                - \\
                ${INFILE_2},${LAYER_NAME_1},${LAYER_NAME_2} \\
                ${SINGLE_LAYER_INFILE_1} \\
                --char + \\
                --char \\*=blue \\
                --char black \\
                --char :thumbsup:
    """

    fill_char = [c[0] for c in fill_map][-1]
    num_layers = sum([len(layers) for ds, layers in infile])

    # ==== Render individual features ==== #
    if iterate:

        if num_layers > 1:
            raise click.ClickException(
                "Can only iterate over a single layer.  Specify only one infile for "
                "single-layer datasources and `INFILE,LAYERNAME` for multi-layer datasources.")

        if len(infile) > 1 or num_layers > 1 or len(crs_def) > 1 \
                or len(char_map) > 1 or len(all_touched) > 1:
            raise click.ClickException(
                "Can only iterate over 1 layer - all layer-specific arguments can only be "
                "specified once each.")

        # User is writing to an output file.  Don't prompt for next feature every time
        # TODO: Don't just compare to '<stdout>' but sys.stdout.name throws an exception
        if not no_prompt and hasattr(outfile, 'name') and outfile.name != '<stdout>':
            no_prompt = True

        if not char_map:
            char_map = [(gj2ascii.DEFAULT_CHAR, None)]

        # The odd list slicing is due to infile looking something like this:
        # [
        #     ('sample-data/polygons.geojson', ['polygons']),
        #     ('sample-data/multilayer-polygon-line', ['lines', 'polygons'])
        # ]
        if num_layers > 0:
            layer = infile[-1][1][-1]
        else:
            layer = None
        in_ds = infile[-1][0]
        if in_ds == '-' and not no_prompt:

            # This exception blocks a feature - see the content for more info.
            raise click.ClickException(
                "Unfortunately features cannot yet be directly iterated over when reading "
                "from stdin.  The simplest workaround is to use:" + os.linesep * 2 +
                "    $ cat data.geojson | gj2ascii - --iterate --no-prompt | more" + 
                os.linesep * 2 +
                "This issue has been logged: https://github.com/geowurster/gj2ascii/issues/25"
            )
        with fio.open(infile[-1][0], layer=layer, crs=crs_def[-1]) as src:

            if properties == '%all':
                properties = src.schema['properties'].keys()

            # Get the last specified parameter when possible in case there's a bug in the
            # validation above.
            kwargs = {
                'width': width,
                'char': [_c[0] for _c in char_map][-1],
                'fill': fill_char,
                'properties': properties,
                'all_touched': all_touched[-1],
                'bbox': bbox,
                'colormap': _build_colormap(char_map, fill_map)
            }
            if no_style:
                kwargs['colormap'] = None

            for feature in gj2ascii.paginate(src.filter(bbox=bbox), **kwargs):
                click.echo(feature, file=outfile)
                if not no_prompt and click.prompt(
                        "Press enter for next feature or 'q + enter' to exit",
                        default='', show_default=False, err=True) \
                        not in ('', os.linesep):  # pragma no cover
                    raise click.Abort()

    # ==== Render all input layers ==== #
    else:

        # if len(crs_def) not in (1, num_layers) or len(char_map) not in (0, 1, num_layers) \
        #     or len(all_touched) not in (1, num_layers)

        # User didn't specify any characters/colors but the number of input layers exceeds
        # the number of available colors.
        if not char_map:
            if num_layers > len(gj2ascii.ANSI_COLORMAP):
                raise click.ClickException(
                    "can't auto-generate color ramp - number of input layers exceeds number "
                    "of colors.  Specify one `--char` per layer.")
            elif num_layers is 1:
                char_map = [(gj2ascii.DEFAULT_CHAR, None)]
            else:
                char_map = [(str(_i), gj2ascii.DEFAULT_CHAR_COLOR[str(_i)])
                            for _i in range(num_layers)]
        elif len(char_map) is not num_layers:
            raise click.ClickException(
                "Number of `--char` arguments must equal the number of layers being "
                "processed.  Found %s characters and %s layers.  Characters and colors will "
                "be generated if none are supplied." % (len(char_map), num_layers))
        if not char_map:
            char_map = {gj2ascii.DEFAULT_CHAR: None}

        # User didn't specify a bounding box.  Compute the minimum bbox for all layers.
        if not bbox:
            coords = []
            for ds, layer_names in infile:
                for layer, crs in zip_longest(layer_names, crs_def):
                    with fio.open(ds, layer=layer, crs=crs) as src:
                        coords += list(src.bounds)
            bbox = (min(coords[0::4]), min(coords[1::4]), max(coords[2::4]), max(coords[3::4]))

        # Render everything
        rendered_layers = []
        overall_lyr_idx = 0
        for ds, layer_names in infile:
            for layer, crs, at in zip_longest(layer_names, crs_def, all_touched):
                char = [_c[0] for _c in char_map][overall_lyr_idx]
                overall_lyr_idx += 1
                with fio.open(ds, layer=layer, crs=crs) as src:
                    rendered_layers.append(
                        # Layers will be stacked, which requires fill to be set to a space
                        gj2ascii.render(
                            src, width=width, fill=' ', char=char, all_touched=at, bbox=bbox))

        stacked = gj2ascii.stack(rendered_layers, fill=fill_char)
        if no_style:
            styled = stacked
        else:
            styled = gj2ascii.style(stacked, stylemap=_build_colormap(char_map, fill_map))
        click.echo(styled, file=outfile)