├── .babelrc ├── .circleci └── config.yml ├── .editorconfig ├── .eslintrc ├── .gitignore ├── .npmignore ├── LICENSE ├── README.md ├── demo ├── colors.js ├── grid.html ├── gridvalue.js ├── metrics.js ├── micapse.html ├── micapse_12h_1km.txt ├── midcapse2.html └── random.html ├── dist ├── maptalks.gridlayer.es.js ├── maptalks.gridlayer.es.js.map ├── maptalks.gridlayer.js ├── maptalks.gridlayer.js.map └── maptalks.gridlayer.min.js ├── karma.config.js ├── package-lock.json ├── package.json ├── pnpm-lock.yaml ├── rollup.config.js ├── src ├── common.js ├── gridlayer.js ├── index.js └── renderer │ ├── canvas-renderer.js │ └── gl-renderer.js ├── test ├── .eslintrc └── test.js └── yarn.lock /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | ["es2015", { "loose" : true, "modules" : false }] 4 | ], 5 | "plugins": [ 6 | "external-helpers", 7 | "transform-proto-to-assign" 8 | ], 9 | "ignore": [ 10 | "dist/*.js" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Javascript Node CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-javascript/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/node:8-browsers 11 | environment: 12 | CHROME_BIN: "/usr/bin/google-chrome" 13 | 14 | # Specify service dependencies here if necessary 15 | # CircleCI maintains a library of pre-built images 16 | # documented at https://circleci.com/docs/2.0/circleci-images/ 17 | # - image: circleci/mongo:3.4.4 18 | 19 | working_directory: ~/repo 20 | 21 | steps: 22 | - checkout 23 | 24 | # Download and cache dependencies 25 | - restore_cache: 26 | keys: 27 | - v1-dependencies-{{ checksum "package.json" }} 28 | # fallback to using the latest cache if no exact match is found 29 | - v1-dependencies- 30 | 31 | - run: npm install 32 | 33 | - save_cache: 34 | paths: 35 | - node_modules 36 | key: v1-dependencies-{{ checksum "package.json" }} 37 | 38 | # run tests! 39 | - run: npm test 40 | - run: bash <(curl -s https://codecov.io/bash) 41 | 42 | 43 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [{package,bower}.json] 15 | indent_style = space 16 | indent_size = 2 17 | 18 | [.travis.yml] 19 | indent_style = space 20 | indent_size = 2 21 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "root": true, 3 | "extends": [ 4 | "eslint:recommended" 5 | ], 6 | "parserOptions": { 7 | "ecmaVersion": 2015, 8 | "sourceType": "module" 9 | }, 10 | "env": { 11 | "browser": true, 12 | "es6": true, 13 | "node": true 14 | }, 15 | "rules": { 16 | "no-prototype-builtins": "off" 17 | }, 18 | "globals": { 19 | "maptalks": true, 20 | "importScripts" : true, 21 | "WorkerGlobalScope": true, 22 | "globalThis": true 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .tmp/ 2 | *.gz 3 | .idea/ 4 | *.iml 5 | .tern-port 6 | doc/ 7 | 8 | ### *tags 9 | GPATH 10 | GRTAGS 11 | GTAGS 12 | TAGS 13 | 14 | ### OSX template 15 | .DS_Store 16 | 17 | # Created by .ignore support plugin (hsz.mobi) 18 | ### Node template 19 | # Logs 20 | logs 21 | *.log 22 | 23 | # Runtime data 24 | pids 25 | *.pid 26 | *.seed 27 | 28 | # Directory for instrumented libs generated by jscoverage/JSCover 29 | lib-cov 30 | 31 | # Coverage directory used by tools like istanbul 32 | coverage 33 | 34 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 35 | .grunt 36 | 37 | # node-waf configuration 38 | .lock-wscript 39 | 40 | # Compiled binary addons (http://nodejs.org/api/addons.html) 41 | build/Release 42 | 43 | # Dependency directory 44 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 45 | node_modules 46 | 47 | #example deploy config 48 | examples/replace.json 49 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /**/* 2 | dist/*.gz 3 | !dist/*.js 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2017 MapTalks 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # maptalks.gridlayer 2 | 3 | [](https://circleci.com/gh/maptalks/maptalks.gridlayer) 4 | [](https://github.com/maptalks/maptalks.gridlayer) 5 | 6 | GridLayer plugin for maptalks.js. A layer draws grids. 7 | 8 |  9 | 10 | ## Examples 11 | 12 | * [Random square grids](https://maptalks.github.io/maptalks.gridlayer/demo/random.html). 13 | * [Customer analysis grid](https://maptalks.github.io/maptalks.gridlayer/demo/grid.html). 14 | * [1KM grid of rainfall forecast of 2017-06-07](https://maptalks.github.io/maptalks.gridlayer/demo/micapse.html). 15 | 16 | ## Install 17 | 18 | * Install with npm: ```npm install maptalks.gridlayer```. 19 | * Download from [dist directory](https://github.com/maptalks/maptalks.gridlayer/tree/gh-pages/dist). 20 | * Use unpkg CDN: ```https://cdn.jsdelivr.net/npm/maptalks.gridlayer/dist/maptalks.gridlayer.min.js``` 21 | 22 | ## Usage 23 | 24 | As a plugin, `maptalks.gridlayer` must be loaded after `maptalks.js` in browsers. 25 | ```html 26 | 27 | 28 | 29 | 59 | ``` 60 | ## Supported Browsers 61 | 62 | Chrome, Firefox, other modern and mobile browsers. 63 | 64 | ## API Reference 65 | 66 | ```GridLayer``` is a subclass of [maptalks.Layer](https://maptalks.github.io/docs/api/Layer.html) and inherits all the methods of its parent. 67 | 68 | ### `Constructor` 69 | 70 | ```javascript 71 | new maptalks.GridLayer(id, data, options) 72 | ``` 73 | 74 | * id **String** layer id 75 | * data **Object[] | Object** see data format below 76 | * options **Object** options 77 | * renderer **String** renderer, canvas or gl, gl by default 78 | * symbol **Object** symbol of the grid, supported properties: `lineWidth` (fixed to 1 in gl renderer on windows [due to ANGLE](https://bugs.chromium.org/p/angleproject/issues/detail?id=334)), `lineColor`, `polygonFill`, `polygonOpacity` 79 | * Other options defined in [maptalks.Layer](https://maptalks.github.io/docs/api/Layer.html) 80 | 81 | ```javascript 82 | [ 83 | { 84 | center : [0, 0], // center of the grid 85 | width : 100, // width of the grid cell 86 | height : 100, // height of the grid cell 87 | // unit of cell width/height, possible values: 88 | // * projection : projected coordinate 89 | // * meter : meters of geographic distance 90 | // * degree : longtitude/latitude degrees 91 | altitude : 0, // altitude of grid in meter 92 | unit : 'projection', 93 | cols : [1, Infinity], 94 | rows : [2, 5], 95 | // data format 96 | data : [ 97 | //[col, row, { properties : properties, symbol : symbol}] 98 | //supported symbol properties : polygonFill, polygonOpacity 99 | //col: col_index or [beginIndex, endIndex] 100 | //row: col_index or [beginIndex, endIndex] 101 | // col is 1, row is 2 102 | [1, 2, { properties : { foo : 1, foo2 : 'foo' }, symbol : { polygonFill : '#f00' } }], 103 | //col is from 2 to 4 (3 columns), row is 5 104 | [[2, 4] , 5, { symbol : { polygonFill : '#f00' } }], 105 | //col is from 2 to 4 (3 columns), row is from 7 to 8 (2 rows) 106 | [[2, 4] , [7, 8], { symbol : { polygonFill : '#f00' } }] 107 | ] 108 | } 109 | ] 110 | ``` 111 | 112 | ### `getGrid(gridIndex = 0)` 113 | 114 | get layer's grid value 115 | * gridIndex **Number** grid's index, default is 0 116 | 117 | **Returns** `Object` 118 | 119 | ### `setGrid(grid, gridIndex = 0)` 120 | 121 | set a new grid value to the layer 122 | 123 | * grid **Object** new grid value 124 | * gridIndex **Number** grid's index, default is 0 125 | 126 | ### `setGridData(data, gridIndex = 0)` 127 | 128 | update layer's grid data 129 | 130 | * data **Array** set new data 131 | 132 | **Returns** `this` 133 | 134 | ### `redraw()` 135 | 136 | redraw the layer 137 | 138 | **Returns** `this` 139 | 140 | ### `isEmpty()` 141 | 142 | If the layer is empty 143 | 144 | **Returns** `Boolean` 145 | 146 | ### `clear()` 147 | 148 | clear the layer 149 | 150 | **Returns** `this` 151 | 152 | ### `getGridExtent(gridIndex = 0)` 153 | 154 | Get grid's geographic extent 155 | 156 | * gridIndex **Number** grid's index, default is 0 157 | 158 | **Returns** `maptalks.Extent` 159 | 160 | ### `GetCellAt(coordinate, gridIndex = 0)` 161 | 162 | Get cell index at coordinate 163 | 164 | * coordinate **maptalks.Coordinate** coordinate 165 | * gridIndex **Number** grid's index, default is 0 166 | 167 | **Returns** `Number[]` [col, row] 168 | 169 | ### `GetCellGeometry(col, row, gridIndex = 0)` 170 | 171 | Get cell's geometry 172 | 173 | * col **Number** cell's col 174 | * row **Number** cell's row 175 | * gridIndex **Number** grid's index, default is 0 176 | 177 | **Returns** `maptalks.Geometry` cell geometry 178 | 179 | ### `VisitAround(coordinate, cb, gridIndex = 0)` 180 | 181 | Visit data cells around given coordinate 182 | 183 | * coordinate **maptalks.Coordinate** coordinate 184 | * cb **Function** callback function, parameter is [col, row, { properties, symbol }], return false to break the visiting 185 | * gridIndex **Number** grid's index, default is 0 186 | 187 | ### `identify(coordinate, gridIndex = 0)` 188 | 189 | Return cell index and cell geometry at coordinate 190 | 191 | * coordinate **maptalks.Coordinate** coordinate 192 | * gridIndex **Number** grid's index, default is 0 193 | 194 | **Returns** `Object` { col : col, row : row, geometry : cellGeometry } 195 | 196 | ### `toJSON()` 197 | 198 | export the GridLayer's JSON. 199 | 200 | ```javascript 201 | var json = gridlayer.toJSON(); 202 | ``` 203 | 204 | **Returns** `Object` 205 | 206 | ## Contributing 207 | 208 | We welcome any kind of contributions including issue reportings, pull requests, documentation corrections, feature requests and any other helps. 209 | 210 | ## Develop 211 | 212 | The only source file is ```index.js```. 213 | 214 | It is written in ES6. 215 | 216 | ### Scripts 217 | 218 | * Install dependencies 219 | ```shell 220 | $ pnpm i 221 | ``` 222 | 223 | * Watch source changes and generate runnable bundle repeatedly 224 | ```shell 225 | $ npm run dev 226 | ``` 227 | 228 | * Tests 229 | ```shell 230 | $ npm test 231 | ``` 232 | 233 | * Watch source changes and run tests repeatedly 234 | ```shell 235 | $ npm run dev 236 | ``` 237 | 238 | * Package and generate minified bundles to dist directory 239 | ```shell 240 | $ npm run build 241 | ``` 242 | 243 | * Lint 244 | ```shell 245 | $ npm run lint 246 | ``` 247 | -------------------------------------------------------------------------------- /demo/colors.js: -------------------------------------------------------------------------------- 1 | var colors = []; 2 | 3 | for (var r = 15; r <= 255; r += 30) 4 | for (var g = 15; g <= 255; g += 30) 5 | for (var b = 15; b <= 255; b += 30) colors.push([r, g, b]); 6 | 7 | console.log(colors.length); 8 | 9 | function sortedColors(color) { 10 | var unsorted = colors.slice(); 11 | var last = color || [200, 200, 200]; 12 | var sorted = []; 13 | 14 | while (sorted.length < colors.length) { 15 | var minDist = Infinity, 16 | minIndex; 17 | for (var i = 0; i < unsorted.length; i++) { 18 | var c = unsorted[i]; 19 | var dist = distRieRGB(last[0], last[1], last[2], c[0], c[1], c[2]); 20 | if (dist < minDist) { 21 | minIndex = i; 22 | minDist = dist; 23 | } 24 | } 25 | sorted.push(unsorted[minIndex]); 26 | last = unsorted[minIndex]; 27 | unsorted.splice(minIndex, 1); 28 | } 29 | return sorted; 30 | } 31 | -------------------------------------------------------------------------------- /demo/grid.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |