[GRASS-user] Creating a png file with multiple vector maps

Chris Bartolomei surfcjb at yahoo.com
Thu Feb 11 09:54:31 PST 2021


 Good morning Anna,It took quite a while of trial and error but I worked out a method that kindof works:First off - unless someone says otherwise, you can't use the PNG driver (d.mon) method to overlay more than one polygon vector. Sorry - it just doesn't work. You CAN use the ps.map method - that works really well generating the image however it by default assumes you are printing on an A4 piece of paper so there's all sorts of white space.  The image is centered at the top of this fictional piece of paper. In your postscript rules file you can use the "maploc" command to position the image elsewhere on the page. This is necessary because the next trick changes the paper dimensions but it assumes the origin is the lower left corner and therefore clips anything that is above the new dimensions. Back to postscript commands in the rules file first though ... the ps.map maploc command uses inches (why?? it should be points) so an A4 page is 8.27" x 11.69"  points are 1/72 of an inch thus 595p x 842p - it also has a default 36p margin (0.5 inch). You'll need those numbers later. maploc also lets you set the size of your image box:  maploc {x offset from left edge} {y offset from top} {width of box} {height of box} Note: this is all done via a BASH script with GRASS 7.4 on Linux (RHEL 7), not python. This is my postscript rules file:
maploc 0.1 6.815 6.5 4.875 #468p x 351p map box moved down towards the bottom of the page# note that if you push it too far down to where the box would run off the bottom, the image is# resized to fit on the page so do some testing to come up with the correct values# also I found the computational region controls the aspect ratio so although I say 
# 6.5 x 4.875 with the above maploc command, I got a 6.5 x 3.8 inch box.border y # add a border to the map frame (box)  color 81:81:81 # shade of gray  end # end the border controlsvareas admin_area # top vector layer to display  layer 1 # attribute table to use  rgbcolumn area_color # name of column holding R:G:B values to fill the polygons  color 153:153:153 #boundary color  end # end the admin_area controlsvareas Country # this is the bottom vectors to display  color 210:210:210 #boundary color  fcolor 153:153:153 #fill color for all polygons  end # end the Country controls
Here's the command to run to generate the postscript file:
ps.map input=$HOME/ps_rules.txt out=$HOME/color_admin.ps --overwrite
To convert the postscript to PNG I had to use ghostscript - there are other tools you can use though.
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dDEVICEWIDTHPOINTS=473 -dDEVICEHEIGHTPOINTS=276 -dFIXEDMEDIA -dPSFitPage -sOutputFile=$HOME/color_admin.png -c "<</PageOffset [-34 78]>> setpagedevice" -f $HOME/color_admin.ps
So the above line needs some explaining (http://www.ghostscript.com/doc/9.27/Use.htm) but in a nutshell, the parameters to play with are first the Pageoffset [x y] values. They are in points not inches ... 1/72 inch = 1 point ... remember the 1/2" margins? the -34 gives me 2 points of white space to the left edge of the map frame, the 78 I had to play with to push the map frame down to the right spot.Next is the DEVICEWIDTHPOINTS and DEVICEHEIGHTPOINTS ... again in points ... this "trims" the paper to height and width ... set something then run it and view the results. Adjust and run again until you get it correct.
It's a royal pain but it seems to work this way. It would sure be nice to create a GRASS workspace file and just say "convert this workspace to an image" with everything all laid out nicely - like Arc does exporting their mxd map files...
I hope this helps someone !:)
Chris

    On Wednesday, February 10, 2021, 11:08:00 PM EST, Anna Petrášová <kratochanna at gmail.com> wrote:  
 
 

On Tue, Feb 9, 2021 at 4:41 PM Chris Bartolomei <surfcjb at yahoo.com> wrote:

 Hi Anna - thank you for the suggestion - I tried it but alas, still it only outputs a single vector map (layer). I can get either the Country vector or the admin_areas vector, but not both overlaid.:(Chris


I realized you are using both environmental variables and d.mon, that might cause some issues, you use one or the other. So try to remove the lines starting with d.mon.
Hope that helps,Anna 

    On Tuesday, February 9, 2021, 1:20:52 PM EST, Anna Petrášová <kratochanna at gmail.com> wrote:  
 
 Hi,
On Tue, Feb 9, 2021 at 10:25 AM Chris Bartolomei via grass-user <grass-user at lists.osgeo.org> wrote:

Good morning :)I'm using GRASS 7.4.1 on a Linux cluster so I only have command-line capability. I have two vector layers (a country boundary polygon and part of an administrative area map - also polygons). I am trying to automate creating a PNG file of the admin areas overlaying the country boundary therefore all work has to be command-line (in a bash script). I've tried this two ways - using the d.mon start=png method and also the ps.map method as described below. The d.mon method appears to generate the image with only one vector map (not both) and only colors the borders - it won't use the fill_color setting. The ps.map method seems to work but assumes the image is on a sheet of paper so there's a ton of extra white-space. I'd like to use d.mon but I can use ps.map if someone could please let me know how to export only the computational region without all the extra 'paper' in the image. Here's my code:
g.region vector='Country'export GRASS_RENDER_IMMEDIATE=pngexport GRASS_RENDER_WIDTH=640export GRASS_RENDER_HEIGHT=480export GRASS_RENDER_TRANSPARENT=trueexport GRASS_RENDER_TRUECOLOR=trueexport GRASS_RENDER_FILE=$HOME/country_admin.pngexport GRASS_RENDER_FILE_COMPRESSION=0export GRASS_MESSAGE_FORMAT=plaind.mon start=pngd.vect map=Country color=210:210:210 fill_color=153:153:153 display=shape type=aread.vect map=admin_area color=153:153:153 rgb_column=area_color display=shape type=aread.mon stop=png
This only produces a png with the last vector listed and only the borders are colored with the rgb_column values.

I think you are missing  GRASS_RENDER_FILE_READ=TRUE:https://grass.osgeo.org/grass78/manuals/pngdriver.html

Regarding rgb_column, I am not sure, didn't have time to test.
Anna

If I do this without the d.mon start/stop lines ... i.e. relying on the GRASS_RENDER_IMMEDIATE=png only, then only one vector map is converted to png however it DOES do the color fill properly. With either above method the png is the correct size.
Now using ps.map (same env variable set as above):
g.region vector='Country'
ps.map input=$HOME/ps_rules.txt out=$HOME/country_admin.ps --overwrite 
  where ps_rules.txt is:border y  color 81:81:81  endvareas admin_area  layer 1  rgbcolumn area_color  color 153:153:153  endvareas Country  color 210:210:210  fcolor 153:153:153  end
We don't have pstopng but we do have ghostscript:
gs-dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -r300 -sOutputFile=$HOME/country_admin.png $HOME/country_admin.ps
This creates the correct image (color fills, etc) but has white margins and a lot of white space below the image like it is printed at the top of a piece of paper.
does anyone have any idea how to create a png with multiple vector maps overlaying each other (and not have the extra whitespace too)?
v/rChris
_______________________________________________
grass-user mailing list
grass-user at lists.osgeo.org
https://lists.osgeo.org/mailman/listinfo/grass-user

  
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/grass-user/attachments/20210211/116e868f/attachment.html>


More information about the grass-user mailing list