Skip to content

Commit

Permalink
refactor: use JS modules instead of JSON files
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed Jan 4, 2021
1 parent bfdd4ab commit 306b61d
Show file tree
Hide file tree
Showing 17 changed files with 3,248 additions and 3,285 deletions.
8 changes: 4 additions & 4 deletions src/maps/geo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { dsvFormat } from 'd3-dsv';
import stateTopoJSON from './processed/state.topojson.json';
import countyTopoJSON from './processed/county.topojson.json';
import msaTopoJSON from './processed/msa.topojson.json';
import hrrTopoJSON from './processed/hrr.topojson.json';
import stateTopoJSON from './processed/state.topojson.js';
import countyTopoJSON from './processed/county.topojson.js';
import msaTopoJSON from './processed/msa.topojson.js';
import hrrTopoJSON from './processed/hrr.topojson.js';
import citiesRaw from './processed/cities.csv.js';
import { generateGeo } from './utils';

Expand Down
2 changes: 1 addition & 1 deletion src/maps/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import boundsInfo from './processed/bounds.json';
import boundsInfo from './processed/bounds.js';
import { dsvFormat } from 'd3-dsv';
import stateRaw from './processed/state.csv.js';
import msaRaw from './processed/msa.csv.js';
Expand Down
22 changes: 13 additions & 9 deletions src/maps/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs');
const path = require('path');
const centerOfMass = require('@turf/center-of-mass').default;
const { topology } = require('topojson-server');
const geojsonExtent = require('@mapbox/geojson-extent');
const bbox = require('@turf/bbox').default;
const { LngLatBounds, LngLat } = require('mapbox-gl');
const fetch = require('node-fetch');

Expand All @@ -15,7 +15,7 @@ const data = require('./raw/name_id_info.json');
const rows = data.all;

function computeBounds(geojson, scale = 1) {
const bounds = geojsonExtent(geojson);
const bounds = bbox(geojson);

const mapglBounds = new LngLatBounds(new LngLat(bounds[0], bounds[1]), new LngLat(bounds[2], bounds[3]));

Expand All @@ -41,7 +41,11 @@ function computeBounds(geojson, scale = 1) {
* @param {string} csv
*/
function wrapModule(csv) {
return `export default \`${csv}\`;`;
return `export default \`${csv}\`;\n`;
}

function wrapJSONModule(json) {
return `export default ${JSON.stringify(json)};\n`;
}

async function states(level = 'state') {
Expand All @@ -68,7 +72,7 @@ async function states(level = 'state') {
wrapModule(dsvFormat(',').format(infos, ['id', 'postal', 'name', 'population', 'area', 'lat', 'long'])),
);
const topo = topology({ [level]: geo }, QUANTIZATION);
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.json`), JSON.stringify(topo));
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.js`), wrapJSONModule(topo));
return geo;
}

Expand Down Expand Up @@ -116,7 +120,7 @@ function msa(level = 'msa') {
wrapModule(dsvFormat(',').format(infos, ['id', 'name', 'population', 'lat', 'long'])),
);
const topo = topology({ [level]: geo }, QUANTIZATION);
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.json`), JSON.stringify(topo));
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.js`), wrapJSONModule(topo));
return geo;
}

Expand Down Expand Up @@ -221,7 +225,7 @@ async function counties(level = 'county') {
),
);
const topo = topology({ [level]: geo }, QUANTIZATION);
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.json`), JSON.stringify(topo));
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.js`), wrapJSONModule(topo));
return geo;
}

Expand Down Expand Up @@ -266,7 +270,7 @@ async function hrr(level = 'hrr') {
);
// fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.geo.json`), JSON.stringify(geo));
const topo = topology({ [level]: geo }, QUANTIZATION / 2.5);
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.json`), JSON.stringify(topo));
fs.writeFileSync(path.resolve(__dirname, `./processed/${level}.topojson.js`), wrapJSONModule(topo));
return geo;
}

Expand Down Expand Up @@ -295,8 +299,8 @@ function cities() {
await cities();

fs.writeFileSync(
path.resolve(__dirname, `./processed/bounds.json`),
JSON.stringify(
path.resolve(__dirname, `./processed/bounds.js`),
wrapJSONModule(
{
states: computeBounds(statesGeo).toArray(),
msa: computeBounds(msaGeo).toArray(),
Expand Down
1 change: 1 addition & 0 deletions src/maps/processed/bounds.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 0 additions & 42 deletions src/maps/processed/bounds.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/maps/processed/cities.csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,4 @@ Lakeland,100710,12.862676015490699,-9.755940333969049
Erie,100671,12.130576960313281,4.4321878624611415
Tyler,100223,1.0510316547141727,-6.388692032121341
Pearland,100065,1.0846493984653667,-9.158349771069709
College Station,100050,0.17666129608188338,-8.106484617447487`;
College Station,100050,0.17666129608188338,-8.106484617447487`;
Loading

0 comments on commit 306b61d

Please sign in to comment.