Speaking with a cartographer from National Geographic last night, my interest was again piqued (peaked?) in an open-source map publishing toolset built on PostGIS and Inkscape (as per his workflow and how others use Illustrator with Avenza Map Publisher using ArcGIS data)
With the PostGIS ST_AsSVG command, I'm not sure how to work with the output as an SVG file. I can see in other discussions that SVG's can be exported somehow, but I'm not sure how that works exactly.
With that said, assuming it's perhaps just a matter of orienting the command to export a static SVG file – would/could it even be possible for Inkscape to read an SVG directly out of PostGIS?
Best Answer
The Postgis function ST_AsSVG simply converts geometries into their SVG point or path equivalents, and does not allow you to add any attributes/styling directly. For example, a query with a Point, Linestring and Polygon:
returns:
which is a point, an unclosed linestring and a closed line, ie, a polygon. M means move to, L means line to and Z means close. There are other geometry types in SVG, but this is what ST_AsSVG returns (see docs for the Multi version of the same).
By itself, this isn't very helpful, as you are missing the
path tags
and any styling information, a full example of which would look something like:See w3schools SVG Tutorial for more information.
So, if you want to actually create valid SVG using a Postgis query, you will need to add path tags and color, line, fill, etc formatting. This can be accomplished by using the
concat operator
and acase statement
to set the formatting. For example,which returns:
Finally, if you wrap all of that in
array_to_string
andarray_agg
, and add the head and footer, ie, the<svg height="400" width="450">
and</svg>
, you will end up with one block of SVG representing all your geometries, eg,which now returns the single record:
TL;DR, other options
At which point you are probably thinking, TL;DR, and it is true this is a lot of work, and the case statements to make the stroke color, width, etc, could get very long and unwieldy.
There are other options. Geoserver (with a Postgis/vector data source) supports SVG as an output format and you can use SLDs or CartoCSS within Geoserver to style them up, then just request a WMS with SVG as output format and you are done. As @Scro also said, you can use tools like TileMill to do the styling for you (not tested). So, after a very long answer, I am basically suggesting that you should not use ST_AsSVG directly for anything other than very quick and dirty SVGs.