TypeScript में डेटा लेयर विज़ुअलाइज़ करें

डेटा लेयर रिस्पॉन्स, GeoTIFF फ़ाइल में होता है. अपनी पसंद का डेटा पाने के लिए, अपने टूल का इस्तेमाल किया जा सकता है. उदाहरण के लिए, मान लें कि आपके पास एक GeoTIFF इमेज है जिसमें पूरे इलाके के तापमान की वैल्यू दिखाई गई है. TypeScript का इस्तेमाल करके, कम तापमान को नीले रंग और ज़्यादा तापमान को लाल रंग से मैप किया जा सकता है. इससे एक रंगीन इमेज बनाई जा सकती है, जिसे तुरंत तापमान के पैटर्न देखने के लिए आसानी से समझा जा सकता है.

इस TypeScript कोड को GeoTIFFs नाम की खास इमेज फ़ाइलें लेने और उन्हें किसी एचटीएमएल कैनवस (जैसे कोई डिजिटल पिक्चर फ़्रेम) का इस्तेमाल करके वेबसाइट पर दिखाने के लिए डिज़ाइन किया गया है. कोड में इन कॉम्पोनेंट का इस्तेमाल किया जाता है:

  • GeoTIFF इमेज: GeoTIFFs इमेज डेटा की कई परतें सेव कर सकता है. इससे वे मैप या वैज्ञानिक विश्लेषण के लिए काम की बन जाती हैं.
  • आरजीबी इमेज: हम इस तरह की इमेज को सबसे ज़्यादा जानते हैं (जैसे कि फ़ोटो). हर पिक्सल में लाल, हरे, और नीले रंग की वैल्यू होती हैं, जिनसे रंग तय होता है.
  • पैलेट: ये पेंट के सेट की तरह होते हैं. इनमें पहले से तय रंगों की एक सूची होती है, जिनका इस्तेमाल इमेज को रंग देने के लिए किया जा सकता है.

यह पेज दिखाता है कि पिक्सल डेटा की वैल्यू (किसी डिजिटल इमेज के अलग-अलग पिक्सल में सेव की गई जानकारी, जिसमें रंग की वैल्यू और अन्य एट्रिब्यूट शामिल हैं) कैसे मिलता है. साथ ही, यह GeoTIFF से अक्षांश और देशांतर का हिसाब लगाता है और उसे TypeScript ऑब्जेक्ट में सेव करता है.

नीचे दिया गया कोड स्निपेट उस टाइप की परिभाषा दिखाता है जहां हम इस उदाहरण में, दिलचस्पी का डेटा सेव करते हैं. फ़ील्ड और डेटा का प्रकार TypeScript में "type" है. इस उदाहरण में हमने टाइप की जांच करने, टाइप की गड़बड़ियों को कम करने, और आपके कोड को भरोसेमंद बनाने की अनुमति देने का विकल्प चुना है, जिससे आपके कोड को मैनेज करना आसान हो जाता है. उस डेटा को स्टोर करने के लिए एक प्रकार तय करें, ताकि पिक्सल वैल्यू और अक्षांश/देशांतर बाउंडिंग बॉक्स जैसी कई वैल्यू दी जा सकें.

export interface GeoTiff {
  width: number;
  height: number;
  rasters: Array<number>[];
  bounds: Bounds;
}

मुख्य फ़ंक्शन

कोड में ऐसे कई फ़ंक्शन होते हैं जो एक साथ काम करते हैं:

  • renderRGB: एक आरजीबी GeoTIFF इमेज और वैकल्पिक तौर पर एक मास्क (पारदर्शिता के लिए) बनाता है. यह वेबसाइट कैनवस एलिमेंट बनाता है, GeoTIFF के हर पिक्सल को लूप करता है. साथ ही, उससे जुड़े पिक्सल को कैनवस पर कलर करता है.
  • renderPalette: डेटा की एक लेयर और रंग पैलेट के साथ GeoTIFF लेता है, पैलेट में मौजूद रंगों के हिसाब से GeoTIFF डेटा वैल्यू को मैप करता है, पैलेट के रंगों का इस्तेमाल करके नई आरजीबी इमेज बनाता है और कैनवस पर इमेज दिखाने के लिए renderRGB कॉल करता है.

