Published
Edited
Jan 28, 2022
4 stars
Insert cell
Insert cell
Insert cell
tile()[0]
Insert cell
w2 = width * 4
Insert cell
projection = d3
.geoMercator()
.center([-68.4, -23.6])
.scale(800000 / (2 * Math.PI))
.translate([width / 2, height / 2])
Insert cell
splitZmap = svg`<svg viewBox="0 0 ${width} ${height}">
<g id ='hillshade'>
${classedSplitZ.map(
(h) =>
`<path d ='M${[h.a.x, h.a.y]}L${[
h.b.x,
h.b.y
]}' stroke="gray" stroke-opacity="0.5"/>`
)}
</g>
<g id ='water'>
${segment_water.map((s) => {
let color = viridish[Math.floor(Math.random() * viridish.length)];
return s.map(
(w) =>
` <path d ='M${[w.a.x, w.a.y]}L${[
w.b.x,
w.b.y
]}' stroke="${color}" stroke-opacity="0.5"/>`
);
})}
</g>
<g id = 'water_outlines'>
${poly_water
.flat()
.map((p) => `<path d="${path(p)}" stroke="teal" fill="none"/>`)}
</g>
</svg>`
Insert cell
Insert cell
poly.flat()
Insert cell
classedSplitZ = diss
.map((h) =>
h.features
.map((m) =>
path(m)
.split("Z")
.map((s) => Polygon.fromPath(s))
)
.flat()
)
.flat()
.map((m) => m.getHatches(PI / 3, 2, 0))
.flat()
Insert cell
hillshade = tiles
.filter((t) => t.data.hillshade)
.map((h) => h.data.hillshade)
.map((m) => m.features)
.flat()
Insert cell
diss = poly.map((p) => turf.dissolve(turf.featureCollection(p))).flat()
Insert cell
poly = hs_nest
.map((e) =>
e.values.map((v) =>
v.values
.map((h) => {
if (h.geometry.type != "Polygon") {
return turf.flatten(h).features.flat();
} else {
return h;
}
})
.flat()
)
)
.flat()
Insert cell
waterSplitZ = poly_water
.map((h) =>
h.features
.map(
(m) => Polygon.fromPath(path(m))
// .split("Z")
// .map((s) => Polygon.fromPath(s))
)
.flat()
)
.flat()
.map((m) => m.getHatches(-PI / 3, 1, 0))
.flat()
Insert cell
segment_water = poly_water
.map((h) =>
h.features
.map((m) => Polygon.fromPath(path(m)))
.map((m) => m.getHatches(-PI / 3, 1.5, 0))
)
.flat()
Insert cell
poly_water = water_nest
.map((e) =>
e.values
.map((v) => {
if (v.geometry.type != "Polygon") {
return turf.flatten(v).features.flat();
} else {
return v;
}
})
.flat()
)
.map((m) => turf.dissolve(turf.featureCollection(m)))
Insert cell
hs_nest = d3
.nest()
.key(function (d) {
return d.properties.class;
})
.key(function (d) {
return d.properties.level;
})
.entries(hillshade)
Insert cell
water_nest = d3
.nest()
.key(function (d) {
return d.properties.kind;
})
.entries(geom_water)
Insert cell
geom_water = water
.filter((m) => m.features.length > 0)
.map((m) => m.features)
.flat()
.filter((f) => f.geometry.type != "MultiLineString")
.filter((f) => f.geometry.type != "LineString")
.filter((f) => f.geometry.type != "Point")
// .filter((f) => f.properties.boundary == true)
Insert cell
tiles = Promise.all(
tile().map(async (t) => {
let data = new vt.VectorTile(
new Protobuf(
await d3.buffer(
`https://a.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2/${t.z}/${t.x}/${t.y}.mvt?access_token=pk.eyJ1Ijoid2FzaGluZ3RvbnBvc3QiLCJhIjoibWJkTGx1SSJ9.6cMdwgs-AYrRtQsEkXlHqg`
)
)
).layers;

t.data = Object.keys(data).reduce((initialData, key) => {
initialData[key] = {
type: "FeatureCollection",
features: [...Array(data[key].length).keys()].map((item, i) => {
let feature = data[key].feature(i).toGeoJSON(t.x, t.y, t.z);
return feature;
})
};

return initialData;
}, {});

return t;
})
)
Insert cell
tile().map(
(t) =>
`https://a.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2/${t.z}/${t.x}/${t.y}.mvt?access_token=pk.eyJ1Ijoid2FzaGluZ3RvbnBvc3QiLCJhIjoibWJkTGx1SSJ9.6cMdwgs-AYrRtQsEkXlHqg`
)[0]
Insert cell
water = Promise.all(
tile().map(async (d) => {
d.data = await d3.json(
`https://tile.nextzen.org/tilezen/vector/v1/256/all/${d.z}/${d.x}/${d.y}.json?api_key=ztkh_UPOQRyakWKMjH_Bzg`
);
return d.data.water;
})
)
Insert cell
tile().map(
(d) =>
`https://tile.nextzen.org/tilezen/vector/v1/256/all/${d.z}/${d.x}/${d.y}.json?api_key=ztkh_UPOQRyakWKMjH_Bzg`
)[0]
Insert cell
Protobuf = require("pbf@3/dist/pbf.js")
Insert cell
d3 = require("d3-geo@1", "d3-selection@1", "d3-fetch@1", "d3-tile@0.0", "d3-collection@latest")
Insert cell
tile = d3.tile()
.size([width, height])
.scale(projection.scale() * 2 * Math.PI)
.translate(projection([0,0]))
Insert cell
height = 800
Insert cell
path = d3.geoPath(projection)
Insert cell
clip = require("polygon-clipping@0.15.3")
Insert cell
turf = require("@turf/turf@latest")
Insert cell
vt = require('https://bundle.run/@mapbox/vector-tile@1.3.1')
Insert cell
Insert cell
xyz = require("https://bundle.run/xyz-affair@0.9.1")
Insert cell
Insert cell
bounds = [
[-69.04083251953125, -23.96617587126503],
[-67.379150390625, -22.92804166565175]
]
Insert cell
xyz(bounds, 11)
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