Skip to content

Instantly share code, notes, and snippets.

@ramiroaznar
Last active April 20, 2016 19:51
Show Gist options
  • Save ramiroaznar/0c287de99c27ea6d01eeef69a0864508 to your computer and use it in GitHub Desktop.
Save ramiroaznar/0c287de99c27ea6d01eeef69a0864508 to your computer and use it in GitHub Desktop.
Straight Lines vs Great Circles with CartoDB.js
<!DOCTYPE html>
<html>
<head>
<title>Straight Lines vs Great Circles with CartoDB.js</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="http://cartodb.com/assets/favicon.ico" />
<link rel="stylesheet" href="http://libs.cartocdn.com/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<script src="http://libs.cartocdn.com/cartodb.js/v3/3.15/cartodb.js"></script>
<style>
html, body,#map {
width:100%;
height:100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id='map'></div>
<script type="text/SQL" id="sql_greatCircles">
SELECT
ST_Transform(
ST_Segmentize(
ST_Makeline(
a.the_geom,
b.the_geom
)::geography,
100000
)::geometry,
3857
) as the_geom_webmercator
FROM
ramirocartodb.cbd_offices_locations a, ramirocartodb.cbd_offices_locations b
WHERE
a.cartodb_id <> b.cartodb_id
</script>
<script type="text/SQL" id="sql_lines">
SELECT
ST_MakeLine(a.the_geom_webmercator, b.the_geom_webmercator) AS the_geom_webmercator,
row_number() OVER() AS cartodb_id
FROM
ramirocartodb.cbd_offices_locations a,
ramirocartodb.cbd_offices_locations b
WHERE
a.cartodb_id <> b.cartodb_id
</script>
<script type="text/javascript">
function main() {
var greatCircles = $('#sql_greatCircles').text(); // get great circles query
var lines = $('#sql_lines').text(); // get lines query
var map = new L.Map('map', {
zoomControl: false,
center: [43, -30],
zoom: 3
});
cartodb.createLayer(map, {
user_name: 'ramirocartodb',
type: 'cartodb',
sublayers: [
{
type: "http", // add basemap without labels
urlTemplate: "http://{s}.basemaps.cartocdn.com/dark_nolabels/{z}/{x}/{y}.png",
subdomains: [ "a", "b", "c" ]
},
{
type: "mapnik", // add lines
sql: lines,
cartocss: "#cbd_offices_locations{line-color: white;line-width: 2;line-opacity: 0.7;}"
},
{
type: "mapnik", // add great circles
sql: greatCircles,
cartocss: "#cbd_offices_locations{line-color: #5CA2D1;line-width: 2;line-opacity: 0.7;}"
},
{
type: "mapnik", // add points
sql: 'SELECT * FROM cbd_offices_locations',
cartocss: "#cbd_offices_locations{marker-fill-opacity: 0.9;marker-line-color: #000000;marker-line-width: 1;marker-line-opacity: 1;marker-placement: point;marker-type: ellipse;marker-width: 10;marker-fill: #2E5387;marker-allow-overlap: true;}#cbd_offices_locations::labels {text-name: [city];text-face-name: 'DejaVu Sans Book';text-size: 10;text-label-position-tolerance: 10;text-fill: #FFFFFF;text-halo-fill: #FFF;text-halo-radius: 0;text-dy: -10;text-allow-overlap: true;text-placement: point;text-placement-type: simple}"
}
]
})
.addTo(map);
}
window.onload = main;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment