make-geo-picking-texture-ogc.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <canvas></canvas>
  2. <script src="ogc-parser.js"></script>
  3. <script>
  4. function wait(ms = 0) {
  5. return new Promise((resolve) => {
  6. setTimeout(resolve, ms);
  7. });
  8. }
  9. // # need to draw to 2nd canvas, then shave off non perfect pixels
  10. async function main() {
  11. const ctx = document.querySelector('canvas').getContext('2d');
  12. ctx.canvas.width = 2048;
  13. ctx.canvas.height = 2048;
  14. ctx.fillStyle = '#444';
  15. ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  16. ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
  17. ctx.scale(ctx.canvas.width / 360, ctx.canvas.height / -180);
  18. function setColor(color) {
  19. ctx.fillStyle = color;
  20. ctx.strokeStyle = color;
  21. }
  22. function setEraseColor() {
  23. setColor('#000');
  24. }
  25. const handlers = {
  26. point,
  27. lineString,
  28. polygon,
  29. multiPoint,
  30. multiLineString,
  31. multiPolygon,
  32. };
  33. function point(d) {
  34. ctx.fillRect(...d.point, 1, 1);
  35. }
  36. function setPathFromPoints(points, backward = false) {
  37. if (backward) {
  38. const numPoints = points.length / 2;
  39. const lastPoint = numPoints - 1;
  40. ctx.moveTo(...points.slice(lastPoint * 2, lastPoint * 2 + 2));
  41. for (let i = lastPoint - 1; i >= 0; i -= 2) {
  42. ctx.lineTo(...points.slice(i * 2, i * 2 + 2));
  43. }
  44. } else {
  45. ctx.moveTo(...points.slice(0, 2));
  46. for (let i = 2; i < points.length; i += 2) {
  47. ctx.lineTo(...points.slice(i, i + 2));
  48. }
  49. }
  50. }
  51. function stroke(ctx) {
  52. ctx.save();
  53. ctx.setTransform(1, 0, 0, 1, 0, 0);
  54. ctx.stroke();
  55. ctx.restore();
  56. }
  57. function lineString(d) {
  58. ctx.beginPath();
  59. setPathFromPoints(d.points);
  60. stroke(ctx);
  61. }
  62. function polygon(d) {
  63. ctx.beginPath();
  64. d.rings.forEach((ring, ndx) => {
  65. setPathFromPoints(ring, ndx !== 0);
  66. ctx.closePath();
  67. });
  68. ctx.fill();
  69. stroke(ctx);
  70. }
  71. function multiPoint(d) {
  72. for (let i = 0; i < d.points.length; i += 2) {
  73. ctx.fillRect(...d.points.slice(i, i + 2));
  74. }
  75. }
  76. function multiLineString(d) {
  77. d.lineStrings.forEach((lineString) => {
  78. ctx.beginPath();
  79. setPathFromPoints(lineString);
  80. stroke(ctx);
  81. });
  82. }
  83. function multiPolygon(d) {
  84. d.polygons.forEach((polygon) => {
  85. ctx.beginPath();
  86. polygon.forEach((ring, ndx) => {
  87. setPathFromPoints(ring, ndx !== 0);
  88. });
  89. ctx.fill();
  90. stroke(ctx);
  91. });
  92. }
  93. const colors = {};
  94. const req = await fetch('level1.json');
  95. const areas = await req.json();
  96. let min = [Number.MAX_VALUE, Number.MAX_VALUE];
  97. let max = [Number.MIN_VALUE, Number.MIN_VALUE];
  98. console.log('num areas:', areas.length);
  99. for (let ndx = 0; ndx < areas.length; ++ndx) {
  100. const area = areas[ndx];
  101. try {
  102. const buf = new Uint8Array(base64ToUint8Array(area.geom));
  103. area.geom = ogcParser.parse(buf);
  104. } catch (e) {
  105. console.log('ERROR:', e);
  106. console.log(JSON.stringify(area, null, 2));
  107. throw e;
  108. }
  109. if (!colors[area.NAME_0]) {
  110. colors[area.NAME_0] = rgb(r(), r(), r());
  111. }
  112. const color = colors[area.NAME_0];
  113. console.log(ndx, area.NAME_0);
  114. area.geom.primitives.forEach((primitive) => {
  115. const fn = handlers[primitive.type];
  116. setColor(color);
  117. fn(primitive);
  118. });
  119. min[0] = Math.min(min[0], area.geom.envelope[0]);
  120. min[0] = Math.min(min[0], area.geom.envelope[1]);
  121. min[1] = Math.min(min[1], area.geom.envelope[2]);
  122. min[1] = Math.min(min[1], area.geom.envelope[3]);
  123. max[0] = Math.max(max[0], area.geom.envelope[0]);
  124. max[0] = Math.max(max[0], area.geom.envelope[1]);
  125. max[1] = Math.max(max[1], area.geom.envelope[2]);
  126. max[1] = Math.max(max[1], area.geom.envelope[3]);
  127. if (ndx % 100 === 99) {
  128. await wait();
  129. }
  130. }
  131. console.log('min', min);
  132. console.log('max', max);
  133. }
  134. function r(min, max) {
  135. if (min === undefined) {
  136. min = 0;
  137. max = 1;
  138. } else if (max === undefined){
  139. max = min;
  140. min = 0;
  141. }
  142. return min + Math.random() * (max - min);
  143. }
  144. function rgb(r, g, b) {
  145. return `rgb(${r * 255 | 0},${g * 255 | 0},${b * 255 | 0})`;
  146. }
  147. function hsl(h, s, l) {
  148. return `hsl(${h * 360 | 0},${s * 100 | 0}%,${l * 100 | 0}%)`;
  149. }
  150. function base64ToUint8Array(base64) {
  151. const raw = window.atob(base64);
  152. const rawLength = raw.length;
  153. const array = new Uint8Array(new ArrayBuffer(rawLength));
  154. for(let i = 0; i < rawLength; ++i) {
  155. array[i] = raw.charCodeAt(i);
  156. }
  157. return array;
  158. }
  159. main();
  160. </script>