Skip to content

Instantly share code, notes, and snippets.

@rob4acre
Last active October 17, 2017 07:59
Show Gist options
  • Save rob4acre/a2e16749cf46bfcc6688bc8ee69f6217 to your computer and use it in GitHub Desktop.
Save rob4acre/a2e16749cf46bfcc6688bc8ee69f6217 to your computer and use it in GitHub Desktop.
Beam Coverage
license: gpl-3.0

Display a zoomable Orthographic projection with countries and some locations shown. Displays a longitude slider that allows the longitude to be set dynamically. The aim is to show which earth stations are visible from a satellite in geostationary orbit at the selected longitude.

Bits have been copied from many blocks

forked from rob4acre's block: Beam Coverage

id name
-1 Northern Cyprus
-2 Kosovo
-3 Somaliland
4 Afghanistan
8 Albania
10 Antarctica
12 Algeria
16 American Samoa
20 Andorra
24 Angola
28 Antigua and Barbuda
31 Azerbaijan
32 Argentina
36 Australia
40 Austria
44 Bahamas
48 Bahrain
50 Bangladesh
51 Armenia
52 Barbados
56 Belgium
60 Bermuda
64 Bhutan
68 Bolivia, Plurinational State of
70 Bosnia and Herzegovina
72 Botswana
74 Bouvet Island
76 Brazil
84 Belize
86 British Indian Ocean Territory
90 Solomon Islands
92 Virgin Islands, British
96 Brunei Darussalam
100 Bulgaria
104 Myanmar
108 Burundi
112 Belarus
116 Cambodia
120 Cameroon
124 Canada
132 Cape Verde
136 Cayman Islands
140 Central African Republic
144 Sri Lanka
148 Chad
152 Chile
156 China
158 Taiwan, Province of China
162 Christmas Island
166 Cocos (Keeling) Islands
170 Colombia
174 Comoros
175 Mayotte
178 Congo
180 Congo, the Democratic Republic of the
184 Cook Islands
188 Costa Rica
191 Croatia
192 Cuba
196 Cyprus
203 Czech Republic
204 Benin
208 Denmark
212 Dominica
214 Dominican Republic
218 Ecuador
222 El Salvador
226 Equatorial Guinea
231 Ethiopia
232 Eritrea
233 Estonia
234 Faroe Islands
238 Falkland Islands (Malvinas)
239 South Georgia and the South Sandwich Islands
242 Fiji
246 Finland
248 Åland Islands
250 France
254 French Guiana
258 French Polynesia
260 French Southern Territories
262 Djibouti
266 Gabon
268 Georgia
270 Gambia
275 Palestinian Territory, Occupied
276 Germany
288 Ghana
292 Gibraltar
296 Kiribati
300 Greece
304 Greenland
308 Grenada
312 Guadeloupe
316 Guam
320 Guatemala
324 Guinea
328 Guyana
332 Haiti
334 Heard Island and McDonald Islands
336 Holy See (Vatican City State)
340 Honduras
344 Hong Kong
348 Hungary
352 Iceland
356 India
360 Indonesia
364 Iran, Islamic Republic of
368 Iraq
372 Ireland
376 Israel
380 Italy
384 Côte d'Ivoire
388 Jamaica
392 Japan
398 Kazakhstan
400 Jordan
404 Kenya
408 Korea, Democratic People's Republic of
410 Korea, Republic of
414 Kuwait
417 Kyrgyzstan
418 Lao People's Democratic Republic
422 Lebanon
426 Lesotho
428 Latvia
430 Liberia
434 Libya
438 Liechtenstein
440 Lithuania
442 Luxembourg
446 Macao
450 Madagascar
454 Malawi
458 Malaysia
462 Maldives
466 Mali
470 Malta
474 Martinique
478 Mauritania
480 Mauritius
484 Mexico
492 Monaco
496 Mongolia
498 Moldova, Republic of
499 Montenegro
500 Montserrat
504 Morocco
508 Mozambique
512 Oman
516 Namibia
520 Nauru
524 Nepal
528 Netherlands
531 Curaçao
533 Aruba
534 Sint Maarten (Dutch part)
535 Bonaire, Sint Eustatius and Saba
540 New Caledonia
548 Vanuatu
554 New Zealand
558 Nicaragua
562 Niger
566 Nigeria
570 Niue
574 Norfolk Island
578 Norway
580 Northern Mariana Islands
581 United States Minor Outlying Islands
583 Micronesia, Federated States of
584 Marshall Islands
585 Palau
586 Pakistan
591 Panama
598 Papua New Guinea
600 Paraguay
604 Peru
608 Philippines
612 Pitcairn
616 Poland
620 Portugal
624 Guinea-Bissau
626 Timor-Leste
630 Puerto Rico
634 Qatar
638 Réunion
642 Romania
643 Russian Federation
646 Rwanda
652 Saint Barthélemy
654 Saint Helena, Ascension and Tristan da Cunha
659 Saint Kitts and Nevis
660 Anguilla
662 Saint Lucia
663 Saint Martin (French part)
666 Saint Pierre and Miquelon
670 Saint Vincent and the Grenadines
674 San Marino
678 Sao Tome and Principe
682 Saudi Arabia
686 Senegal
688 Serbia
690 Seychelles
694 Sierra Leone
702 Singapore
703 Slovakia
704 Viet Nam
705 Slovenia
706 Somalia
710 South Africa
716 Zimbabwe
724 Spain
728 South Sudan
729 Sudan
732 Western Sahara
740 Suriname
744 Svalbard and Jan Mayen
748 Swaziland
752 Sweden
756 Switzerland
760 Syrian Arab Republic
762 Tajikistan
764 Thailand
768 Togo
772 Tokelau
776 Tonga
780 Trinidad and Tobago
784 United Arab Emirates
788 Tunisia
792 Turkey
795 Turkmenistan
796 Turks and Caicos Islands
798 Tuvalu
800 Uganda
804 Ukraine
807 Macedonia, the former Yugoslav Republic of
818 Egypt
826 United Kingdom
831 Guernsey
832 Jersey
833 Isle of Man
834 Tanzania, United Republic of
840 United States
850 Virgin Islands, U.S.
854 Burkina Faso
858 Uruguay
860 Uzbekistan
862 Venezuela, Bolivarian Republic of
876 Wallis and Futuna
882 Samoa
887 Yemen
894 Zambia
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
background: #fcfcfa;
height: 500px;
position: relative;
width: 960px;
}
.border {
fill: black;
stroke: black;
}
.clip {
fill: #fcfcfa;
}
.stroke {
fill: none;
stroke: #000;
stroke-width: 3px;
}
.fill {
fill: #ccc;
}
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
}
.sphere {
fill: #7BB5FF;
}
.gradient {
fill: url(#gradient);
}
.country {
fill: #336633;
/*fill-opacity: 0.7;*/
stroke: #fff;
stroke-width: 0.5px;
}
.equator {
stroke: black;
stroke-width: 0.5px;
}
.cityname {
fill-opacity: 1;
fill: black;
font-size:10px;
font-family: "Arial", sans-serif;
}
.city {
fill: red;
stroke: none;
}
.satellite {
fill: blue;
}
.beam {
fill: red;
fill-opacity: 0.15;
stroke: #B10000;
stroke-linejoin: round;
stroke-width: 1;
pointer-events: none; /* ensures that mouseover countries works thru the beam */
}
.ticks {
font: 10px sans-serif;
}
.track,
.track-inset,
.track-overlay {
stroke-linecap: round;
}
.track {
stroke: #000;
stroke-opacity: 0.3;
stroke-width: 10px;
}
.track-inset {
stroke: #ddd;
stroke-width: 8px;
}
.track-overlay {
pointer-events: stroke;
stroke-width: 50px;
stroke: transparent;
cursor: crosshair;
}
.handle {
fill: #fff;
stroke: #000;
stroke-opacity: 0.5;
stroke-width: 1.25px;
}
div.tooltip {
color: #222;
background: #fff;
border-radius: 3px;
box-shadow: 0px 0px 2px 0px #a6a6a6;
padding: .2em;
text-shadow: #f5f5f5 0 1px 0;
opacity: 0.9;
position: absolute;
font-size:12px;
font-family: "Arial", sans-serif;
}
.hidden {
display: none;
}
</style>
<div id="map"></div>
<span id="projection-menu"></span>
<script src="//d3js.org/d3.v4.min.js"></script>
<script src="//d3js.org/queue.v1.min.js"></script>
<script src="//d3js.org/d3-geo-projection.v1.min.js"></script>
<script src="//d3js.org/topojson.v2.min.js"></script>
<script>
var width = 960,
height = 800,
longitude = 0; // initial longitude
var stationpoints; //track states
var stationnames;
// the projection - the globe in this case
var projection = d3.geoOrthographic()
.center([0, 10])
.scale(290)
.rotate([longitude,0])
var path = d3.geoPath(projection).pointRadius(4);
; // projection needs a path
var graticule = d3.geoGraticule(); // the long/lat lines
var sphere = {type: "Sphere"}; // a sphere for the nice blue sea
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
// setup the filter for the country labels
var defs = svg.append("defs")
var filter = defs.append("filter")
.attr("x","0")
.attr("y","0")
.attr("width","1")
.attr("height","1")
.attr("id","solid")
filter.append("feFlood")
.attr("flood-color","#ffff99")
.attr("flood-opacity","0.5")
filter.append("feComposite")
.attr("in","SourceGraphic")
// end filter for country labels
// setup the gradient to make the earth look brighter at top left
var gradient = svg.append("svg:defs")
.append("svg:linearGradient")
.attr("id", "gradient")
.attr("x1", "0%")
.attr("y1", "0%")
.attr("fx1", "50%")
.attr("fy1", "50%")
.attr("x2", "100%")
.attr("y2", "100%")
.attr("spreadMethod", "pad");
gradient.append("svg:stop") // middle step setting
.attr("offset", "50%")
.attr("stop-color", "#fff")
.attr("stop-opacity", 0.6);
gradient.append("svg:stop") // final step setting
.attr("offset", "100%")
.attr("stop-color", "#006")
.attr("stop-opacity", 0.6);
// end setup gradient
// the frame around the earth
svg.append("rect")
.attr("class","border")
.attr("x",0)
.attr("y",0)
.attr("width", 960)
.attr("height", 600)
var g = svg.append("g")
// Draw the sphere for the earth
g.append("path")
.datum(sphere)
.attr("class", "sphere")
.attr("d", path)
// draw a gradient sphere because it looks cool
g.append("path")
.datum(sphere)
.attr("class", "gradient")
.attr("d", path)
// TODO: use d3-tip - add ground station data and satellite data?
// Some code for the country label tooltip
var offsetL = document.getElementById('map').offsetLeft+10;
var offsetT = document.getElementById('map').offsetTop+10;
var tooltip = d3.select("#map")
.append("div")
.attr("class", "tooltip hidden");
// TODO: use d3-queue ??
// load data for drawing the world, countries and earth stations
queue()
.defer(d3.json, "world-110m.json") // world map topojson
.defer(d3.tsv, "countries.tsv") // country name data
.defer(d3.csv, "stations.csv") // the earth station locations
.await(ready);
// draw the world, countries and stations
function ready(error, world, names, stations) {
if (error) throw error;
// load country data features from the topojson
var countries = topojson.feature(world, world.objects.countries).features;
// folds the TSV country name data (separate file) into the
// countries object
countries = countries.filter(function(d) {
return names.some(function(n) {
if (d.id == n.id) return d.name = n.name;
});
}).sort(function(a, b) {
return a.name.localeCompare(b.name);
});
// draw the earth stations - these are appended as topojson points
// therefore they rotate with the earth
g.selectAll(".station")
.data(stations)
.enter()
.append("path")
.attr("class", function(d) {
if (d.zl == 0) {
return "city"
} else {
return "city hidden"
}
})
.datum(function(d){
return {type: "Point", coordinates:[d.lon,d.lat] }
})
.attr("d", path)
// the station labels, these are positioned by a functions, which means
// we can refresh them as the globe is rotated
g.selectAll("text")
.data(stations)
.enter()
.append("text")
.attr("class", function(d) {
if (d.zl == 0) {
return "cityname"
} else {
return "cityname hidden"
}
})
.attr("dy", 10) // set y position relative
.attr("text-anchor", "middle") // set anchor y justification
.attr("dominant-baseline", "middle") // set x justification
.text(function(d) { return d.station })
// show countries with some sexy mouseover effects
g.selectAll(".country")
.data(countries)
.enter().insert("path", ".graticule")
.attr("class", "country")
.attr("d", path)
.on('mouseenter', function(d, i) {
d3.select(this).style('fill','#339933');
})
.on('mouseleave', function(d, i) {
d3.select(this).style('fill','#336633');
tooltip.classed("hidden", true);
})
.on("mousemove", function(d) {
label = d.name;
var mouse = d3.mouse(svg.node())
.map( function(d) { return parseInt(d); } );
tooltip.classed("hidden", false)
.attr("style", "left:"+(mouse[0]+offsetL)+"px;top:"+(mouse[1]+offsetT)+"px")
.html(label);
})
stationpoints = d3.selectAll(".city");
stationnames = d3.selectAll(".cityname");
positionCities() // draw the city dots and labels using current longitude
};
// draw the graticule on top of the sphere and countries
g.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path)
// This function redrawa the earth station labels in the
// correct position as the globe rotates and hides them
// if they are behind the earth
function positionCities() {
projection.rotate([longitude,0]);
g.selectAll(".cityname")
.attr("x", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("y", function(d) {
return projection([d.lon, d.lat])[1];
})
// determine visibility and hide label if behind earth
.attr("visibility", function(d) {
var diff = (Math.abs(longitude + parseFloat(d.lon)) + 90) % 360;
if (diff < 180) {
return 'visible';
} else {
return 'hidden';
}
})
// this is the text background - again need to hide when it is
// behind the earth
.attr("filter", function(d) {
var diff = (Math.abs(longitude + parseFloat(d.lon)) + 90) % 360;
if (diff < 180) {
return 'url(#solid)';
} else {
return '';
}
})
}
// Draw the Equator
g.append("path")
.datum({type: "LineString", coordinates:
[[-180, 0], [-90, 0], [0, 0], [90, 0], [180, 0]]})
.attr("class", "equator")
.attr("d", path);
// Draw the beams isobands
// TODO: load from a file
g.append("path")
.datum({type: "Polygon", coordinates:
[[[0, 55],[40,45],[50,0],[0, -33],[-37, 17],[0, 55]]]})
.attr("class", "beam")
.attr("d", path);
g.append("path")
.datum({type: "Polygon", coordinates:
[[[0, 45],[40,25],[40,0],[0, -3],[-27, 20],[0, 55]]]})
.attr("class", "beam")
.attr("d", path);
g.append("path")
.datum({type: "Polygon", coordinates:
[[[0, 35],[30,25],[20,10],[0, 10],[-7, 20],[0, 55]]]})
.attr("class", "beam")
.attr("d", path);
// zoom and pan stuff
var zoom = d3.zoom()
.scaleExtent([1, 10])
.translateExtent([[0,0],[960,600]])
.extent([[0,0],[960,460]])
.on("zoom",function() {
g.attr("transform", d3.event.transform);
var k = d3.event.transform["k"] // get the current zoom level
// change cityname font-size / dy as zoom in
g.selectAll(".cityname")
.attr("style", function(d) {
var fs = 10 - (0.7*k);
return "font-size:"+fs+"px";
})
.attr("dy", function(d) {
var fs = 10 - (0.7*k);
return fs;
})
// change city label size as zoom in
g.selectAll(".city")
.attr("d", path.pointRadius(4 - (0.3 * k)));
// show selected stations only when zoomed in
stationnames.classed('hidden', function(d) {
if (k > 6) {
return false;
} else {
if (parseInt(d.zl) > 1) {
return true;
} else {
return false;
}
}
})
// show selected stations only when zoomed in
stationpoints.classed('hidden', function(d) {
if (k > 6) {
return false;
} else {
if (parseInt(d.zl) > 1) {
return true;
} else {
return false;
}
}
})
});
svg.call(zoom)
// this is just to provide a background for the slider and the satellites
// so that when the earth is zoomed it is clipped
svg.append("rect")
.attr("class","clip")
.attr("x",0)
.attr("y",600)
.attr("width", 960)
.attr("height", 600)
// linear scale for the slider
var x = d3.scaleLinear()
.domain([180, -180])
.range([0, width])
.clamp(true);
var slider = svg.append("g")
.attr("class", "slider")
.attr("transform", "translate(0 620)")
slider.append("line")
.attr("class", "track")
.attr("x1", x.range()[0])
.attr("x2", x.range()[1])
.select(function() {
return this.parentNode.appendChild(this.cloneNode(true));
})
.attr("class", "track-inset")
.select(function() {
return this.parentNode.appendChild(this.cloneNode(true));
})
.attr("class", "track-overlay")
.call(d3.drag()
.on("start.interrupt", function() { slider.interrupt(); })
.on("start drag", function() { slide(x.invert(d3.event.x)); }));
slider.insert("g", ".track-overlay")
.attr("class", "ticks")
.attr("transform", "translate(0," + 18 + ")")
.selectAll("text")
.data(x.ticks(10))
.enter().append("text")
.attr("x", x)
.attr("text-anchor", "middle")
.text(function(d) { return d + "°"; });
var handle = slider.insert("circle", ".track-overlay")
.attr("class", "handle")
.attr("cx", 480)
.attr("id", "longitude")
.attr("r", 9);
var feature = g.selectAll("path");
function slide(h) {
handle.attr("cx", x(h));
longitude = h;
setGlobeRotation()
}
// function to rotate the globe to current longitude
function setGlobeRotation() {
positionCities()
g.selectAll("path").attr("d", path);
}
// the satellites g element
var sats = svg.append("g")
.attr("class", "sats")
.attr("transform", "translate(0 660)")
// read the satellites file, draw the sats
// TODO: handle when multiple satellites have the same longitude
d3.csv("satellites.csv", function(error, satellites) {
// satellite marker
// TODO: make it sexier
sats.selectAll("rect")
.data(satellites)
.enter()
.append("rect")
.attr("x", function(d) {
return x(d.longitude) - 5;
})
.attr("y", -10)
.attr("height", 20)
.attr("width", 10)
.attr("class","satellite")
.on("click", function(d){
// move slider - this updates the globe rotation and labels
slide(x.invert(x(d.longitude)))
})
// satellite names
sats.selectAll("text")
.data(satellites)
.enter()
.append("text") // append text
.attr("x", function(d) {
return x(d.longitude);
})
.attr("dx", 15) // set y position of bottom of text
.attr("class", "cityname")
.attr("text-anchor", "start") // set anchor y justification
.attr("alignment-baseline", "middle") // set anchor y justification
.text(function(d) {
return d.name;
})
});
</script>
name longitude
GEO1 23
GEO2 -34
GEO3 123
GEO4 -98
code station country lat lon zl
JPN Tokyo Japan 35.68501691 139.7514074 0
USA New York United States 40.74997906 -73.98001693 0
MEX Mexico City Mexico 19.44244244 -99.1309882 0
IND Mumbai India 19.01699038 72.8569893 0
BRA Sao Paulo Brazil -23.55867959 -46.62501998 0
IND Delhi India 28.6699929 77.23000403 0
CHN Shanghai China 31.21645245 121.4365047 0
IND Kolkata India 22.4949693 88.32467566 0
BGD Dhaka Bangladesh 23.72305971 90.40857947 0
ARG Buenos Aires Argentina -34.60250161 -58.39753137 0
USA Los Angeles United States 33.98997825 -118.1799805 0
PAK Karachi Pakistan 24.86999229 66.99000891 0
EGY Cairo Egypt 30.04996035 31.24996822 0
BRA Rio de Janeiro Brazil -22.92502317 -43.22502079 0
JPN Osaka Japan 34.75003522 135.4601448 0
CHN Beijing China 39.92889223 116.3882857 0
PHL Manila Philippines 14.60415895 120.9822172 0
RUS Moscow Russia 55.75216412 37.61552283 0
TUR Istanbul Turkey 41.10499615 29.01000159 0
FRA Paris French Republic 48.86669293 2.333335326 0
KOR Seoul Korea, South 37.5663491 126.999731 0
NGA Lagos Nigeria 6.443261653 3.391531071 0
IDN Jakarta Indonesia -6.174417705 106.8294376 0
USA Chicago United States 41.82999066 -87.75005497 0
CHN Guangzhou China 23.1449813 113.3250101 0
GBR London United Kingdom 51.49999473 -0.116721844 0
PER Lima Peru -12.04801268 -77.05006209 0
IRN Tehran Iran 35.67194277 51.42434403 0
COD Kinshasa Congo (Kinshasa) -4.329724102 15.31497188 0
COL Bogota Colombia 4.596423563 -74.08334396 0
CHN Shenzhen China 22.55237051 114.1221231 0
CHN Wuhan China 30.58003135 114.270017 0
CHN Hong Kong China 22.3049809 114.1850093 0
CHN Tianjin China 39.13002626 117.2000191 0
IND Chennai India 13.08998781 80.27999874 0
TWN Taipei Taiwan 25.03583333 121.5683333 0
IND Bangalore India 12.96999514 77.56000972 0
THA Bangkok Thailand 13.74999921 100.5166447 0
PAK Lahore Pakistan 31.55997154 74.35002478 0
CHN Chongqing China 29.56497703 106.5949816 0
IND Hyderabad India 17.39998313 78.47995357 0
CHL Santiago Chile -33.45001382 -70.66704085 0
USA Miami United States 25.7876107 -80.22410608 0
BRA Belo Horizonte Brazil -19.91502602 -43.91500452 0
ESP Madrid Kingdom of Spain 40.40002626 -3.683351686 0
USA Philadelphia United States 39.99997316 -75.16999597 0
IND Ahmedabad India 23.03005292 72.58000362 0
VNM Ho Chi Minh City Vietnam 10.78002545 106.6950272 0
CAN Toronto Canada 43.69997988 -79.42002079 0
SGP Singapore Singapore 1.293033466 103.8558207 0
AGO Luanda Angola -8.838286114 13.23442704 0
IRQ Baghdad Iraq 33.3386485 44.39386877 0
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment