-
-
Save dontcallmedom/47ffb55cd1caec64a737d63aa2d2fae8 to your computer and use it in GitHub Desktop.
Revisions
-
dontcallmedom revised this gist
Mar 2, 2018 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)=>( toArray(tr.children).map((td)=>( td.innerText.trim() ) -
rolfen revised this gist
Dec 24, 2017 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)) -
rolfen revised this gist
Dec 24, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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, launch your debugging console and get ready to copy-paste. First, define this function, it is a dependency: -
rolfen revised this gist
Dec 24, 2017 . 1 changed file with 6 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,10 +2,14 @@ ... to place markers on a map. 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. First, define this function, it is a dependency: ``` function toArray(nl) -
rolfen revised this gist
Dec 24, 2017 . 1 changed file with 25 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,8 @@ # 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 ``` var C1 = toArray(document.querySelectorAll("#mw-content-text > table.wikitable tbody tr")).map((tr)=>( toArray(tr.children).map((td)=>( td.innerText.trim() ) ) )); ``` 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 ``` 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 }) }); ``` ### Google Chrome console: Copy result to clipboard: ``` copy(JSON.stringify(C2,null,2)) ``` -
rolfen revised this gist
Dec 24, 2017 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 -
rolfen created this gist
Dec 24, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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))