ArcView Shapefile to SVG converter (shp2svg, ogis2svg.pl)
version 0.5.1 (2007-05-08)
This converter can be used under the terms of www.gnu.org/copyleft/lesser.html
For many interactive Webmapping applications the data should be converted from a GIS format. ArcView Shapefiles are a common source format for GIS data exchange. This format can be read and written by many commercial and open source GIS applications. The www.gdal.org/ogr/ OGR Simple Features Library can be used to convert from different formats to the ESRI shapefile format. The "ogis2svg.pl" converter builds on the freely available "shp2pgsl" converter developed by the https://postgis.refractions.net Postgis project. The ogis2svg.pl converter takes the text input from shp2pgsql and converts it to SVG files.
ogis2svg.pl has the following features:
- Converts OGC "Simple Features for SQL" format to SVG (Currently only POINT, MULTILINESTRING and MULTIPOLYGON supported)
- POINT features are converted to either circles or symbols
- Output as relative coordinates (may reduce filesize)
- Output can be rounded (may reduce filesize)
- Automatically detects the viewBox according to min/max corner points; alternatively, the user may specify a viewBox (in input units)
- Attributes from the dbase file can be selected and included as attributes in a separate namespace
- The output file also contains a DTD extension for the GIS non-graphical attributes within the file header
- A selected unique attribute can be used as a unique svg id
- The user can select events that he wants to attach to the path-elements
- The user can select a group attribute. Attributes that share the same value are grouped and can share the same styles
- groups can be sorted ascending or descending
- Use of a stylesheet text file to attach styles to a group when using the group feature
- Conversion of XML reserved characters (e.g. &, < >) within attribute data
- A scale factor and input/output units can be specified to reduce GIS data to a map scale. This is useful when importing shapefiles via SVG to Adobe Illustrator
Installation requirements:
The tool was tested on Linux (SuSE 9.3), MacOSX 10.4 and Windows XP. It should run on other Unix OS and other Windows versions as well. On Windows we recommend the free ActivePerl distribution from ActiveState.com. For ActivePerl you can use the ppm (perl package manager) to install additional perl modules, such as the required Math::Round module. On all the other platforms please use the normal way to install perl modules (download → unpack → cd to unpacked directory → perl Makefile.PL → make → make check → make install). For your convenience we have now added a windows executable that you can use instead of the ogis2svg.pl perlscript, created with the ActiveState perlApp kit.
Tools required for the conversion:
- www.perl.org interpreter >=5.8, Windows: www.activestate.com/activeperl
- Perl module Math::Round (Windows users can use the ogis2svg.exe file instead)
- shp2pgsql tool from the postgis.refractions.net Postgis project (see binaries below)
- perl script ogis2svg.pl, Unix and MacOSX users might want to set the x-flag to make it executable (chmod ugo+x ogis2svg.pl)
For your convenience you can download the following shp2pgsql binaries. You should put the shp2pgsql binaries and the ogis2svg.pl script or ogis2svg.exe in a directory within your path, so it can be used from any location.
For Windows users that do not want to install ActivePerl we have also provided an executable file ogis2svg.exe that was created with the ActivePerl "PerlApp" software. Just use it instead of the perl script.
Conversion process
After the installation open a shell or dos window and cd to the directory containing your shapefile and dbase file.
At the command prompt type in:
ogis2svg.pl --input yourshapefilename --output yourshapename.svg --roundval 0.1
Depending on your OS or setup you will also have to include the perl command in the PATH variable. On the Windows platform (assuming that the perl interpreter is included in the %PATH% variable) you would use:
perl ogis2svg.pl --input yourshapename --output yourshapename.svg --roundval 0.1
Windows users that use the ogis2svg.exe executable will have to use the following command:
ogis2svg.exe --input yourshapename --output yourshapename.svg --roundval 0.1
This will get the parameters for input (shapefilename without extension), output and rounding and start the conversion program. The roundval argument specifies how the resulting SVG coordinates are rounded. A value of 1 means that we round to full integers, a value of 0.1 means that we round to one decimal place, a value of 0.01 means that we round to two decimal places and so on ... For meters as units 1 or zero decimal place will be accurate enough (roundval 1 or 0.1), for degrees as base units I recommend to use at least 5 decimal places (roundval 0.00001). The more decimal places you use, the bigger and more accurate your resulting SVG file will be. Please note that if you use different units for input and output that the roundval parameter relates to the output units. Finding out the correct parameters is important. You might need 2 or 3 trys. After starting the conversion tool you will be led through a number of questions.
Optional flags are --inputunits m (currently only "m" is supported), --outputunits mm (currently "m", "dm", "cm", "mm" are supported), --viewBox '480000 -300000 350000 220000' (an optional viewBox (specified in input units) that is used for generating the viewBox attribute and the referenceframe rectangle), --scale 25000 (an optional map scale) and --referenceframe (an optional reference frame containing the xmin, xmax, ymin, ymax values in real world coordinates).
Questions that the tool will or might want to ask (question text is in italic, remarks in normal text):
- The following attributes are available. Please select the attributes you want to include in the SVG export:
This is followed by a question sequence for each attribute whether you want to include it or not
- You selected the following Attributes: attrib1, attrib2, attrib3
Type in attribute Name or 'none' if you don't want to include a unique id:
attrib1 to attrib3 are the attributes that are converted from your dbase file. If you select an attribute as a svg-attribute, make sure it is unique. Otherwise you will run into troubles when you load the SVG file in your SVG viewer.
- Do you want to group the data according to one attribute? (type 'y' or 'n')
If you want to group your elements (e.g. all road elements of class 1), type 'y'
- Optionally (if grouping was selected):
Please select one of the following Attributes for grouping: attrib2, attrib3
Type in attribute Name:
Select a group attribute. attrib1 is not available any more, if you selected it as a unique id.
- Optionally (if grouping was selected):
Do you want to include the group-Attrib attrib2 as a path-attribute?
Type 'y' or 'n'. 'n' indicates that you want to use the attribute only for grouping.:
Here you need to tell the converter if you want to include the grouping attribute or just use it for the grouping purpose.
- Optionally (if grouping was selected):
Do you want to sort the group alphabetically, by nr ascending or by nr descending?
Type 'a', 'd', 'nra' or 'nrd':
Here you can specify the sort order for your groups. a is alphabetic, d is alphabetic descending, nra is by number ascending, nrd is by number descending
- Optionally (if grouping was selected):
Do you want to enter a style-file containing group/style matches?
Enter filename (incl. extension) or just press enter if you don't have one:
Here you can optionally enter the path/filename of a stylesheet file. The stylesheet file is a simple text file where you have the group attribute values, a tab as delimiter, and the CSS style attribute to be included in the SVG group, such as fill, stroke and opacity
- Would you like to include event-handlers to the element? Type (y|n)
Here you can tell the converter if you want to add event-handlers to the individual elements. It is recommended to use these event handlers only for <use /> elements (in case you generate symbols). Others can have the event handlers globally on the group.
- Optionally (if you selected event-handlers):
Please enter the eventhandlers you want to include, separated by commas, e.g. "onclick,onmouseover,onmouseout":
Here you can tell the converter if you want to add event-handlers to the individual path elements.
- Optionally (if you convert POINT features):
How would you like to represent the POINT elements? Type (circle/symbol)
Here you can select between circle or symbol representation
- Optionally (if you selected symbol representation):
Do you have a column indicating the symbol type?
Tpye "none" if you don't, or one of the following columns: ...
Type in attribute name:
Here you can select a column that is used to define the symbol id. This column has to be prepared in Arcview prior to conversion.
- Optionally (if you selected symbol representation):
Do you want the program to automatically generate symbol definitions for you? Type (y|n)
Here you can tell the program to generate symbol definitions matching each individual symbol reference. These are circles with random fills. You can later replace them by your own symbol definitions.
Dealing with special characters
The special characters, such as the & sign, >, < are automatically replaced using the perl subroutine "replaceSpecialChars()" at the end of the ogis2svg.pl script. This is necessary because otherwise they would cause troubles with XML wellformedness. All other special characters (such as german umlauts, french accents, etc.) are now handled corrrectly, because we now output Unicode utf8 (since version 0.2). In case you want to add a special character here for replacement, just add an additional line in the function replaceSpecialChars(). The subroutine simply replaces the special characters with their xx; equivalent. If you later want to change the characters back (e.g. for displaying the attribute data on mouse over) using javascript you can do the following:
function replaceSpecialChars(myString) {
re = new RegExp("&","g");
myString = myString.replace(re,String.fromCharCode(38));
re = new RegExp(""","g");
myString = myString.replace(re,String.fromCharCode(34));
re = new RegExp("<","g");
myString = myString.replace(re,String.fromCharCode(60));
re = new RegExp(">","g");
myString = myString.replace(re,String.fromCharCode(62));
return myString;
}
A note about utf8 output and text editors:
Foreign characters in UTF8 and the use of certain text-editors can lead to frustration. We recommend that you check your text editors documentation on how to detect and set the correct encoding. Some editors require special settings. In the popular Ultraedit editor on the Windows platform you need to configure the editor: "Advanced" --> "Options" --> "General" --> "Open/Save/Convert" --> checkbox "utf8 automatic detection". In "Kate" on KDE/Unix/Linux you need to configure as follows: "Settings" --> "Configure Kate" --> "Open/Save" --> "Encoding: Unicode/utf8". In BBedit on MacOSX you can force in the open dialog to read the file ("Read AS") as UTF8.
Known bugs and limitations
- The postgis tool shp2pgsql is used in different versions where the text-output varies. You might run into problems, if your version does not match the versions I tested (see binaries above). In case you use a different version please send me the output and error-message and I'll try to fix it. A later version will eventually work without the shp2pgsql tool and use a perl module directly to read the shapefiles
Version history
Version 0.1: 2004-10 (initial version)
Version 0.2: 2005-03-02
- added utf8 support for output, foreign characters should now work without problems
- removed -db support, with the Postgis AsSVG() function this functionality is not needed anymore
- added better rounding support: instead of nrdigits you now specify a roundval: e.g. 0.001, 1 or 100
- added descending alphabetical ordering of groups
- improved file reading to also support newer versions of the shp2pgsql program
- added DTD extension for correct XML validation for elements with custom namespace attributes
- added a windows binary as an alternative to the pl script
Version 0.2.1: 2005-03-08
- corrected a bug with DTD extension
Version 0.3: 2005-04-19
- added support for POINT features, represented as circles or symbol elements
- ids are now checked to conform to xs:NCName data type, special characters are removed
Version 0.4: 2005-06-21
- added support map scale, input and output coordinates
- shp2pgsql is now called from ogis2svg.pl to eliminate the need for two steps in the conversion process
Version 0.5: 2005-09-12
- added a '--ads' flag for setting default values for the ADS (Atlas of Switzerland) team; watch out for $ads variables if you want to get rid of this flag
- corrected a rounding bug for '"M"oveto-coordinates' when dealing with scale conversions
- corrected a replacing bug in MULTILINESTRINGs where the character 'z' appeared in attribute values
- added replacement tables in function 'makeCorrectId()' to make better XML ids, this only effects german Umlauts and some french special characters
- added LGPL license in header of the file
Version 0.5.1: 2007-05-08
- added a '--viewbox=x y width height' option, values are used if specified instead of the calculated min/max values
- corrected --referenceframe option in case no scale was given
- corrected grouping behaviour if only one attribute was selected
- generated new executable for Windows which doesn't expire
Feedback
Please report bugs, errors and feature wishes to Andreas Neumann. If you find the tool useful and have a cool project to show me, I'd be glad if you share a link to the project where you used my conversion tool.
References:
- postgis.refractions.net Postgis spatial extension: This project contains the shp2pgsql converter.