Classes

This is the main class of this module. It allows you to download tiles of a Web Map Service (WMS).

new WMSDownloader(options: object?)
Parameters
options (object?) Config options of the WMSDownloader instance. See examples examples and json schema .
Example
// create a WMSDownloader instance with default options
const WMSDownloader = require('wms-downloader');
const dl = new WMSDownloader();

// create a WMSDownloader instance with custom options
const WMSDownloader = require('wms-downloader');
const dl = new WMSDownloader({
 'request': {
   'userAgent': 'Mozilla/5.0 QGIS/2.18.3',
   'timeout': 30000,
   'proxy': {
     'http': {
       'host': '10.208.20.71',
       'port': 4239,
       'user': 'NameOfUser',
       'password': 'PasswordOfUser',
       'exclude': ['http://12.101.20.18/', 'http://12.208.28.48/']
     }
   }
 }js
});
Static Members
SUPPORTED_FORMATS
Instance Members
getRequestObject(url)
start(options, callback)
cancel(id, callback)
getProgress(id)

helper functions

Creates a full GetMap request from the following parameters.

createGetMap(wms: Object, bbox: String, width: (Number | String), height: (Number | String)): String
Parameters
wms (Object) Object with the GetMap base url and necessary key value pairs (kvp) like { 'getmap': { 'url':'http://', kvp: {'key': 'value' } } }
bbox (String) BBOX of the GetMap request
width ((Number | String)) Width of the GetMap request
height ((Number | String)) Height of the GetMap request
Returns
String: Complete GetMap request
Example
const wms = {
  'getmap': {
    'url': 'http://www.bielefeld01.de/md/WMS/statistische_gebietsgliederung/02?',
    'kvp': {
      'SERVICE': 'WMS',
      'VERSION': '1.3.0',
      'REQUEST': 'GetMap',
      'FORMAT': 'image/png',
      'TRANSPARENT': 'true',
      'LAYERS': 'stadtbezirke_pl',
      'CRS': 'EPSG:25832',
      'STYLES': ''
    }
  }
};

const width = 1048;
const height = 953;
const bbox = '458163.5475413472,5754265.480964899,478190.04895206343,5772476.602953842';

let getMapUrl = createGetMap(wms, bbox, width, height);
console.log(getMapUrl);
//http://www.bielefeld01.de/md/WMS/statistische_gebietsgliederung/02?SERVICE=WMS&VERSION=1.3.0&REQUEST=GetMap&FORMAT=image/png&TRANSPARENT=true&LAYERS=stadtbezirke_pl&CRS=EPSG:25832&STYLES=&BBOX=458163.5475413472,5754265.480964899,478190.04895206343,5772476.602953842&WIDTH=1048&HEIGHT=953&

Creates world file content.

createWorldFile(x0: Number, y0: Number, res: Number): string
Parameters
x0 (Number) X value of start point (top-left)
y0 (Number) Y value of start point (top-left)
res (Number) Ground resolution
Returns
string: Content of world file
Example
const x0 = 458000;
const y0 = 5754000;
const res = 1;

let wordlFileContent = createWorldFile(x0, y0, res);
console.log(wordlFileContent);
// 1
// 0.0
// 0.0
// -1
// 458000.5
// 5753999.5

Crops a tile with the gutter size.

cropTile(oldFile: String, newFile: String, tileSizePx: Number, gutterSizePx: Number, callback: Function)
Parameters
oldFile (String) File to be crop
newFile (String) New cropped file
tileSizePx (Number) Size of the new tile
gutterSizePx (Number) Size of gutter in the old tile
callback (Function) function(err) {}
Example
const oldFile = __dirname + '/cropTileOld.png';
const newFile = __dirname + '/cropTileNew.png';
const tileSizePx = 1000; // Size of the new tile
const gutterSizePx = 150;

cropTile(oldFile, newFile, tileSizePx, gutterSizePx, (err)=>{
  if(err){
    console.error(err);
  }else{
    console.log('Tile cropped!');
  }
});

Calculates the ground resolution from scale and dpi. If groundResolution is not set, it will be calculated and set into the array.

determineGroundResolution(res: Array<Object>)
Parameters
res (Array<Object>) Resolutions defined with scales like [ {'dpi': 72, 'scale': 5000 }, {'dpi': 72, 'scale': 10000 } ]
Example
const res = [ {'dpi': 72, 'scale': 5000 },  {'dpi': 72, 'scale': 10000 } ];

determineGroundResolution(res);
console.log(res);
// [ { dpi: 72, scale:  5000, groundResolution: 1.7638888888888888 },
//   { dpi: 72, scale: 10000, groundResolution: 3.5277777777777777 } ]

Returns format details of a supported image format (for example image/png).

getFormatDetails(mimeType: String): (null | Object)
Parameters
mimeType (String) MIME-Type of a supported image format.
Returns
(null | Object): Format details like {'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}
Example
console.log(getFormatDetails('image/png'));
//{'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}

Calculates the number of tiles of a task.

getNumberOfTiles(options: Object): Number
Parameters
options (Object) Task options (see example)
Returns
Number: Number of all tiles of this task
Example
// task options
const task = {
  'task': {
    'area': {
      'bbox': {
        'xmin': 455000,
        'ymin': 5750000,
        'xmax': 479000,
        'ymax': 5774000
      }
    }
  },
  'tiles': {
    'maxSizePx': 2500,
    'gutterPx': 250,
    'resolutions': [
      {
        'id': 'id_of_groundResolution_10',
        'groundResolution': 10
      },
      {
        'id': 'id_of_resolution_25000',
        'scale': 25000,
        'dpi': 72
      }
    ]
  },
  'wms': [
    {
      'id': 'id_of_wms_stadtbezirke'
    }
  ]
};

// all tiles of this task
const count = getNumberOfTiles(task);
console.log(count); // 8

Returns the correct request object with the right proxy settings.

getRequestObject(config: any, url: String): Object
Parameters
config (any)
url (String) URL of tile
Returns
Object: Object from request module (let request = require('request');)

Returns an array with all supported formats

getSupportedFormats(): Array<Object>
Returns
Array<Object>: Array of all supported formats like [{'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}]
Example
console.log(getSupportedFormats());
//[ { title: 'PNG',
//    fileExt: 'png',
//    worldFileExt: 'pgw',
//    mimeType: 'image/png' },
//  { title: 'PNG 8-Bit',
//    fileExt: 'png',
//    worldFileExt: 'pgw',
//    mime_type: 'image/png; mode=8bit' },
//  { title: 'JPG',
//    fileExt: 'jpg',
//    worldFileExt: 'jgw',
//    mimeType: 'image/jpeg' },
//  { title: 'GIF',
//    fileExt: 'gif',
//    worldFileExt: 'gfw',
//    mimeType: 'image/gif' },
//  { title: 'TIFF',
//    fileExt: 'tif',
//    worldFileExt: 'tfw',
//    mimeType: 'image/tiff' },
//  { title: 'SVG',
//    fileExt: 'svg',
//    worldFileExt: 'sgw',
//    mimeType: 'image/svg+xml' } ]

It handles recursive all resolutions of a task.

handleResolution(options: Object, ws: String, resIdx: Number, config: Object, progress: Array, callback: Function)
Parameters
options (Object)
ws (String) Task workspace
resIdx (Number) Index of resolution
config (Object) See options of the WMSDownloader constructor
progress (Array) Array of the progress of all WMSDownloader tasks.
callback (Function) function(err){}

It handles a download task of Web Map Services.

handleTask(options: Object, config: Object, progress: Array, callback: Function)
Parameters
options (Object)
config (Object) See options of the WMSDownloader constructor
progress (Array) Array of the progress of all WMSDownloader tasks.
callback (Function) function(err){}

It handles recursive all tiles of a resolution of a Web Map Service.

handleTiles(options: Object, wms: Object, ws: String, tiles: Object, xIdx: Number, yIdx: Number, res: Number, config: Object, progress: Array, callback: Function)
Parameters
options (Object)
wms (Object) WMS object
ws (String) WMS workspace
tiles (Object) Object with tile parameters
xIdx (Number) X-Index of tile
yIdx (Number) Y-Index of tile
res (Number) Ground resolution
config (Object) See options of the WMSDownloader constructor
progress (Array) Array of the progress of all WMSDownloader tasks.
callback (Function) function(err){}

It handles recursive all Web Map Services of a resolution.

handleWMS(options: Object, ws: String, res: Object, wmsIdx: Number, config: Object, progress: Array, callback: Function)
Parameters
options (Object)
ws (String) Resolution workspace
res (Object) Resolution object
wmsIdx (Number) Index of WMS
config (Object) See options of the WMSDownloader constructor
progress (Array) Array of the progress of all WMSDownloader tasks.
callback (Function) function(err){}

Validates a json by a json schema.

isValid(instance: Object, schema: Object): (Boolean | Array<ValidationError>)
Parameters
instance (Object) json
schema (Object) json schema
Returns
(Boolean | Array<ValidationError>): Is valid: true; Is invalid: Array of Errors
Example
const json1 = { name: 'VW Golf', price: 10000 }; // valid
const json2 = { name: 'VW Passat', price: '20000 EUR'}; // invalid

const schema = {
  'type': 'object',
  'properties': {
    'name': {
      'type': 'string'
    },
    'price': {
      'type': 'number'
    }
  },
  'required': [
    'name',
    'price'
  ]
};

const validationJson1 = isValid(json1, schema);
if (validationJson1 === true) {
  console.log('json1 is valid.');
} else {
  console.log('json1 is invalid.');
}

const validationJson2 = isValid(json2, schema);

if (validationJson2 === true) {
  console.log('json2 is valid.');
} else {
  console.log('json2 is invalid.');
  console.log(validationJson2);
}

Downloads and writes a tile.

writeTile(file: String, url: String, request: Object, callback: Function)
Parameters
file (String) Path where the tile is to be stored.
url (String) URL of tile
request (Object) Object from request module (let request = require('request');)
callback (Function) function(err, res){}