Testing @mourner's polylabel with D3.
Orange dot = centroid ; white dot = normal polylabel; green dot = elliptic polylabel.
In this variant from Polylabel with D3 we set different scales for x and y, so as to find the "largest ellipse" to accomodate "horizontal" text labels (the ellipse ratio would match the text bbox ratio).
Built with blockbuilder.org, base map by @mbostock.
xxxxxxxxxx
<meta charset="utf-8">
<style>
.stroke {
fill: none;
stroke: #000;
stroke-width: 3px;
}
.fill {
fill: #fff;
}
.graticule {
fill: none;
stroke: #777;
stroke-width: 0.5px;
stroke-opacity: 0.5;
}
.land {
fill: #ccc;
}
.boundary {
fill: none;
stroke: #555;
stroke-width: 0.5px;
}
</style>
<svg width="960" height="484"></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/d3-geo-projection.v2.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script src="polylabel.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
var projection = d3.geoRobinson()
.rotate([-10.5,0])
.translate([width / 2, height / 2])
.precision(0.1);
var graticule = d3.geoGraticule();
var path = d3.geoPath()
.projection(projection);
var defs = svg.append("defs");
defs.append("path")
.datum({type: "Sphere"})
.attr("id", "sphere")
.attr("d", path);
defs.append("clipPath")
.attr("id", "clip")
.append("use")
.attr("xlink:href", "#sphere");
svg.append("use")
.attr("class", "stroke")
.attr("xlink:href", "#sphere");
svg.append("use")
.attr("class", "fill")
.attr("xlink:href", "#sphere");
svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("clip-path", "url(#clip)")
.attr("d", path);
var lines = svg.append("g"),
circles = svg.append("g");
console.clear()
d3.json("https://unpkg.com/visionscarto-world-atlas@0.0.6/world/110m.json", function(error, world) {
if (error) throw error;
if (true)
svg.insert("path", ".graticule")
.datum(topojson.feature(world, world.objects.countries))
.attr("class", "land")
.attr("clip-path", "url(#clip)")
.attr("d", path);
if (true)
svg.insert("path", ".graticule")
.datum(topojson.mesh(world, world.objects.countries, function(a, b) { return a !== b; }))
.attr("class", "boundary")
.attr("clip-path", "url(#clip)")
.attr("d", path);
var w = topojson.feature(world, world.objects.countries);
w.features.forEach(function(d, i){
// get the larget sub-polygon's coordinates
var coord = d.geometry.coordinates;
if (d.geometry.type == 'MultiPolygon') {
var u = d3.scan(
coord.map(function(p){
return -d3.polygonArea(p[0]);
})
);
coord = coord[u];
}
var o = projection(d3.geoCentroid(d));
var n = polylabel([coord[0].map(projection)]);
// Find the best *ellipse* with x/y ratio = alpha
var alpha = 4;
var m = polylabel([coord[0].map(projection).map(function(d){
d[0] /= alpha;
return d;
})]);
m[0] *= alpha;
lines.append('line')
.attr('x1', o[0])
.attr('y1', o[1])
.attr('x2', n[0])
.attr('y2', n[1])
.attr('stroke', 'green')
.attr('stroke-width', 1);
lines.append('line')
.attr('x1', o[0])
.attr('y1', o[1])
.attr('x2', m[0])
.attr('y2', m[1])
.attr('stroke', 'green')
.attr('stroke-width', 1);
circles.append('circle')
.attr('cx', o[0])
.attr('cy', o[1])
.attr('r', 2)
.attr('stroke', 'orange')
.attr('fill', 'yellow')
.attr('stroke-width', 1);
circles.append('circle')
.attr('cx', n[0])
.attr('cy', n[1])
.attr('r', 2)
.attr('stroke', 'green')
.attr('fill', 'white')
.attr('stroke-width', 1);
circles.append('circle')
.attr('cx', m[0])
.attr('cy', m[1])
.attr('r', 3)
.attr('stroke', 'green')
.attr('fill', 'green');
});
});
</script>
https://d3js.org/d3.v4.min.js
https://d3js.org/d3-geo-projection.v2.min.js
https://d3js.org/topojson.v1.min.js