### Initialize gdal3.js with Vite + Vue3 Source: https://gdal3.js.org/docs/index This example shows how to install gdal3.js and use it within a Vite + Vue3 project. It utilizes Vite's asset handling to import WASM, data, and JS worker URLs. ```bash pnpm add gdal3.js # or yarn add gdal3.js # or npm install gdal3.js ``` ```vue ``` -------------------------------- ### Install and Initialize gdal3.js in Node.js Source: https://gdal3.js.org/docs/index This snippet demonstrates how to install gdal3.js for a Node.js environment and initialize the Gdal object. This allows server-side geospatial processing using Gdal functionalities. ```bash pnpm add gdal3.js # or yarn add gdal3.js # or npm install gdal3.js ``` ```javascript const initGdalJs = require('gdal3.js/node'); initGdalJs().then((Gdal) => { console.log('Gdal initialized in Node.js:', Gdal); }); ``` -------------------------------- ### Install and Initialize gdal3.js with Webpack Source: https://gdal3.js.org/docs/index This demonstrates installing gdal3.js using npm/yarn/pnpm and initializing it within a Webpack build. It includes configuring CopyWebpackPlugin to handle WASM and data files. ```bash pnpm add gdal3.js # or yarn add gdal3.js # or npm install gdal3.js ``` ```javascript import initGdalJs from 'gdal3.js'; initGdalJs({ path: 'static' }).then((Gdal) => { console.log('Gdal initialized with Webpack:', Gdal); }); ``` ```javascript const CopyWebpackPlugin = require('copy-webpack-plugin'); // ... inside module.exports plugins: [ new CopyWebpackPlugin({ patterns: [ { from: '../node_modules/gdal3.js/dist/package/gdal3WebAssembly.wasm', to: 'static' }, { from: '../node_modules/gdal3.js/dist/package/gdal3WebAssembly.data', to: 'static' } ] }) ] ``` -------------------------------- ### Get Dataset Information with GDAL.js Source: https://gdal3.js.org/docs/module-f_getInfo This snippet demonstrates how to open a dataset and retrieve its information using GDAL.js. It first opens a file to get a dataset object, then passes this object to `Gdal.getInfo` to obtain detailed metadata. The resulting information is then logged to the console. ```javascript const dataset = (await Gdal.open("...")).datasets[0]; const datasetInfo = await Gdal.getInfo(dataset); console.log(datasetInfo); ``` -------------------------------- ### Get OGR Data Source Information with ogrinfo (JavaScript) Source: https://gdal3.js.org/docs/module-a_ogrinfo This snippet demonstrates how to use the ogrinfo function from the GDAL3.js library to retrieve information about an OGR-supported data source. It requires initializing GDAL3.js, opening a dataset, and then calling ogrinfo with the dataset object. The function returns a Promise that resolves with information about the data source. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.geojson')).datasets[0]; const info = await Gdal.ogrinfo(dataset); ``` -------------------------------- ### Get Output File Paths and Sizes with gdal3.js Source: https://gdal3.js.org/docs/module-f_getOutputFiles This snippet demonstrates how to use the `getOutputFiles` function from the gdal3.js library to retrieve information about files generated by GDAL operations. The function returns a promise resolving to an array of objects, each detailing the `path` and `size` of an output file. This functionality is primarily intended for browser environments, as it returns an empty array when run on Node.js. ```javascript const files = await Gdal.getOutputFiles(); files.forEach((fileInfo) => { console.log(`file path: ${fileInfo.path}, file size: ${fileInfo.size}`); }); ``` -------------------------------- ### Open File with Open Options (Node.js - JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens a file from the Node.js filesystem with specific open options. These options are passed as an array of strings to the driver, allowing customization of how the file is read, for example, specifying column names for CSV files. ```javascript const result = await Gdal.open('test/points.csv', ['X_POSSIBLE_NAMES=lng', 'Y_POSSIBLE_NAMES=lat']); ``` -------------------------------- ### Get File Bytes Source: https://gdal3.js.org/docs/module-f_getFileBytes Retrieves the byte content of a file given its path. This is useful for downloading or processing file data directly. ```APIDOC ## GET /getFileBytes ### Description Get bytes of the file. ### Method GET ### Endpoint /getFileBytes ### Parameters #### Query Parameters - **filePath** (string) - Required - The path of the file to be downloaded. ### Request Example ``` // Download file from "/output" path on the browser. const files = await Gdal.getOutputFiles(); const filePath = files[0].path; const fileBytes = Gdal.getFileBytes(filePath); const fileName = filePath.split('/').pop(); saveAs(fileBytes, filename); function saveAs(fileBytes, fileName) { const blob = new Blob([fileBytes]); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = fileName; link.click(); } ``` ### Response #### Success Response (200) - **data** (Uint8Array) - An array of bytes representing the file content. #### Response Example ```json { "data": "Uint8Array of file bytes" } ``` ``` -------------------------------- ### Get Raster Dataset Information using GDAL JS gdalinfo Source: https://gdal3.js.org/docs/module-a_gdalinfo This function retrieves detailed information about a GDAL-supported raster dataset. It requires the GDAL JS library to be initialized and a dataset object obtained through Gdal.open(). The function returns a promise that resolves with an object containing various dataset properties. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.tif')).datasets[0]; const info = await Gdal.gdalinfo(dataset); ``` -------------------------------- ### Get Dataset Information with GDAL3.js Source: https://gdal3.js.org/docs/index Retrieves detailed information about opened geospatial datasets using GDAL3.js. This function is applicable to both vector and raster datasets, providing insights into their structure and properties. ```javascript /* ======== Dataset Info ======== */ // https://gdal3.js.org/docs/module-f_getInfo.html const mbTilesDatasetInfo = await Gdal.getInfo(mbTilesDataset); // Vector const tifDatasetInfo = await Gdal.getInfo(tifDataset); // Raster ``` -------------------------------- ### Get File Bytes with GDAL3.js and Save to Browser Source: https://gdal3.js.org/docs/module-f_getFileBytes Retrieves the byte array of a file specified by its path using `Gdal.getFileBytes`. The returned byte array can then be used to create a downloadable file in the browser using a helper function `saveAs`. This function is useful for client-side file manipulation and download. ```javascript const files = await Gdal.getOutputFiles(); const filePath = files[0].path; const fileBytes = Gdal.getFileBytes(filePath); const fileName = filePath.split('/').pop(); saveAs(fileBytes, filename); function saveAs(fileBytes, fileName) { const blob = new Blob([fileBytes]); const link = document.createElement('a'); link.href = URL.createObjectURL(blob); link.download = fileName; link.click(); } ``` -------------------------------- ### Initialize GDAL3.js and Open Datasets Source: https://gdal3.js.org/docs/index Initializes the GDAL3.js library and opens multiple datasets, including MBTiles and GeoTIFF formats. It returns dataset objects that can be further processed. Assumes the existence of 'a.mbtiles' and 'b.tif' files. ```javascript const Gdal = await initGdalJs(); const files = ['a.mbtiles', 'b.tif']; // [Vector, Raster] const result = await Gdal.open(files); // https://gdal3.js.org/docs/module-f_open.html const mbTilesDataset = result.datasets[0]; const tifDataset = result.datasets[1]; ``` -------------------------------- ### Open File with Virtual File System Handler (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens a file using a specified virtual file system (VFS) handler, such as `/vsizip/` for zip archives. This is useful for accessing files within archives or other virtual file systems without extracting them. It accepts the file path, an empty options array, and the VFS handler. ```javascript const result = await Gdal.open(file, [], ['vsizip']); ``` -------------------------------- ### Initialize gdal3.js via Local Script Tag Source: https://gdal3.js.org/docs/index This code illustrates how to include gdal3.js locally and initialize the Gdal object. Ensure the 'gdal3.js' file is correctly linked in your HTML. ```html ``` -------------------------------- ### Open Single File from Node.js Filesystem (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens a single file directly from the Node.js filesystem. This method is straightforward, taking a file path string as an argument. It returns a Promise that resolves to a dataset list. ```javascript const result = await Gdal.open('test/polygon.geojson'); ``` -------------------------------- ### Initialize gdal3.js via CDN (No Worker) Source: https://gdal3.js.org/docs/index This snippet demonstrates how to include gdal3.js using a CDN and initialize it without using a web worker. This method is suitable for direct browser integration but may not be compatible with web worker environments. ```html ``` -------------------------------- ### FileInfo Type Definition Source: https://gdal3.js.org/docs/TypeDefs Defines the structure for basic file information, including its path and size. ```APIDOC ## FileInfo Type Definition ### Description Provides basic information about a file, such as its local path and size in bytes. ### Type `Object` ### Properties - **`path`** (`string`) - Local path of the opened file. - **`size`** (`number`) - File size in bytes. ``` -------------------------------- ### FilePath Type Definition Source: https://gdal3.js.org/docs/TypeDefs Defines the structure for file paths, including local, real, and potentially all available paths. ```APIDOC ## FilePath Type Definition ### Description Represents file path information, including the local path, the real (temporary) path, and an optional nested structure for all file paths. ### Type `Object` ### Properties - **`local`** (`string`) - Local path of the file. Example: `/output/polygon-line-point.mbtiles` - **`real`** (`string`) - Real (temporary) path of the file. Example: `/tmp/gdaljsGClKZk/polygon-line-point.mbtiles` - **`all`** (`FilePath`) - An optional, nested object containing all file paths. ``` -------------------------------- ### Open Multiple Files from Node.js Filesystem (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens multiple files from the Node.js filesystem simultaneously. The file paths are provided as an array of strings to the `Gdal.open()` function. This enables batch processing of multiple datasets. ```javascript const result = await Gdal.open(['test/polygon.geojson', 'test/line.geojson']); ``` -------------------------------- ### Open Network File (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens a file fetched from a network URL. The file content is obtained using `fetch`, converted to a Blob, and then created as a `File` object before being passed to `Gdal.open()`. This allows processing of remote files as if they were local. ```javascript const fileData = await fetch('test/polygon.geojson'); const file = new File([await fileData.blob()], "polygon.geojson"); const result = await Gdal.open(file); ``` -------------------------------- ### ogrinfo Source: https://gdal3.js.org/docs/module-a_ogrinfo Lists various information about an OGR-supported data source to stdout. It also allows editing data by executing SQL statements. ```APIDOC ## ogrinfo ### Description The ogrinfo program lists various information about an OGR-supported data source to stdout (the terminal). By executing SQL statements it is also possible to edit data. ### Method `POST` ### Endpoint `/websites/gdal3_js/ogrinfo` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dataset** (TypeDefs.Dataset) - Required - The dataset to get information from. - **options** (Array) - Optional - An array of options to configure the ogrinfo command. ### Request Example ```json { "dataset": "dataset_object", "options": ["option1", "option2"] } ``` ### Response #### Success Response (200) - **Promise.** - Returns information about the OGR-supported data source. #### Response Example ```json { "success": true, "data": "Information about the dataset..." } ``` ``` -------------------------------- ### Convert Geospatial Data with ogr2ogr (JavaScript) Source: https://gdal3.js.org/docs/module-a_ogr2ogr Demonstrates using the ogr2ogr function to convert a dataset to GeoJSON format with a specified target spatial reference system (EPSG:4326). Requires GDAL.js initialization and an opened dataset. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.mbtiles')).datasets[0]; const options = [ '-f', 'GeoJSON', '-t_srs', 'EPSG:4326' ]; const filePath = await Gdal.ogr2ogr(dataset, options); ``` -------------------------------- ### Initialize gdal3.js as an ES Module Source: https://gdal3.js.org/docs/index This snippet shows how to import and initialize gdal3.js as an ES module in modern JavaScript environments. This approach is recommended for projects using module bundlers. ```javascript import 'gdal3.js'; initGdalJs().then((Gdal) => { console.log('Gdal initialized as ES module:', Gdal); }); ``` -------------------------------- ### Convert Raster Data using gdal_translate in JavaScript Source: https://gdal3.js.org/docs/module-a_gdal_translate Demonstrates how to use the gdal_translate function to convert a raster dataset to a different format (e.g., PNG). It requires initializing the GDAL.js environment and opening a source dataset. The function accepts an array of options for controlling the translation process. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.tif')).datasets[0]; const options = [ '-of', 'PNG' ]; const filePath = await Gdal.gdal_translate(dataset, options); ``` -------------------------------- ### DatasetList Type Definition Source: https://gdal3.js.org/docs/TypeDefs Defines the structure for a list of GDAL Datasets, potentially including any errors encountered. ```APIDOC ## DatasetList Type Definition ### Description Represents a collection of GDAL datasets, along with any errors that may have occurred during their processing. ### Type `Object` ### Properties - **`datasets`** (`Array.`) - A list of Dataset objects. - **`errors`** (`Array.`) - A list of error messages. ``` -------------------------------- ### Open Single File from HTML Input (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens a single file selected by an HTML file input element. It takes the `FileList` object from the input's `target.files` property and passes it to `Gdal.open()`. The function returns a Promise that resolves to a dataset list. ```javascript function onFileChange({ target }) { const result = await Gdal.open(target.files[0]); } ``` -------------------------------- ### Open Multiple Files from HTML Input (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens multiple files selected by an HTML file input element with the `multiple` attribute. It passes the `FileList` object directly to `Gdal.open()`, which handles iterating through the files. The function returns a Promise that resolves to a dataset list. ```javascript function onFileChange({ target }) { const result = await Gdal.open(target.files); } ``` -------------------------------- ### f/getInfo Source: https://gdal3.js.org/docs/module-f_getInfo Lists information about a raster or vector dataset. ```APIDOC ## GET /f/getInfo ### Description Lists information about a raster/vector dataset. ### Method GET ### Endpoint /f/getInfo ### Parameters #### Query Parameters - **dataset** (TypeDefs.Dataset) - Required - The dataset to get information for. ### Response #### Success Response (200) - **Promise.** - A promise that resolves to an object containing file information. #### Response Example ```json { "type": "raster", "bandCount": 1, "width": 514, "height": 515, "projectionWkt": "PROJCS[\"unnamed\",GEOGCS[\"NAD27\",DATUM[\"North_American_Datum_1927\",SPHEROID[\"Clarke 1866\",6378206.4,294.978698213898,AUTHORITY[\"EPSG\",\"7008\"]],AUTHORITY[\"EPSG\",\"6267\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4267\"]],PROJECTION[\"Cylindrical_Equal_Area\"],PARAMETER[\"standard_parallel_1\",33.75],PARAMETER[\"central_meridian\",-117.333333333333],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"Easting\",EAST],AXIS[\"Northing\",NORTH]]", "coordinateTransform": { "0": -28493.166784412522, "1": 60.02213698319374, "2": 0, "3": 4255884.5438021915, "4": 0, "5": -60.02213698319374 }, "corners": [ [ -28493.166784412522, 4255884.5438021915 ], [ 2358.211624949061, 4255884.5438021915 ], [ 2358.211624949061, 4224973.143255847 ], [ -28493.166784412522, 4224973.143255847 ] ], "driverName": "GeoTIFF", "dsName": "/input/cea.tif" } ``` ```json { "type": "vector", "layerCount": 1, "featureCount": 2, "layers": [ { "name": "polygon", "featureCount": 2 } ], "dsName": "/input/polygon.geojson", "driverName": "GeoJSON" } ``` ``` -------------------------------- ### Dataset Type Definition Source: https://gdal3.js.org/docs/TypeDefs Defines the structure for a GDAL Dataset object, representing an opened data source. ```APIDOC ## Dataset Type Definition ### Description Represents an opened GDAL dataset, holding information about its memory address, file path, type, and metadata. ### Type `Object` ### Properties - **`pointer`** (`number`) - Memory address of this dataset allocated from the native code. It must be greater than zero. - **`path`** (`string`) - Local path of the opened file. - **`type`** (`string`) - Dataset type. (raster/vector) - **`info`** (`Object`) - Result of gdalinfo or ogrinfo. ``` -------------------------------- ### Reproject Coordinates with gdaltransform (JavaScript) Source: https://gdal3.js.org/docs/module-a_gdaltransform This snippet demonstrates how to use the gdaltransform utility in GDAL3.JS to reproject coordinates. It takes an array of input coordinates and an array of options to specify source and target spatial reference systems. The function returns a Promise that resolves with the reprojected coordinates. ```javascript const coords = [ [27.143757, 38.4247972], ]; const options = [ '-s_srs', 'EPSG:4326', '-t_srs', 'EPSG:3857', '-output_xy', ]; const newCoords = await Gdal.gdaltransform(coords, options); console.log(newCoords); // [ [ 3021629.2074563554, 4639610.441991095 ] ] ``` -------------------------------- ### DatasetInfo Type Definition Source: https://gdal3.js.org/docs/TypeDefs Defines the structure for detailed information about a GDAL Dataset, applicable to both raster and vector types. ```APIDOC ## DatasetInfo Type Definition ### Description Provides comprehensive information about a GDAL dataset, including its type, driver, dimensions (for raster), projection, coordinate transformation, and layer details (for vector). ### Type `Object` ### Properties - **`type`** (`string`) - Dataset type. (raster or vector) - **`dsName`** (`string`) - (raster/vector) Name of the data source. - **`driverName`** (`string`) - (raster/vector) Long name of a driver. - **`bandCount`** (`number`) - (raster only) Number of raster bands on this dataset. - **`width`** (`number`) - (raster only) Raster width in pixels. - **`height`** (`number`) - (raster only) Raster height in pixels. - **`projectionWkt`** (`string`) - (raster only) Projection definition string for this dataset. - **`coordinateTransform`** (`Array.`) - (raster only) Affine transformation coefficients. - **`corners`** (`Array.>`) - (raster only) Corner coordinates. - **`layerCount`** (`number`) - (vector only) Number of layers in this dataset. - **`featureCount`** (`number`) - (vector only) Number of features in this dataset. - **`layers`** (`Array.`) - (vector only) Layers - **`layers[].name`** (`string`) - Layer name - **`layers[].featureCount`** (`number`) - Feature count in this layer. ``` -------------------------------- ### Reproject Image using gdalwarp Source: https://gdal3.js.org/docs/module-a_gdalwarp Demonstrates how to reproject a GeoTIFF image to a new spatial reference system (EPSG:4326) using the gdalwarp function. This involves opening a dataset, defining warping options, and executing the warp operation. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.tif')).datasets[0]; const options = [ '-of', 'GTiff', '-t_srs', 'EPSG:4326' ]; const filePath = await Gdal.gdalwarp(dataset, options); ``` -------------------------------- ### Gdal.open Function Source: https://gdal3.js.org/docs/module-f_open The Gdal.open function is used to open files for processing. It supports various input types including FileList, File objects, network URLs, and local file paths. It also allows for custom open options and virtual file system handlers. ```APIDOC ## POST /f/open ### Description Opens files selected with an HTML input element, from the network, or from the filesystem using various options and virtual file system handlers. ### Method POST ### Endpoint /f/open ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **files** (FileList | Array. | Array. | string) - Required - The file(s) to open. Can be a FileList from an input element, an array of File objects, an array of file paths, or a single file path. - **openOptions** (Array.) - Optional - Open options passed to candidate drivers. - **VFSHandlers** (Array.) - Optional - List of Virtual File System handlers (e.g., ['vsizip']). See https://gdal.org/user/virtual_file_systems.html ### Request Example ```json { "files": "test/polygon.geojson", "openOptions": ["X_POSSIBLE_NAMES=lng", "Y_POSSIBLE_NAMES=lat"], "VFSHandlers": ["vsizip"] } ``` ### Response #### Success Response (200) - **datasetList** (TypeDefs.DatasetList) - A list of opened dataset objects. - **errorList** (Array) - A list of errors encountered during the opening process. #### Response Example ```json { "datasetList": [ { "filename": "/vsicurl/http/path/to/file.geojson", "driver": "GeoJSON", "size": 1024 } ], "errorList": [] } ``` ``` -------------------------------- ### Open File from Virtual gdal3.js Path (JavaScript) Source: https://gdal3.js.org/docs/module-f_open Opens files that have been previously processed and saved within the gdal3.js virtual file system, typically under `/input/` or `/output/`. This allows chaining operations and accessing intermediate or final results. ```javascript const result = await Gdal.open('/output/polygon.geojson'); const result2 = await Gdal.open('/input/polygon.geojson'); ``` -------------------------------- ### Convert Latitude/Longitude to Pixel Coordinates using GDAL Location Info Source: https://gdal3.js.org/docs/module-a_gdal_location_info This snippet demonstrates how to use the gdal_location_info function from the gdal3.js library to convert a latitude and longitude point into pixel and line coordinates within a geospatial dataset. It requires a dataset object and an array of coordinates (latitude, longitude) as input, returning a promise that resolves to an array containing the pixel and line values. The coordinates are assumed to be in WGS84 format. ```javascript const coords = [45.5,-108.5]; const pixelCoords = await Gdal.gdal_location_info(dataset,coords); console.log(pixelCoords); // { "pixel": 3256, "line": 8664 } ``` -------------------------------- ### Burn Vector Geometries to Raster using gdal_rasterize (JavaScript) Source: https://gdal3.js.org/docs/module-a_gdal_rasterize This snippet demonstrates how to use the gdal_rasterize function to convert vector data (like GeoJSON) into a raster format (like GeoTIFF). It requires initialization of GDAL3.js and specifies output format options. ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.geojson')).datasets[0]; const options = [ '-of', 'GTiff', '-co', 'alpha=yes' ]; const filePath = await Gdal.gdal_rasterize(dataset, options); ``` -------------------------------- ### gdal_location_info Source: https://gdal3.js.org/docs/module-a_gdal_location_info Converts a latitude and longitude into a pixel and line in the dataset. This function assumes WGS84 coordinates. ```APIDOC ## POST /gdal_location_info ### Description Converts a latitude and longitude into a pixel and line in the dataset. This utility always acts as if the -wgs84 option was passed to `gdalLocationinfo`. ### Method POST ### Endpoint /gdal_location_info ### Parameters #### Request Body - **dataset** (TypeDefs.Dataset) - Required - The dataset to be converted. - **coords** (Array.>) - Required - Coordinates to be converted. Example: [45.5,-108.5] lat/lon -wgs84. ### Request Example ```json { "dataset": "path/to/your/dataset.tif", "coords": [45.5, -108.5] } ``` ### Response #### Success Response (200) - **pixel** (number) - The pixel coordinate. - **line** (number) - The line coordinate. #### Response Example ```json { "pixel": 3256, "line": 8664 } ``` ``` -------------------------------- ### Warp (Reproject) TIFF with GDAL3.js Source: https://gdal3.js.org/docs/index Reprojects a raster dataset (TIFF) to a specified target spatial reference system (EPSG:4326) using GDAL3.js's `gdalwarp` function. This is useful for aligning data to different coordinate systems. The output is retrieved as bytes. ```javascript /* ======== Warp (reprojection) ======== */ const options = [ // https://gdal.org/programs/gdalwarp.html#description '-of', 'GTiff', '-t_srs', 'EPSG:4326' ]; const output = await Gdal.gdalwarp(tifDataset, options); // https://gdal3.js.org/docs/module-a_gdalwarp.html const bytes = await Gdal.getFileBytes(output); // https://gdal3.js.org/docs/module-f_getFileBytes.html ``` -------------------------------- ### Raster Translation (TIFF to PNG) with GDAL3.js Source: https://gdal3.js.org/docs/index Translates a raster dataset from TIFF format to PNG using GDAL3.js's `gdal_translate` function. This allows for format conversion and specification of output options. The output is obtained as bytes. ```javascript /* ======== Raster translate (tif -> png) ======== */ const options = [ // https://gdal.org/programs/gdal_translate.html#description '-of', 'PNG' ]; const output = await Gdal.gdal_translate(tifDataset, options); // https://gdal3.js.org/docs/module-a_gdal_translate.html const bytes = await Gdal.getFileBytes(output); // https://gdal3.js.org/docs/module-f_getFileBytes.html ``` -------------------------------- ### Rasterize MBTiles to TIFF with GDAL3.js Source: https://gdal3.js.org/docs/index Performs rasterization on a vector dataset (MBTiles) into a TIFF format using GDAL3.js's `gdal_rasterize` function. This operation converts vector features into raster pixels, with options for output format and creation. ```javascript /* ======== Rasterize (mbtiles -> tif) ======== */ const options = [ // https://gdal.org/programs/gdal_rasterize.html#description '-of', 'GTiff', '-co', 'alpha=yes' ]; const output = await Gdal.gdal_rasterize(mbTilesDataset, options); // https://gdal3.js.org/docs/module-a_gdal_rasterize.html const bytes = await Gdal.getFileBytes(output); // https://gdal3.js.org/docs/module-f_getFileBytes.html ``` -------------------------------- ### Vector Translation (MBTiles to GeoJSON) with GDAL3.js Source: https://gdal3.js.org/docs/index Translates a vector dataset from MBTiles format to GeoJSON using GDAL3.js's `ogr2ogr` function. This process involves specifying output format and target SRS. The output is retrieved as bytes. ```javascript /* ======== Vector translate (mbtiles -> geojson) ======== */ const options = [ // https://gdal.org/programs/ogr2ogr.html#description '-f', 'GeoJSON', '-t_srs', 'EPSG:4326' ]; const output = await Gdal.ogr2ogr(mbTilesDataset, options); // https://gdal3.js.org/docs/module-a_ogr2ogr.html const bytes = await Gdal.getFileBytes(output); // https://gdal3.js.org/docs/module-f_getFileBytes.html ``` -------------------------------- ### gdal_translate Source: https://gdal3.js.org/docs/module-a_gdal_translate Converts raster data between different formats, with options for subsetting, resampling, and rescaling. ```APIDOC ## gdal_translate ### Description Converts raster data between different formats, potentially performing operations like subsetting, resampling, and rescaling pixels. ### Method ``` await Gdal.gdal_translate(dataset, options, outputName) ``` ### Endpoint N/A (This is a JavaScript function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dataset** (TypeDefs.Dataset) - Required - The dataset to be converted. - **options** (Array) - Optional - An array of options for the translation. See GDAL translate documentation for details (https://gdal.org/programs/gdal_translate.html#description). - **outputName** (string) - Optional - The destination file name without extension. ### Request Example ```javascript const Gdal = await initGdalJs(); const dataset = (await Gdal.open('data.tif')).datasets[0]; const options = [ '-of', 'PNG' ]; const filePath = await Gdal.gdal_translate(dataset, options); ``` ### Response #### Success Response - **filePath** (Promise.) - A promise that resolves with the paths of the created files. #### Response Example ```json { "filePath": "path/to/output.png" } ``` ``` -------------------------------- ### Close GDAL3.js Datasets Source: https://gdal3.js.org/docs/index Closes all opened geospatial datasets managed by GDAL3.js. It's important to release resources by closing datasets when they are no longer needed. ```javascript // Close all datasets. // https://gdal3.js.org/docs/module-f_close.html Gdal.close(mbTilesDataset); Gdal.close(tifDataset); ``` -------------------------------- ### Coordinate Transformation with GDAL3.js Source: https://gdal3.js.org/docs/index Transforms coordinates from one spatial reference system (EPSG:4326) to another (EPSG:3857) using GDAL3.js's `gdaltransform` function. This utility is essential for working with data in different projections. ```javascript /* ======== Transform (Coordinate) ======== */ const coords = [ [27.143757, 38.4247972], ]; const options = [ // https://gdal.org/programs/gdaltransform.html#description '-s_srs', 'EPSG:4326', '-t_srs', 'EPSG:3857', '-output_xy', ]; const newCoords = await Gdal.gdaltransform(coords, options); // https://gdal3.js.org/docs/module-a_gdaltransform.html console.log(newCoords); // [ [ 3021629.2074563554, 4639610.441991095 ] ] ``` -------------------------------- ### Close GDAL Dataset (JavaScript) Source: https://gdal3.js.org/docs/module-f_close Frees the memory associated with a GDAL dataset. This function should be called when operations on the dataset are complete to prevent memory leaks. It takes a GDAL dataset object as input and returns a Promise that resolves when the dataset is closed. ```javascript Gdal.close(dataset); ``` -------------------------------- ### Gdal.close(dataset) Source: https://gdal3.js.org/docs/module-f_close Closes a GDAL dataset, freeing all associated memory. It is crucial to call this function when you are done with a dataset to prevent memory leaks. ```APIDOC ## Gdal.close(dataset) ### Description Closes the dataset. The memory associated with the dataset will be freed. Datasets **must** be closed when you're finished with them, or the memory consumption will grow forever. ### Method `Gdal.close` (Function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body * **dataset** (`TypeDefs.Dataset`) - Required - Dataset to be closed. ### Request Example ```javascript Gdal.close(dataset); ``` ### Response #### Success Response (void) This function returns a Promise that resolves with `void` upon successful closing of the dataset. #### Response Example ```json // No response body, Promise resolves to undefined ``` #### Error Handling // Specific error handling details not provided in the source text. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.