/**
 * Renders an RGB GeoTiff image into an HTML canvas.
 *
 * The GeoTiff image must include 3 rasters (bands) which
 * correspond to [Red, Green, Blue] in that order.
 *
 * @param  {GeoTiff} rgb   GeoTiff with RGB values of the image.
 * @param  {GeoTiff} mask  Optional mask for transparency, defaults to opaque.
 * @return {HTMLCanvasElement}  Canvas element with the rendered image.
 */
export function renderRGB(rgb: GeoTiff, mask?: GeoTiff): HTMLCanvasElement {
  // Create an HTML canvas to draw the image.
  // https://www.w3schools.com/tags/canvas_createimagedata.asp
  const canvas = document.createElement('canvas');

  // Set the canvas size to the mask size if it's available,
  // otherwise set it to the RGB data layer size.
  canvas.width = mask ? mask.width : rgb.width;
  canvas.height = mask ? mask.height : rgb.height;

  // Since the mask size can be different than the RGB data layer size,
  // we calculate the "delta" between the RGB layer size and the canvas/mask
  // size. For example, if the RGB layer size is the same as the canvas size,
  // the delta is 1. If the RGB layer size is smaller than the canvas size,
  // the delta would be greater than 1.
  // This is used to translate the index from the canvas to the RGB layer.
  const dw = rgb.width / canvas.width;
  const dh = rgb.height / canvas.height;

  // Get the canvas image data buffer.
  const ctx = canvas.getContext('2d')!;
  const img = ctx.getImageData(0, 0, canvas.width, canvas.height);

  // Fill in every pixel in the canvas with the corresponding RGB layer value.
  // Since Javascript doesn't support multidimensional arrays or tensors,
  // everything is stored in flat arrays and we have to keep track of the
  // indices for each row and column ourselves.
  for (let y = 0; y < canvas.height; y++) {
    for (let x = 0; x < canvas.width; x++) {
      // RGB index keeps track of the RGB layer position.
      // This is multiplied by the deltas since it might be a different
      // size than the image size.
      const rgbIdx = Math.floor(y * dh) * rgb.width + Math.floor(x * dw);
      // Mask index keeps track of the mask layer position.
      const maskIdx = y * canvas.width + x;

      // Image index keeps track of the canvas image position.
      // HTML canvas expects a flat array with consecutive RGBA values.
      // Each value in the image buffer must be between 0 and 255.
      // The Alpha value is the transparency of that pixel,
      // if a mask was not provided, we default to 255 which is opaque.
      const imgIdx = y * canvas.width * 4 + x * 4;
      img.data[imgIdx + 0] = rgb.rasters[0][rgbIdx]; // Red
      img.data[imgIdx + 1] = rgb.rasters[1][rgbIdx]; // Green
      img.data[imgIdx + 2] = rgb.rasters[2][rgbIdx]; // Blue
      img.data[imgIdx + 3] = mask // Alpha
        ? mask.rasters[0][maskIdx] * 255
        : 255;
    }
  }

  // Draw the image data buffer into the canvas context.
  ctx.putImageData(img, 0, 0);
  return canvas;
}

हेल्पर फ़ंक्शन

इस कोड में कई हेल्पर फ़ंक्शन भी शामिल होते हैं, जो अतिरिक्त फ़ंक्शन चालू करते हैं:

  • createPalette: हेक्साडेसिमल कलर कोड की सूची के आधार पर, इमेज को कलर करने में इस्तेमाल किए जाने वाले रंगों की सूची बनाता है.
  • colorToRGB: "#FF00FF" जैसे कलर कोड को उसके लाल, हरे, और नीले कॉम्पोनेंट में बदलता है.
  • normalize, lerp, clamp: इमेज प्रोसेसिंग के लिए गणित से जुड़े हेल्पर फ़ंक्शन.

/**
 * Renders a single value GeoTiff image into an HTML canvas.
 *
 * The GeoTiff image must include 1 raster (band) which contains
 * the values we want to display.
 *
 * @param  {GeoTiff}  data    GeoTiff with the values of interest.
 * @param  {GeoTiff}  mask    Optional mask for transparency, defaults to opaque.
 * @param  {string[]} colors  Hex color palette, defaults to ['000000', 'ffffff'].
 * @param  {number}   min     Minimum value of the data range, defaults to 0.
 * @param  {number}   max     Maximum value of the data range, defaults to 1.
 * @param  {number}   index   Raster index for the data, defaults to 0.
 * @return {HTMLCanvasElement}  Canvas element with the rendered image.
 */
