[GRASS-SVN] r69378 - grass-addons/grass7/misc/m.printws

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 5 09:16:36 PDT 2016


Author: kuszinger
Date: 2016-09-05 09:16:36 -0700 (Mon, 05 Sep 2016)
New Revision: 69378

Modified:
   grass-addons/grass7/misc/m.printws/m.printws.html
   grass-addons/grass7/misc/m.printws/m.printws.py
Log:
Flexi option, find_program for imagemagick, official temp region


Modified: grass-addons/grass7/misc/m.printws/m.printws.html
===================================================================
--- grass-addons/grass7/misc/m.printws/m.printws.html	2016-09-05 13:24:33 UTC (rev 69377)
+++ grass-addons/grass7/misc/m.printws/m.printws.html	2016-09-05 16:16:36 UTC (rev 69378)
@@ -186,6 +186,30 @@
 again. After this alignment with the <i>Pan</i> tool the workspace must be saved again
 so m.printws could print up to the newly set extents.
 
+<h3>Illustration with the <i>Flexi</i> page option</h3>
+When creating figures for scientific articles or book illustration purposes maps usually
+don't have elements like titles and others outside the map frame.
+In research papers map content is described in subtitles as <i>Figure nn.:</i>
+and so on.
+In this case page options like A4, or A4 landscape would always have white stripes on
+the top/bottom or left/right even when margin is set to be zero.
+This is because the ratio of the
+edges of the map never exactly follows the ratio of the A4 or other page
+edges. Supporting the creation of publication ready images (especially when
+the target format is tif, png or jpg) the <b>Flexi option is introduced</b>
+as a virtual page size. Using the Flexi page:
+<ul>
+  <li>Titles and other external elements won't be generated even if they would be defined.</li>
+  <li>Page content will be reduced to the map area only.</li>
+  <li>There will be no margin independently of the ratio of the map edges.</li>
+</ul>
+<small><i><b>Background:</b> The Flexi mechanism actually sets up the same layout
+as it is specified with margins and titles on a 300x300mm page and then
+cuts off everything outside the map frame together with cancelling the
+rendering of title elements.</i></small><br><br>
+Using the dpi option of m.printws an ideal resolution could easily be set.
+
+
 <h2>EXAMPLE</h2>
 
 

Modified: grass-addons/grass7/misc/m.printws/m.printws.py
===================================================================
--- grass-addons/grass7/misc/m.printws/m.printws.py	2016-09-05 13:24:33 UTC (rev 69377)
+++ grass-addons/grass7/misc/m.printws/m.printws.py	2016-09-05 16:16:36 UTC (rev 69378)
@@ -48,7 +48,7 @@
 #%option
 #% key: page
 #% type: string
-#% options: A4landscape,A4portrait,LETTERlandscape,LETTERportrait,A3landscape,A3portrait
+#% options: A4landscape,A4portrait,LETTERlandscape,LETTERportrait,A3landscape,A3portrait,Flexi
 #% answer: A4landscape
 #% description: Output map page size
 #% guisection: Output
@@ -56,7 +56,7 @@
 #%option
 #% key: format
 #% type: string
-#% options: jpg,png,bmp,pdf,ppm
+#% options: pdf,png,tiff,bmp,ppm,jpg
 #% answer: pdf
 #% description: Output file format
 #% guisection: Output
@@ -194,9 +194,10 @@
 # initialize global vars
 TMPFORMAT = 'BMP'
 TMPLOC = None
+LAYERCOUNT = 10
+# Following declarations MAY will used in future for sure.
 SRCGISRC = None
 GISDBASE = None
-LAYERCOUNT = 10
 # temp dir
 REMOVE_TMPDIR = True
 PROXIES = {}
@@ -234,7 +235,9 @@
 PAGEDIC['A3landscape'] = (420.0, 297.0, '', 'A3')
 PAGEDIC['LETTERportrait'] = (215.9, 297.4, '', 'Letter')
 PAGEDIC['LETTERlandscape'] = (297.4, 215.9, '', 'Letter')
+PAGEDIC['Flexi'] = (300, 300, '', 'Flexi')
 
+
 # HTML DECODE
 HTMLDIC = {}
 HTMLDIC['>'] = '>'
@@ -565,7 +568,21 @@
 
 def main():
 
+    # Following declarations MAY will used in future for sure.
     global GISDBASE, LAYERCOUNT, LASTFILE
+    
+    # Check if ImageMagick is available since it is essential
+    if os.name == 'nt':
+        if grass.find_program('magick', '-version'):
+            grass.verbose(_('printws: ImageMagick is available: OK!'))
+        else:
+            grass.fatal('ImageMagick is not accessible. See documentation of m.printws module for details.')
+    else:
+        if grass.find_program('convert', '-version'):
+            grass.verbose(_('printws: ImageMagick is available: OK!'))
+        else:
+            grass.fatal('ImageMagick is not accessible. See documentation of m.printws module for details.')
+    
     textmacros = {}
     # %nam% macros are kept for backward compatibility
     textmacros['%TIME24%'] = time.strftime("%H:%M:%S")
@@ -582,13 +599,12 @@
     textmacros['\$DATEMDY'] = textmacros['%DATEMDY%']
     textmacros['\$USERNAME'] = textmacros['%USERNAME%']
 
-    textmacros['\$SPC'] = u'\u00A0' #?? d.text won't display at string end hmmm
+    textmacros['\$SPC'] = u'\u00A0' #?? d.text won't display this at string end hmmm
 
 
     # saves region for restoring at end
-    savedregionname = "tmp.%s.%d" % (
-        os.path.basename(sys.argv[0]), os.getpid())
-    grass.run_command("g.region", save=savedregionname, overwrite=True)
+    # doing with official method:
+    grass.use_temp_region()
 
     # getting/setting screen/print dpi ratio
 
@@ -659,8 +675,8 @@
             pageoption = options['page']
         else:
             pageoption = 'A4landscape'
+        
         # parsing titles, etc.
-
         if len(options['titlefont']) > 0:
             isAsterisk = options['titlefont'].find('*')
             if isAsterisk > 0:
@@ -811,6 +827,17 @@
             pagemarginsindotstitles, pagesizesindots)
 
         mpfd = getmapframeindots(mapulindots, mapsizesindots, mxfdtitles)
+        if pageoption == 'Flexi':
+            # For 'Flexi' page we modify the setup to create
+            # a page containing only the map without margins
+            grass.verbose(_("printws: pre Flexi mapframe: " + str(mpfd)))
+            mpfd['b'] = mpfd['b'] - mpfd['t']
+            mpfd['t'] = 0
+            mpfd['r'] = mpfd['r'] - mpfd['l']
+            mpfd['l'] = 0
+            os.environ['GRASS_RENDER_WIDTH'] = str(mpfd['r'])
+            os.environ['GRASS_RENDER_HEIGHT'] = str(mpfd['b'])
+            grass.verbose(_("printws: post Flexi mapframe: " + str(mpfd)))
         mapframe = str(mpfd['t']) + ',' + str(mpfd['b']) + \
             ',' + str(mpfd['l']) + ',' + str(mpfd['r'])
 
@@ -831,7 +858,9 @@
 
         # ------------------- INMAP -------------------
 
-        imcommand = 'convert  -limit memory 720000000 -limit map 720000000 -units PixelsPerInch -density ' + \
+        # Do not limit -map. It was: -limit map 720000000 before...
+        # So we can grow on disk as long as it lasts
+        imcommand = 'convert  -limit memory 720000000 -units PixelsPerInch -density ' + \
             str(int(dpioption)) + ' '
 
         if os.name == 'nt':
@@ -871,47 +900,50 @@
         grass.run_command("g.region", ewres=str(newewres), nsres=str(newnsres))
 
         # ------------------- OUTSIDE MAP texts, etc -------------------
-        os.environ['GRASS_RENDER_FRAME'] = maxframe
+        if pageoption =='Flexi':
+            grass.verbose(_('m.printws: WARNING! Felxi mode, will not create titles, etc...'))
+        else:
+            os.environ['GRASS_RENDER_FRAME'] = maxframe
 
-        dict = {}
-        dict['task'] = "d.text"
-        dict['color'] = titlecolor
-        dict['font'] = titlefont
-        dict['charset'] = "UTF-8"
+            dict = {}
+            dict['task'] = "d.text"
+            dict['color'] = titlecolor
+            dict['font'] = titlefont
+            dict['charset'] = "UTF-8"
 
-        if len(options['maintitle']) > 1:
-            dict['text'] = decodetextmacros(options['maintitle'], textmacros)
-            dict['at'] = "50," + str(titletoppercent)
-            dict['align'] = "uc"
-            dict['size'] = str(maintitlesize)
-            render(str(dict), dict, {})
+            if len(options['maintitle']) > 1:
+                dict['text'] = decodetextmacros(options['maintitle'], textmacros)
+                dict['at'] = "50," + str(titletoppercent)
+                dict['align'] = "uc"
+                dict['size'] = str(maintitlesize)
+                render(str(dict), dict, {})
 
-        if len(options['subtitle']) > 1:
-            dict['text'] = decodetextmacros(options['subtitle'], textmacros)
-            dict['at'] = "50," + str(subtitletoppercent)
-            dict['align'] = "uc"
-            dict['size'] = str(subtitlesize)
-            render(str(dict), dict, {})
+            if len(options['subtitle']) > 1:
+                dict['text'] = decodetextmacros(options['subtitle'], textmacros)
+                dict['at'] = "50," + str(subtitletoppercent)
+                dict['align'] = "uc"
+                dict['size'] = str(subtitlesize)
+                render(str(dict), dict, {})
 
-        dict['size'] = str(pssize)
+            dict['size'] = str(pssize)
 
-        if len(options['psundercentral']) > 1:
-            dict['text'] = decodetextmacros(
-                options['psundercentral'], textmacros)
-            dict['at'] = "50,1"
-            dict['align'] = "lc"
-            render(str(dict), dict, {})
-        if len(options['psunderleft']) > 1:
-            dict['text'] = decodetextmacros(options['psunderleft'], textmacros)
-            dict['at'] = "0,1"
-            dict['align'] = "ll"
-            render(str(dict), dict, {})
-        if len(options['psunderright']) > 1:
-            dict['text'] = decodetextmacros(
-                options['psunderright'], textmacros)
-            dict['at'] = "100,1"
-            dict['align'] = "lr"
-            render(str(dict), dict, {})
+            if len(options['psundercentral']) > 1:
+                dict['text'] = decodetextmacros(
+                    options['psundercentral'], textmacros)
+                dict['at'] = "50,1"
+                dict['align'] = "lc"
+                render(str(dict), dict, {})
+            if len(options['psunderleft']) > 1:
+                dict['text'] = decodetextmacros(options['psunderleft'], textmacros)
+                dict['at'] = "0,1"
+                dict['align'] = "ll"
+                render(str(dict), dict, {})
+            if len(options['psunderright']) > 1:
+                dict['text'] = decodetextmacros(
+                    options['psunderright'], textmacros)
+                dict['at'] = "100,1"
+                dict['align'] = "lr"
+                render(str(dict), dict, {})
 
         # ------------------- GENERATING OUTPUT FILE -------------------
 
@@ -957,9 +989,7 @@
             sys.stderr.write('%s\n' % TMPDIR % ' <---- this')
 
     # restoring pre-script region
-    grass.run_command("g.region", region=savedregionname, overwrite=True)
-    grass.run_command('g.remove', flags="f", type="region",
-                      pattern=savedregionname)
+    # - not necessary as we are using grass.use_temp_region() in the future
 
     return 0
 



More information about the grass-commit mailing list