Add a d3 example
This commit is contained in:
1 parent
5af738593e
commit
2633d8784c
3 files changed
+155
No files matched your search
@@ -0,0 +1,59 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="X-UA-Compatible" content="chrome=1">
|
||||
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
|
||||
<link rel="stylesheet" href="../css/ol.css" type="text/css">
|
||||
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
|
||||
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
|
||||
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
|
||||
<title>d3 integration example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="navbar navbar-inverse navbar-fixed-top">
|
||||
<div class="navbar-inner">
|
||||
<div class="container">
|
||||
<a class="brand" href="./">OpenLayers 3 Examples</a>
|
||||
<ul class="nav pull-right">
|
||||
<li><iframe class="github-watch-button" src="http://ghbtns.com/github-btn.html?user=openlayers&repo=ol3&type=watch&count=true"
|
||||
allowtransparency="true" frameborder="0" scrolling="0" height="20" width="90"></iframe></li>
|
||||
<li><a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-hashtags="openlayers"> </a></li>
|
||||
<li><div class="g-plusone-wrapper"><div class="g-plusone" data-size="medium" data-annotation="none"></div></div></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="container-fluid">
|
||||
|
||||
<div class="row-fluid">
|
||||
<div class="span12">
|
||||
<div id="map" class="map"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row-fluid">
|
||||
|
||||
<div class="span12">
|
||||
<h4 id="title">d3 integration example</h4>
|
||||
<p id="shortdesc">Example of using ol3 and d3 together.</p>
|
||||
<div id="docs">
|
||||
<p>The example loads TopoJSON geometries and uses d3 (<code>d3.geo.path</code>) to render these geometries to a canvas element that is then used as the image of an ol3 image layer.</p>
|
||||
<p>See the <a href="d3.js" target="_blank">d3.js source</a> to see how this is done.</p>
|
||||
</div>
|
||||
<div id="tags">d3</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script src="http://d3js.org/d3.v3.min.js"></script>
|
||||
<script src="http://d3js.org/topojson.v1.min.js"></script>
|
||||
<script src="loader.js?id=d3" type="text/javascript"></script>
|
||||
<script src="../resources/social-links.js" type="text/javascript"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Vendored
+95
@@ -0,0 +1,95 @@
|
||||
// NOCOMPILE
|
||||
// this example uses d3 for which we don't have an externs file.
|
||||
goog.require('ol');
|
||||
goog.require('ol.Map');
|
||||
goog.require('ol.View2D');
|
||||
goog.require('ol.extent');
|
||||
goog.require('ol.layer.Image');
|
||||
goog.require('ol.layer.Tile');
|
||||
goog.require('ol.proj');
|
||||
goog.require('ol.source.ImageCanvas');
|
||||
goog.require('ol.source.Stamen');
|
||||
|
||||
|
||||
var map = new ol.Map({
|
||||
layers: [
|
||||
new ol.layer.Tile({
|
||||
source: new ol.source.Stamen({
|
||||
layer: 'watercolor'
|
||||
})
|
||||
})
|
||||
],
|
||||
renderers: ['canvas'],
|
||||
target: 'map',
|
||||
view: new ol.View2D({
|
||||
center: ol.proj.transform([-97, 38], 'EPSG:4326', 'EPSG:3857'),
|
||||
zoom: 4
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
/**
|
||||
* Load the topojson data and create an ol.layer.Image for that data.
|
||||
*/
|
||||
d3.json('data/us.json', function(error, us) {
|
||||
var features = topojson.feature(us, us.objects.counties);
|
||||
|
||||
/**
|
||||
* This function uses d3 to render the topojson features to a canvas.
|
||||
* @param {ol.Extent} extent Extent.
|
||||
* @param {number} resolution Resolution.
|
||||
* @param {ol.Size} size Size.
|
||||
* @param {ol.proj.Projection} projection Projection.
|
||||
* @return {HTMLCanvasElement}
|
||||
*/
|
||||
var canvasFunction = function(extent, resolution, size, projection) {
|
||||
var canvasWidth = size[0];
|
||||
var canvasHeight = size[1];
|
||||
|
||||
var canvas = d3.select(document.createElement('canvas'));
|
||||
canvas.attr('width', canvasWidth).attr('height', canvasHeight);
|
||||
|
||||
var context = canvas.node().getContext('2d');
|
||||
|
||||
var d3Projection = d3.geo.mercator().scale(1).translate([0, 0]);
|
||||
var d3Path = d3.geo.path().projection(d3Projection);
|
||||
|
||||
var pixelBounds = d3Path.bounds(features);
|
||||
var pixelBoundsWidth = pixelBounds[1][0] - pixelBounds[0][0];
|
||||
var pixelBoundsHeight = pixelBounds[1][1] - pixelBounds[0][1];
|
||||
|
||||
var geoBounds = d3.geo.bounds(features);
|
||||
var geoBoundsLeftBottom = ol.proj.transform(
|
||||
geoBounds[0], 'EPSG:4326', projection);
|
||||
var geoBoundsRightTop = ol.proj.transform(
|
||||
geoBounds[1], 'EPSG:4326', projection);
|
||||
var geoBoundsWidth = geoBoundsRightTop[0] - geoBoundsLeftBottom[0];
|
||||
if (geoBoundsWidth < 0) {
|
||||
geoBoundsWidth += ol.extent.getWidth(projection.getExtent());
|
||||
}
|
||||
var geoBoundsHeight = geoBoundsRightTop[1] - geoBoundsLeftBottom[1];
|
||||
|
||||
var widthResolution = geoBoundsWidth / pixelBoundsWidth;
|
||||
var heightResolution = geoBoundsHeight / pixelBoundsHeight;
|
||||
var r = Math.max(widthResolution, heightResolution);
|
||||
var scale = r / (resolution / pixelRatio);
|
||||
|
||||
var center = ol.proj.transform(ol.extent.getCenter(extent),
|
||||
projection, 'EPSG:4326');
|
||||
d3Projection.scale(scale).center(center)
|
||||
.translate([canvasWidth / 2, canvasHeight / 2]);
|
||||
d3Path = d3Path.projection(d3Projection).context(context);
|
||||
d3Path(features);
|
||||
context.stroke();
|
||||
|
||||
return canvas[0][0];
|
||||
};
|
||||
|
||||
var layer = new ol.layer.Image({
|
||||
source: new ol.source.ImageCanvas({
|
||||
canvasFunction: canvasFunction,
|
||||
projection: 'EPSG:3857'
|
||||
})
|
||||
});
|
||||
map.addLayer(layer);
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
{"type":"Topology","objects":{"counties":{"type":"GeometryCollection","geometries":[{"type":"Polygon","id":53073,"arcs":[[0,1,2,3]]},{"type":"Polygon","id":30105,"arcs":[[4,5,6,7,8,9]]},{"type":"Polygon","id":30029,"arcs":[[10,11,12,13,14,15,16,17,18,19]]},{"type":"Polygon","id":16021,"arcs":[[20,21,22,23]]},{"type":"Polygon","id":30071,"arcs":[[-9,24,25,26,27,28,29]]},{"type":"Polygon","id":38079,"arcs":[[30,31,32,33]]},{"type":"Polygon","id":30053,"arcs":[[-19,34,35,-21,36]]},{"type":"Polygon","id":38009,"arcs":[[-32,37,38,39,40]]},{"type":"Polygon","id":30035,"arcs":[[41,42,-11,43]]},{"type":"Polygon","id":30041,"arcs":[[44,45,46,47]]},{"type":"Polygon","id":30005,"arcs":[[-29,-28,48,49,-48,50]]},{"type":"Polygon","id":30019,"arcs":[[51,52,-5,53]]},{"type":"Polygon","id":38067,"arcs":[[54,55,56,57]]},{"type":"Polygon","id":27069,"arcs":[[58,59,-55,60]]},{"type":"Polygon","id":38095,"arcs":[[61,62,63,-34,64,65]]},{"type":"Polygon","id":38019,"arcs":[[-57,66,67,-66,68]]},{"type":"Polygon","id":53047,"arcs":[[69,70,71,72,73,74,-1,75]]},{"type":"Polygon","id":53065,"arcs":[[76,77,78,79,80]]},{"type":"Polygon","id":53051,"arcs":[[-23,81,82,-77,83]]},{"type":"Polygon","id":53019,"arcs":[[-80,84,-70,85]]},{"type":"Polygon","id":30051,"arcs":[[86,87,88,-46,89]]},{"type":"Polygon","id":38023,"arcs":[[90,91,92,93]]},{"type":"Polygon","id":38013,"arcs":[[94,95,96,97,-91,98]]},{"type":"Polygon","id":30101,"arcs":[[99,-88,100,-42]]},{"type":"Polygon","id":38075,"arcs":[[101,102,-95,103,-40]]},{"type":"Polygon","id":27135,"arcs":[[104,105,-59,106,107]]},{"type":"Polygon","id":30091,"arcs":[[-93,108,109,-52,110]]},{"type":"Polygon","id":16017,"arcs":[[-36,111,112,113,114,-82,-22]]},{"type":"Polygon","id":38101,"arcs":[[-103,115,116,117,-96]]},{"type":"MultiPolygon","id":53055,"arcs":[[[118]],[[119]],[[120]]]},{"type":"Polygon","id":27071,"arcs":[[121,122,123,124,125]]},{"type":"Polygon","id":53057,"arcs":[[126,-2,-75,127,128,129]]},{"type":"Polygon","id":38105,"arcs":[[-98,130,131,132,-109,-92]]},{"type":"Polygon","id":38049,"arcs":[[133,134,135,-116,-102,-39]]},{"type":"Polygon","id":27137,"arcs":[[136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,-122,151]]},{"type":"Polygon","id":30085,"arcs":[[-110,-133,152,153,-6,-53]]},{"type":"Polygon","id":53007,"arcs":[[-74,154,155,156,157,-128]]},{"type":"Polygon","id":38061,"arcs":[[158,159,160,-131,-97,-118]]},{"type":"Polygon","id":27089,"arcs":[[161,162,163,164,165,-60,-106]]},{"type":"Polygon","id":38069,"arcs":[[-64,166,167,168,-134,-38,-31]]},{"type":"Polygon","id":38071,"arcs":[[169,170,171,-62,-68]]},{"type":"Polygon","id":38099,"arcs":[[-56,-166,172,173,-170,-67]]},{"type":"Polygon","id":27007,"arcs":[[-124,174,175,176,177,178,-162,-105,179]]},{"type":"Polygon","id":30073,"arcs":[[-101,-87,180,181,-12,-43]]},{"type":"MultiPolygon","id":53029,"arcs":[[[182,183]],[[184]]]},{"type":"Polygon","id":53009,"arcs":[[185,186,187]]},{"type":"Polygon","id":38005,"arcs":[[-63,-172,188,189,190,-167]]},{"type":"Polygon","id":30015,"arcs":[[-50,191,192,193,194,-181,-90,-45]]},{"type":"Polygon","id":53061,"arcs":[[-158,195,196,-183,197,-129]]},{"type":"Polygon","id":30089,"arcs":[[-18,198,199,200,201,-112,-35]]},{"type":"Polygon","id":27075,"arcs":[[202,203,-137,204]]},{"type":"Polygon","id":38063,"arcs":[[205,206,207,208,-189,-171,-174]]},{"type":"Polygon","id":38035,"arcs":[[-165,209,210,211,-206,-173]]},{"type":"Polygon","id":27119,"arcs":[[212,213,214,215,216,217,218,-210,-164]]},{"type":"Polygon","id":27113,"arcs":[[-179,219,-215,220,-213,-163]]},{"type":"Polygon","id":30083,"arcs":[[221,222,223,224,-153]]},{"type":"Polygon","id":53017,"arcs":[[225,226,-155,-73]]},{"type":"Polygon","id":38053,"arcs":[[-161,227,228,229,230,-222,-132]]},{"type":"Polygon","id":53031,"arcs":[[231,232,233,-186,234]]},{"type":"Polygon","id":30099,"arcs":[[-195,235,236,-13,-182]]},{"type":"Polygon","id":30055,"arcs":[[-225,237,238,239,-7,-154]]},{"type":"Polygon","id":16079,"arcs":[[-202,240,241,242,243,244,-113]]},{"type":"Polygon","id":30047,"arcs":[[245,-199,-17]]},{"type":"Polygon","id":53063,"arcs":[[-83,-115,246,247,248,249,-78]]},{"type":"Polygon","id":27029,"arcs":[[250,251,252,-216,-220,-178]]},{"type":"Polygon","id":16055,"arcs":[[-245,253,-247,-114]]},{"type":"Polygon","id":30033,"arcs":[[-240,254,255,256,257,-25,-8]]},{"type":"Polygon","id":27125,"arcs":[[-214,-221]]},{"type":"Polygon","id":53025,"arcs":[[-72,258,259,260,261,262,263,-226]]},{"type":"Polygon","id":53043,"arcs":[[-85,-79,-250,264,265,-259,-71]]},{"type":"Polygon","id":30049,"arcs":[[266,267,268,269,270,-14,-237]]},{"type":"MultiPolygon","id":53035,"arcs":[[[271]],[[272,273,274,275,276]]]},{"type":"Polygon","id":27061,"arcs":[[-151,277,278,-175,-123]]},{"type":"Polygon","id":38055,"arcs":[[279,280,281,282,283,-159,-117,-136]]},{"type":"Polygon","id":38027,"arcs":[[-209,284,285,286,-190]]},{"type":"Polygon","id":38103,"arcs":[[-191,-287,287,288,289,290,-168]]},{"type":"Polygon","id":38083,"arcs":[[-169,-291,291,292Line truncated
|
||||
Reference in new issue
Block a user