export function renderPalette({
  data,
  mask,
  colors,
  min,
  max,
  index,
}: {
  data: GeoTiff;
  mask?: GeoTiff;
  colors?: string[];
  min?: number;
  max?: number;
  index?: number;
}): HTMLCanvasElement {
  // First create a palette from a list of hex colors.
  const palette = createPalette(colors ?? ['000000', 'ffffff']);
  // Normalize each value of our raster/band of interest into indices,
  // such that they always map into a value within the palette.
  const indices = data.rasters[index ?? 0]
    .map((x) => normalize(x, max ?? 1, min ?? 0))
    .map((x) => Math.round(x * (palette.length - 1)));
  return renderRGB(
    {
      ...data,
      // Map each index into the corresponding RGB values.
      rasters: [
        indices.map((i: number) => palette[i].r),
        indices.map((i: number) => palette[i].g),
        indices.map((i: number) => palette[i].b),
      ],
    },
    mask,
  );
}

/**
 * Creates an {r, g, b} color palette from a hex list of colors.
 *
 * Each {r, g, b} value is a number between 0 and 255.
 * The created palette is always of size 256, regardless of the number of
 * hex colors passed in. Inbetween values are interpolated.
 *
 * @param  {string[]} hexColors  List of hex colors for the palette.
 * @return {{r, g, b}[]}         RGB values for the color palette.
 */
export function createPalette(hexColors: string[]): { r: number; g: number; b: number }[] {
  // Map each hex color into an RGB value.
  const rgb = hexColors.map(colorToRGB);
  // Create a palette with 256 colors derived from our rgb colors.
  const size = 256;
  const step = (rgb.length - 1) / (size - 1);
  return Array(size)
    .fill(0)
    .map((_, i) => {
      // Get the lower and upper indices for each color.
      const index = i * step;
      const lower = Math.floor(index);
      const upper = Math.ceil(index);
      // Interpolate between the colors to get the shades.
      return {
        r: lerp(rgb[lower].r, rgb[upper].r, index - lower),
        g: lerp(rgb[lower].g, rgb[upper].g, index - lower),
        b: lerp(rgb[lower].b, rgb[upper].b, index - lower),
      };
    });
}

/**
 * Convert a hex color into an {r, g, b} color.
 *
 * @param  {string} color  Hex color like 0099FF or #0099FF.
 * @return {{r, g, b}}     RGB values for that color.
 */
export function colorToRGB(color: string): { r: number; g: number; b: number } {
  const hex = color.startsWith('#') ? color.slice(1) : color;
  return {
    r: parseInt(hex.substring(0, 2), 16),
    g: parseInt(hex.substring(2, 4), 16),
    b: parseInt(hex.substring(4, 6), 16),
  };
}

/**
 * Normalizes a number to a given data range.
 *
 * @param  {number} x    Value of interest.
 * @param  {number} max  Maximum value in data range, defaults to 1.
 * @param  {number} min  Minimum value in data range, defaults to 0.
 * @return {number}      Normalized value.
 */
export function normalize(x: number, max: number = 1, min: number = 0): number {
  const y = (x - min) / (max - min);
  return clamp(y, 0, 1);
}

/**
 * Calculates the linear interpolation for a value within a range.
 *
 * @param  {number} x  Lower value in the range, when `t` is 0.
 * @param  {number} y  Upper value in the range, when `t` is 1.
 * @param  {number} t  "Time" between 0 and 1.
 * @return {number}    Inbetween value for that "time".
 */
export function lerp(x: number, y: number, t: number): number {
  return x + t * (y - x);
}

/**
 * Clamps a value to always be within a range.
 *
 * @param  {number} x    Value to clamp.
 * @param  {number} min  Minimum value in the range.
 * @param  {number} max  Maximum value in the range.
 * @return {number}      Clamped value.
 */
export function clamp(x: number, min: number, max: number): number {
  return Math.min(Math.max(x, min), max);
}