Skip to content

Commit

Permalink
improve map creation
Browse files Browse the repository at this point in the history
  • Loading branch information
eegli committed Nov 7, 2021
1 parent 024c887 commit 76814a2
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 86 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ yarn-debug.log*
yarn-error.log*

# local env files
.env
.env.local
.env.development.local
.env.test.local
Expand Down
1 change: 0 additions & 1 deletion data/NUTS_RG_03M_2021_4326_SUI.json

This file was deleted.

43 changes: 0 additions & 43 deletions maps.ts

This file was deleted.

79 changes: 79 additions & 0 deletions maps/custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"name": "Frauenfeld"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
8.893089294433594,
47.568708387119784
],
[
8.871545791625977,
47.55886268619808
],
[
8.875408172607422,
47.54403272736696
],
[
8.901586532592773,
47.53557313731042
],
[
8.915319442749023,
47.550926863012464
],
[
8.91514778137207,
47.56812927946671
],
[
8.893089294433594,
47.568708387119784
]
]
]
}
},
{
"type": "Feature",
"properties": {
"name": "Alpen"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
5.11962890625,
44.56699093657141
],
[
6.646728515625,
44.19795903948531
],
[
13.853759765625,
47.517200697839414
],
[
12.5244140625,
48.070738264258296
],
[
5.11962890625,
44.56699093657141
]
]
]
}
}
]
}
1 change: 1 addition & 0 deletions maps/switzerland.json

Large diffs are not rendered by default.

29 changes: 7 additions & 22 deletions src/config/helpers/index.ts → src/config/helpers/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,28 @@ import TbboxPolygon from '@turf/bbox-polygon';
import { FeatureCollection, MultiPolygon, Polygon } from '@turf/helpers';
import { nanoid } from 'nanoid';
import {
BaseMapProperties,
MapDataCollection,
MapIdCollection,
MapProperties,
SwissMapProperties,
} from '../types';

export function computeMapData<
T extends FeatureCollection<
Polygon | MultiPolygon,
MapProperties | SwissMapProperties
>
>(m: T): MapDataCollection {
T extends FeatureCollection<Polygon | MultiPolygon, BaseMapProperties>
>(m: T, category: string): MapDataCollection {
return m.features.reduce((acc, curr) => {
const bb = Tbbox(curr);
const bbPoly = TbboxPolygon(bb);

const properties: MapProperties = {
name: 'unknown',
category: 'unknown',
};

// Narrow down type - Swiss or custom maps
if ('NAME_LATN' in curr.properties) {
properties.name = curr.properties.NAME_LATN;
properties.category = 'switzerland';
} else {
properties.name = curr.properties.name;
properties.category = 'custom';
}

delete curr.id;

// Use an ID in order to avoid collisions between custom and Swiss maps
acc[nanoid(12)] = {
feature: {
...curr,
properties,
properties: {
name: curr.properties.name,
category: category,
},
},

area: Tarea(curr.geometry) * 1e-6,
Expand Down
15 changes: 9 additions & 6 deletions src/config/maps.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import defaultMapData from '../../data/NUTS_RG_03M_2021_4326_SUI.json';
import customMapData from '../../maps';
import { computeMapData, computeMapIds } from './helpers';
import customMapData from '../../maps/custom.json';
import defaultMapData from '../../maps/switzerland.json';
import { computeMapData, computeMapIds } from './helpers/creator';
import {
MapDataCollection,
MapFeatureCollection,
MapIdCollection,
SwissMapProperties,
} from './types';

/* Importing a GeoJSON data set and computing the required properties.
Here, the default maps are Swiss cantons and regions */
const swissMaps: MapDataCollection = computeMapData(
defaultMapData as MapFeatureCollection<SwissMapProperties>
defaultMapData as MapFeatureCollection,
'switzerland'
);

/* Custom maps can easily be added and don't need to be loaded from a
GeoJSON file */
const customMaps: MapDataCollection = computeMapData(customMapData);
const customMaps: MapDataCollection = computeMapData(
customMapData as MapFeatureCollection,
'custom'
);

export const MAPS: MapDataCollection = { ...swissMaps, ...customMaps };

Expand Down
22 changes: 9 additions & 13 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,22 @@ export type GameConfig = {

export type LatLngLiteral = { lat: number; lng: number };

export type MapProperties = {
export interface BaseMapProperties {
name: string;
category: string;
};
}

export type SwissMapProperties = {
CNTR_CODE: string;
NAME_LATN: string;
NUTS_ID: string;
FID: string;
};
export interface MapProperties extends BaseMapProperties {
category: string;
}

/* Input map data */
export type MapFeatureCollection<T = MapProperties> = FeatureCollection<
export type MapFeatureCollection = FeatureCollection<
Polygon | MultiPolygon,
T
BaseMapProperties
>;

/* Full, computed map data */
export type MapDataCollection<T = MapProperties> = Record<
export type MapDataCollection = Record<
string,
{
// Area in km^2
Expand All @@ -61,7 +57,7 @@ export type MapDataCollection<T = MapProperties> = Record<
// Poly bounding box: SW SE NE NW
bbLiteral: Record<'NE' | 'SE' | 'SW' | 'NW', LatLngLiteral>;
// Base can be used directly with google maps
feature: Feature<Polygon | MultiPolygon, T>;
feature: Feature<Polygon | MultiPolygon, MapProperties>;
}
>;

Expand Down

1 comment on commit 76814a2

@vercel
Copy link

@vercel vercel bot commented on 76814a2 Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.