Public
Edited
Sep 28, 2023
Insert cell
# base map - UD Fall 2023 - Nick Spinelli
Insert cell
d3 = require("d3@6")
Insert cell
chart = {
const width = 960,
height = 960;
const svg = d3.create("svg")
.attr("viewBox", [300, 315, width-800, height-445]);

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

var path1 = d3.geoPath().projection(projection);//any path that comes from qgis
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");


g.selectAll("path2") //d3 geopath
.data(streets.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','1')
.style("stroke-width", '.25')
.style("stroke", "rgb(0,0,0)")


g.selectAll("path3") //d3 geopath
.data(bldgs.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", path3) //The d attribute defines a path to be drawn, only applies to appended elements
.style("fill", "rgb(240,240,240)")
.style("fill-opacity", "1")
.style('stroke-opacity','.1')
.style("stroke-width", '.5')
.style("stroke", "rgb(0,0,0)")

var c = svg.selectAll("circle") //use this line the first time we create a circle
.data(mypoints.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.style('fill','black')
.style('fill-opacity','1')
.on('mouseover',rinktext)
.on('mouseout',removerinkText)

function rinktext(event,d){//this is everything that happens when we hover over a fountain
//console.log(d)

svg
.append("text")
.attr('x','100')
.attr('y','220')
.attr('class','rink_position')
.style('font-family','helvetica')
.style('font-size','10px')
.style('font-weight','light')//use 'fill' to change color
.text(d.properties.Position)

var wrap = svg.selectAll("text.rink_position")
.each(function(d, i) { wrap_text(d3.select(this), 75) });

svg
.append("line")
.attr('x1','240')
.attr('y1','195')
.attr('x2', path1.centroid(d)[0])
.attr('y2', path1.centroid(d)[1])
.attr('class','fLine')
.style('stroke-width', '1')
.style("stroke", "rgb(0,0,0)")
.style('stroke-dasharray', '6 4')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(test1)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("class", 'rink')
.style('fill','black')
.style('stroke','none')
.style('stroke-width','.5')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(test2)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.attr("class", 'rink')
.style('fill','none')
.style('stroke','blue')
.style('stroke-width','2')
}//this is where fountainText function ends

function removerinkText(){
d3.selectAll('text.rink_position').remove()//geometry type, then class name
d3.selectAll('line.fLine').remove()
d3.selectAll('polyline.rink').remove()
}

c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(Housing.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.style('stroke','black')
.style('stroke-width','1')
.style('fill','red')
.style('fill-opacity','1')

c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(Fuel.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.style('stroke','black')
.style('stroke-width','1')
.style('fill','red')
.style('fill-opacity','1')

c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(childcare.features)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return path1.centroid(d)[0]})
.attr("cy", function(d) {return path1.centroid(d)[1]})
.attr('r',3)
.style('stroke','black')
.style('stroke-width','1')
.style('fill','red')
.style('fill-opacity','1')
c.enter().append("circle") // use this line everytime after the first time we create a circle
.data(personal_spots)
.enter() //there are more data than elements, this selects them
.append("circle") //appends path to data
.attr("cx", function(d) {return projection([d.long,d.lat])[0]})//projection([d.long,d.lat])[0]
.attr("cy", function(d) {return projection([d.long,d.lat])[1]})
.attr('r',3)
.style('stroke','black')
.style('stroke-width','1')
.style('fill','cyan')
.style('fill-opacity','1')

var t = svg.selectAll("text") //use this line the first time we create a circle
.data(bldgs.features)
.enter() //there are more data than elements, this selects them
.append("text") //appends path to data
.attr("x", function(d) {return path1.centroid(d)[0]})
.attr("y", function(d) {return path1.centroid(d)[1]-7})
.style('font-family','helvetica')
.style('font-size','8px')
.style('text-anchor','middle')
.style('font-weight','bold')//use 'fill' to change color
.text(function(d) {return d.properties.name})

svg
.append("text")
.attr('x','100')
.attr('y','100')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('After School Locations')

svg
.append("text")
.attr('x','100')
.attr('y','120')
.style('font-family','helvetica')
.style('font-size','10px')
.style('font-weight','light')//use 'fill' to change color
.text('this is a personal map')

svg
.append("text")
.attr('x','100')
.attr('y','200')
.style('font-family','helvetica')
.style('font-size','12px')
.style('font-weight','bold')//use 'fill' to change color
.text('Enjoy')

svg
.append("line")
.attr('x1','100')
.attr('y1','104')
.attr('x2','200')
.attr('y2','104')
.style("stroke-width", '1')
.style("stroke", "rgb(0,0,0)")

var p = svg.selectAll("polyline") //use this line the first time we create a polyline
.data(test1)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.style('fill','none')
.style('stroke','black')
.style('stroke-width','2')

p.enter().append("polyline") //use this line the first time we create a polyline
.data(test2)
.enter() //there are more data than elements, this selects them
.append("polyline") //appends path to data
.attr("points", function(d) {return d})//we use a function in order to draw ALL of the fountains - loop through the fountains
.style('fill','cyan')
.style('stroke','black')
.style('stroke-width','2')

return svg.node();
}
Insert cell
bldgs = FileAttachment("Buildings3.geojson").json()
Insert cell
bbox = FileAttachment("BoundingBox.geojson").json()
Insert cell
streets = FileAttachment("Streets.geojson").json()
Insert cell
mypoints = FileAttachment("pointssss.geojson").json()
Insert cell
Fuel = FileAttachment("Alternative_Fuel_Station.geojson").json()
Insert cell
childcare = FileAttachment("Child_Care_Centers.geojson").json()
Insert cell
Housing= FileAttachment("Somerset_County_Housing.geojson").json()
Insert cell
Insert cell
personal_spots = d3.csv(personal_spots_link,d3.autoType)
Insert cell
import { wrap_text, wrap_text_nchar } from "@ben-tanen/svg-text-and-tspan-word-wrapping"
Insert cell
test1 = FileAttachment("Urban Data class 3.txt").text()
Insert cell
test2 = FileAttachment("Urban Data class 3-2.txt").text()
Insert cell
test3 = FileAttachment("Urban Data class 3-3.txt").text()
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