├── .gitignore ├── us-albers-territories-screenshot.png ├── rollup.config.js ├── LICENSE ├── package.json ├── README.md └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | *.swp 4 | package-lock.json 5 | -------------------------------------------------------------------------------- /us-albers-territories-screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stamen/geo-albers-usa-territories/HEAD/us-albers-territories-screenshot.png -------------------------------------------------------------------------------- /rollup.config.js: -------------------------------------------------------------------------------- 1 | export default { 2 | input: 'src/index.js', 3 | external: ['d3-geo'], 4 | output: { 5 | file: 'dist/geo-albers-usa-territories.js', 6 | format: 'umd', 7 | name: 'geoAlbersUsaTerritories', 8 | globals: { 9 | 'd3-geo': 'd3' 10 | } 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Stamen Design 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geo-albers-usa-territories", 3 | "version": "0.1.0", 4 | "description": "A map projection including all US overseas territories.", 5 | "main": "dist/geo-albers-usa-territories.js", 6 | "scripts": { 7 | "prepublish": "rollup --config", 8 | "postpublish": "git push && git push --tags" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/stamen/geo-albers-usa-territories.git" 13 | }, 14 | "keywords": [ 15 | "Map", 16 | "Geo", 17 | "GIS", 18 | "D3" 19 | ], 20 | "author": "Alan McConchie", 21 | "//": "Contributors list derived from https://github.com/d3/d3-geo/blob/master/src/projection/albersUsa.js", 22 | "contributors": [ 23 | "Curran Kelleher", 24 | "Mike Fogel", 25 | "Noah Veltman", 26 | "Mike Bostock" 27 | ], 28 | "license": "MIT", 29 | "bugs": { 30 | "url": "https://github.com/stamen/geo-albers-usa-territories/issues" 31 | }, 32 | "homepage": "https://github.com/stamen/geo-albers-usa-territories#readme", 33 | "devDependencies": { 34 | "rollup": "^2.26.11", 35 | "d3-geo": "^2.0.1" 36 | }, 37 | "peerDependencies": { 38 | "d3-geo": "^2.0.1" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geo-albers-usa-territories 2 | A map projection including all US overseas territories. 3 | 4 | ## How to use? 5 | 6 | If you're using a ` 10 | ``` 11 | 12 | Otherwise, you can use it as a dependency with NPM (`npm install geo-albers-usa-territories`) and `require` it like this: 13 | 14 | ```js 15 | const geoAlbersUsaTerritories = require("geo-albers-usa-territories") 16 | ``` 17 | 18 | Or, if your environment suppots ES6 modules: 19 | 20 | ```js 21 | import geoAlbersUsaTerritories from "geo-albers-usa-territories"; 22 | ``` 23 | 24 | Here's a [complete working example that uses this library](https://vizhub.com/curran/06639ba4d76d4cf08e69f4f42a0c9a99). 25 | 26 | ## Where did this come from? 27 | 28 | Original source: https://observablehq.com/@almccon/u-s-map-with-puerto-rico-us-virgin-islands-american-samoa-gua 29 | 30 | An extension of https://observablehq.com/@d3/u-s-map-with-puerto-rico 31 | 32 | Which in turn was derived from https://github.com/d3/d3-geo/blob/master/src/projection/albersUsa.js 33 | 34 | ![US Albers w/ Territories map projection screenshot](https://github.com/stamen/us-albers-territories/raw/master/us-albers-territories-screenshot.png) 35 | 36 | Note that this package marks `d3-geo` as a `peerDependency`, so you'll need to directly depend on `d3-geo` as well as this package. This is to avoid the possibility of bundling multiple copies of `d3-geo`. 37 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | import { geoAlbers, geoConicEqualArea } from 'd3-geo'; 2 | 3 | var epsilon = 0.000001; 4 | 5 | function multiplex(streams) { 6 | return { 7 | point(x, y) { 8 | for (const s of streams) s.point(x, y); 9 | }, 10 | sphere() { 11 | for (const s of streams) s.sphere(); 12 | }, 13 | lineStart() { 14 | for (const s of streams) s.lineStart(); 15 | }, 16 | lineEnd() { 17 | for (const s of streams) s.lineEnd(); 18 | }, 19 | polygonStart() { 20 | for (const s of streams) s.polygonStart(); 21 | }, 22 | polygonEnd() { 23 | for (const s of streams) s.polygonEnd(); 24 | }, 25 | }; 26 | } 27 | 28 | export function geoAlbersUsaTerritories() { 29 | var cache, 30 | cacheStream, 31 | lower48 = geoAlbers(), 32 | lower48Point, 33 | alaska = geoConicEqualArea() 34 | .rotate([154, 0]) 35 | .center([-2, 58.5]) 36 | .parallels([55, 65]), 37 | alaskaPoint, 38 | hawaii = geoConicEqualArea() 39 | .rotate([157, 0]) 40 | .center([-3, 19.9]) 41 | .parallels([8, 18]), 42 | hawaiiPoint, 43 | puertoRico = geoConicEqualArea() 44 | .rotate([66, 0]) 45 | .center([0, 18]) 46 | .parallels([8, 18]), 47 | puertoRicoPoint, 48 | guamMariana = geoConicEqualArea() 49 | .rotate([-145, 0]) 50 | .center([0, 16]) 51 | .parallels([10, 20]), 52 | guamMarianaPoint, 53 | americanSamoa = geoConicEqualArea() 54 | .rotate([170, 0]) 55 | .center([0, -14]) 56 | .parallels([-14, 0]), 57 | americanSamoaPoint, 58 | point, 59 | pointStream = { 60 | point: function (x, y) { 61 | point = [x, y]; 62 | }, 63 | }; 64 | 65 | function albersUsaTerritories(coordinates) { 66 | var x = coordinates[0], 67 | y = coordinates[1]; 68 | return ( 69 | (point = null), 70 | (lower48Point.point(x, y), point) || 71 | (alaskaPoint.point(x, y), point) || 72 | (hawaiiPoint.point(x, y), point) || 73 | (puertoRicoPoint.point(x, y), point) || 74 | (guamMarianaPoint.point(x, y), point) || 75 | (americanSamoaPoint.point(x, y), point) 76 | ); 77 | } 78 | 79 | albersUsaTerritories.invert = function (coordinates) { 80 | var k = lower48.scale(), 81 | t = lower48.translate(), 82 | x = (coordinates[0] - t[0]) / k, 83 | y = (coordinates[1] - t[1]) / k; 84 | return (y >= 0.12 && y < 0.234 && x >= -0.225 && x < -0.185 85 | ? alaska 86 | : y >= 0.166 && y < 0.234 && x >= -0.185 && x < -0.08 87 | ? hawaii 88 | : y >= 0.204 && y < 0.234 && x >= 0.3 && x < 0.38 89 | ? puertoRico 90 | : y >= 0.05 && y < 0.204 && x >= -0.415 && x < -0.225 91 | ? guamMariana 92 | : y >= 0.18 && y < 0.234 && x >= -0.415 && x < -0.225 93 | ? americanSamoa 94 | : lower48 95 | ).invert(coordinates); 96 | }; 97 | 98 | albersUsaTerritories.stream = function (stream) { 99 | return cache && cacheStream === stream 100 | ? cache 101 | : (cache = multiplex([ 102 | lower48.stream((cacheStream = stream)), 103 | alaska.stream(stream), 104 | hawaii.stream(stream), 105 | puertoRico.stream(stream), 106 | guamMariana.stream(stream), 107 | americanSamoa.stream(stream), 108 | ])); 109 | }; 110 | 111 | albersUsaTerritories.precision = function (_) { 112 | if (!arguments.length) return lower48.precision(); 113 | lower48.precision(_); 114 | alaska.precision(_); 115 | hawaii.precision(_); 116 | puertoRico.precision(_); 117 | guamMariana.precision(_); 118 | americanSamoa.precision(_); 119 | return reset(); 120 | }; 121 | 122 | albersUsaTerritories.scale = function (_) { 123 | if (!arguments.length) return lower48.scale(); 124 | lower48.scale(_); 125 | alaska.scale(_ * 0.35); 126 | hawaii.scale(_); 127 | puertoRico.scale(_); 128 | guamMariana.scale(_); 129 | americanSamoa.scale(_); 130 | return albersUsaTerritories.translate(lower48.translate()); 131 | }; 132 | 133 | albersUsaTerritories.translate = function (_) { 134 | if (!arguments.length) return lower48.translate(); 135 | var k = lower48.scale(), 136 | x = +_[0], 137 | y = +_[1]; 138 | 139 | lower48Point = lower48 140 | .translate(_) 141 | .clipExtent([ 142 | [x - 0.455 * k, y - 0.238 * k], 143 | [x + 0.455 * k, y + 0.238 * k], 144 | ]) 145 | .stream(pointStream); 146 | 147 | alaskaPoint = alaska 148 | .translate([x - 0.275 * k, y + 0.201 * k]) 149 | .clipExtent([ 150 | [x - 0.425 * k + epsilon, y + 0.12 * k + epsilon], 151 | [x - 0.185 * k - epsilon, y + 0.234 * k - epsilon], 152 | ]) 153 | .stream(pointStream); 154 | 155 | hawaiiPoint = hawaii 156 | .translate([x - 0.18 * k, y + 0.212 * k]) 157 | .clipExtent([ 158 | [x - 0.185 * k + epsilon, y + 0.166 * k + epsilon], 159 | [x - 0.08 * k - epsilon, y + 0.234 * k - epsilon], 160 | ]) 161 | .stream(pointStream); 162 | 163 | puertoRicoPoint = puertoRico 164 | .translate([x + 0.335 * k, y + 0.224 * k]) 165 | .clipExtent([ 166 | [x + 0.3 * k, y + 0.204 * k], 167 | [x + 0.38 * k, y + 0.234 * k], 168 | ]) 169 | .stream(pointStream); 170 | 171 | guamMarianaPoint = guamMariana 172 | .translate([x - 0.415 * k, y + 0.14 * k]) 173 | .clipExtent([ 174 | [x - 0.45 * k, y + 0.05 * k], 175 | [x - 0.39 * k, y + 0.21 * k], 176 | ]) 177 | .stream(pointStream); 178 | 179 | americanSamoaPoint = americanSamoa 180 | .translate([x - 0.415 * k, y + 0.215 * k]) 181 | .clipExtent([ 182 | [x - 0.45 * k, y + 0.21 * k], 183 | [x - 0.39 * k, y + 0.234 * k], 184 | ]) 185 | .stream(pointStream); 186 | return reset(); 187 | }; 188 | 189 | function reset() { 190 | cache = cacheStream = null; 191 | return albersUsaTerritories; 192 | } 193 | 194 | return albersUsaTerritories.scale(1070); 195 | } 196 | --------------------------------------------------------------------------------