Skip to content

Instantly share code, notes, and snippets.

@texodus
Last active July 14, 2022 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save texodus/fc0cdabe27dc121a1a4038545a9e9b23 to your computer and use it in GitHub Desktop.
Save texodus/fc0cdabe27dc121a1a4038545a9e9b23 to your computer and use it in GitHub Desktop.
Perspective / Evictions
license: apache-2.0
<!--
Copyright (c) 2017, the Perspective Authors.
This file is part of the Perspective library, distributed under the terms of
the Apache License 2.0. The full license can be found in the LICENSE file.
-->
<!DOCTYPE html>
<html>
<head>
<meta
name="viewport"
content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no"
/>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@finos/perspective@latest/dist/cdn/perspective.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer@latest/dist/cdn/perspective-viewer.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-datagrid@latest/dist/cdn/perspective-viewer-datagrid.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-d3fc@latest/dist/cdn/perspective-viewer-d3fc.js"
></script>
<script
type="module"
src="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer-openlayers/dist/cdn/perspective-viewer-openlayers.js"
></script>
<script src="https://cdn.jsdelivr.net/npm/fflate@0.7.3/umd/index.min.js"></script>
<link
rel="stylesheet"
crossorigin="anonymous"
href="https://cdn.jsdelivr.net/npm/@finos/perspective-viewer/dist/css/themes.css"
/>
<script type="module">
import {worker} from "https://cdn.jsdelivr.net/npm/@finos/perspective@latest/dist/cdn/perspective.js";
const WORKER = worker();
const URL = `https://data.sfgov.org/resource/5cei-gny5.csv?$limit=50000`;
async function get_evictions() {
const arrow = localStorage.getItem(URL);
if (arrow !== null) {
console.log("Using cached data from localStorage");
try {
const buffer = fflate.strToU8(arrow, true);
return await WORKER.table(
fflate.decompressSync(buffer).buffer
);
} catch (e) {
console.error("Can't load cached data", e);
localStorage.clear();
}
}
console.log("Downloading data from data.sfgov.org");
const resp = await fetch(
URL
// "5cei-gny5.csv"
// "evictions.all.arrow"
);
const csv = await resp.text();
const table = await WORKER.table(csv);
(async () => {
console.log("Caching data");
const view = await table.view();
const arrow = await view.to_arrow();
try {
const x = fflate.compressSync(new Uint8Array(arrow));
const zipped = fflate.strFromU8(x, true);
localStorage.setItem(URL, zipped);
} catch (e) {
console.error(
"Can't cache data from data.sfgov.org",
e
);
}
})();
return table;
}
async function load() {
const el =
document.getElementsByTagName("perspective-viewer")[0];
const evictions = get_evictions();
const layout_json = await fetch("layout.json");
const layout = await layout_json.json();
el.load(await evictions);
el.restore(layout);
}
load();
</script>
<style>
perspective-viewer {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
}
</style>
</head>
<body>
<perspective-viewer editable> </perspective-viewer>
</body>
</html>
{
"plugin": "Map Scatter",
"plugin_config": {},
"settings": true,
"theme": "Material Dark",
"group_by": ["bucket lon", "bucket lat"],
"split_by": ["neighborhood"],
"columns": ["bucket lon", "bucket lat", null, null, null],
"filter": [["lon", "is not null", null]],
"sort": [],
"expressions": [
"// lon\nvar x[2];\nindexof(\"shape\", ' .+?( )', x);\nvar y := substring(\"shape\", 7, x[1] - 7);\nfloat(y)",
"// lat\nvar x[2];\nindexof(\"shape\", ' .+?( )', x);\nvar y := substring(\"shape\", x[0], length(\"shape\") - x[1]);\nfloat(y)",
"// bucket lon\nvar x[2];\nindexof(\"shape\", ' .+?( )', x);\nvar y := substring(\"shape\", 7, x[1] - 7);\nbucket(float(y), 0.0025) + 0.00125",
"// bucket lat\nvar x[2];\nindexof(\"shape\", ' .+?( )', x);\nvar y := substring(\"shape\", x[0], length(\"shape\") - x[1]);\nbucket(float(y), 0.0025) + 0.00125"
],
"aggregates": {"lon": "avg", "bucket lon": "avg", "bucket lat": "avg"}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment