Public
Edited
Dec 18, 2023
1 fork
1 star
Insert cell
Insert cell
xyz_tiles = xyz(bounds, 6)
Insert cell
map = svg`<svg viewBox="0 0 ${width} ${height}">
<g id="water">


${segment_water.map((m) =>
m.map(
(h, i) =>
`<g id = "hatch${i}">
<path d ='M${[h.a.x, h.a.y]}L${[
h.b.x,
h.b.y
]}' stroke-opacity="0.3" stroke="black"/>
</g>`
)
)}

</g>
<g id="twborders">
${tw.features.map((m) => `<path d =${path(m)} fill="none" />`)}
)}
</g>
<g id = "hillshade">
${classedSplitZ.map(
(h, i) => `<path d ='M${[h.a.x, h.a.y]}L${[h.b.x, h.b.y]}' stroke="black"/>`
)}
</g>
<g id = "parks">
${science_parks.features.map(
(h, i) =>
`<circle fill="red" cx = ${path.centroid(h)[0]} cy = "${
path.centroid(h)[1]
}" r= "2" id = "${h.properties.name_en}"></circle>`
)}
${addedscienceparks.features.map(
(a, i) =>
`<circle fill="red" cx = ${projection(a.geometry.coordinates)[0]} cy = "${
projection(a.geometry.coordinates)[1]
}" r= "2" id = "${a.properties.name_en}"></circle>`
)}
</g>

</svg>`
Insert cell
clipFlat = clipped
.map((c) =>
c
.map((m) => turf.flatten(m))
.map((l) => l.features)
.flat()
.map((m) => turf.rewind(m))
)
.map((c) =>
turf
.dissolve(turf.featureCollection(c))
.features.map((m) => turf.rewind(m, { reverse: true }))
)
Insert cell
turf.tesselate(water_rewind.features[0])
Insert cell
rewind = turf.rewind(site, { reverse: true })
Insert cell
bounds = [
site.geometry.coordinates[0][0],
site.geometry.coordinates[0][2]
]
Insert cell
site = zoomed_bbox
Insert cell
zoomed_bbox = turf.bboxPolygon(
turf.bbox(
turf.buffer(turf.bboxPolygon(turf.bbox(tw)), 2, {
unit: "miles"
})
)
)
Insert cell
counties10t = FileAttachment("counties-10t.json").json()
Insert cell
hsinchu = turf.combine(
turf.featureCollection(
topojson
.feature(counties10t, counties10t.objects.counties)
.features.filter((f) => f.properties.COUNTYENG.includes("Hsinchu City"))
)
)
Insert cell
kaohsiung = turf.combine(
turf.featureCollection(
topojson
.feature(counties10t, counties10t.objects.counties)
.features.filter((f) => f.properties.COUNTYENG.includes("Kaohsiung"))
)
)
Insert cell
tw = topojson.feature(counties10t, counties10t.objects.nation)
Insert cell
geojson = FileAttachment("map(12).geojson").json()
Insert cell
ctr = turf.center(site)
Insert cell
projection = d3
.geoMercator()
.center(turf.center(twFrame.features[0]).geometry.coordinates)
.translate([0, 0])
.fitSize([width, height], rewind)
Insert cell
tessel
Insert cell
manual_union = [
turf.rewind(
turf.union(turf.difference(geom_water[3], geom_water[6]), geom_water[4]),
{ reverse: true }
),
turf.rewind(
turf.union(geom_water[6], turf.difference(geom_water[7], geom_water[4])),
{ reverse: true }
)
]
Insert cell
segment_water = tessel.features
.map((m) =>
path(m)
.split("M")
.filter((f) => f != "")
.map((m) => "M" + m)
.flat()
.map((s) => Polygon.fromPath(s))
)
.flat()
.flat()
.map((m, i) => m.getHatches(hatchpattern(i), 6, 0))
.filter((f) => f.length > 0)
Insert cell
water_nest = d3
.nest()
.key(function (d) {
return d.properties.kind;
})
.entries(geom_water)
Insert cell
geom_water = mb_water
.map((m) => m.water)
.filter((m) => m != undefined)
.map((m) => m.features)
.flat()
.filter((f) => f.geometry.type != "MultiLineString")
.filter((f) => f.geometry.type != "LineString")
.filter((f) => f.geometry.type != "Point")
.flat()
// .map((o) => turf.intersect(o, zoomed_bbox))
// .map((m) => turf.rewind(m, { reverse: true }))
Insert cell
biggerframe = xyz(
[twFrame.features[0].geometry.coordinates[0][0],
twFrame.features[0].geometry.coordinates[0][2]],
7
)
Insert cell
(twFrame.features[0].geometry.coordinates[0][0],
twFrame.features[0].geometry.coordinates[0][2])
Insert cell
twFrame = FileAttachment("tw-frame.geojson").json()
Insert cell
mb_water = Promise.all(
biggerframe.map(async (t) => {
let data = new vt.VectorTile(
new Protobuf(
await d3.buffer(
`https://api.mapbox.com/v4/mapbox.mapbox-streets-v8/${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.data;
})
)
Insert cell
hatchpattern = function (i) {
if (i % 2 == 0) {
return PI / 3
} else {
return -(PI / 3)
}
}
Insert cell
water_rewind = turf.rewind(
turf.flatten(geom_water.reduce((p, m) => turf.union(p, m))),
{ reverse: true }
)
Insert cell
tessel = turf.rewind(turf.tesselate(water_rewind.features[0]), {
reverse: true
})
Insert cell
classedSplitZ = clipFlat
.map(
(f, i) =>
f.map((m) =>
path(m)
.split("Z")
.map((s) => Polygon.fromPath(s))
.map((m) => m.getHatches(-PI / 3, 1.5, 0))
)

// .flat()
// .flat()
//
//
)
.flat()
.flat()
.filter((f) => f.length > 0)
.flat()
Insert cell
tiles = Promise.all(
xyz_tiles.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.eyJ1IjoibGlmZXdpbm5pbmciLCJhIjoiYWZyWnFjMCJ9.ksAPTz72HyEjF2AOMbRNvg`
)
)
).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
tileArr = Promise.all(
xyz_tiles.map(
(t) =>
`https://a.tiles.mapbox.com/v4/mapbox.mapbox-terrain-v2/${t.z}/${t.x}/${t.y}.mvt?access_token=pk.eyJ1IjoibGlmZXdpbm5pbmciLCJhIjoiYWZyWnFjMCJ9.ksAPTz72HyEjF2AOMbRNvg`
)
)
Insert cell
height = 800
Insert cell
hillshade = tiles
.filter((t) => Object.keys(t.data).includes("hillshade"))
.map((h) => h.data.hillshade)
.map((h) => h.features)
.flat()
Insert cell
// diss.map((m) => m.map((f) => path(f)))
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()
//.flat()
Insert cell
clipped = poly.map((p) =>
p
.map((o) => turf.intersect(tw_rew, o))
.filter((f) => f != null)
.map((m) => turf.rewind(m, { reverse: true }))
)
Insert cell
tw_rew = turf.rewind(twBigisland.features[1], { reverse: true })
Insert cell
hs_nest = d3
.nest()
.key(function (d) {
return d.properties.class;
})
.key(function (d) {
return d.properties.level;
})
.entries(hillshade)
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-scale@2.1.2", "d3-collection@latest")
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
import { Polygon, PI } from "@makio135/utilities"
Insert cell
xyz = require("https://bundle.run/xyz-affair@0.9.1")
Insert cell
twBigisland = FileAttachment("tw-bigisland.geojson").json()
Insert cell
science_parks = FileAttachment("science_parks.geojson").json()
Insert cell
addedscienceparks = FileAttachment("addedscienceparks.geojson").json()
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