wiki:CodeSamples/PHP/MPUCalculator

This page is one of the MapGuide Community CodeSamples. Visit the CodeSamples page to view more!

Calculating meters-per-unit

NOTE: As of MapGuide Open Source 2.6, the CREATERUNTIMEMAP operation in the mapagent can return this value for you, thus it is no longer necessary to use this script for the purpose of obtaining this value for MapGuide 2.6 onwards

The meters-per-unit is a crucial value needed for calculating scales and setting up appropriate parameters for rendering. If you are using the AJAX/Fusion viewers, this value is automatically calculated for you, but is not accessible via methods in the MgMap? class. If you are setting up a simple MapGuide viewer with OpenLayers or are setting up a tiling run with MgCooker? using the official method, you need to have this value defined.

The ability to calculate this value is available via the official MapGuide API, but not via the HTTP mapagent. This is why applications like MgCooker? require you to fill in this value manually because it does not have the ability to calculate this value using the HTTP APIs provided by MapGuide.

Here is a simple PHP script that can automatically calculate the meters-per-unit value, save the contents of the script below into a directory under the www directory of your MapGuide installation (eg. mpucalc) and open the following URL in your web browser:

http://yourservername:port/mapguide/mpucalc/mpucalc.php?MapDefinition=Library://Path/To/My.MapDefinition

eg. http://localhost:8008/mapguide/mpucalc/mpucalc.php?MapDefinition=Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition

It will output the meters-per-unit value for the specified Map Definition.

mpucalc.php

<html>
    <head>
        <title>Meters-per-unit calculator</title>
    </head>
    <body>
    <?php

    MgInitializeWebTier(dirname(__FILE__)."/../webconfig.ini");

    if (!isset($_REQUEST["MapDefinition"]))
    {
        echo "ERROR: MapDefinition parameter is required";
    }
    else
    {
        $csFactory = new MgCoordinateSystemFactory();
        $siteConnection = new MgSiteConnection();
        $userInfo = new MgUserInformation("Anonymous", "");
        $siteConnection->Open($userInfo);
        $map = new MgMap($siteConnection);
        $mdfId = new MgResourceIdentifier($_REQUEST["MapDefinition"]);
        $map->Create($mdfId, $mdfId->GetName());
        
        $mapCs = $csFactory->Create($map->GetMapSRS());
        $metersPerUnit = $mapCs->ConvertCoordinateSystemUnitsToMeters(1.0);
        
        echo "Meters per unit is: $metersPerUnit";
    }
    ?>
    </body>
</html>
Last modified 4 years ago Last modified on Sep 6, 2013 8:34:45 AM

Attachments (1)

Download all attachments as: .zip