Skip to content

Instantly share code, notes, and snippets.

@milkbread
Created July 19, 2013 15:51
Show Gist options
  • Save milkbread/6040213 to your computer and use it in GitHub Desktop.
Save milkbread/6040213 to your computer and use it in GitHub Desktop.
HTML: TopoJSON: linear overlap example

Visualize linear overlaps using TopoJSON

This is a simple example on how we can use TopoJSON to indicate the overlap of linestrings!

Therefore, I made a simple shapefile with 3 linestrings that overlap to 100%! And this is a prerequisite of your dataset, too! You can find the compressed file 'shapes.rar' within this Gist...

Decompress the file and make a TopoJSON of it:

topojson -p -o linestrings_topo.json linestrings.shp

(Haven't installed topojson? It is pretty easy...on Ubuntu)

Short 'codesprint':

  • set a new html-file with a leaflet map

  • open the TopoJSON-file

  • do for each arc:

    • get the members

    • set the popup text

    • set color

    • get feature of the arc

    • make and add a new polyline to the leaflet map (set color and popup)

  • add all polylines to the layer control (especially this is not good coded!)

That's it...pretty simple, I think! Finally...sorry, it is very hard coded!

This example is based on a previous tutorial: D3-Geodata-Basics

<!DOCTYPE html>
<html>
<head>
<title>Testmap</title>
<meta charset="utf-8" />
<script src="http://cdn.leafletjs.com/leaflet-0.6.1/leaflet.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://bl.ocks.org/milkbread/raw/5779939/RKMapping_0.4.4.js"></script> <!--http://bl.ocks.org/milkbread/raw/5779939-->
<script src="http://bl.ocks.org/milkbread/raw/5829814/RKAggregation_1.0.js"></script> <!--http://bl.ocks.org/milkbread/raw/5829814 this function needs RKMapping.js additionally-->
<style>
@import url(http://cdn.leafletjs.com/leaflet-0.6.1/leaflet.css);
</style>
</head>
<body>
<div id="map" style="width: 960px; height: 600px"></div>
<script>
//BASEMAP
var map = L.map('map').setView([51.5,11], 6);
var toolserver = L.tileLayer('http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png');
var stamen = L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {attribution: 'Add some attributes here!'}).addTo(map);
var baseLayers = {"stamen": stamen, "toolserver-mapnik":toolserver};
var overlays = {};
//OPEN TOPOJSON-FILE
d3.json("linestrings_topo.json", function(error, topology) {
//console.log(topology)
//READ ALL EXISTING ARCS
topology.arcs.map(function(arc,i){
//GET THE MEMBERS OF CURRENT ARC
var members = getMembers(i);
var popupContent = 'Hey, hey ... I belong to: <br>';
//ADD ALL MEMBERS TO THE 'POPUPCONTENT'
members.forEach(function(member){popupContent = popupContent + "<b>" + member.name + "<br>"})
//GET THE Feature OF THE ARC
var arcFeature = topojson.feature(topology, {type: 'LineString', arcs:[i], members:members});
//console.log(i)
//D'OH...CHANGE THE COORDINATES TO FIT FOR LEAFLET-POLYLINE
arcFeature.geometry.coordinates = arcFeature.geometry.coordinates.map(function(d){return [d[1],d[0]]})
//MAKE A POLYLINE USING THE ARCFEATURE
for (var j=0;j<members.length;j++){
console.log(j)
//DEFINE THE COLOR...NEEDS A MORE COMPLICATED SOLUTION FOR COMPLETELY AUTOMATIC ATTRIBUTION
switch (j){
case 0: color='#f00'; weight=5;
break;
case 1: color='#00f'; weight=3.5;
break;
case 2: color='#0f0'; weight=2;
break;
}
var polyline = L.polyline(arcFeature.geometry.coordinates, {color: color, weight: weight, opacity:1})
.bindPopup(popupContent)
.addTo(map);
//console.log(polyline)
overlays["Polyline"+i+"_"+j] = polyline;
}
})
function getMembers(arc_id){
var members = [];
//CHECK FOR EXISTENCE OF ARC WITHIN EACH LINESTRING ... !!!VERYHARD CODED!!!
topology.objects.linestrings.geometries.forEach(function(feature){
//console.log(feature.arcs)
//console.log('indexof: '+feature.arcs.indexOf(arc_id))
if(feature.arcs.indexOf(arc_id)!=-1)members.push(feature.properties)
})
//console.log('members',members)
return members;
}
//ADD THE LINESTRINGS TO THE LAYER-CONTROL
L.control.layers(baseLayers,overlays).addTo(map);
})
</script>
</body>
</html>
Display the source blob
Display the rendered blob
Raw
{"type":"Topology","transform":{"scale":[0.00020623809088696392,0.0001819747860767325],"translate":[11.950293705181386,52.07173889515269]},"objects":{"linestrings":{"type":"GeometryCollection","geometries":[{"type":"LineString","arcs":[0,1,2],"properties":{"name":"linie1"}},{"type":"LineString","arcs":[3,1,4],"properties":{"name":"linie2"}},{"type":"LineString","arcs":[5,1,6],"properties":{"name":"linie3"}}]}},"arcs":[[[3288,9999],[722,-1697],[187,-1940]],[[4197,6362],[829,-2696],[1978,909]],[[7004,4575],[829,-788],[615,-1242]],[[0,9483],[1122,-2060],[321,-1485],[936,-1666],[1818,2090]],[[7004,4575],[-214,-3576],[-1149,-999]],[[7860,9302],[-428,-2061],[-2112,2152],[-107,-2394],[-1016,-637]],[[7004,4575],[294,606],[2701,1818],[-455,-3030]]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment