Use D3 to project points rather than leaflet (or OL)
Last active
February 8, 2016 23:38
-
-
Save akbstone/598c3554d078e057c131 to your computer and use it in GitHub Desktop.
Leaflet + D3 using D3 geo to project
This file contains 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 characters
<html> | |
<head></head> | |
<body> | |
<style> | |
@import url(//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.css); | |
#map { | |
width: 960px; | |
height: 500px; | |
} | |
svg { | |
position: relative; | |
} | |
path { | |
fill: #000; | |
fill-opacity: .2; | |
stroke: #fff; | |
stroke-width: 1.5px; | |
} | |
path:hover { | |
fill: brown; | |
fill-opacity: .7; | |
} | |
</style> | |
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.5/leaflet.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.1/jquery.js"></script> | |
</body> | |
</html> | |
<div id="map"></div> | |
<script> | |
var doItD3Style = function(map){ | |
var projection = d3.geo.mercator(); | |
map.on("viewreset", redraw); | |
map.on("dragend", redraw); | |
// map.on("zoom", redraw); | |
redraw(); | |
function redraw(){ | |
$(g2[0]).empty(); | |
var b = map.getPixelBounds(), | |
w = b.max.x - b.min.x, | |
h = b.max.y - b.min.y, | |
z = map.getZoom(), | |
c = map.getCenter(), | |
$p = $(map.getPanes().mapPane), | |
os = $p.offset(); | |
svg2 | |
.attr({ | |
width:w, | |
height:h, | |
}) | |
.style({ | |
left:-os.left + 'px', | |
top:-os.top + 'px' | |
}) | |
projection | |
.scale((1 << 8 + z) / 2 / Math.PI) | |
.translate([w/2,h/2]) | |
.center([c.lng,c.lat]); | |
for(var i = -200;i <= 180;i += 20){ | |
for(var j = -85;j <= 85; j += 5){ | |
var pt = projection([i,j]); | |
var _g = g2.append('g') | |
.attr({ | |
transform:"translate(" + pt[0] + "," + pt[1] + ")", | |
}); | |
_g.append('circle') | |
.attr({ | |
r:5 | |
}) | |
.style({ | |
fill:"#FF0000" | |
}) | |
_g.append('text') | |
.text(i + ',' + j) | |
} | |
} | |
} | |
} | |
var doItLeafletStyle = function(map,collection){ | |
var transform = d3.geo.transform({point: projectPoint}), | |
path = d3.geo.path().projection(transform); | |
var feature = g.selectAll("path") | |
.data(collection.features) | |
.enter().append("path"); | |
map.on("viewreset", reset); | |
reset(); | |
// Reposition the SVG to cover the features. | |
function reset() { | |
var bounds = path.bounds(collection), | |
topLeft = bounds[0], | |
bottomRight = bounds[1]; | |
svg .attr("width", bottomRight[0] - topLeft[0]) | |
.attr("height", bottomRight[1] - topLeft[1]) | |
.style("left", topLeft[0] + "px") | |
.style("top", topLeft[1] + "px"); | |
g .attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")"); | |
feature.attr("d", path); | |
} | |
// Use Leaflet to implement a D3 geometric transformation. | |
function projectPoint(x, y) { | |
var point = map.latLngToLayerPoint(new L.LatLng(y, x)); | |
this.stream.point(point.x, point.y); | |
} | |
} | |
var lmap = new L.Map("map", {center: [37.8, -96.9], zoom: 4}) | |
.addLayer(new L.TileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")); | |
var svg = d3.select(lmap.getPanes().overlayPane).append("svg"), | |
g = svg.append("g").attr("class", "leaflet-zoom-hide"); | |
var svg2 = d3.select(lmap.getPanes().overlayPane).append("svg"), | |
g2 = svg2.append("g").attr("class", "leaflet-zoom-hide"); | |
d3.json("us_states.json", function(error, collection) { | |
if (error) throw error; | |
doItLeafletStyle(lmap,collection); | |
doItD3Style(lmap) | |
}); | |
</script> | |
<script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment