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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Sep 10 09:31:20 PDT 2016


Author: kuszinger
Date: 2016-09-10 09:31:19 -0700 (Sat, 10 Sep 2016)
New Revision: 69427

Modified:
   grass-addons/grass7/misc/m.printws/m.printws.html
   grass-addons/grass7/misc/m.printws/m.printws.py
Log:
xml "&" workaround, GRASS_REGION provision, documentation enhancements



Modified: grass-addons/grass7/misc/m.printws/m.printws.html
===================================================================
--- grass-addons/grass7/misc/m.printws/m.printws.html	2016-09-10 09:27:26 UTC (rev 69426)
+++ grass-addons/grass7/misc/m.printws/m.printws.html	2016-09-10 16:31:19 UTC (rev 69427)
@@ -18,6 +18,8 @@
 could be saved as normal workspaces and printed in one run. Being a regular GRASS script it could be called from other
 scripts and command line as well. No GUI is necessary to be started since m.printws processes saved workspace files.
 </li>
+<li>Automatic redraw and regeneration of illustrations attached to a publication (See Flexi option).
+</li>
 </ul>
 
 <h2>NOTES</h2>
@@ -62,14 +64,25 @@
 Please consider that modern screens are around 100dpi or higher. If you render
 300 dpi your labels should be drawn using 3 times bigger font size measured in dots.
 </li>
+<li>Some modules like d.wms (which can be added to displayed layers list
+in GUI to download, transform and display external WMS data source) download
+data again for each run. This is normal, however slower than rendering to a map
+display window. <i>The reason is that map display windows won't download again the
+data if you simply redraw the screen meanwhile m.printws cleans up after
+each run so there is no chance to create a cache mehcanism in the background.</i>
+</li>
 </ul>
-The module saves current region and sets it back when finished.
+The module changes region meanwhile it runs but restores the original when
+finished.
 
 <h3>Vector legend (d.legend.vect) information</h3>
 
-The d.legend.vect module integrates well with the GRASS GIS Layer Manager. However, when the user exits the gui, its temporal settings
-and status are not valid anymore. It means that d.legend.vect may face problems when restarting GRASS GIS and trying to use the
-saved workspace file with m.printws. To prevent such issues do the following steps:
+The d.legend.vect module integrates well with the GRASS GIS Layer Manager.
+When the user exits the GUI, its settings
+and status became invalid. It means that d.legend.vect may face problems
+when restarting GRASS GIS and trying to use the
+saved workspace file with m.printws with its old settings.
+To prevent such issues do the following steps:
 
 <ol>
 <li>Compose your vector legend using the GUI as it is provided by d.legend.vect.
@@ -90,7 +103,7 @@
 </li>
 </ol>
 
-Such a prepared vector legend will be printed correctly by m.printws.
+Thusly prepared vector legends will be printed correctly by m.printws.
 
 If you would like to modify your legend, delete the "Input legend file:" field on the In/Out tab and also repeat the process above
 before printing.
@@ -186,7 +199,7 @@
 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>
+<h3>Making illustrations 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>
@@ -202,12 +215,15 @@
   <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>
+  <li>No postprocess is necessary (for example: cutting to size in photoshop ).
+  Illustration redraw can be fully automatic in case of content change
+  by repeating the m.printws run</li>
+</ul><br><br>
 <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.
+An ideal resolution could easily be set using the dpi option of m.printws.
 
 
 <h2>EXAMPLE</h2>

Modified: grass-addons/grass7/misc/m.printws/m.printws.py
===================================================================
--- grass-addons/grass7/misc/m.printws/m.printws.py	2016-09-10 09:27:26 UTC (rev 69426)
+++ grass-addons/grass7/misc/m.printws/m.printws.py	2016-09-10 16:31:19 UTC (rev 69427)
@@ -68,9 +68,9 @@
 #% guisection: Titles
 #%end
 #%option
-#% key: titlefont
+#% key: font
 #% type: string
-#% description: Font for title and postscript under the map
+#% description: Font for title above and postscript under the map
 #% guisection: Titles
 #%end
 #%option G_OPT_C
@@ -94,7 +94,7 @@
 #%option
 #% key: subtitlesize
 #% type: integer
-#% description: Main title font size in layout units
+#% description: Subtitle font size in layout units
 #% guisection: Titles
 #%end
 #%option
@@ -174,8 +174,12 @@
 import sys
 import os
 
-if os.name <> 'nt':
+# Windows pwd module workaround
+hasPwd = True
+try:
     import pwd
+except ImportError:
+    hasPwd = False
 
 
 import atexit
@@ -246,6 +250,27 @@
 HTMLDIC['"'] = '"'
 
 
+
+def cleanthisandthat(intext):
+    # As of 10. September 2016 some modules (d.wms) creates
+    # lines in XML workspace files which are not well-formed
+    # before parsing them, we need to correct it.
+    # Handled errors:
+    #    single & which is not & in urls
+    # Once workspace files are always good this function could be NOOP
+    outtext = ''
+    for line in intext.splitlines():
+        m = re.search('http\://',line)
+        if m:
+            line2 = re.sub('\&amp\;','SAVED___amp\;',line)
+            line3 = re.sub('\&','&',line2)
+            line4 = re.sub('SAVED___amp\;','&',line3)
+            outtext = outtext + line4 + "\n"
+        else:
+            outtext = outtext + line + "\n"
+    return outtext
+
+
 def cleanup():
 
     # No cleanup is done here
@@ -253,9 +278,6 @@
     # kept for later
     grass.verbose(_("Module cleanup"))
 
-
-
-
 # test
 # m.printws.py --overwrite input=/home/kuszi/grassdata/workspaces_7/EURASEAA.gxw dpi=100 output=/home/kuszi/grassdata/mapdefs/euraseeaa.bmp page=A4portrait maintitle=$DISPLAY pagemargin=0
 
@@ -287,7 +309,11 @@
     params = task.getElementsByTagName("parameter")
     paramdic['task'] = command
     for p in params:
-        paramdic[p.getAttribute('name')] = upsizeifnecessary(paramdic['task'],p.getAttribute('name'),p.getElementsByTagName("value")[0].childNodes[0].data,UPSIZE)
+        elements = p.getElementsByTagName("value") #sometimes there are empty <value> tags in workspace files
+        if len(elements) > 0:
+            nodes = elements[0].childNodes
+            if len(nodes) > 0:
+                paramdic[p.getAttribute('name')] = upsizeifnecessary(paramdic['task'],p.getAttribute('name'),nodes[0].data,UPSIZE)
     
     flags = task.getElementsByTagName("flag")
     for f in flags:
@@ -298,7 +324,9 @@
 def processoverlay(dom,flagdic,paramdic):
     params = dom.getElementsByTagName("parameter")
     for p in params:
-        paramdic[p.getAttribute('name')] = upsizeifnecessary(paramdic['task'],p.getAttribute('name'),p.getElementsByTagName("value")[0].childNodes[0].data,UPSIZE)
+        elements=p.getElementsByTagName("value") #sometimes there are empty <value> tags in workspace files
+        if len(elements) > 0:
+            paramdic[p.getAttribute('name')] = upsizeifnecessary(paramdic['task'],p.getAttribute('name'),elements[0].childNodes[0].data,UPSIZE)
     
     flags = dom.getElementsByTagName("flag")
     for f in flags:
@@ -336,8 +364,9 @@
     displaydic = {}    # adding support for more displays
     grass.verbose(_("Layers: "))
     f = open(wspname, 'r')
-    text = f.read()
+    textraw = f.read()
     f.close()
+    text = cleanthisandthat(textraw)
     model = xml.dom.minidom.parseString(text)
     displays = model.getElementsByTagName("display")
     for display in displays:
@@ -588,8 +617,8 @@
     textmacros['%TIME24%'] = time.strftime("%H:%M:%S")
     textmacros['%DATEYMD%'] = time.strftime("%Y.%m.%d")
     textmacros['%DATEMDY%'] = time.strftime("%m/%d/%Y")
-    if os.name == 'nt':
-        textmacros['%USERNAME%'] = '(windows user)'
+    if not hasPwd:
+        textmacros['%USERNAME%'] = '(user unknown)'
     else:
         textmacros['%USERNAME%'] = pwd.getpwuid(os.getuid())[0]
     # using $ for macros in the future. New items should be created
@@ -677,13 +706,13 @@
             pageoption = 'A4landscape'
         
         # parsing titles, etc.
-        if len(options['titlefont']) > 0:
-            isAsterisk = options['titlefont'].find('*')
+        if len(options['font']) > 0:
+            isAsterisk = options['font'].find('*')
             if isAsterisk > 0:
                 titlefont = getfontbypattern(
-                    options['titlefont'].replace('*', ''))
+                    options['font'].replace('*', ''))
             else:
-                titlefont = options['titlefont']
+                titlefont = options['font']
         else:
             titlefont = getfontbypattern('Open')  # try to find something UTF-8
         grass.verbose(_("printws: titlefont: " + titlefont))
@@ -817,6 +846,25 @@
 
         grass.run_command("g.region", ewres=str(newewres), nsres=str(newnsres))
 
+        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+        # it seems that d.wms uses the GRASS_REGION from region info
+        # others may also do so we set it
+        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+        kv2 = {}
+        kv2['e'] = kv['east']
+        kv2['n'] = kv['north']
+        kv2['s'] = kv['south']
+        kv2['w'] = kv['west']
+        kv2['ewres'] = str(newewres)
+        kv2['nsres'] = str(newnsres)
+        #kv2['rows']    #- autocalculated to resolution - no need to set explicitly
+        #kv2['cols']    #- autocalculated to resolution - no need to set explicitly
+        #grass.message(str(kv2))
+        #grass.message(grass.region_env(**kv2))
+        #grass.message(s)
+        os.environ['GRASS_REGION'] = grass.region_env(**kv2)
+
+
         # Getting mapping area in dots
         # Correcting mxfd to leave space for title and subscript
         pagemarginstitles = copy.deepcopy(pagemargins)
@@ -849,8 +897,7 @@
         grass.verbose(_("printws: page: " + str(pagesizesindots)))
         grass.verbose(_("printws: margins: " + str(pagemarginsindots)))
         grass.verbose(_("printws: mapUL: " + str(mapulindots)))
-        grass.verbose(
-            _("printws: mapsizes (corrected): " + str(mapsizesindots)))
+        grass.verbose(_("printws: mapsizes (corrected): " + str(mapsizesindots)))
         grass.verbose(_("printws: ewres (corrected): " + str(newewres)))
         grass.verbose(_("printws: nsres (corrected): " + str(newnsres)))
 
@@ -858,6 +905,8 @@
 
         # ------------------- INMAP -------------------
 
+
+
         # 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 ' + \
@@ -875,8 +924,9 @@
             grass.verbose(_(lay[1] + ' at: ' + lay[0] + ' opacity'))
             if lay[0] == '1':
                 if lastopacity <> '1':
-                    LASTFILE = os.path.join(TMPDIR, str(os.getpid(
-                    )) + '_DIS_' + str(displaycounter) + '_GEN_' + str(LAYERCOUNT) + '.' + TMPFORMAT)
+                    LASTFILE = os.path.join(TMPDIR, str(os.getpid()) + \
+                        '_DIS_' + str(displaycounter) + '_GEN_' + \
+                        str(LAYERCOUNT) + '.' + TMPFORMAT)
                     os.environ['GRASS_RENDER_FILE'] = LASTFILE
                     LAYERCOUNT = LAYERCOUNT + 2
                     imcommand = imcommand + ' ' + LASTFILE
@@ -896,9 +946,12 @@
 
         # setting resolution back to pre-script state since map rendering is
         # finished
+        # CHANGE: not necessary anymore since we use temp_region now
+        # However, since we did set GRASS_REGION, let's redo it here
 
-        grass.run_command("g.region", ewres=str(newewres), nsres=str(newnsres))
+        os.environ.pop('GRASS_REGION')
 
+
         # ------------------- OUTSIDE MAP texts, etc -------------------
         if pageoption =='Flexi':
             grass.verbose(_('m.printws: WARNING! Felxi mode, will not create titles, etc...'))



More information about the grass-commit mailing list