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

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