From SVG to Geo Coordinates – A Complete Guide

Why This Task Is Not Trivial?

First of all what do we have? There is a vector shape, which may represent a map, which we’d like to convert into a GEO map. In other words there is a SVG file containing the source shape, that you’d like to convert in geoJSON or whatever collection of geo points. This is not trivial, of course, first of all because there’s no an algorithm or automation that can do this, and because everybody knows that the resulting map will be only approached, but will never be so accurate as the vector shape. This is because in a vector shape you may contain Bézier Curves, which I’ll show a little bit later in this post, that are difficult to represent in geo coordinates.

So the first task will be to find an approaching algorithm. However there are two things that are optimistic:

  1. You can’t effectively represent Bézier curves in geo coordinates, but even if you could do it there’s no practical need, because the collection of geo coordinates will be huge and this will slow down you’re application. Remember that geoJSON is yet again possibly used by your browser and the amount of geo points will be proportionally slowing down the app.
  2. As you may know Google’s visualization map is representing the World’s countries again with quite approached maps. Take a look at the following image – the country borders are quite sharpened:

Google Visualization Map

So far we know that we need an approaching algorithm that will convert vector lines and possibly curves in geo coordinates, but before we proceed we’ve to understand the SVG format.

SVG Format and Possible Approaches

Let me give you an example of a SVG file. By opening such file with a text editor you can see the file format:


This text represent this shape:


First thing to notice is there are things quite different from the HTML markup. Of course this things are noticeable. There is a M, c and z where the first two are followed by some numbers. What are these characters? Well lets see at There are lots of SVG specific commands, but we need to understand only few of them.

1. M (absolute) m (relative) – moveto

Start a new sub-path at the given (x,y) coordinate. M (uppercase) indicates that absolute coordinates will follow; m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs of coordinates, the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands will be relative if the moveto is relative, and absolute if the moveto is absolute. If a relative moveto (m) appears as the first element of the path, then it is treated as a pair of absolute coordinates. In this case, subsequent pairs of coordinates are treated as relative even though the initial moveto is interpreted as an absolute moveto.

2. Z or z – closepath

Close the current subpath by drawing a straight line from the current point to current subpath’s initial point. Since the Z and z commands take no parameters, they have an identical effect.

3. C (absolute) c (relative) – curveto (x1 y1 x2 y2 x y)+

Draws a cubic Bézier curve from the current point to (x,y) using (x1,y1) as the control point at the beginning of the curve and (x2,y2) as the control point at the end of the curve. C (uppercase) indicates that absolute coordinates will follow; c (lowercase) indicates that relative coordinates will follow. Multiple sets of coordinates may be specified to draw a polybézier. At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.

4. L (absolute) l (relative) – lineto (x y)+

Draw a line from the current point to the given (x,y) coordinate which becomes the new current point. L (uppercase) indicates that absolute coordinates will follow; l (lowercase) indicates that relative coordinates will follow. A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the new current point is set to the final set of coordinates provided.

1. Bézier Curves

So far we know that the especially the c command draws a curve – a Bézier curve. But what is a Bézier curve anyway? Here’s a little explanation you can find at Wikipedia. Here’s an example of a cubic Bézier curve:

Cubic Bezier CurveFirst thing we see is that here there are four points – p0, p1, p2, p3. The curve between p0 and p3 is defined by them and the two control points – p1 and p2. To be more precise let me show you an animation of how this curve is constructed:

Cubic Bezier Curve

This is the way the red line is constructed with the help of the green and blue lines, and of course the points p1 and p2. The result is a smooth line that’s often used in animation and shapes. This is our case. As you’ve seen the example above is constructed only with curves.

Before we proceed let me say that there are lots of types of Bézier curves not only cubic. Here they are:

1.1. Linear Bézier curve

Linear Bezier Curve

1.2. Quadratic Bézier curve

Quadratic Bezier Curve1.3. And event Higher-order curves:

Bezier Curves

2. What You Need To Know About The SVG Format

However the most important thing to know is the heart of SVG curve format. First you MOVE to a point with the M command. Lets see how does it look in our case:

M76.484,153.703 – This simply means: move to the point with coordinates (76.484, 153.703)

The next step is to draw a curve. We’ve seen that a cubic curve is defined by four points – p0, p1, p2 and p3, but what we have in the SVG format:


