Parse SVG-map with python module SAX
Bas den Brok - mapping course



1. Read coordinates of outcrops and their numbers from SVG-map with python using SAX
2. convert them to Swissgrid-values (CH1903) and
3. store these values as a table in text format that can be imported in QGIS.

In a next stage we will use the table to find the corresponding altitude using the api3.geo.admin.ch server from Swisstopo

Understanding of python, some understanding of Dutch and a little understanding of QGIS required.


Also see here: https://docs.python.org/3/library/xml.sax.html
and other descriptions on the web.

We want to read coordinates and numbers of outcrops on a geological map in SVG format and write them to a text file to import in QGIS. To this end we selected only the layer with the outcrop localities and numbers. They consists of crosses and numbers. Here below is a picture showing part of the map:


SVG code of a cross and its number looks like this (below). These are lines from the above SVG-map file layer ontsluitingsnummers 2021-04-15 DETA.svg.


Outcrop number (325.2 in this example) is within a text-tag, coordinates of the cross are within the two path-tags, here in line 770 (d="m 4234.59 etc. ...) and line 775. There are two paths becaus a cross consists of two lines. We only need one as we can calculate the middle of the line which is the centre of the cross i.e. the outcrop point. In some cases the text-tag come before the path-tags, in other cases the path tags come first. We first parse the SVG file, read the coordinates and the outcrop number and produce a text file.

We use: svgkaartparsenmetsaxNIEUW2.py
Do not use this file. Only to help you write your own one.

Here below is the start of the code where the sub-class EmpHandler is defined. Look at it carefully. You can’t just copy paste it.


Character function (below) needs to be adjusted to the things to be read. This takes some time.


Part two of character function and endElement function. You’ll be busy with this a while :)


Processing itself is very simple:


Output text file 2021-04-15 DETA2.txt looks like this (below):


From now on it is very easy.

With the following python code we convert the above text-file with the coordinates of the SVG-map to Swissgrid coordinates CH1903:

txtfilelezen6.py

So we read the above text file 2021-04-15 DETA2.txt and we produce the below text file 2021-04-15 2 DETA tabelle.txt which is a list with (i) the outcrop number, (ii) x-coordinate of outcrop on SVG-map, (iii) y-coordinate of outcrop on SVG-map, (iv) Swissgrid CH1903 x-coordinate and (v) Swissgrid CH1903 y-coordinate of outcrop:


Next step could be to import this file in QGIS but we will first read and store the corresponding altitudes (elevations) using the api3.geo.admin.ch server from Swisstopo

Go to: https://www.denbrok.ch/geology/exchange/elevation/swisselevation.html