Skip to content

Commit

Permalink
Added use of geolocation API.
Browse files Browse the repository at this point in the history
  • Loading branch information
emeryberger committed Jan 1, 2021
1 parent 091f80a commit 9accd9b
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 78 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ all: generated-author-info.csv csrankings.js csrankings.min.js # fix-affiliation
clean:
rm $(TARGETS)

csrankings.js: csrankings.ts
csrankings.js: csrankings.ts continents.ts
@echo "Rebuilding JavaScript code."
tsc --project tsconfig.json

csrankings.min.js: csrankings.js
closure-compiler --js csrankings.js > csrankings.min.js
csrankings.min.js: csrankings.js csrankings.ts
google-closure-compiler --js csrankings.js > csrankings.min.js

update-dblp:
$(MAKE) download-dblp
Expand Down
44 changes: 44 additions & 0 deletions continents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function whichContinent(latitude, longitude) {
const point = [longitude, latitude];
for (const cont in continents) {
if (inPolygon(point, pathToList(continents[cont]))) {
return cont;
}
}
return "unknown";
}
// Adapted from https://stackoverflow.com/questions/13905646/get-the-continent-given-the-latitude-and-longitude
// Rough shape of continents.
const continents = {
"northamerica": { latitude: [90, 90, 78.13, 57.5, 15, 15, 1.25, 1.25, 51, 60, 60, 51, 51, 60], longitude: [-168.75, -10, -10, -37.5, -30, -75, -82.5, -105, -180, -180, -168.75, 166.6, 180, 180] },
"asia": { latitude: [90, 42.5, 42.5, 40.79, 41, 40.55, 40.4, 40.05, 39.17, 35.46, 33, 31.74, 29.54, 27.78, 11.3, 12.5, -60, -60, -31.88, -11.88, -10.27, 33.13, 51, 60, 90, 90, 90, 60, 60], longitude: [77.5, 48.8, 30, 28.81, 29, 27.31, 26.75, 26.36, 25.19, 27.91, 27.5, 34.58, 34.92, 34.46, 44.3, 52, 75, 110, 110, 110, 140, 140, 166.6, 180, 180, -180, -168.75, -168.75, -180] },
"europe": { latitude: [90, 90, 42.5, 42.5, 40.79, 41, 40.55, 40.40, 40.05, 39.17, 35.46, 33, 38, 35.42, 28.25, 15, 57.5, 78.13], longitude: [-10, 77.5, 48.8, 30, 28.81, 29, 27.31, 26.75, 26.36, 25.19, 27.91, 27.5, 10, -10, -13, -30, -37.5, -10] },
"australia": { latitude: [-11.88, -10.27, -10, -30, -52.5, -31.88], longitude: [110, 140, 145, 161.25, 142.5, 110] },
"southamerica": { latitude: [1.25, 1.25, 15, 15, -60, -60], longitude: [-105, -82.5, -75, -30, -30, -105] },
"africa": { latitude: [15, 28.25, 35.42, 38, 33, 31.74, 29.54, 27.78, 11.3, 12.5, -60, -60], longitude: [-30, -13, -10, 10, 27.5, 34.58, 34.92, 34.46, 44.3, 52, 75, -30] },
// "asia2" : { latitude: [90, 90, 60, 60], longitude: [-180, -168.75, -168.75, -180] },
// "northAmerica2" : { latitude: [51, 51, 60], longitude: [166.6, 180, 180] },
"antarctica": { latitude: [-60, -60, -90, -90], longitude: [-180, 180, 180, -180] }
};
function inPolygon(point, vs) {
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
let x = point[0], y = point[1];
let inside = false;
for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
let xi = vs[i][0], yi = vs[i][1];
let xj = vs[j][0], yj = vs[j][1];
let intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect)
inside = !inside;
}
return inside;
}
function pathToList(path) {
let l = [];
for (let i = 0; i < path["longitude"].length; i++) {
l.push([path["longitude"][i], path["latitude"][i]]);
}
return l;
}
50 changes: 50 additions & 0 deletions continents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
function whichContinent(latitude: number, longitude: number) : string {
const point = [longitude, latitude];
for (const cont in continents) {
if (inPolygon(point, pathToList(continents[cont]))) {
return cont;
}
}
return "unknown";
}