Here there are only three points (-0.86, -0.27), (-3.816, 0.199), (-4.002, -1.055) – not four. Well these are only the last three points, the first one is the one defined by the M command. Next thing to know is that the M command defines an absolute point in the canvas of the SVG file, while the c (shouldn’t be mistaken with C) is a relative curve. This means that if the p0 is the point (76.484, 153.703), the p1 point is relative to it with small difference in coordinates – (-0.86, -0.27).

However the most important thing to know is that after drawing the curve:

At the end of the command, the new current point becomes the final (x,y) coordinate pair used in the polybézier.

This is extremely important! This means that the last point of this curve becomes the p0 of the next curve, and the three points of the second curve, in our case:


are relative to the last (x, y) of the previous curve – (-4.002, -1.055). Without this you cannot convert any SVG shape into geo coordinates.

3. Lines Instead of Curves

OK, I said about an approaching algorithm, that is only approaching the curved vector shape. This means that I can simply replace the curves with lines. Thus I’ve to change to SVG file format from using the c command to the l command, as l is also relative. The curve:

M76.484,153.703 c-0.86-0.27-3.816,0.199-4.002-1.055

will become

M76.484,153.703 l-4.002-1.055

and so forth. This will result into the more sharpened shape:SVG Line Map

However this is again a SVG file and no Geo coordinates are present. To end up with this there’s a tiny PHP script that collects all the last pairs from the c commands and converts them to Geo Coordinates relative to the World’s center – (Lat, Lon) = (0, 0).

$str = '-0.86-0.27-3.816,0.199-4.002-1.055
$matches = explode('c', $str);
$temp = array();
foreach ($matches as $k => $v) {
    // get the new relative point
    $s = $v;
    // a really dummy regex to find the last pair
    preg_match('/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/', $s, $m);
    // if the array is not empty
    if (!empty($temp)) {
        $m[5] = $temp[0]+$m[5];
        $m[6] = $temp[1]+$m[6];
    echo '[', ($m[5]), ', ', ($m[6]), '],<br />';
    // save the last point
    $temp = array($m[5], $m[6]);

Final Adjustments

I guess you don’t need any shape in the World’s center as you may not need it in the same scale. There are two things to do. First you can scale the map by multiplying by a factor every point:

// a really dummy regex to find the last pair
    preg_match('/(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?(-?\d{1,2}\.?\d{0,3}),?/', $s, $m);
    // multiply by some factor
    $m[5] *= 0.05;
    $m[6] *= -0.05;
    // if the array is not empty
    if (!empty($temp)) {
        $m[5] = $temp[0]+$m[5];
        $m[6] = $temp[1]+$m[6];

and than you can move the shape all over the world by adding some offset to the resulting point:

echo '[', ($m[5]-38.9), ', ', ($m[6]+10.7), '],<br />';

Further Researches

Here we just substitute the curves with lines, which is the most primitive approach. What about substituting each curve with corresponding two lines, or three lines. The resulting shape will be far more accurate in compare with the source “curved” shape. So there are still lots of things to be done. However this may help you do the job.

3 thoughts on “From SVG to Geo Coordinates – A Complete Guide

  1. Superb research and explanation. This is exactly what I’m trying to accomplish in one of my recent projects. This will help me to get me on the right track.

  2. In most cases, to save only the last point change dramatically the shape of the curve.
    My first option was to use the two control points as new points and connect them with lines, but it was not good either.

    Then I thought that maybe the best option is to ass points that are on the curve.

    What you need is:
    – 4 points of a Bezier curve
    – 2 equations to yield values for x and y
    – number of points you want to “rasterize” your curve

    Take this example :
    The four points are 0,0; 35,0; 50,-50 ; 100,-50; (0,0 because it’s relative coordinates).

    This link gives you the explanation to calculate 6 values and create the two equations (one for x one for y).

    Because the Bezier curve is defined in [0,1], it’s easy to pick up values:
    – to have one extra point, take 1/2
    – to have 3 extra points, take 1/4, 1/2 and 3/4

    In my example, I want to add 3 points. The equations give me this:



    Now, you have to change your path.
    It was: c35,0,50-50,100-50
    Meaning: a curve from 0,0 (current point) to 100,-50

    Now you have to draw 4 lines.
    You need to calculate relative coordinates for each new point and the new coordinates of the final point (100,-50).

    you start at 0,0
    then 23.359375,-7.8125
    then you need to subtract the x and the y with the previous

    Then you create 4 lines:


    And voilà! You have 4 segments instead of a Bezier curve.

    Tip: add all x and all y and you find the final point.

Leave a Reply

Your email address will not be published. Required fields are marked *