<commit_before>var fs = require("fs");
var projector = require("../projector");

module.exports = function(path, projection, encoding) {
  encoding = encoding || "utf8";
  projection = projection || "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs";

 
  // loading synchronization
  var loadCallbacks = [];
  var loading = false;
  
  // stored state
  var loadError = null;
  var data = null;
  
  // TODO: project data properly
  var source = function GeoJsonSource(minX, minY, maxX, maxY, mapProjection, callback) {
    if (!data && !loadError) {
      source.load(callback);
    }
    else {
      callback(loadError, data);
    }
  };
  source.load = function(callback) {
    callback && loadCallbacks.push(callback);
    if (!loading) {
      loading = true;
      
      var start = Date.now();
      console.log("Loading data in " + path + "...");
      
      loadJsonFile(path, encoding, function(error, jsonData) {
        loadError = error;
        data = jsonData;
        
        console.log("Loaded in " + (Date.now() - start) + "ms");
        
        if (!error) {
          if (projection !== mapProjection) {
            console.log("Projecting features...");
            start = Date.now();

            projector.project.FeatureCollection(data, projection, mapProjection);

            console.log("Projected in " + (Date.now() - start) + "ms"); 
          }
          console.log("Projection not necessary")
        }
        
        loading = false;
        var callbacks = loadCallbacks;
        loadCallbacks = [];
        callbacks.forEach(function(callback) {
          callback(loadError, data);
        });
      });
    }
  };
  
  return source;
};

var loadJsonFile = function(path, encoding, callback) {
  fs.readFile(path, encoding, function(error, content) {
    var data;
    if (!error) {
      try {
        data = JSON.parse(content);
      }
      catch (ex) {
        error = ex;
      }
    }
    
    callback(error, data);
  });
};<commit_msg>New GeoJson data connector using sexy new Object structure<commit_after>var fs = require("fs");
var projector = require("../projector");


var GeoJsonSource = function(options) {
  this._projection = options.projection || "4326"; //"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs";
  this._path = options.path; // required
  this._encoding = options.encoding || "utf8";

  // loading synchronization
  this._loadCallbacks = [];
  this._loading = false;

  // stored state
  this._loadError = null;
  this._data = null;
  this._projectedData = {};

};

GeoJsonSource.prototype = {
  constructor: GeoJsonSource,

  getShapes: function(minX, minY, maxX, maxY, mapProjection, callback) {
    if (this._projectedData[mapProjection]){
      callback(null, this._projectedData[mapProjection]);
    } else {
      this.load(function(err, data){
        this._project(mapProjection);
        callback(this._loadError, this._projectedData[mapProjection])
      }.bind(this));
    }
  },

  load: function(callback) {
    callback && this._loadCallbacks.push(callback);

    if (!this._loading && !this._loadError && !this._data) { // only load once
      this._loading = true;

      var start = Date.now();
      console.log("Loading data in " + this._path + "...");

      fs.readFile(this._path, this._encoding, function(error, content) {
        if (!error) {
          try {
            this._data = JSON.parse(content);
            console.log("Loaded in " + (Date.now() - start) + "ms");
          }
          catch (ex) {
            this._loadError = ex;
          }
        }

        this._loading = false;

        var callbacks = this._loadCallbacks;
        callbacks.forEach(function(callback) {
          callback(this._loadError, this._data);
        }.bind(this));
        //callback(this._loadError, this._data);
      }.bind(this));
    } else {
      var callbacks = this._loadCallbacks;
      callbacks.forEach(function(callback) {
        callback(this._loadError, this._data);
      }.bind(this));
    }
  },

  _project: function(mapProjection) {
    if (this._projection !== mapProjection) {
      console.log("Projecting features...");
      start = Date.now();

      this._projectedData[mapProjection] = projector.project.FeatureCollection(this._data, this._projection, mapProjection);

      console.log("Projected in " + (Date.now() - start) + "ms"); 
    } else {
      console.log("Projection not necessary")
        this._projectedData[mapProjection] = this._data;
    }
  }

  // _filterByExtent: function(minX, minY, maxX, maxY)


}

module.exports = GeoJsonSource;
{'fraction_non_alphanumeric': 0.14587737843551796, 'fraction_numerical': 0.01226215644820296, 'mean_word_length': 2.5200892857142856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2721091', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1468, 'n_words': 303}