def test_exceptions(): # Bad fill value with pytest.raises(ValueError): gj2ascii.stack([], fill='too-long') # Input layers have different dimensions with pytest.raises(ValueError): gj2ascii.stack(['1', '1234'])
def test_exceptions(self): # Bad fill value with self.assertRaises(ValueError): gj2ascii.stack([], fill='too-long') # Input layers have different dimensions with self.assertRaises(ValueError): gj2ascii.stack(['1', '1234'])
def test_standard(): l1 = gj2ascii.array2ascii([['*', '*', '*', '*', '*'], [' ', ' ', '*', ' ', ' '], ['*', '*', ' ', ' ', ' ']]) l2 = gj2ascii.array2ascii([[' ', ' ', ' ', '+', '+'], [' ', '+', ' ', ' ', ' '], [' ', ' ', '+', '+', '+']]) eo = gj2ascii.array2ascii([['*', '*', '*', '+', '+'], ['.', '+', '*', '.', '.'], ['*', '*', '+', '+', '+']]) assert gj2ascii.stack([l1, l2], fill='.').strip(os.linesep), eo.strip(os.linesep)
def test_standard(self): l1 = gj2ascii.array2ascii([['*', '*', '*', '*', '*'], [' ', ' ', '*', ' ', ' '], ['*', '*', ' ', ' ', ' ']]) l2 = gj2ascii.array2ascii([[' ', ' ', ' ', '+', '+'], [' ', '+', ' ', ' ', ' '], [' ', ' ', '+', '+', '+']]) eo = gj2ascii.array2ascii([['*', '*', '*', '+', '+'], ['.', '+', '*', '.', '.'], ['*', '*', '+', '+', '+']]) self.assertEqual(gj2ascii.stack( [l1, l2], fill='.').strip(os.linesep), eo.strip(os.linesep))
def test_render_multiple(): with fio.open(POLY_FILE) as poly, \ fio.open(LINE_FILE) as lines, \ fio.open(POINT_FILE) as points: coords = list(poly.bounds) + list(lines.bounds) + list(points.bounds) bbox = (min(coords[0::4]), min(coords[1::4]), max(coords[2::4]), max(coords[3::4])) width = 20 lyr_char_pairs = [(poly, '+'), (lines, '-'), (points, '*')] actual = gj2ascii.render_multiple(lyr_char_pairs, width, fill='#') rendered_layers = [] for l, char in lyr_char_pairs: rendered_layers.append(gj2ascii.render(l, width, fill=' ', bbox=bbox, char=char)) expected = gj2ascii.stack(rendered_layers, fill='#') assert compare_ascii(actual.strip(), expected.strip())
def test_multilayer_compute_colormap(runner, multilayer_file, compare_ascii): coords = [] for layer in ('polygons', 'lines'): with fio.open(multilayer_file, layer=layer) as src: coords += list(src.bounds) bbox = min(coords[0::4]), min(coords[1::4]), max(coords[2::4]), max( coords[3::4]) rendered_layers = [] for layer, char in zip(('polygons', 'lines'), ('0', '1')): with fio.open(multilayer_file, layer=layer) as src: rendered_layers.append( gj2ascii.render(src, width=20, fill=' ', char=char, bbox=bbox)) expected = gj2ascii.stack(rendered_layers) # Explicitly define since layers are not consistently listed in order result = runner.invoke( cli.main, [multilayer_file + ',polygons,lines', '--width', '20']) assert result.exit_code == 0 assert compare_ascii(expected.strip(), result.output.strip())
def test_multilayer_compute_colormap(self): coords = [] for layer in ('polygons', 'lines'): with fio.open(MULTILAYER_FILE, layer=layer) as src: coords += list(src.bounds) bbox = min(coords[0::4]), min(coords[1::4]), max(coords[2::4]), max(coords[3::4]) rendered_layers = [] for layer, char in zip(('polygons', 'lines'), ('0', '1')): with fio.open(MULTILAYER_FILE, layer=layer) as src: rendered_layers.append( gj2ascii.render(src, width=20, fill=' ', char=char, bbox=bbox)) expected = gj2ascii.stack(rendered_layers) # Explicitly define since layers are not consistently listed in order result = self.runner.invoke(cli.main, [ MULTILAYER_FILE + ',polygons,lines', '--width', '20' ]) self.assertEqual(result.exit_code, 0) self.assertTrue(compare_ascii(expected.strip(), result.output.strip()))
def test_single_layer(compare_ascii): l1 = gj2ascii.array2ascii([['*', '*', '*', '*', '*'], [' ', ' ', '*', ' ', ' '], ['*', '*', ' ', ' ', ' ']]) assert compare_ascii(l1, gj2ascii.stack([l1]))
def test_single_layer(self): l1 = gj2ascii.array2ascii([['*', '*', '*', '*', '*'], [' ', ' ', '*', ' ', ' '], ['*', '*', ' ', ' ', ' ']]) self.assertTrue(compare_ascii(l1, gj2ascii.stack([l1])))
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)