Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

Example: instead of http://pastie.org/4200997 to have http://pastie.org/4200994

share|improve this question
add comment

2 Answers

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 http://stackoverflow.com/questions/661562/how-to-format-a-float-in-javascript

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
add comment

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);
       };
};

newRuleJSON(feat);

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

console.log(newFrm);

i hope it helps you...

share|improve this answer
add comment

Your Answer

 
discard

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.