<commit_before>import Cartesian2 from '../Core/Cartesian2.js';
import Cartographic from '../Core/Cartographic.js';
import defaultValue from '../Core/defaultValue.js';
import defined from '../Core/defined.js';
import DeveloperError from '../Core/DeveloperError.js';
import GeographicProjection from '../Core/GeographicProjection.js';
import GeographicTilingScheme from '../Core/GeographicTilingScheme.js';
import Rectangle from '../Core/Rectangle.js';
import Resource from '../Core/Resource.js';
import RuntimeError from '../Core/RuntimeError.js';
import TileProviderError from '../Core/TileProviderError.js';
import WebMercatorTilingScheme from '../Core/WebMercatorTilingScheme.js';
import when from '../ThirdParty/when.js';
import UrlTemplateImageryProvider from './UrlTemplateImageryProvider.js';

    /**
     * An imagery provider that provides tiled imagery as generated by
     * {@link http://www.maptiler.org/|MapTiler}, {@link http://www.klokan.cz/projects/gdal2tiles/|GDAL2Tiles}, etc.
     *
     * @alias TileMapServiceImageryProvider
     * @constructor
     *
     * @param {Object} [options] Object with the following properties:
     * @param {Resource|String|Promise<Resource>|Promise<String>} [options.url='.'] Path to image tiles on server.
     * @param {String} [options.fileExtension='png'] The file extension for images on the server.
     * @param {Credit|String} [options.credit=''] A credit for the data source, which is displayed on the canvas.
     * @param {Number} [options.minimumLevel=0] The minimum level-of-detail supported by the imagery provider.  Take care when specifying
     *                 this that the number of tiles at the minimum level is small, such as four or less.  A larger number is likely
     *                 to result in rendering problems.
     * @param {Number} [options.maximumLevel] The maximum level-of-detail supported by the imagery provider, or undefined if there is no limit.
     * @param {Rectangle} [options.rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
     * @param {TilingScheme} [options.tilingScheme] The tiling scheme specifying how the ellipsoidal
     * surface is broken into tiles.  If this parameter is not provided, a {@link WebMercatorTilingScheme}
     * is used.
     * @param {Ellipsoid} [options.ellipsoid] The ellipsoid.  If the tilingScheme is specified,
     *                    this parameter is ignored and the tiling scheme's ellipsoid is used instead. If neither
     *                    parameter is specified, the WGS84 ellipsoid is used.
     * @param {Number} [options.tileWidth=256] Pixel width of image tiles.
     * @param {Number} [options.tileHeight=256] Pixel height of image tiles.
     * @param {Boolean} [options.flipXY] Older versions of gdal2tiles.py flipped X and Y values in tilemapresource.xml.
     * Specifying this option will do the same, allowing for loading of these incorrect tilesets.
     * @returns {UrlTemplateImageryProvider} The imagery provider.
     *
     * @see ArcGisMapServerImageryProvider
     * @see BingMapsImageryProvider
     * @see GoogleEarthEnterpriseMapsProvider
     * @see OpenStreetMapImageryProvider
     * @see SingleTileImageryProvider
     * @see WebMapServiceImageryProvider
     * @see WebMapTileServiceImageryProvider
     * @see UrlTemplateImageryProvider
     *
     * @example
     * var tms = new Cesium.TileMapServiceImageryProvider({
     *    url : '../images/cesium_maptiler/Cesium_Logo_Color',
     *    fileExtension: 'png',
     *    maximumLevel: 4,
     *    rectangle: new Cesium.Rectangle(
     *        Cesium.Math.toRadians(-120.0),
     *        Cesium.Math.toRadians(20.0),
     *        Cesium.Math.toRadians(-60.0),
     *        Cesium.Math.toRadians(40.0))
     * });
     */
    function TileMapServiceImageryProvider(options) {
        options = defaultValue(options, defaultValue.EMPTY_OBJECT);

        //>>includeStart('debug', pragmas.debug);
        if (!defined(options.url)) {
            throw new DeveloperError('options.url is required.');
        }
        //>>includeEnd('debug');

        var deferred = when.defer();
        UrlTemplateImageryProvider.call(this, deferred.promise);

        this._tmsResource = undefined;
        this._xmlResource = undefined;
        this._options = options;
        this._deferred = deferred;
        this._metadataError = undefined;

        this._metadataSuccess = this._metadataSuccess.bind(this);
        this._metadataFailure = this._metadataFailure.bind(this);
        this._requestMetadata = this._requestMetadata.bind(this);

        var resource;
        var that = this;
        when(options.url)
            .then(function(url) {
                resource = Resource.createIfNeeded(url);
                resource.appendForwardSlash();

                that._tmsResource = resource;
                that._xmlResource = resource.getDerivedResource({
                    url : 'tilemapresource.xml'
                });

                that._requestMetadata();
            })
            .otherwise(function(e) {
                deferred.reject(e);
            });
    }

    if (defined(Object.create)) {
        TileMapServiceImageryProvider.prototype = Object.create(UrlTemplateImageryProvider.prototype);
        TileMapServiceImageryProvider.prototype.constructor = TileMapServiceImageryProvider;
    }

    TileMapServiceImageryProvider.prototype._requestMetadata = function() {
        // Try to load remaining parameters from XML
        this._xmlResource.fetchXML().then(this._metadataSuccess).otherwise(this._metadataFailure);
    };

    /**
     * Mutates the properties of a given rectangle so it does not extend outside of the given tiling scheme's rectangle
     */
    function confineRectangleToTilingScheme(rectangle, tilingScheme) {
        if (rectangle.west < tilingScheme.rectangle.west) {
            rectangle.west = tilingScheme.rectangle.west;
        }
        if (rectangle.east > tilingScheme.rectangle.east) {
            rectangle.east = tilingScheme.rectangle.east;
        }
        if (rectangle.south < tilingScheme.rectangle.south) {
            rectangle.south = tilingScheme.rectangle.south;
        }
        if (rectangle.north > tilingScheme.rectangle.north) {
            rectangle.north = tilingScheme.rectangle.north;
        }
        return rectangle;
    }

    function calculateSafeMinimumDetailLevel(tilingScheme, rectangle, minimumLevel) {
        console.log(`checking safe minimum detail for ${minimumLevel}`, rectangle);
        // Check the number of tiles at the minimum level.  If it's more than four,
        // try requesting the lower levels anyway, because starting at the higher minimum
        // level will cause too many tiles to be downloaded and rendered.
        var swTile = tilingScheme.positionToTileXY(Rectangle.southwest(rectangle), minimumLevel);
        var neTile = tilingScheme.positionToTileXY(Rectangle.northeast(rectangle), minimumLevel);
        var tileCount = (Math.abs(neTile.x - swTile.x) + 1) * (Math.abs(neTile.y - swTile.y) + 1);
        console.log(`tile count was ${tileCount}`, tileCount < 4);
        if (tileCount > 4) {
            return 0;
        }
        return minimumLevel;
    }

    TileMapServiceImageryProvider.prototype._metadataSuccess = function(xml) {
        var tileFormatRegex = /tileformat/i;
        var tileSetRegex = /tileset/i;
        var tileSetsRegex = /tilesets/i;
        var bboxRegex = /boundingbox/i;
        var format, bbox, tilesets;
        var tilesetsList = []; //list of TileSets
        var xmlResource = this._xmlResource;
        var metadataError = this._metadataError;
        var deferred = this._deferred;
        var requestMetadata = this._requestMetadata;

        // Allowing options properties (already copied to that) to override XML values

        // Iterate XML Document nodes for properties
        var nodeList = xml.childNodes[0].childNodes;
        for (var i = 0; i < nodeList.length; i++) {
            if (tileFormatRegex.test(nodeList.item(i).nodeName)) {
                format = nodeList.item(i);<commit_msg>whoops, who left those sneaky logs in there<commit_after>import Cartesian2 from '../Core/Cartesian2.js';
import Cartographic from '../Core/Cartographic.js';
import defaultValue from '../Core/defaultValue.js';
import defined from '../Core/defined.js';
import DeveloperError from '../Core/DeveloperError.js';
import GeographicProjection from '../Core/GeographicProjection.js';
import GeographicTilingScheme from '../Core/GeographicTilingScheme.js';
import Rectangle from '../Core/Rectangle.js';
import Resource from '../Core/Resource.js';
import RuntimeError from '../Core/RuntimeError.js';
import TileProviderError from '../Core/TileProviderError.js';
import WebMercatorTilingScheme from '../Core/WebMercatorTilingScheme.js';
import when from '../ThirdParty/when.js';
import UrlTemplateImageryProvider from './UrlTemplateImageryProvider.js';

    /**
     * An imagery provider that provides tiled imagery as generated by
     * {@link http://www.maptiler.org/|MapTiler}, {@link http://www.klokan.cz/projects/gdal2tiles/|GDAL2Tiles}, etc.
     *
     * @alias TileMapServiceImageryProvider
     * @constructor
     *
     * @param {Object} [options] Object with the following properties:
     * @param {Resource|String|Promise<Resource>|Promise<String>} [options.url='.'] Path to image tiles on server.
     * @param {String} [options.fileExtension='png'] The file extension for images on the server.
     * @param {Credit|String} [options.credit=''] A credit for the data source, which is displayed on the canvas.
     * @param {Number} [options.minimumLevel=0] The minimum level-of-detail supported by the imagery provider.  Take care when specifying
     *                 this that the number of tiles at the minimum level is small, such as four or less.  A larger number is likely
     *                 to result in rendering problems.
     * @param {Number} [options.maximumLevel] The maximum level-of-detail supported by the imagery provider, or undefined if there is no limit.
     * @param {Rectangle} [options.rectangle=Rectangle.MAX_VALUE] The rectangle, in radians, covered by the image.
     * @param {TilingScheme} [options.tilingScheme] The tiling scheme specifying how the ellipsoidal
     * surface is broken into tiles.  If this parameter is not provided, a {@link WebMercatorTilingScheme}
     * is used.
     * @param {Ellipsoid} [options.ellipsoid] The ellipsoid.  If the tilingScheme is specified,
     *                    this parameter is ignored and the tiling scheme's ellipsoid is used instead. If neither
     *                    parameter is specified, the WGS84 ellipsoid is used.
     * @param {Number} [options.tileWidth=256] Pixel width of image tiles.
     * @param {Number} [options.tileHeight=256] Pixel height of image tiles.
     * @param {Boolean} [options.flipXY] Older versions of gdal2tiles.py flipped X and Y values in tilemapresource.xml.
     * Specifying this option will do the same, allowing for loading of these incorrect tilesets.
     * @returns {UrlTemplateImageryProvider} The imagery provider.
     *
     * @see ArcGisMapServerImageryProvider
     * @see BingMapsImageryProvider
     * @see GoogleEarthEnterpriseMapsProvider
     * @see OpenStreetMapImageryProvider
     * @see SingleTileImageryProvider
     * @see WebMapServiceImageryProvider
     * @see WebMapTileServiceImageryProvider
     * @see UrlTemplateImageryProvider
     *
     * @example
     * var tms = new Cesium.TileMapServiceImageryProvider({
     *    url : '../images/cesium_maptiler/Cesium_Logo_Color',
     *    fileExtension: 'png',
     *    maximumLevel: 4,
     *    rectangle: new Cesium.Rectangle(
     *        Cesium.Math.toRadians(-120.0),
     *        Cesium.Math.toRadians(20.0),
     *        Cesium.Math.toRadians(-60.0),
     *        Cesium.Math.toRadians(40.0))
     * });
     */
    function TileMapServiceImageryProvider(options) {
        options = defaultValue(options, defaultValue.EMPTY_OBJECT);

        //>>includeStart('debug', pragmas.debug);
        if (!defined(options.url)) {
            throw new DeveloperError('options.url is required.');
        }
        //>>includeEnd('debug');

        var deferred = when.defer();
        UrlTemplateImageryProvider.call(this, deferred.promise);

        this._tmsResource = undefined;
        this._xmlResource = undefined;
        this._options = options;
        this._deferred = deferred;
        this._metadataError = undefined;

        this._metadataSuccess = this._metadataSuccess.bind(this);
        this._metadataFailure = this._metadataFailure.bind(this);
        this._requestMetadata = this._requestMetadata.bind(this);

        var resource;
        var that = this;
        when(options.url)
            .then(function(url) {
                resource = Resource.createIfNeeded(url);
                resource.appendForwardSlash();

                that._tmsResource = resource;
                that._xmlResource = resource.getDerivedResource({
                    url : 'tilemapresource.xml'
                });

                that._requestMetadata();
            })
            .otherwise(function(e) {
                deferred.reject(e);
            });
    }

    if (defined(Object.create)) {
        TileMapServiceImageryProvider.prototype = Object.create(UrlTemplateImageryProvider.prototype);
        TileMapServiceImageryProvider.prototype.constructor = TileMapServiceImageryProvider;
    }

    TileMapServiceImageryProvider.prototype._requestMetadata = function() {
        // Try to load remaining parameters from XML
        this._xmlResource.fetchXML().then(this._metadataSuccess).otherwise(this._metadataFailure);
    };

    /**
     * Mutates the properties of a given rectangle so it does not extend outside of the given tiling scheme's rectangle
     */
    function confineRectangleToTilingScheme(rectangle, tilingScheme) {
        if (rectangle.west < tilingScheme.rectangle.west) {
            rectangle.west = tilingScheme.rectangle.west;
        }
        if (rectangle.east > tilingScheme.rectangle.east) {
            rectangle.east = tilingScheme.rectangle.east;
        }
        if (rectangle.south < tilingScheme.rectangle.south) {
            rectangle.south = tilingScheme.rectangle.south;
        }
        if (rectangle.north > tilingScheme.rectangle.north) {
            rectangle.north = tilingScheme.rectangle.north;
        }
        return rectangle;
    }

    function calculateSafeMinimumDetailLevel(tilingScheme, rectangle, minimumLevel) {
        // Check the number of tiles at the minimum level.  If it's more than four,
        // try requesting the lower levels anyway, because starting at the higher minimum
        // level will cause too many tiles to be downloaded and rendered.
        var swTile = tilingScheme.positionToTileXY(Rectangle.southwest(rectangle), minimumLevel);
        var neTile = tilingScheme.positionToTileXY(Rectangle.northeast(rectangle), minimumLevel);
        var tileCount = (Math.abs(neTile.x - swTile.x) + 1) * (Math.abs(neTile.y - swTile.y) + 1);
        if (tileCount > 4) {
            return 0;
        }
        return minimumLevel;
    }

    TileMapServiceImageryProvider.prototype._metadataSuccess = function(xml) {
        var tileFormatRegex = /tileformat/i;
        var tileSetRegex = /tileset/i;
        var tileSetsRegex = /tilesets/i;
        var bboxRegex = /boundingbox/i;
        var format, bbox, tilesets;
        var tilesetsList = []; //list of TileSets
        var xmlResource = this._xmlResource;
        var metadataError = this._metadataError;
        var deferred = this._deferred;
        var requestMetadata = this._requestMetadata;

        // Allowing options properties (already copied to that) to override XML values

        // Iterate XML Document nodes for properties
        var nodeList = xml.childNodes[0].childNodes;
        for (var i = 0; i < nodeList.length; i++) {
            if (tileFormatRegex.test(nodeList.item(i).nodeName)) {
                format = nodeList.item(i);
{'fraction_non_alphanumeric': 0.0980865688277912, 'fraction_numerical': 0.004272710384543935, 'mean_word_length': 2.7645687645687644, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6710468', 'n_tokens_mistral': 4553, 'n_tokens_neox': 4361, 'n_words': 1167}