Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way that OpenLayers.Format.JSON write() method to serialize coordinates using 6 decimals only ?

Example: instead of to have

share|improve this question
up vote 1 down vote accepted

OpenLayers geometries use 14 significant digits, and the GeoJSON writer simply extracts geometry coordinates using the same number of significant digits:

We use 14 as a “safe” default because, although IEEE 754 double floats (standard on most modern operating systems) support up to about 16 significant digits, 14 significant digits are sufficient to represent sub-millimeter accuracy in any coordinate system that anyone is likely to use with OpenLayers.

If you want to limit the number of digits in the GeoJSON writer, you'd need to replace the function to extract points, by putting the following lines in your code:

    OpenLayers.Format.GeoJSON.prototype.extract["point"] = function (point) {
        return [point.x.toFixed(6), point.y.toFixed(6)];

Note this creates string coordinates rather than floats which may cause problems, so you can check out other options to round floats at

However you may want to consider why you want to reduce the accuracy of the GeoJSON. Is this to format display to a user? If this is the case the GeoJSON should be left alone, and the rounding of the numbers left to the user interface. It is also not going to save any noticeable network traffic.

share|improve this answer

write your own method for your json type:

var feat = vector.features;

function newRuleJSON(feature){
   for(var a = 0; a < feature.length; a++){
       feature[a].geometry.x = feature[a].geometry.x.toFixed(6);
       feature[a].geometry.y = feature[a].geometry.y.toFixed(6);


var toGeoJSON = new OpenLayers.Format.GeoJSON();
var newFrm = toGeoJSON.write(feat);


i hope it helps you...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.