Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Hexadecimal #1

Closed
marcoas opened this issue Jun 18, 2016 · 2 comments
Closed

Feature Request: Hexadecimal #1

marcoas opened this issue Jun 18, 2016 · 2 comments

Comments

@marcoas
Copy link

marcoas commented Jun 18, 2016

(Translated by Google)

Hi
It did not even save more space if you'll keep the numbers in hexadecimal format ?

@igorti
Copy link
Owner

igorti commented Jul 4, 2016

It probably would. Could you make a pull request with an example that illustrates that it's a more efficient format?

@marcoas
Copy link
Author

marcoas commented Jul 4, 2016

(Translated by Google)

Sorry, I do not use GIT . I can not send a 'Pull Request' .
I only copied the code and used it .

Anyway , I send my modified code .

Originally I thought to save the values ​​in Hexadecimal , to save a few bytes. But then , I see that 36 is the maximum possible value , to transform into Javascript, it says in http://www.w3schools.com/jsref/jsref_tostring_number.asp

As an example of the savings, I mention that I have a file with 5 polygons . The file has more than 7200 coordinates. The size of the original file is ~ 212KB and finished ~ 82kb ( ~ 39 % )

These are the new / modified functions:

function GeojsonMinifier() {
  this.precision = 7;
  this.factor = 36;
}

GeojsonMinifier.prototype.encodeGeometry = function(coordinates) {
  var minified = [];
  var previous = coordinates[0];
  minified.push(previous[0], previous[1]);

  for (var i = 1; i < coordinates.length; i++) {
    minified.push( this.zigzag(coordinates[i][0], previous[0]) );
    minified.push( this.zigzag(coordinates[i][1], previous[1]) );
    previous = coordinates[i];
  }

  return minified;
}

GeojsonMinifier.prototype.decodeGeometry = function(coordinates) {
  var decodedGeometry = [];
  var previous = [];
  previous.push(coordinates[0], coordinates[1]);
  decodedGeometry.push(previous);

  for (var i = 2; i < coordinates.length; i+=2) {
    var current = [ parseInt(coordinates[i],this.factor), parseInt(coordinates[i+1],this.factor) ];
    var decoded = [];

    decoded[0] = this.unZigzag(current[0], previous[0] );
    decoded[1] = this.unZigzag(current[1], previous[1] );
    decodedGeometry.push(decoded);
    previous = decoded;
  }

  return decodedGeometry;
}

GeojsonMinifier.prototype.zigzag = function( n, previous ) {
    var current = this.toWholeNumber( n );
    var delta = current - this.toWholeNumber(previous);
    zigzag = (delta << 1) ^ (delta >> 31);
    return zigzag.toString(this.factor);
}

GeojsonMinifier.prototype.unZigzag = function( n, previous ) {
    var zigzag = (n >>> 1) ^ -(n & 1);
    var delta = this.toWholeNumber(previous) + zigzag;
    return this.fromWholeNumber(delta);
}

@marcoas marcoas closed this as completed Jul 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants