[mapguide-users] Re: Is it possible to convert Lat and Long value to X Y (AJAX viewer)

Mauricio Villablanca mgvillablanca at yahoo.com
Fri Feb 25 05:45:46 EST 2011


Yes it is. Either use the Mapguide API or write the code for the formula for
your state plane coordinates. Considering the formulas are quite tedious, I
say use the MgCoordinateSystemFactory class.

Example:  LL84 to CA83-VF (Los Angeles)

function transformCoordinate($source, $target, $x, $y) {
		//converts a coordinate from the source to the target coordinate sytem,
needs site connection first
		
		$coordSysFactory = new MgCoordinateSystemFactory();
		$coordSys1 = $coordSysFactory->CreateFromCode($source);
		$coordSys2 = $coordSysFactory->CreateFromCode($target);
		$csTransform12 = $coordSysFactory->GetTransform($coordSys1, $coordSys2);
		$csTransform21 = $coordSysFactory->GetTransform($coordSys2, $coordSys1);
		
		$geometryFactory = new MgGeometryFactory();
		//echo "Coordinates in CA83-VF: ($x, $y)<br>";
		$coordinateArg = $geometryFactory->CreateCoordinateXY($x, $y);
		$coordinate12 = $csTransform12->Transform($coordinateArg);
		$x12 = $coordinate12->GetX();
		$y12 = $coordinate12->GetY();
		//echo "Coordinates in LL84: ($x12, $y12)<br>";
		$new_coordinate = $geometryFactory->CreateCoordinateXY($x12, $y12);
		
		return $new_coordinate;
		
	}

        $lat = $args["lat"];
	$lon = $args["lon"];
	
	try {
		
		MgInitializeWebTier($webconfigFilePath);
		$userInfo = new MgUserInformation("e7", "e7");
		$siteConnection = new MgSiteConnection();
		$siteConnection->Open($userInfo);
		
		$point = transformCoordinate("LL84", "CA83-VF", $lon, $lat);
		echo $point->GetX() . "," . $point->GetY();
       }
	
	catch (MgException $e){
		$errorMsg = $e->GetMessage();
		$errorDetail = $e->GetDetails();
		echo "$errorMsg: $errorDetail";
	}


The same result using a formula:

        class Point {
		
		function __construct($x, $y) {	
			$this->X = $x;
			$this->Y = $y;	
		}
		
	}
	
	function calculate ($t, $e, $p) {
		//returns calculated expression
		
		$num  = 1 - $e*sin($p);
		$den = 1 + $e*sin($p);
		$den2 = pow($num/$den, $e/2);
		$x = $t/$den2;
		
		return $x;
	}

	function latAndLonToSP ($lat, $lon) {
		//convert lat/lon to CA83-VF coordinates
		 
		$a = 20925604.4742;   //major radius of ellipsoid,  (NAD 83) CA ZONE 0405
(feet), by definition
		$e = 0.081819191042830;  //eccentricity of ellipsoid (NAD 83) CA ZONE 0405
		$angRad = 0.01745329252;  //number of radians in a degree
		$pi4 = 3.1415926535897932384626433832795028841971/4;  //Pi:4
		$p0 = 33.5000 * $angRad;  //latitude of origin, Bb
		$m0 = -118.0000 * $angRad;  //central meridian, Lo
		$p1 = 34.03333333333333 * $angRad;   //latitude of first standard
parallel, Bs
		$p2 = 35.46666666666666 * $angRad;    //latitude of second standard
parallel, Bn
		$X0 = 6561666.66666666667; //False easting of central meridian (feet), Eo
		$Y0 = 1640416.66666666667; //False Northing of central meridian (feet), Nb
	
		//Calculate the coordinate system constants
		$m1 = cos($p1) / sqrt(1 - ($e * $e * sin($p1) * sin($p1)));
		$m2 = cos($p2) / sqrt(1 - ($e * $e * sin($p2) * sin($p2)));
		$t0 = tan($pi4 - ($p0 / 2));
		$t1 = tan($pi4 - ($p1 / 2));
		$t2 = tan($pi4 - ($p2 / 2));
		
		$t0 = calculate($t0, $e, $p0);
		$t1 = calculate($t1, $e, $p1);
		$t2 = calculate($t2, $e, $p2);
	
		$n = log($m1 / $m2) / log($t1 / $t2);
		$f = $m1 / ($n * pow($t1, $n));
		$rho0 = $a * $f * pow($t0, $n);
	
		//Convert the latitude/longitude to a coordinate.
		$lat = $lat * $angRad;
		$lon = $lon * $angRad;
		$t = tan($pi4 - ($lat / 2));
		$t = calculate($t, $e, $lat);
	
		$rho = $a * $f * pow($t, $n);
		$theta = $n * ($lon - $m0);
		//false easting
		$x = $rho * sin($theta) + $X0;
		//false northing
		$y = $rho0 - ($rho * cos($theta)) + $Y0;
		
		//format to 6 decimals
		$x = number_format($x, 6, '.', '');
		$y = number_format($y, 6, '.', '');
		$point = new Point($x, $y);
		
	    return $point;	        
	}

        $lat = $args["lat"];
	$lon = $args["lon"];
        $point = latAndLonToSP($lat, $lon);
	$x = (string) $point->X;
	$y = (string) $point->Y;


-- 
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Is-it-possible-to-convert-Lat-and-Long-value-to-X-Y-AJAX-viewer-tp6023213p6064078.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list