// Adapted from https://stackoverflow.com/questions/13905646/get-the-continent-given-the-latitude-and-longitude
// Rough shape of continents.
const continents : any = {
"northamerica" : { latitude: [90, 90, 78.13, 57.5, 15, 15, 1.25, 1.25, 51, 60, 60, 51, 51, 60], longitude: [-168.75, -10, -10, -37.5, -30, -75, -82.5, -105, -180, -180, -168.75, 166.6, 180, 180] },
"asia" : { latitude: [90, 42.5, 42.5, 40.79, 41, 40.55, 40.4, 40.05, 39.17, 35.46, 33, 31.74, 29.54, 27.78, 11.3, 12.5, -60, -60, -31.88, -11.88, -10.27, 33.13, 51, 60, 90, 90, 90, 60, 60], longitude: [77.5, 48.8, 30, 28.81, 29, 27.31, 26.75, 26.36, 25.19, 27.91, 27.5, 34.58, 34.92, 34.46, 44.3, 52, 75, 110, 110, 110, 140, 140, 166.6, 180, 180, -180, -168.75, -168.75, -180] },
"europe" : { latitude: [90, 90, 42.5, 42.5, 40.79, 41, 40.55, 40.40, 40.05, 39.17, 35.46, 33, 38, 35.42, 28.25, 15, 57.5, 78.13],longitude: [-10, 77.5, 48.8, 30, 28.81, 29, 27.31, 26.75, 26.36, 25.19, 27.91, 27.5, 10, -10, -13, -30, -37.5, -10] },
"australia" : { latitude: [-11.88, -10.27, -10, -30, -52.5, -31.88], longitude: [110, 140, 145, 161.25, 142.5, 110] },
"southamerica" : { latitude: [1.25, 1.25, 15, 15, -60, -60], longitude: [-105, -82.5, -75, -30, -30, -105] },
"africa" : { latitude: [15, 28.25, 35.42, 38, 33, 31.74, 29.54, 27.78, 11.3, 12.5, -60, -60], longitude: [-30, -13, -10, 10, 27.5, 34.58, 34.92, 34.46, 44.3, 52, 75, -30] },
// "asia2" : { latitude: [90, 90, 60, 60], longitude: [-180, -168.75, -168.75, -180] },
// "northAmerica2" : { latitude: [51, 51, 60], longitude: [166.6, 180, 180] },
"antarctica" : { latitude: [-60, -60, -90, -90],longitude: [-180, 180, 180, -180] }
};

function inPolygon(point : Array<number>, vs : Array<Array<number>>) {
// ray-casting algorithm based on
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html/pnpoly.html
let x = point[0], y = point[1];

let inside = false;
for (let i = 0, j = vs.length - 1; i < vs.length; j = i++) {
let xi = vs[i][0], yi = vs[i][1];
let xj = vs[j][0], yj = vs[j][1];

let intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}

return inside;
}

function pathToList(path : any) {
let l = [];
for (let i = 0; i < path["longitude"].length; i++) {
l.push([path["longitude"][i], path["latitude"][i]]);
}
return l;
}

23 changes: 22 additions & 1 deletion csrankings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
/// <reference path="./typescript/d3.d.ts" />
/// <reference path="./typescript/d3pie.d.ts" />
/// <reference path="./typescript/navigo.d.ts" />
/// <reference path="./typescript/continents.d.ts" />
;
;
;
Expand Down Expand Up @@ -250,7 +251,7 @@ class CSRankings {
this.countAuthorAreas();
yield this.loadCountryInfo(this.countryInfo);
this.addListeners();
/* CSRankings.geoCheck(); */
CSRankings.geoCheck();
this.rank();
}))();
}
Expand Down Expand Up @@ -1311,6 +1312,26 @@ class CSRankings {
}
return start;
}
static geoCheck() {
navigator.geolocation.getCurrentPosition((position) => {
const continent = whichContinent(position.coords.latitude, position.coords.longitude);
let regions = document.getElementById("regions");
switch (continent) {
case "northamerica":
return;
case "europe":
case "asia":
case "southamerica":
case "africa":
regions.value = continent;
break;
default:
regions.value = "world";
break;
}
CSRankings.getInstance().rank();
});
}
/*
public static geoCheck(): void {
// Figure out which country clients are coming from and set
Expand Down
Loading

0 comments on commit 9accd9b

Please sign in to comment.