Skip to main content
Community Expert
Jul 3, 2024 8:56 AM UTC
Answered

Illustrator Scripting: transfer real life longitude & latitude on Illustrator Map - help needed(!)

Hi community,

 

this one is a really hard one!

Going back and forth for a couple of days now my brain got completely stucked... so please give me some input or maybe someone can work out a functional script in this case:

 

A bunch of coordinates (longitude & latitude values) must be transfered onto an Illustrator map to set a dot for each location... I'm stuggeling with creating the right formular to set them right... I'm pretty sure it's just a mathematical logical issue thing...

 

 

// the locations in latitude & longitude as vars:
    var lat1 = 48.7491239657; //city_1
    var lon1 = 9.17012929916; //city_1

    var lat2 = 47.6264900005; //city_2
    var lon2 = 7.65646999127; //city_2

    var lat3 = 49.4682342; //city_3
    var lon3 = 8.5611973; //city_3


// the locations manually set on the Illustrator-Map as vars:
    var my_x1 = mm(116.60); //city_1
    var my_y1 = mm(82.97); //city_1

    var my_x2 = mm(39.83); //city_2
    var my_y2 = mm(168.54); //city_2

    var my_x3 = mm(83.67); //city_3
    var my_y3 = mm(29.33); //city_3

//mm to point converter
function mm(n) {
    return n * 2.83464567;
}

 

 

PS. and as always – only scripting solutions appreceated! 😉

 

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Nils M. BarnerCorrect answer
Community Expert
Jul 12, 2024 1:03 PM UTC

Hallo Nils, 

die Variante mit den Start- und Endkoordinaten kommt für dich nicht in Frage?


It took a while to figure it out but this is the best aswer as it leaded to a solution for my issue!

Best tip ever was that something might be wrong with my Illustrator file as the coords were almost 30mm off – so I created a new file and there we go!

Next issue were multiple distortions within the scan of the original map so I had to fix it in two ways:

1.) I started by using the solution pixxxelschubser pointed out

2.) I added a factor to multiply the distortions for left to right and top to button

3.) after fixing this there was still a lot of distortion going on in the corners so additionally I devided the whole map into 4 areas and scaled each of these sections individually based on the distance from the center of the artboard + some additional factors for each direction...

 

This finally produced the desired, precise result!

 

I will not post a finished script here, as the result is too special and only works smoothly for this one map. Hope the way to get there will help anyone seeking for a similar solution.

 

Nils

 

13 replies

pixxxelschubser
Community Expert
Community Expert
Jul 3, 2024 11:29 AM UTC

Hallo Nils, 

hast du eventuell auch die deinen Dokumentengrenzen entsprechenden Geo-Koordinaten? Denn dann könnte man doch normalerweise die City-Koordinaten durch Dreisatz-Berechnung ermitteln.

 

Oder habe ich die Aufgabenstellung nicht richtig verstanden?

Translate
Community Expert
Jul 3, 2024 1:30 PM UTC

Hi pixxxelschubser 😉

I was thinking of setting up a x/y start coordinate to give all points a fixed reference...

 

Wie meinst du genau mit der Dreisatzberechnung?

 

Translate
pixxxelschubser
Community Expert
Community Expert
Jul 5, 2024 12:46 AM UTC

Hallo Nils,

sofern du die entsprechenden Start und Endkoordinaten passend zu deiner Dokumentengröße verfügbar hast, ist es "simpler Dreisatz".

 

gegeben ist:

ein Dokument in der Größe 200 mm × 297 mm

ein Kartenausschnitt von 9,0° Ost bis 10,0° Ost und 49,0° Nord bis 48,0° Nord

die X-Koordinate von Stuttgart Bad Cannstatt ist fast exakt 9,2° Ost

Berechnung:

also pos X = (X-Koordinate Stuttgart Bad Cannstatt - KarteGeoCoorLinks) * Dokument Rechts / (KarteGeoCoorRechts - KarteGeoCoorLinks)

in mm
pos X = (9,2 - 9) * 200 / (10 - 9)

pos X = (0,2) * 200 / (1)

pos X = 40

 

bzw in pt

pos X = (9,2 - 9) * 566.929 / (10 - 9)

pos X = (0,2) * 566.929 / (1)

pos X = 113.385826771653

 

Translate
m1b
Community Expert
Community Expert
Jul 3, 2024 10:12 AM UTC

Hi @Nils M. Barner, Edit: I have updated my code below (and thanks to the solveMatrix function from ChatGPT!) and I hope it will work this time! 🙂

 

