Published
Edited
Feb 10, 2020
Importers
Insert cell
Insert cell
Insert cell
Insert cell
drawmap = () => {
const svg = d3.create('svg')
.style('display', 'block')
.style('margin', `0 ${sizes.margin}px`)
.style('height', `${sizes.height}px`)
.style('width', `${sizes.width}px`)
.style('background', '#fff')
const path = d3.geoPath().projection(projection)
svg
.append('g')
.selectAll('path')
.data(topojson.feature(topology, topology.objects.states).features)
.enter()
.append('path')
.attr('d', path)
return svg.node()
}
Insert cell
function geoWesternUsa (){
const epsilon = 1e-6
var cache,
cacheStream,
lower48 = d3.geoConicEqualArea()
.parallels([29.5, 45.5])
.scale(1070)
.translate([400, 269])
.rotate([108, 0])
.center([7, 38.7]),
lower48point,
alaska = d3.geoConicEqualArea().rotate([145, 0]).center([-4, 58.5]).parallels([55, 65]), // EPSG:3338
alaskaPoint,
hawaii = d3.geoConicEqualArea().rotate([157, 0]).center([-8, 19.9]).parallels([8, 18]), // ESRI:102007
hawaiiPoint,
point,
pointStream = {
point: (x,y) => {
point = [x, y]
}
}
function westernUsa(coordinatesarr) {
var [x, y] = coordinatesarr
return point = null,
(lower48point.point(x, y), point) ||
(alaskaPoint.point(x, y), point) ||
(hawaiiPoint.point(x, y), point)
}
function reset () {
cache = cacheStream = null
return westernUsa
}
// westernUsa.fitHeight = function(height, object) {
// return fitHeight(westernUsa, height, object)
// }
// westernUsa.fitSize = function(size, object) {
// return fitSize(westernUsa, size, object)
// }
// westernUsa.fitWidth = function(width, object) {
// return fitWidth(westernUsa, width, object)
// }
// westernUsa.fitExtent = function(extent, object) {
// return fitExtent(westernUsa, extent, object)
// }
westernUsa.invert = function(coordinatesarr) {
var k = lower48.scale(),
[xt, yt] = lower48.translate(),
[xc, yc] = coordinatesarr,
x = (xc - xt) / k,
y = (yc - yt) / k
return (y >= 0.120 && y < 0.234 && x >= -0.425 && x < -0.214 ? alaska
: y >= 0.166 && y < 0.234 && x >= -0.214 && x < -0.115 ? hawaii
: lower48).invert(coordinatesarr)
}
westernUsa.stream = function(stream) {
return cache &&
(cacheStream === stream)
? cache
: cache = multiplex([
lower48.stream(cacheStream = stream),
alaska.stream(stream),
hawaii.stream(stream)
])
}
westernUsa.precision = function(_) {
if ( !arguments.length) {
return lower48.precision()
}
lower48.precision(_), alaska.precision(_), hawaii.precision(_)
return reset()
}
westernUsa.scale = function(k) {
if ( !arguments.length) {
return lower48.scale()
}
lower48.scale(k), alaska.scale(k * alaskascalar), hawaii.scale(k)
return westernUsa.translate(lower48.translate())
}
westernUsa.translate = function(t) {
if (!arguments.length) {
return lower48.translate()
}
var k = lower48.scale(),
[x, y] = t
x = +x
y = +y
lower48point = lower48
.translate([x + .25 * k, y])
.clipExtent(
[[x - 0.455 * k, y - 0.238 * k],
[x + 0.455 * k, y + 0.238 * k]])
.stream(pointStream)

alaskaPoint = alaska
.translate([x - 0.17 * k, y + 0.08 * k]) // will have to change
.clipExtent(
[[x - 1 * k + epsilon, y - k + epsilon],
[x - 0 * k - epsilon, y + k - epsilon]])
.stream(pointStream)

hawaiiPoint = hawaii
.translate([x - .190 * k, y + 0.212 * k])
.clipExtent(
[[x - 0.214 * k + epsilon, y + 0.166 * k + epsilon],
[x + 0.115 * k - epsilon, y + 0.234 * k - epsilon]])
.stream(pointStream)

return reset()
}
return westernUsa.scale(900)
}
Insert cell
function multiplex(streams) {
const {length} = streams
const fnns = ['sphere', 'lineStart', 'lineEnd', 'polygonStart', 'polygonEnd']
function pointfn (x, y) {
var i = -1
while (++i < length) {
streams[i].point(x, y)
}
}
const o = Object.fromEntries(fnns.map(name => [name, function () {
var i = -1
while (++i < length) {
streams[i][name]()
}
}]))
o.point = pointfn
return o
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
d3g = require('d3-geo')
Insert cell
Insert cell
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more