Skip to content

Commit

Permalink
feat: borders
Browse files Browse the repository at this point in the history
  • Loading branch information
neocarto committed Mar 15, 2022
1 parent 00b8ebe commit 5b6ce84
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/borders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as topojsonclient from "topojson-client";
import * as topojsonserver from "topojson-server";
const topojson = Object.assign({}, topojsonclient, topojsonserver);

export function borders({ geojson, id, values }) {
const topo = topojson.topology({ d: geojson });
const ids = geojson.features.map((d) => d.properties[id]);
const neighbors = topojson.neighbors(topo.objects["d"].geometries);
const valbyid = new Map(
geojson.features.map((d) => [d.properties[id], d.properties[values]])
);

let result = [];
ids.forEach((e) => {
let r = neighbors[ids.indexOf(e)].map((i) => ({
properties: {
i: e,
j: ids[i],
var_i: +valbyid.get(e),
var_j: +valbyid.get(ids[i]),
rel: +valbyid.get(e) / +valbyid.get(ids[i]),
abs: +valbyid.get(e) - +valbyid.get(ids[i])
}
}));
r.forEach((e) => {
let geom = topojson.mesh(
topo,
topo.objects["d"],
(a, b) =>
(a.properties[id] == e.properties.i) &
(b.properties[id] == e.properties.j)
);

let output = Object.assign({ type: "Feature" }, e);
result.push(Object.assign(output, { geometry: geom }));
});
});

return { type: "FeatureCollection", features: result };
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {merge} from "./merge.js";
export {quickdraw} from "./quickdraw.js";
export {table2geo} from "./table2geo.js";
export {links} from "./links.js";
export {borders} from "./borders.js";

// layer functions

Expand Down

0 comments on commit 5b6ce84

Please sign in to comment.