d3.orthoLabel = function() { var falseSVG = d3.select(document.createElementNS(d3.namespaces.svg, 'svg')); // projection variables: var centroid = [0,0]; var scale = 1000; var projection = d3.geoAzimuthalEquidistant() .translate([0,0]); // path variables var pathScale = 1; var distance = 1; var features = []; function orthoLabel() { } // // Set Geographic Centroid // orthoLabel.geoCentroid = function(c) { if (c) { centroid[0] = -c[0]; centroid[1] = -c[1]; projection.rotate(centroid); return orthoLabel; } else { return centroid; } } // // Set Geographic Zoom // orthoLabel.geoScale = function(z) { if (z) { scale = z; projection.scale(scale); return orthoLabel; } else { return centroid; } } /////// // // Set Path Scale // orthoLabel.pathScale = function(s) { if (s) { pathScale = s; return orthoLabel; } else { return pathScale; } } // // Set distance between sampled points // orthoLabel.distance = function(i) { if (i) { distance = i; return orthoLabel; } else { return distance; } } // // Take a selection and get geojson representing path: // orthoLabel.selection = function(selection) { features=[]; selection.each(function(d) { var points = []; var d = distance; var i = 0; var node = d3.select(this).node(); var l = node.getTotalLength(); var n = l / d; var p0 = node.getPointAtLength(i * d); p0 = projection.invert([p0.x*pathScale,p0.y*pathScale]) while (i < n) { var p = node.getPointAtLength(i * d); points.push(projection.invert([p.x*pathScale,p.y*pathScale])); i++; } points.push(p0); // Check to see if the geojson follows the right hand rule. Given context assuming the antipode is not intended to be in the feature. var feature = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[points]},"properties":null}; if(d3.geoContains(feature,centroid)) { points.reverse(); feature = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[points]},"properties":null}; } // add to features array features.push(feature); }) return orthoLabel; } // // Get a path(s) (.attr("d")) and turn it to geojson. // orthoLabel.path = function(path) { features=[]; path = falseSVG.append("path") .attr("d",path); var points = []; var d = distance; var i = 0; var node = path.node(); var l = node.getTotalLength(); var n = l / d; var t = function(a) { return a }; var p0 = node.getPointAtLength(i * d); p0 = projection.invert([p0.x*pathScale,p0.y*pathScale]) while (i < n) { var p = node.getPointAtLength(i * d); points.push(projection.invert([p.x*pathScale,p.y*pathScale])); i++; } points.push(p0); // Check to see if the geojson follows the right hand rule. Given context assuming the antipode is not intended to be in the feature. var feature = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[points]},"properties":null}; if(d3.geoContains(feature,centroid)) { points.reverse(); feature = {"type":"Feature","geometry":{"type":"Polygon","coordinates":[points]},"properties":null}; } // add to features array features.push(feature); return orthoLabel; } orthoLabel.features = function() { return features; } orthoLabel.featureCollection = function() { return {"type":"FeatureCollection","features":features} } return orthoLabel; }