[mapguide-users] Transform Coordinates from MgPoint
Jim O'Leary
joleary.public at gmail.com
Tue May 15 16:03:44 EDT 2007
This code does actually work when you convert from BC Albers to LL84,
but you then have to convert the return point's x and y from rads to
degrees.
$x = $x / M_PI * 180.0;// M_PI is the PHP constant for pi
$y = $y / M_PI * 180.0;
It fails when you convert from LL84 to BC Albers.
However, you can use methods of the MgCoordinateSystem object
to convert back and forth.
$coordSysFactory = new MgCoordinateSystemFactory();
$geometryFactory = new MgGeometryFactory();
$coord = $geometryFactory->CreateCoordinateXY((double) $x, (double) $y);
// creates an MgCoordinateSystem object
$coordSys = $coordSysFactory->Create($BC_Albers); // $BC_Albers is the WKT
if($currentSRS == 'albers')
{
$newCoord = $coordSys->ConvertToLonLat($coord);
}
else
{
$newCoord = $coordSys->ConvertFromLonLat($coord);
}
$point = $geometryFactory->CreatePoint($newCoord);
$x = $point->GetCoordinate()->GetX();
$y = $point->GetCoordinate()->GetY();
Jim O wrote:
>
> I am able to transform coordinates on the fly from a FeatureReader,
> but not from an MgPoint. Both codes below produce an MgGeometry object,
> and both codes produce a $transform object like this:
>
> $coordSysFactory = new MgCoordinateSystemFactory();
> $source = $coordSysFactory->Create($map->GetMapSRS());
> $target = $coordSysFactory->Create($LL84WKT);// $LL84WKT is a the WKT
> string for LL84
> $transform = new MgCoordinateSystemTransform($source, $target);
>
> I've pasted the doxygen comments in the code below for clarity.
> The FeatureReader code that works goes:
>
> $agfWriter = new MgAgfReaderWriter();
> while ($featureReader->ReadNext())
> {
> // MgByteReader * GetGeometry (CREFSTRING propertyName)=0
> // Gets the Geometry for the specified property.
> $byteReader = $featureReader->GetGeometry('GEOMETRY');
> // MgGeometry * Read (MgByteReader *agf)
> // Translates an MgByteReader object into an MgGeometry object.
> $geometry = $agfReaderWriter->Read($byteReader);
> $geometry = $geometry->Transform($transform);
>
> The MgPoint code that doesn't work goes:
>
> // MgCoordinate * CreateCoordinateXY (double x, double y)
> // Creates an MgCoordinate object with Dimension = XY
> $coord = $geometryFactory->CreateCoordinateXY((double) $_REQUEST['x'],
> (double) $_REQUEST['y']);
> // MgPoint * CreatePoint (MgCoordinate *coordinate)
> // Creates a point from a coordinate.
> $point = $geometryFactory->CreatePoint($coord);
> // MgPoint is a subclass of MgGeometry
> $point = $point->Transform($transform);
>
> One MgGeometry object can transform, but one cannot. All I get from the
> second is a nonsense number. Suggestions?
>
--
View this message in context: http://www.nabble.com/Transform-Coordinates-from-MgPoint-tf3719024s16610.html#a10630003
Sent from the MapGuide Users mailing list archive at Nabble.com.
More information about the mapguide-users
mailing list