Public
Edited
Jan 29, 2024
Insert cell
# base map - UD Spring 2024
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 900,
height = 900;
const svg = d3.create("svg")
.attr("viewBox", [25, 0, width-100, height-100]);

// Use Mercator projection
var projection = d3
.geoMercator()
.fitSize([width - 50, height - 50], bbox1);

var path1 = d3.geoPath().projection(projection);
var path2 = d3.geoPath().projection(projection);
var path3 = d3.geoPath().projection(projection);
var path4 = d3.geoPath().projection(projection);

var g = svg.append("g").attr("id", "paths");

svg
.append("text")
.attr('x','25')
.attr('y','25')
.style('font-family','garamond')
.style('font-size','24px')
.style('font-weight','bold')
.text('London')

svg
.append("text")
.attr('x','25')
.attr('y','50')
.style('font-family','garamond')
.style('font-size','18px')
.style('font-weight','bold')
.text('Places To Visit:')
svg
.append("line")
.attr('x1','25')
.attr('y1','33')
.attr('x2','142')
.attr('y2','33')
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")

g.selectAll("path2") //d3 geopath
.data(underground.features) //get data to define path
.enter() //there are more data than elements, this selects them
.append("path") //appends path to data
.attr('class','outlines')
.attr("d", path2) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "none")
.style('stroke-opacity','.5')
.style("stroke-width", '.5')
.style("stroke", "black")

g.selectAll("path3")
.data(base1.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path3)
.style("fill", "rgb(245,245,245)")
.style("fill-opacity", ".25")
.style('stroke-opacity','.1')
.style("stroke-width", '.5')
.style("stroke", "black")

g.selectAll("path3")
.data(brownfields.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path3)
.style("fill", "rgb(255,255,255)")
.style("fill-opacity", "1")
.style('stroke-opacity','.5')
.style("stroke-width", '.25')
.style("stroke", "black")

g.selectAll("path3")
.data(thames.features)
.enter()
.append("path")
.attr('class','outlines')
.attr("d", path3)
.style("fill", "rgb(0,0,0)")
.style("fill-opacity", ".65")
.style('stroke-opacity','1')
.style("stroke-width", '.1')
.style("stroke", "white")


var c = svg.selectAll("circle") //d3 geopath
.data(Points)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr('class','pointsfr')
.attr("cx", function(d) {return projection([d.Long,d.Lat])[0]})
.attr("cy", function(d) {return projection([d.Long,d.Lat])[0]})
.attr('r',3)
.attr('fill','black')
.style('fill-opacity','1')
.on('mouseover',pointsinfo)
.on('mouseout',pointsinfoout)
function pointsinfo(event,d){
d3.select(this).attr('fill','red')

svg.append('text')
.attr('class','PointsText')
.attr('x','25')
.attr('y','75')
.attr("font-family","Garamond")
.style('font-weight','bold')
.style('font-weight','bold')
.attr("font-size","16px")
.attr('fill','black')
.attr('text-anchor','left')
.text(d.Name)

svg.append('text')
.attr('class','PointsText')
.attr('x','25')
.attr('y','100')
.attr("font-family","Garamond")
.style('font-weight','bold')
.style('font-weight','bold')
.attr("font-size","12px")
.attr('fill','black')
.attr('text-anchor','left')
.text(d.Description)
svg.append('line')
.attr('class','PointLine')
.attr('x1','30')
.attr('y1','100')
.attr('x2',projection([d.Long,d.Lat])[0])
.attr('y2',projection([d.Long,d.Lat])[0])
.style("stroke-width", '.5')
.style("stroke", "black")
.style('stroke-dasharray', '6 4')

var p = svg.selectAll("polyline")
.data(irons)
.enter() //there are more data than elements, this selects them
.append("polyline")
.attr('points',function(d){return d})
.attr('transform','translate(200,200)')
// .attr('transform','scale(5)')
.style("stroke-width", '.5')
.style("stroke", 'red')
.attr('fill','red')

p.enter().append("polyline")
.data(icon_list)
.enter() //there are more data than elements, this selects them
.append("polyline")
.attr('points',function(d){return d.pts})
.attr('transform',function(d){return'translate('+projection([d.data.Long,d.data.Lat])[0]+','+projection([d.data.Long,d.data.Lat])[1]+')'})
// .attr('transform','scale(.5)')
.style("stroke-width", '.5')
.style("stroke", 'red')
.attr('fill','red')

}

function pointsinfoout(event,d){
d3.selectAll('circle.pointsfr').attr('fill','black')
d3.selectAll('text.PointsText').remove()
d3.selectAll('line.PointLine').remove()

}
return svg.node();
}
Insert cell
icon_list={
var list = []
for (let i = 0; i < Points.length; i++) {
Points[i]
list.push({data: Points[i],pts:irons})
}

return list
}
Insert cell
irons = FileAttachment("irons2.txt").tsv({array:true})
Insert cell
base1 = FileAttachment("Base@1.geojson").json()
Insert cell
subSts = FileAttachment("sub_stops.geojson").json()
Insert cell
bldgs = FileAttachment("bldgs.geojson").json()
Insert cell
bbox = FileAttachment("site_boundary.geojson").json()
Insert cell
subLns = FileAttachment("subway_lines.geojson").json()
Insert cell
bbox1 = FileAttachment("Bbox.geojson").json()
Insert cell
brownfields = FileAttachment("Brownfields.geojson").json()
Insert cell
thames = FileAttachment("Thames.geojson").json()
Insert cell
underground = FileAttachment("Underground.geojson").json()
Insert cell
Insert cell
Points=d3.csv(Points_Link,d3.autoType)
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