main page - page initiale - hauptseite - página principal projects - projets - projekte - proyectos authors - auteurs - autoren - autores papers - exposés - berichte - papeles help - aide - hilfe - ayuda

carto:papers:svg:utils:shp2svg

ArcView Shapefile to SVG converter (shp2svg, ogis2svg.pl)

version 0.5.1 (2007-05-08)

This converter can be used under the terms of the LGPL license

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 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 Postgis project. The ogis2svg.pl converter takes the text input from shp2pgsql and converts it to SVG files.

ogis2svg.pl has the following features:

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:

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):

  1. 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
  2. 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.
  3. 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'
  4. 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.
  5. 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.
  6. 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
  7. 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
  8. 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.
  9. 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.
  10. 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
  11. 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.
  12. 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 &#xxx; 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("&#038;","g");
       myString = myString.replace(re,String.fromCharCode(38));
       re = new RegExp("&#034;","g");
       myString = myString.replace(re,String.fromCharCode(34));
       re = new RegExp("&#060;","g");
       myString = myString.replace(re,String.fromCharCode(60));
       re = new RegExp("&#062;","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

Version history

Version 0.1: 2004-10 (initial version)

Version 0.2: 2005-03-02

Version 0.2.1: 2005-03-08

Version 0.3: 2005-04-19

Version 0.4: 2005-06-21

Version 0.5: 2005-09-12

Version 0.5.1: 2007-05-08

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:




Last modified: Tuesday, 08-May-2007 15:48:39 CEST
© carto:net (andré m. winter & andreas neumann)
original URL for reference: http://www.carto.net /papers/svg/utils/shp2svg/index.shtml
del.icio.us Add this page to del.icio.us