|
| 1 | +import React, { useMemo } from "react"; |
| 2 | +import { CustomProjection, Graticule } from "@visx/geo"; |
| 3 | +import type { Projection } from "@visx/geo/lib/types"; |
| 4 | +// import { geoConicConformal } from "d3-geo"; |
| 5 | +import { geoConicConformal } from "@visx/vendor/d3-geo"; |
| 6 | +import * as topojson from "topojson-client"; |
| 7 | +import { Topology } from "topojson-specification"; |
| 8 | + |
| 9 | +// --- TYPES --- |
| 10 | +interface StatsCanFederalProperties { |
| 11 | + PRNAME: string; |
| 12 | + PRUID: string; |
| 13 | +} |
| 14 | + |
| 15 | +interface StatsCanProvincialProperties { |
| 16 | + CDNAME: string; |
| 17 | + PRNAME: string; |
| 18 | + CDUID: string; |
| 19 | +} |
| 20 | + |
| 21 | +interface StatsCanRegionProperties { |
| 22 | + PRUID: string; |
| 23 | + PRNAME: string; |
| 24 | + CDUID: string; |
| 25 | + CDNAME: string; |
| 26 | + CDTYPE: string; |
| 27 | + CSDUID: string; |
| 28 | + CSDNAME: string; |
| 29 | + CSDTYPE: string; |
| 30 | +} |
| 31 | + |
| 32 | +// StatsCan standard properties (based on your mapshaper output) |
| 33 | +type StatsCanProperties = |
| 34 | + | StatsCanFederalProperties |
| 35 | + | StatsCanProvincialProperties |
| 36 | + | StatsCanRegionProperties; |
| 37 | + |
| 38 | +interface PolygonGeometry { |
| 39 | + type: "Polygon"; |
| 40 | + coordinates: number[][][]; // [Ring][Point] |
| 41 | +} |
| 42 | + |
| 43 | +interface MultiPolygonGeometry { |
| 44 | + type: "MultiPolygon"; |
| 45 | + coordinates: number[][][][]; |
| 46 | +} |
| 47 | + |
| 48 | +interface FeatureShape { |
| 49 | + type: "Feature"; |
| 50 | + geometry: PolygonGeometry | MultiPolygonGeometry; |
| 51 | + properties: StatsCanProperties; |
| 52 | +} |
| 53 | + |
| 54 | +interface CanadaMapProps { |
| 55 | + width: number; |
| 56 | + height: number; |
| 57 | + data: Topology; // Generic Topology to accept any generated file |
| 58 | + events?: boolean; |
| 59 | + // Optional: Allow the parent to color shapes based on data (e.g., sequential palette) |
| 60 | + getFill?: (feature: FeatureShape) => string; |
| 61 | + onSelect?: (feature: FeatureShape) => void; |
| 62 | +} |
| 63 | + |
| 64 | +export const CanadaMap = ({ |
| 65 | + width, |
| 66 | + height, |
| 67 | + data, |
| 68 | + events = false, |
| 69 | + getFill, |
| 70 | + onSelect, |
| 71 | +}: CanadaMapProps) => { |
| 72 | + // 1. CONVERT TO GEOJSON (Dynamic Key) |
| 73 | + const world = useMemo<FeatureShape[]>(() => { |
| 74 | + if (!data) return []; |
| 75 | + |
| 76 | + // Auto-detect the primary layer key so we don't crash on "PROVINCE" vs "regions" |
| 77 | + const keys = Object.keys(data.objects); |
| 78 | + if (keys.length === 0) return []; |
| 79 | + const primaryLayer = keys[0]; // e.g., "provinces" or "lcsd000a25a_e" |
| 80 | + const topodata = topojson.feature(data, data.objects[primaryLayer]); |
| 81 | + |
| 82 | + // @ts-expect-error - topojson types are loose, but we know the structure |
| 83 | + const features = topodata.features as FeatureShape[]; |
| 84 | + return features; |
| 85 | + }, [data]); |
| 86 | + |
| 87 | + // 2. DEFINE PROJECTION |
| 88 | + // geoConicConformal + fitSize is the "Magic Bullet" here. |
| 89 | + // It works for the whole country OR a single province drill-down automatically. |
| 90 | + const projection = useMemo( |
| 91 | + () => |
| 92 | + geoConicConformal() |
| 93 | + .parallels([50, 70]) |
| 94 | + .rotate([96, 0]) |
| 95 | + .fitSize([width, height], { |
| 96 | + type: "FeatureCollection", |
| 97 | + features: world, |
| 98 | + }), |
| 99 | + [width, height, world], |
| 100 | + ); |
| 101 | + console.dir({ projection }); |
| 102 | + if (world.length === 0) return null; |
| 103 | + |
| 104 | + return ( |
| 105 | + <svg width={width} height={height}> |
| 106 | + <CustomProjection<FeatureShape> projection={"mercator"} data={world}> |
| 107 | + {(customProjection) => { |
| 108 | + return ( |
| 109 | + <g> |
| 110 | + {customProjection.features.map((feature, i) => { |
| 111 | + const { path, feature: f } = feature; |
| 112 | + // Use provided fill logic or default to StatsCan grey |
| 113 | + // const fillColor = getFill ? getFill(f) : "#EAEAEA"; |
| 114 | + const fillColor = "#EAEAEA"; |
| 115 | + |
| 116 | + return ( |
| 117 | + <path |
| 118 | + key={`map-feature-${i}`} |
| 119 | + d={path || ""} |
| 120 | + fill={fillColor} |
| 121 | + stroke="#FFFFFF" |
| 122 | + strokeWidth={0.5} |
| 123 | + // style={{ |
| 124 | + // transition: "fill 0.2s ease", |
| 125 | + // cursor: events ? "pointer" : "default", |
| 126 | + // outline: "none", // Prevents focus ring issues on click |
| 127 | + // }} |
| 128 | + // onClick={() => { |
| 129 | + // if (events && onSelect) onSelect(f); |
| 130 | + // }} |
| 131 | + // onMouseEnter={(e) => { |
| 132 | + // if (events && !getFill) |
| 133 | + // e.currentTarget.style.fill = "#CFD8DC"; // Simple hover if no data color |
| 134 | + // }} |
| 135 | + // onMouseLeave={(e) => { |
| 136 | + // if (events && !getFill) |
| 137 | + // e.currentTarget.style.fill = fillColor; |
| 138 | + // }} |
| 139 | + /> |
| 140 | + ); |
| 141 | + })} |
| 142 | + </g> |
| 143 | + ); |
| 144 | + }} |
| 145 | + </CustomProjection> |
| 146 | + </svg> |
| 147 | + ); |
| 148 | +}; |
0 commit comments