[mapserver-users] RE: Measure tool?

Ludwig Kniprath ludwig at kni-online.de
Fri Nov 12 14:14:32 EST 2010


Hi Bob,
we use clientside javascript-functions. They both need an array of 
points ("digi_points") with real-world-coordiantes (properties "pointX" 
and "pointY") as input-parameter.
This array is filled by clicks on the map, the click-coordiantes 
(pixel-distance from the click-point to the upper left corner of the 
map-grafic) ar calculated to real-wolrd coordinates with the upper left 
corner-coordinates and the cellsize of the map, these two parameters are 
returned to the browser together with the grafic by php-mapscript.

function get_digi_points_length(digi_points) {
     var calculated_length = 0;
     for ( var i = 0; i < (digi_points.length - 1); i++) {
         var from_point = digi_points[i];
         var to_point = digi_points[i + 1];
         var delta_x = to_point.pointX - from_point.pointX;
         var delta_y = to_point.pointY - from_point.pointY;
         var delta_length = Math.sqrt(Math.pow(delta_x, 2) + 
Math.pow(delta_y, 2));
         calculated_length += delta_length;
     }
     return calculated_length;
}

//precheck the digi_points for closed ring: digi_points[0] == 
digi_points[digi_points.length - 1]
function get_digi_points_area(digi_points) {
     var calculated_area = 0;
     for ( var i = 0; i < (digi_points.length - 1); i++) {
         var from_point = digi_points[i];
         var to_point = digi_points[i + 1];
         var delta_area = from_point.pointX * to_point.pointY - 
to_point.pointX * from_point.pointY;
         calculated_area += delta_area;
     }
     calculated_area *= 0.5;
     // if digitized counter-clockwise the result is negative!
     calculated_area = Math.abs(calculated_area);
     return calculated_area;
}

Ludwig


More information about the mapserver-users mailing list