Skip to content

Instantly share code, notes, and snippets.

@dontcallmedom
Forked from rolfen/ReadMe.md
Created March 2, 2018 13:51
Show Gist options
  • Select an option

  • Save dontcallmedom/47ffb55cd1caec64a737d63aa2d2fae8 to your computer and use it in GitHub Desktop.

Select an option

Save dontcallmedom/47ffb55cd1caec64a737d63aa2d2fae8 to your computer and use it in GitHub Desktop.

Revisions

  1. dontcallmedom revised this gist Mar 2, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ function toArray(nl)
    ## Stage 1: parse HTML

    ```
    var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(
    var C1 = toArray(document.querySelectorAll("#mw-content-text table.wikitable tbody tr")).map((tr)=>(
    toArray(tr.children).map((td)=>(
    td.innerText.trim()
    )
  2. @rolfen rolfen revised this gist Dec 24, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -87,6 +87,8 @@ var C2 = C1.map(function(city){
    });
    ```

    The result is in C2 and it's an array of objects.

    ### Google Chrome console: Copy result to clipboard:
    ```
    copy(JSON.stringify(C2,null,2))
  3. @rolfen rolfen revised this gist Dec 24, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ This code runs in the browser debugging console.
    I parse this page to extract geographical coordibates for major cities:
    https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

    Just open this page in your browser then launch your debugging console.
    Just open this page in your browser, launch your debugging console and get ready to copy-paste.

    First, define this function, it is a dependency:

  4. @rolfen rolfen revised this gist Dec 24, 2017. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -2,10 +2,14 @@

    ... to place markers on a map.

    First, I parse this page to extract geographical coordibates for major cities:
    This code runs in the browser debugging console.

    I parse this page to extract geographical coordibates for major cities:
    https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

    I need this function, it is a dependency:
    Just open this page in your browser then launch your debugging console.

    First, define this function, it is a dependency:

    ```
    function toArray(nl)
  5. @rolfen rolfen revised this gist Dec 24, 2017. 1 changed file with 25 additions and 14 deletions.
    39 changes: 25 additions & 14 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,8 @@
    Parsing this page to extract GPS coords for major cities:
    # GPS coordinates of major cities in the world

    ... to place markers on a map.

    First, I parse this page to extract geographical coordibates for major cities:
    https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

    I need this function, it is a dependency:
    @@ -16,16 +20,20 @@ function toArray(nl)
    }
    ```

    // stage 1: parse HTML

    // JSON.stringify(toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(toArray(tr.children).map((td)=>(td.innerText.trim())))).reduce((acc,tbodyArr)=>(acc.concat(tbodyArr))),3)
    ## Stage 1: parse HTML

    // or simpler:

    var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(toArray(tr.children).map((td)=>(td.innerText.trim()))));
    ```
    var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(
    toArray(tr.children).map((td)=>(
    td.innerText.trim()
    )
    )
    ));
    ```

    /* we get an array of type:
    The array assigend to C1 is of type:

    ```
    [
    [
    "16°30′S",
    @@ -43,13 +51,13 @@ var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable t
    ],
    // etc.
    ]
    ```

    */

    // Stage 2: translate to GPS coordinates
    // source: http://stackoverflow.com/a/1140335

    ## Stage 2: translate to GPS coordinates
    Source: http://stackoverflow.com/a/1140335

    ```
    function ConvertDMSToDD(degrees, minutes, seconds, direction) {
    var dd = parseInt(degrees) + parseInt(minutes)/60 + parseInt(seconds)/(60*60);
    @@ -73,6 +81,9 @@ var C2 = C1.map(function(city){
    "coords" : coords
    })
    });
    ```

    // copy result to clipboard (Chrome console only)
    copy(JSON.stringify(C2,null,2))
    ### Google Chrome console: Copy result to clipboard:
    ```
    copy(JSON.stringify(C2,null,2))
    ```
  6. @rolfen rolfen revised this gist Dec 24, 2017. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    // Parsing this page to extract GPS coords for major cities:
    // https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes
    Parsing this page to extract GPS coords for major cities:
    https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

    // I need this function, it is a dependency:
    I need this function, it is a dependency:

    ```
    function toArray(nl)
    {
    var arr = [];
    @@ -13,6 +14,7 @@ function toArray(nl)
    }
    return(arr);
    }
    ```

    // stage 1: parse HTML

  7. @rolfen rolfen created this gist Dec 24, 2017.
    76 changes: 76 additions & 0 deletions ReadMe.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    // Parsing this page to extract GPS coords for major cities:
    // https://en.wikipedia.org/w/index.php?title=List_of_cities_by_longitude&printable=yes

    // I need this function, it is a dependency:

    function toArray(nl)
    {
    var arr = [];
    if(isNaN(nl.length)) {
    throw "Cannot get length";
    } else {
    for(var i=-1,l=nl.length;++i!==l;arr[i]=nl[i]);
    }
    return(arr);
    }

    // stage 1: parse HTML

    // JSON.stringify(toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(toArray(tr.children).map((td)=>(td.innerText.trim())))).reduce((acc,tbodyArr)=>(acc.concat(tbodyArr))),3)

    // or simpler:

    var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>(toArray(tr.children).map((td)=>(td.innerText.trim()))));

    /* we get an array of type:

    [
    [
    "16°30′S",
    "180°00′W",
    "Rabi Island",
    "N/A",
    "Fiji"
    ],
    [
    "51°53′N",
    "176°39′W",
    "Adak",
    "Alaska",
    "United States"
    ],
    // etc.
    ]

    */

    // Stage 2: translate to GPS coordinates
    // source: http://stackoverflow.com/a/1140335


    function ConvertDMSToDD(degrees, minutes, seconds, direction) {
    var dd = parseInt(degrees) + parseInt(minutes)/60 + parseInt(seconds)/(60*60);

    if (direction == "S" || direction == "W") {
    dd = dd * -1;
    } // Don't do anything for N or E
    return dd;
    }

    var C2 = C1.map(function(city){
    var latLong = [city[0],city[1]];
    coords = latLong.map(function(l){
    var p = l.split(/[^\d\w]+/);
    return ConvertDMSToDD(p[0],p[1],0,p[2]);
    });
    return ({
    "lat/long" : latLong,
    "name" : city[2],
    "state" : city[3],
    "country" :city[4],
    "coords" : coords
    })
    });

    // copy result to clipboard (Chrome console only)
    copy(JSON.stringify(C2,null,2))