1.1.8
This is the main class of this module. It allows you to download tiles of a Web Map Service (WMS).
(object?)
Config options of the WMSDownloader instance. See examples
examples
and
json schema
.
// 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
});
Returns all supported formats.
const WMSDownloader = require('wms-downloader');
console.log(WMSDownloader.SUPPORTED_FORMATS);
//[ { 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' } ]
Starts a download task. Every download task runs always asynchronously.
const WMSDownloader = require('wms-downloader');
const dl = new WMSDownloader();
const taskOptions = {
'task': {
'id': 'id_of_my_first_download',
'title': 'My first WMS download.',
'format': 'image/png',
'workspace': __dirname + '/tiles',
'area': {
'bbox': {
'xmin': 455000,
'ymin': 5750000,
'xmax': 479000,
'ymax': 5774000
}
}
},
'tiles': {
'maxSizePx': 2500,
'gutterPx': 250,
'resolutions': [{
'id': 'id_of_resolution_10',
'groundResolution': 10
}]
},
'wms': [{
'id': 'id_of_wms_stadtbezirke',
'getmap': {
'url': 'http://www.bielefeld01.de/md/WMS/statistische_gebietsgliederung/02?',
'kvp': {
'SERVICE': 'WMS',
'REQUEST': 'GetMap',
'VERSION': '1.3.0',
'LAYERS': 'stadtbezirke_pl',
'STYLES': '',
'CRS': 'EPSG:25832',
'FORMAT': 'image/png',
'TRANSPARENT': 'TRUE',
'MAP_RESOLUTION': 72
}
}
}]
};
dl.start(taskOptions, (err) => {
if (err) {
console.log(err);
} else {
console.log('Finished');
}
});
Cancels a download task.
const WMSDownloader = require('wms-downloader');
const dl = new WMSDownloader();
const taskOptions = {
'task': {
'id': 'id_of_my_first_download',
'title': 'My first WMS download.',
'format': 'image/png',
'workspace': __dirname + '/tiles',
'area': {
'bbox': {
'xmin': 455000,
'ymin': 5750000,
'xmax': 479000,
'ymax': 5774000
}
}
},
'tiles': {
'maxSizePx': 2500,
'gutterPx': 250,
'resolutions': [{
'id': 'id_of_resolution_10',
'groundResolution': 1
}]
},
'wms': [{
'id': 'id_of_wms_stadtbezirke',
'getmap': {
'url': 'http://www.bielefeld01.de/md/WMS/statistische_gebietsgliederung/02?',
'kvp': {
'SERVICE': 'WMS',
'REQUEST': 'GetMap',
'VERSION': '1.3.0',
'LAYERS': 'stadtbezirke_pl',
'STYLES': '',
'CRS': 'EPSG:25832',
'FORMAT': 'image/png',
'TRANSPARENT': 'TRUE',
'MAP_RESOLUTION': 72
}
}
}]
};
// start download
dl.start(taskOptions, (err) => {
if (err) {
console.log(err);
} else {
console.log('Download was finished.');
}
});
// cancel download after 10 seconds
setTimeout(() => {
dl.cancel('id_of_my_first_download',(err, id)=>{
if(err){
console.error(err);
}else{
console.log('Download "' + id + '" was canceled.');
}
});
}, 10000);
Returns the progress of a download task.
(string)
Id of the task
object
:
See the
progress object
in the example.
const WMSDownloader = require('wms-downloader');
const dl = new WMSDownloader();
const taskOptions = {
'task': {
'id': 'id_of_my_first_download',
'title': 'My first WMS download.',
'format': 'image/png',
'workspace': __dirname + '/tiles',
'area': {
'bbox': {
'xmin': 455000,
'ymin': 5750000,
'xmax': 479000,
'ymax': 5774000
}
}
},
'tiles': {
'maxSizePx': 2500,
'gutterPx': 250,
'resolutions': [{
'id': 'id_of_resolution_10',
'groundResolution': 1
}]
},
'wms': [{
'id': 'id_of_wms_stadtbezirke',
'getmap': {
'url': 'http://www.bielefeld01.de/md/WMS/statistische_gebietsgliederung/02?',
'kvp': {
'SERVICE': 'WMS',
'REQUEST': 'GetMap',
'VERSION': '1.3.0',
'LAYERS': 'stadtbezirke_pl',
'STYLES': '',
'CRS': 'EPSG:25832',
'FORMAT': 'image/png',
'TRANSPARENT': 'TRUE',
'MAP_RESOLUTION': 72
}
}
}]
};
// print progress
const progressInterval = setInterval(() => {
// progress object
const progress = dl.getProgress(taskOptions.task.id);
// {
// 'tiles': 144, // complete number of tiles
// 'tilesCompleted': 4, // number of completed tiles
// 'startDate': '2018-09-13T10:44:56.011Z', // start date (new Date())
// 'lastTileDate': '2018-09-13T10:45:05.691Z', // completion date (new Date()) of the last tile
// 'percent': 2.78, // progress in percent
// 'waitingTime': 337196 // waiting time in ms
// }
console.log('Progress: ' + progress.percent + '%, Waiting time: ' + progress.waitingTime + ' ms');
}, 1000);
// start download
dl.start(taskOptions, (err) => {
// stop progress printing
clearInterval(progressInterval);
if (err) {
console.log(err);
} else {
console.log('Download was finished.');
}
});
Creates a full GetMap request from the following parameters.
(Object)
Object with the GetMap base url and necessary key value pairs (kvp) like
{ 'getmap': { 'url':'http://', kvp: {'key': 'value' } } }
(String)
BBOX of the GetMap request
String
:
Complete GetMap request
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.
(Number)
X value of start point (top-left)
(Number)
Y value of start point (top-left)
(Number)
Ground resolution
string
:
Content of world file
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.
(String)
File to be crop
(String)
New cropped file
(Number)
Size of the new tile
(Number)
Size of gutter in the old tile
(Function)
function(err) {}
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.
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
).
(String)
MIME-Type of a supported image format.
(null | Object)
:
Format details like
{'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}
console.log(getFormatDetails('image/png'));
//{'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}
Calculates the number of tiles of a task.
(Object)
Task options (see example)
Number
:
Number of all tiles of this task
// 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.
(any)
(String)
URL of tile
Object
:
Object from request module (let request = require('request');)
Returns an array with all supported formats
Array<Object>
:
Array of all supported formats like
[{'title':'PNG','fileExt':'png','worldFileExt':'pgw','mimeType':'image/png'}]
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.
It handles a download task of Web Map Services.
It handles recursive all tiles of a resolution of a Web Map Service.
(Object)
(Object)
WMS object
(String)
WMS workspace
(Object)
Object with tile parameters
(Number)
X-Index of tile
(Number)
Y-Index of tile
(Number)
Ground resolution
(Array)
Array of the progress of all WMSDownloader tasks.
(Function)
function(err){}
It handles recursive all Web Map Services of a resolution.
Validates a json by a json schema.
(Boolean | Array<ValidationError>)
:
Is valid: true; Is invalid: Array of Errors
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.