Map.prototype.addDataSource = function(ds, callback) { log.debug('Adding a %s data source processor', ds.format); var self = this; if (ds.getTile) { if (ds.source.status && ds.source.status.complete) { log.debug('Adding the datasource %s to add to the map %s', ds.source.id, this.map.id); self.dataSources.push(ds); // self.map.dataSources.push(ds.source); callback(null, ds); } else { log.debug('Processing the datasource %s to add to the map %s', ds.source.id, this.map.id); ds.processSource(function(err) { if (err) { self.dataSourceErrors[ds.source.id] = err; log.error('error processing the source', err); } else { self.dataSources.push(ds); // self.map.dataSources.push(ds.source); log.debug('Adding the datasource %s to add to the map %s', ds.source.id, this.map.id); } callback(null, ds); }, function(updatedDataSource, callback) { callback(null, updatedDataSource); if (self.config.progressCallback) { self.config.progressCallback(null, updatedDataSource); } }); } } else { var DataSource = Formats.getFormat(ds.format); var dsObj = new DataSource({source: ds}); log.debug('Processing the datasource %s to add to the map %s', ds.id, self.map.id); try { dsObj.processSource(function(err, source) { if (err) { self.dataSourceErrors[ds.id] = err; log.error('Error processing the datasource %s', ds.id, err); } else { self.dataSources.push(dsObj); log.debug('Adding the datasource %s to add to the map %s', dsObj.source.id, self.map.id); } callback(err, dsObj); }, function(updatedDataSource, callback) { callback(null, updatedDataSource); if (self.config.progressCallback) { self.config.progressCallback(null, updatedDataSource); } }); } catch (e) { console.log('e is', e); console.error(e.stack); } } };
GeoPackage.prototype._addSourceToGeoPackage = function(s, progressCallback, callback) { var cache = this.cache.cache; var params = cache.cacheCreationParams; log.info('params are', params); log.debug('Checking if %s - %s should be added to the cache', s.source.name, s.source.id.toString()); if (params.dataSources.indexOf(s.source.id.toString()) === -1) { log.debug('%s - %s is not being added to the cache', s.source.name, s.source.id.toString()); return callback(); } log.info('Adding %s - %s to the cache', s.source.name, s.source.id.toString()); if (s.source.vector) { this._addVectorSourceToGeoPackage(s, progressCallback, callback); } else { this._addRasterSourceToGeoPackage(s, progressCallback, callback); } };
FeatureModel.getFeatureCount({sourceId: source.id, cacheId: null}, function(resultArray){ log.debug("The source already has features", resultArray); if (resultArray[0].count !== '0') { return callback(true); } else { return callback(false); } });
dsObj.processSource(function(err, source) { if (err) { self.dataSourceErrors[ds.id] = err; log.error('Error processing the datasource %s', ds.id, err); } else { self.dataSources.push(dsObj); log.debug('Adding the datasource %s to add to the map %s', dsObj.source.id, self.map.id); } callback(err, dsObj); }, function(updatedDataSource, callback) {
cache.getTile(format, z, x, y, params, function(err, stream) { log.debug('Got the stream for the tile %d %d %d for cache %s', z, x, y, cache.id); if (err) { return callback(err); } var ws = fs.createOutputStream(path.join(dir, filename)); stream.pipe(ws); callback(null, stream); });
ds.processSource(function(err) { if (err) { self.dataSourceErrors[ds.source.id] = err; log.error('error processing the source', err); } else { self.dataSources.push(ds); // self.map.dataSources.push(ds.source); log.debug('Adding the datasource %s to add to the map %s', ds.source.id, this.map.id); } callback(null, ds); }, function(updatedDataSource, callback) {
GeoPackage.prototype._isAlreadyProcessed = function(callback) { log.debug('is it already processed?', this.source); FeatureModel.getFeatureCount({sourceId: this.source.id, cacheId: null}, function(resultArray){ log.debug("The source already has features", resultArray); if (resultArray[0].count !== '0') { return callback(true); } else { return callback(false); } }); };
exports.isAlreadyProcessed = function(source, callback) { log.debug('Checking if the source %s is already processed', source.id); if (source.status && source.status.complete) { return callback(true); } FeatureModel.getFeatureCount({sourceId: source.id, cacheId: null}, function(resultArray){ log.debug("The source already has features", resultArray); if (resultArray[0].count !== '0') { return callback(true); } else { return callback(false); } }); };
GeoPackage.prototype._addVectorSourceToGeoPackage = function(vectorSource, progressCallback, sourceFinishedCallback) { log.info('Adding the features for cache %s from the source %s - %s', this.cache.cache.id, vectorSource.source.name, vectorSource.source.id); var tableName = vectorSource.source.name ? vectorSource.source.name.toString() : vectorSource.source.id.toString(); tableName = tableName.replace(/[^a-z0-9]/gi,''); var cache = this.cache.cache; var extent = this._calculateExtentFromGeometry(cache.geometry); var propertyColumnNames = []; log.debug('vector source properties', vectorSource.source); for (var i = 0; vectorSource.source.properties && i < vectorSource.source.properties.length; i++) { propertyColumnNames.push(vectorSource.source.properties[i].key); } console.log('property column names', propertyColumnNames); var sourceFeaturesCreated = 0; // write these to the geoPackage var self = this; FeatureModel.getAllFeaturesByCacheIdAndSourceId(cache.id, vectorSource.source.id, extent[0], extent[1], extent[2], extent[3], '3857', function(err, features) { log.info('Adding %d features to the GeoPackage', features.length); self.geoPackage.createFeatureTable(extent, tableName, propertyColumnNames, function(err) { if (err) { console.log('Failure to create the feature table', err); return sourceFinishedCallback(err); } self.geoPackage.addFeaturesToGeoPackage(features, tableName, function(err) { if (err) { console.log('Failed to add features to the GeoPackage', err); return sourceFinishedCallback(err); } console.log('features.length', features.length); if (!cache.cacheCreationParams || !cache.cacheCreationParams.noGeoPackageIndex) { self.geoPackage.indexGeoPackage(tableName, sourceFinishedCallback); } else { sourceFinishedCallback(); } }, function(progress, callback) { cache.formats.geopackage.generatedFeatures = cache.formats.geopackage.generatedFeatures + progress.featuresAdded - sourceFeaturesCreated; cache.formats.geopackage.percentComplete += (100 * ((progress.featuresAdded - sourceFeaturesCreated) / features.length)) / self.cache.map.dataSources.length; sourceFeaturesCreated = progress.featuresAdded; progressCallback(cache, callback); }); }); }); };