Published
Edited
Jul 8, 2021
Importers
Insert cell
Phil = FileAttachment("test4 (1)@1.json").json()
Insert cell
world = FileAttachment("gistfile1@3.json").json()
Insert cell
few = FileAttachment("a few countries.json").json()
Insert cell
PhilMap4 = {

let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
yield container;

let map = L.map(container).setView([35, 10], 2);

function getColor(d) {
return d > 1000000 ? '#800026' :
d > 500000 ? '#BD0026' :
d > 100000 ? '#E31A1C' :
d > 50000 ? '#FC4E2A' :
d > 10000 ? '#FD8D3C' :
d > 5000 ? '#FEB24C' :
d > 1000 ? '#FED976' :
'#FFEDA0';
}
//initialize svg to add to map
// let PhilAreasLayer = L.geoJson({clickable:true}).addTo(map) // we have to make the svg layer clickable
L.svg({clickable:true}).addTo(map) // we have to make the svg layer clickable
//Create selection using D3
const overlay = d3.select(map.getPanes().overlayPane)
const svg = overlay.select('svg').attr("pointer-events", "auto")
// create a group that is hidden during zooming
const g = svg.append('g').attr('class', 'leaflet-zoom-hide')
// Use Leaflets projection API for drawing svg path (creates a stream of projected points)
const projectPoint = function(x, y) {
const point = map.latLngToLayerPoint(new L.LatLng(y, x))
this.stream.point(point.x, point.y)
}
// Use d3's custom geo transform method to implement the above
const projection = d3.geoTransform({point: projectPoint})
// creates geopath from projected points (SVG)
const pathCreator = d3.geoPath().projection(projection)



const areaPaths = g.selectAll('path')
.data(world.features)
.join('path')
.attr('fill', function(d, i) {
return getColor(world.features[i].properties.num)
})
//'red'
//getColor(VanAreas.features.properties.ID_1)
.attr('stroke', 'black')
.attr("z-index", 3000)
.attr('stroke-width', 2.5)
//.on("mouseover", mouseover )


//map.fitBounds(PhilAreasLayer.getBounds());
// Function to place svg based on zoom
const onZoom = () => areaPaths.attr('d', pathCreator)
// initialize positioning
onZoom()
// reset whenever map is moved
map.on('zoomend', onZoom)
}
Insert cell
fewcountries = {

let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
yield container;

let map = L.map(container).setView([35, 10], 2);

let PhilAreasLayer2 = L.geoJson(few, { //instantiates a new geoJson layer using built in geoJson handling
weight: 2, //Attributes of polygons including the weight of boundaries and colors of map.
color: "#432",
onEachFeature: areaClick
}).bindPopup(function (Layer) { //binds a popup when clicking on each polygon to access underlying data
return '<a href="http://ridgez.ca/auditory.html" target="_blank"><h1> Test Title </h1><img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/100x100_logo.png" width=100 height=100 /></a>';
})

.addTo(map); //Adds the layer to the map.




map.on('click', () =>
mutable nHood = null
);
map.fitBounds(PhilAreasLayer2.getBounds());
}
Insert cell
test = {

let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
yield container;

let map = L.map(container).setView([35, 10], 2);

//initialize svg to add to map
// let PhilAreasLayer = L.geoJson({clickable:true}).addTo(map) // we have to make the svg layer clickable
L.svg({clickable:true}).addTo(map) // we have to make the svg layer clickable
//Create selection using D3
const overlay = d3.select(map.getPanes().overlayPane)
const svg = overlay.select('svg').attr("pointer-events", "auto")
// create a group that is hidden during zooming
const g = svg.append('g').attr('class', 'leaflet-zoom-hide')
// Use Leaflets projection API for drawing svg path (creates a stream of projected points)
const projectPoint = function(x, y) {
const point = map.latLngToLayerPoint(new L.LatLng(y, x))
this.stream.point(point.x, point.y)
}
// Use d3's custom geo transform method to implement the above
const projection = d3.geoTransform({point: projectPoint})
// creates geopath from projected points (SVG)
const pathCreator = d3.geoPath().projection(projection)



const areaPaths = g.selectAll('path')
.data(world.features)
.join('path')
//'red'
//getColor(VanAreas.features.properties.ID_1)
.attr('stroke', 'black')
.attr('fill', 'white')

.attr("z-index", 3000)
.attr('stroke-width', 2.5)
//.on("mouseover", mouseover )

let PhilAreasLayer2 = L.geoJson(capital, { //instantiates a new geoJson layer using built in geoJson handling
weight: 2, //Attributes of polygons including the weight of boundaries and colors of map.
color: "#432",
}).bindPopup(function (Layer) { //binds a popup when clicking on each polygon to access underlying data
return '<a href="http://ridgez.ca/auditory.html" target="_blank"><h1> Test Title </h1><img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/100x100_logo.png" width=100 height=100 /></a>';
})
//only works with https

.addTo(map); //Adds the layer to the map.

//map.fitBounds(PhilAreasLayer.getBounds());
// Function to place svg based on zoom
const onZoom = () => areaPaths.attr('d', pathCreator)
// initialize positioning
onZoom()
// reset whenever map is moved
map.on('zoomend', onZoom)
map.fitBounds(PhilAreasLayer2.getBounds());
}
Insert cell
capital = FileAttachment("capitals@1.json").json()
Insert cell
PhilMap2 = {

let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
yield container;

let map = L.map(container).setView([10.5, 120.9842], 7);
let osmLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function getColor(d) {
return d > 1000000 ? '#800026' :
d > 500000 ? '#BD0026' :
d > 100000 ? '#E31A1C' :
d > 50000 ? '#FC4E2A' :
d > 10000 ? '#FD8D3C' :
d > 5000 ? '#FEB24C' :
d > 1000 ? '#FED976' :
'#FFEDA0';
}
//initialize svg to add to map
// let PhilAreasLayer = L.geoJson({clickable:true}).addTo(map) // we have to make the svg layer clickable
L.svg({clickable:true}).addTo(map) // we have to make the svg layer clickable
//Create selection using D3
const overlay = d3.select(map.getPanes().overlayPane)
const svg = overlay.select('svg').attr("pointer-events", "auto")
// create a group that is hidden during zooming
const g = svg.append('g').attr('class', 'leaflet-zoom-hide')
// Use Leaflets projection API for drawing svg path (creates a stream of projected points)
const projectPoint = function(x, y) {
const point = map.latLngToLayerPoint(new L.LatLng(y, x))
this.stream.point(point.x, point.y)
}
// Use d3's custom geo transform method to implement the above
const projection = d3.geoTransform({point: projectPoint})
// creates geopath from projected points (SVG)
const pathCreator = d3.geoPath().projection(projection)



const areaPaths = g.selectAll('path')
.data(Phil.features)
.join('path')
.attr('fill', function(d, i) {
return getColor(Phil.features[i].properties.num)
})
//'red'
//getColor(VanAreas.features.properties.ID_1)
.attr('stroke', 'black')
.attr("z-index", 3000)
.attr('stroke-width', 2.5)
//.on("mouseover", mouseover )

map.on('click', () =>
mutable nHood = null
);
//map.fitBounds(PhilAreasLayer.getBounds());
// Function to place svg based on zoom
const onZoom = () => areaPaths.attr('d', pathCreator)
// initialize positioning
onZoom()
// reset whenever map is moved
map.on('zoomend', onZoom)
}
Insert cell
viewof dataStuff = V({
width: 400,
height:300,
mark: "bar",
data: {values: D},
transform: [
{filter: {field: "province", oneOf: mapSelection}}
],
encoding: {
x: {field: "key", type: "nominal"},
y: {field: "displacedP", type: "quantitative"},
}
})
Insert cell
html`<div style="
background: #fff;
display: grid;
grid-template-areas:
'a'
'b';
grid-gap: 10px;
">
<div name="a" style="grid-area:a;border:solid 1px #ccc;text-align: center">${viewof dataStuff}</div>
<div name="b" style="grid-area:b;border:solid 1px #ccc;text-align: center">${viewof PhilMap}</div>
</div>`
Insert cell
viewof PhilMap = {
let width = 900
let height = 400
let container = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
yield container;

let map = L.map(container);
let osmLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);

