Version 2 (modified by 11 years ago) ( diff ) | ,
---|
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:
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>
Attachments (1)
- mpucalc.php (929 bytes ) - added by 13 years ago.
Download all attachments as: .zip