/** PRINTER METHODS */ /** * Print Service */ var printService = function (wms,d) { a = d.split('|'); return print_address (a[0],wms,a[1],a[2]); } /** * Generate print address * @type {string} t Template name * @type {string} w WMS url * @type {number} x Width dimension * @type {number} y Height dimension * @return url for map print */ var print_address = function(t,w,x,y) { proj4.defs("EPSG:32633","+proj=utm +zone=33 +datum=WGS84 +units=m +no_defs"); url = w + '?SERVICE=WMS&VERSION=1.3&REQUEST=GetPrint&FORMAT=pdf&EXCEPTIONS=application/vnd.ogc.se_inimage&TRANSPARENT=true&SRS=EPSG:32633&DPI=300&'; url += '&TEMPLATE=' + encodeURIComponent(t) + '&'; var ext = ol.proj.transformExtent( generate_extent( map.getView().getCenter() , x, y) , 'EPSG:3857' , 'EPSG:32633' ); url += 'map0:extent=' + ext[0] + ',' + ext[1] + ',' + ext[2] + ',' + ext[3] + '&'; var scale = null; var res = map.getView().getResolution(); var units = map.getView().getProjection().getUnits(); scale = Math.round ( getScaleFromResolution(res, units) ); scale = fixedScale ( scale ); grid_interval = gridInterval ( scale ); url += 'map0:rotation=0&map0:scale='+scale+'&map0:grid_interval_x='+grid_interval+'&map0:grid_interval_y='+grid_interval+'&LAYERS='; // MANCA LA SCALA var VISIBLE_LAYERS_ARR = $('#layersTree').jstree( true ).get_selected(); VISIBLE_LAYERS_ARR = remove_j2_from_visible_layers (VISIBLE_LAYERS_ARR); url += encodeURIComponent( VISIBLE_LAYERS_ARR.join(',') ); return url; } /** * Generate extent from center, width & height * @type {array} c Map center * @type {number} w Width * @type {number} h Height * @return array of extent */ var generate_extent = function (c,w,h) { px = map.getPixelFromCoordinate(c); var a = map.getCoordinateFromPixel( [ px[0] , px[1] + h/2 ] ) var b = map.getCoordinateFromPixel( [ px[0] + w/2 , px[1] ] ) var c = map.getCoordinateFromPixel( [ px[0] , px[1] - h/2 ] ) var d = map.getCoordinateFromPixel( [ px[0] - w/2 , px[1] ] ) return ol.extent.boundingExtent([a,b,c,d]) } /** * Get map resolution from scale * @param {*} scale * @param {*} units */ // var getResolutionFromScale = function (scale, units) { // var resolution; // if (scale) { // var normScale = normalizeScale(scale); // resolution = 1 / (normScale * 4374754 * 72); // } // return resolution; // } /** * Get map scale from resolution * @param {*} resolution * @param {*} units */ var getScaleFromResolution = function (resolution, units) { INCHES_PER_UNIT = { 'inches': 1.0, 'ft': 12.0, 'mi': 63360.0, 'm': 39.37, 'km': 39370, 'dd': 4374754, 'yd': 36 }; if (units == null) { units = "degrees"; } var scale = resolution * INCHES_PER_UNIT[units] * 72; return scale; }; var getResolutionFromScale = function (scale , units) { INCHES_PER_UNIT = { 'inches': 1.0, 'ft': 12.0, 'mi': 63360.0, 'm': 39.37, 'km': 39370, 'dd': 4374754, 'yd': 36 }; if (units == null) { units = "degrees"; } var resolution = scale / (INCHES_PER_UNIT[units] * 72); return resolution; } /** * Normalize scale * @param {*} scale */ var normalizeScale = function (scale) { var normScale = (scale > 1.0) ? (1.0 / scale) : scale; return normScale; } /** * Scale to fixed value * @param {number} scale */ var fixedScale = function (scale) { var mapScales = [ 10, 50, 100, 200, 500, 1000, 2000, 3000, 4000, 5000, 10000, 15000, 20000, 25000, 40000, 50000, 75000, 100000, 150000, 200000, 500000, 1000000, 10000000 ]; for ( var i in mapScales ) { if ( scale < mapScales[i] ) { d = scale - mapScales[i-1]; e = mapScales[i] - scale; if ( d < e ) { return mapScales[i-1]; } else { return mapScales[i]; } } } } /** * Grid interval according to map scale * @param {number} scale */ var gridInterval = function (mapScale) { var grid_interval = 10; if (mapScale > 100 && mapScale <= 250) { grid_interval = 25; } else if (mapScale > 250 && mapScale <= 1000) { grid_interval = 50; } else if (mapScale > 1000 && mapScale <= 2500) { grid_interval = 100; } else if (mapScale > 2500 && mapScale <= 5000) { grid_interval = 250; } else if (mapScale > 5000 && mapScale <= 12000) { grid_interval = 500; } else if (mapScale > 12000 && mapScale <= 25000) { grid_interval = 1000; } else if (mapScale > 25000 && mapScale <= 50000) { grid_interval = 2000; } else if (mapScale > 50000 && mapScale <= 100000) { grid_interval = 5000; } else if (mapScale > 100000 && mapScale <= 500000) { grid_interval = 10000; } else if (mapScale > 500000 && mapScale <= 1000000) { grid_interval = 50000; } else if (mapScale > 1000000 && mapScale <= 5000000) { grid_interval = 100000; } else if (mapScale > 5000000 && mapScale <= 10000000) { grid_interval = 250000; } else if (mapScale > 10000000 && mapScale <= 50000000) { grid_interval = 2500000; } else if (mapScale > 50000000 && mapScale <= 100000000) { grid_interval = 5000000; } else if (mapScale > 100000000) { grid_interval = 10000000; } return grid_interval; }