Skip to content

Commit

Permalink
grass.jupyter: Add save PNG method to Map (#2371)
Browse files Browse the repository at this point in the history
Map from grass.jupyter can now save PNG images. This is easier than using the filename property and it is consistent with TimeSeriesMap. Includes test for file existence after saving.
  • Loading branch information
wenzeslaus committed May 17, 2022
1 parent d207d49 commit 62e17c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions python/grass/jupyter/map.py
Expand Up @@ -192,3 +192,7 @@ def show(self):
from IPython.display import Image # pylint: disable=import-outside-toplevel

return Image(self._filename)

def save(self, filename):
"""Saves a PNG image of map to the specified *filename*"""
shutil.copy(self._filename, filename)
14 changes: 13 additions & 1 deletion python/grass/jupyter/testsuite/map_test.py
Expand Up @@ -82,7 +82,7 @@ def test_defaults(self):
def test_filename(self):
"""Test that Map creates maps with unique filenames."""
# Create map with unique filename
custom_filename = "test_filename.png"
custom_filename = "test_filename_provided.png"
grass_renderer = gj.Map(filename=custom_filename)
# Add files to self for cleanup later
self.files.append(custom_filename)
Expand All @@ -101,6 +101,18 @@ def test_filename_property(self):
# Make sure image was created
self.assertFileExists(grass_renderer.filename)

def test_save_file(self):
"""Test saving of file"""
grass_renderer = gj.Map()
# Add a vector and a raster to the map
grass_renderer.run("d.rast", map="elevation")
custom_filename = "test_filename_save.png"
grass_renderer.save(custom_filename)
# Add files to self for cleanup later
self.files.append(custom_filename)
# Make sure image was created
self.assertFileExists(custom_filename)

def test_hw(self):
"""Test that Map creates maps with custom height and widths."""
# Create map with height and width parameters
Expand Down

0 comments on commit 62e17c3

Please sign in to comment.