The idea is you call the "getTransform" function, giving it your example transformation pairs [ [lat,long], [x, y] ] and it returns a function that you can use thereafter for transforming your coordinates. Let me know if it works in practice!

- Mark

 

 

/**
 * @file Transform Latitude Longitude.js
 * Demo of transforming lat,long coordinates
 * into Illustrator coordinates.
 *
 * Notes:
 * - This is a quick, simple method and hardly tested,
 *   so be careful to check the results.
 * - It knows nothing about actual cartography or
 *   map projections!
 * - It works using the supplied example transformations.
 *
 * @author m1b, chatGPT
 * @discussion https://community.adobe.com/t5/illustrator-discussions/illustrator-scripting-transfer-real-life-longitude-amp-latitude-on-illustrator-map-help-needed/m-p/14716499
 */
(function () {

    const mm = 2.834645;

    // these examples are necessary to calibrate the transformer
    var city1 = [48.7491239657, 9.17012929916],
        city2 = [47.6264900005, 7.65646999127],
        city3 = [49.4682342, 8.5611973];

    var city1_pts = [116.60 * mm, 82.97 * mm],
        city2_pts = [39.83 * mm, 168.54 * mm],
        city3_pts = [83.67 * mm, 29.33 * mm];

    // generate a transform function using some examples
    var transform = getTransform([
        [city1, city1_pts],
        [city2, city2_pts],
        [city3, city3_pts],
    ]);

    // calculate position of a new city
    var city4 = [48.123456, 8.654321],
        city4_pts = transform(city4);

    $.writeln('city4_pts = ' + city4_pts);

})();

/**
 * Returns a transform function that transforms
 * [latitude, longitude] into Illustrator [x, y],
 * given an array of example `pairs`.
 * @author m1b and chatGPT
 * @version 2024-07-03
 * @param {Array<Array>} pairs - array of [lat_lon, pts] pairs.
 * @returns {Function}
 */
function getTransform(pairs) {

    var A = [],
        B = [];

    for (var i = 0; i < pairs.length; i++) {
        A.push([pairs[i][0][0], pairs[i][0][1], 1, 0, 0, 0]);
        A.push([0, 0, 0, pairs[i][0][0], pairs[i][0][1], 1]);
        B.push(pairs[i][1][0], pairs[i][1][1]);
    }

    // Solve for the affine transformation matrix using matrix algebra (A * X = B)
    var X = solveMatrix(A, B);

    var m = [
        [X[0], X[1], X[2]],
        [X[3], X[4], X[5]],
    ];

    // Transform function to apply the affine transformation
    return (

        function transform(latLon) {
            return [
                m[0][0] * latLon[0] + m[0][1] * latLon[1] + m[0][2],
                m[1][0] * latLon[0] + m[1][1] * latLon[1] + m[1][2],
            ];
        }

    );


};

/**
 * Solves a system of linear equations using Gaussian elimination.
 * 
 * This function solves the system of equations represented by the augmented matrix [A|B],
 * where A is an n x n matrix and B is an n x 1 vector.
 * 
 * @author chatGPT
 * @version 2024-07-03
 * @param {Array<Array<Number>>} A - The coefficient an n x n matrix of the system of equations.
 * @param {Array<Number>} B - The constant terms vector of the system of equations.
 * @returns {Array<Number>} - The solution vector X such that A * X = B.
 */
function solveMatrix(A, B) {

    var n = A.length;

    for (var i = 0; i < n; i++)
        A[i].push(B[i]);

    for (var i = 0; i < n; i++) {

        var maxRow = i;

        for (var k = i + 1; k < n; k++)
            if (Math.abs(A[k][i]) > Math.abs(A[maxRow][i]))
                maxRow = k;

        var tmp = A[maxRow];
        A[maxRow] = A[i];
        A[i] = tmp;

        for (var k = i + 1, c; k < n; k++) {

            c = -A[k][i] / A[i][i];

            for (var j = i; j < n + 1; j++) {
                if (i === j)
                    A[k][j] = 0;
                else
                    A[k][j] += c * A[i][j];
            }

        }

    }

    var X = new Array(n);

    for (var i = n - 1; i >= 0; i--) {

        X[i] = A[i][n] / A[i][i];

        for (var k = i - 1; k >= 0; k--)
            A[k][n] -= A[k][i] * X[i];
    }

    return X;

};

 

Translate
Community Expert
Jul 3, 2024 1:33 PM UTC

@mark142840: is this the fixed version now? or should I still ignore?

 

Translate
m1b
Community Expert
Community Expert
Jul 3, 2024 1:40 PM UTC

Please take it for a test drive!

Translate