Published
Edited
Oct 19, 2020
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
map = svg`<svg viewBox="0 0 ${width} ${height}">
<g class="basemap-layer">
${tiles.map(
([x, y, z], i, { translate: [tx, ty], scale: k }) => svg`
<image xlink:href="${getTonerTileUrl(x, y, z)}" x="${Math.round(
(x + tx) * k
)}" y="${Math.round((y + ty) * k)}" width="${k}" height="${k}">
`
)}
</g>
<g class="data-layer">
${tiles.map(
([x, y, z], i, { translate: [tx, ty], scale: k }) => svg`
<image xlink:href="${getTileUrl(x, y, z)}" x="${Math.round(
(x + tx) * k
)}" y="${Math.round((y + ty) * k)}" width="${k}" height="${k}">
`
)}
</g>
</svg>`
Insert cell
Insert cell
getTonerTileUrl = (x, y, z) =>
`https://stamen-tiles-${
"abc"[Math.abs(x + y) % 3]
}.a.ssl.fastly.net/toner-lite/${z}/${x}/${y}${
devicePixelRatio > 1 ? "@2x" : ""
}.png`
Insert cell
tiles = tile()
Insert cell
tile = d3
.tile()
.size([width, height])
.scale(projection.scale() * 2 * Math.PI)
.translate(projection([0, 0]))
Insert cell
projection = d3
.geoMercator()
.center(center)
.scale(Math.pow(2, zoom) / (2 * Math.PI))
.translate([width / 2, height / 2])
Insert cell
center = [-74.006, 40.7128]
Insert cell
height = 600
Insert cell
Insert cell
test = getTileUrl(1, 6, 3)
Insert cell
getTileUrl = (x, y, z) => {
const { layergroupid, cdn_url } = mapsApiResult;
const {
templates: { https }
} = cdn_url;
const baseURL = https.url.replace(
"{s}",
https.subdomains[Math.abs(x + y) % https.subdomains.length]
);
return `${baseURL}/${cartoUserName}/api/v1/map/${layergroupid}/${z}/${x}/${y}${
devicePixelRatio > 1 ? "@2x" : ""
}.png`;
}
Insert cell
layerUrl = mapsApiResult.metadata.url.raster.urlTemplate
Insert cell
mapsApiResult = {
const res = await fetch(
`${mapsApiUrl}?config=${encodeURIComponent(JSON.stringify(mapsApiConfig))}`
);
return await res.json();
}
Insert cell
mapsApiConfig = ({
layers: [
{
type: "cartodb",
options: {
sql,
cartocss: cartoCSS,
cartocss_version: "2.1.0"
}
}
]
})
Insert cell
sql = `select the_geom_webmercator from ${table}`
Insert cell
table = "map_pluto_likely_rs_2016v1"
Insert cell
// CartoCSS docs: https://carto.com/developers/styling/cartocss/
cartoCSS = geomType === "Points"
? `#layer {
marker-width: 7;
marker-fill: orange;
marker-fill-opacity: 1;
marker-allow-overlap: true;
marker-line-width: 1;
marker-line-color: #FFFFFF;
marker-line-opacity: 1;
}`
: `#layer {
polygon-fill: orange;
polygon-opacity: 1;
line-width: 0;
}`
Insert cell
mapsApiUrl = `https://${cartoUserName}.carto.com/api/v1/map/`
Insert cell
cartoUserName = "chenrick"
Insert cell
Insert cell
import { radio, slider } from "@jashkenas/inputs"
Insert cell
d3 = require("d3-geo@2", "d3-tile@1")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more