//initialize svg to add to map
// let PhilAreasLayer = L.geoJson({clickable:true}).addTo(map) // we have to make the svg layer clickable
let PhilAreasLayer2 = L.geoJson(Phil, { //instantiates a new geoJson layer using built in geoJson handling
weight: 2, //Attributes of polygons including the weight of boundaries and colors of map.
color: "#432",
onEachFeature: areaClick
}).bindPopup(function (Layer) { //binds a popup when clicking on each polygon to access underlying data
return `Province: ${Layer.feature.properties.NAME_1} \n Number of People Dislocated: ${Layer.feature.properties.num}`;
})

.addTo(map); //Adds the layer to the map.




map.on('click', () =>
mutable nHood = null
);
map.fitBounds(PhilAreasLayer2.getBounds());

}
Insert cell
//We are writing the event listener function for each feature in our geojson layer
function areaClick(feature, layer) {
layer.on({
click:whenClicked //callback function
});
}
Insert cell
//We update our data in the function using mutable. This overrides observables defaults so we can push data from within //function scope globally
whenClicked = (event) => {
mutable nHood = event.target.feature.properties.NAME_1
}
Insert cell
mutable nHood = "ILOILO"
Insert cell
mapSelection = {
if(nHood == null ){
return Array.from(new Set(newMap.map(d => d.NAME_1))) //Set is a new datastructure in ES6 -> only unique values
} else {
return [nHood.toUpperCase()]
}
}
Insert cell
md` ## libraries and data`
Insert cell
L = require('leaflet@1.2.0')
Insert cell
d3 = require('d3@5')
Insert cell
V = require("@observablehq/vega-lite@0.2")
Insert cell
html`<link href='${resolve('leaflet@1.2.0/dist/leaflet.css')}' rel='stylesheet' />`
Insert cell
newMap = {
let i=Phil.features.length;
let j=0;
let k=0;
let temp ={};
let newCrime=[];
for(k=0;k<i; k++)
{
newCrime[k]= Phil.features[k].properties;
}
return (newCrime);
}
Insert cell
D =d3.csv('https://raw.githubusercontent.com/zavenanarsh/data/master/typhoon-yolanda-dswd-dromic-consolidated-27jan2014_pcoded.csv',d => {
return {
//+d to convert string in to int
key:d.Municipality,
province: d.Province,
displacedP: d.displaced_people,
displacedF: d.displaced_families,
houseD: d.TOTAL_houses_damaged,
}
})
Insert cell
import {values} from "@zavenanarsh/haiyan-data-wrangling"
Insert cell
md` ## Helper functions`
Insert cell
function isEmpty(obj) {
for (var prop in obj) {
return false;
}
return true;
}
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