Start a new topic

Problem with mgGetCoord3d

Original Post by: jrhea Thu Jun 23 22:34:22 2011


I have a .flt file that is located at N30 W84. I wrote a function that converts from lat,long to geocentric coordinates. In order to test this function I wrote, I want to use the mgGetCoord3d function to verify that I get the same results as when I calculate it with my custom function. The problem is, mgGetCoord3d returns (x,y,z)=0,0,0. I have tried this with multiple flt files and I always get the same result. What am I doing wrong? Thanks in advance...


#include <stdio.h>

#include <tchar.h>

#include <math.h>

#include <stdlib.h> /* exit */

#include <iostream>

using namespace std;


#include "mgapiall.h"

#include "mgapistruc1.h"


#define PI 3.14159


struct Point

{

double x;

double y;

double z;

};


//Function to convert the geographic coordinates to geocentric coordinates.

Point GeographicToGeocentric (double latitude, double longitude)

{

Point result;

// convert latitude and longitude from degrees to radians

double latrads = latitude / 180 * PI;

double lonrads = longitude / 180 * PI;


//calculate the geocentric x, y, z coordinates using trigonometric functions

result.z = sin(latrads);

double adjustedradius = cos(latrads);

result.x = adjustedradius * cos(lonrads);

result.y = adjustedradius * sin(lonrads);


return result;

}


int main(int argc, char* argv[])

{

Point result;

mgrec* db;

double x,y,z;


mgInit(&argc;, argv);


if (!(db = mgOpenDb(argv[1])))

{

printf("\nError in mgOpenDb(%s)\n", argv[1]);

exit(1);

}


cout<<mgGetCoord3d(db,fltCoord3d,&x,&y,&z)<<endl;

cout<<"File's coordinates (x,y,z): " << x << "," << y << "," << z << endl;


result = GeographicToGeocentric(30,84);

cout<<"Calculated coordinates (x,y,z): " << result.x << "," << result.y << "," << result.z << endl;


return 0;

}



This is the output:


1

File's coordinates (x,y,z): 0,0,0

Calculated coordinates (x,y,z): 0.0905254,0.861281,0.5


Original Post by: ChrisRogers Fri Jun 24 15:30:58 2011


btw, I didn’t post the code, but I did use the mgProject and mgUnproject methods hoping that would be the way to convert to geocentric, but it didn’t quite work. i will post the code when i get back in the office. thanks in advance.


Keep in mind that mgProject and mgUnproject will convert from the current projection to lat/lon/elevation. It will not automatically change your projection.

Original Post by: ChrisRogers Fri Jun 24 15:27:54 2011


There is no public method that I know of. There is a pro tool called Convert Database Projection. The fltProjection attribute on the fltHeader just tells you what projection the vertex positions currently represent. If you write your own routine to convert the projection, make sure you also change the fltProjection on the fltHeader to reflect the current projection of the db.

Original Post by: jrhea Fri Jun 24 02:03:41 2011


btw, I didn't post the code, but I did use the mgProject and mgUnproject methods hoping that would be the way to convert to geocentric, but it didn't quite work. i will post the code when i get back in the office. thanks in advance.

Original Post by: jrhea Thu Jun 23 23:50:25 2011


Thanks that did help clarify some things for me. I noticed in the fltHeader there is an attribute called fltProjection. Is there a method to convert from flat earth to geocentric? That is ultimately what I want to do. thanks!

Original Post by: ChrisRogers Thu Jun 23 22:50:59 2011


The problem is that fltHeader does not contain a fltCoord3d. The function mgGetCoord3d expects you to give it a node and the name of a fltCoord3d record inside the node. The fltHeader node uses some double values to represent the db origin. Take a look at the data dictionary for fltHeader. The origin is stored in two doubles representing Lat and Lon. (fltOriginLat1 & fltOriginLong1)


You might also be interested in the mgProject and mgUnProject functions. These functions will convert between coord3d (x/y/z) and mgprojcoord (lat/lon/elevation) using a database's projection.


Hope this helps.

Login to post a comment