aboutsummaryrefslogtreecommitdiff
path: root/com/ProjDef.cpp
blob: c64a6e1927268827206aa9f7923560c1324ebd5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ProjDef.cpp : Implementation of CProjDef
#include "stdafx.h"
#include "COMTest1.h"
#include "ProjDef.h"
#include "proj_api.h"

/////////////////////////////////////////////////////////////////////////////
// CProjDef


STDMETHODIMP CProjDef::Initialize(BSTR proj_string, int *success)
{
	USES_CONVERSION;
	psProj = pj_init_plus( W2A(proj_string) );

	if( psProj != NULL )
		*success = 1;
	else
	{
		SetProjError( "pj_init_plus failed." );
		//SetError( (const char *) W2A(proj_string) );
		*success = 0;
	}

	return S_OK;
}

STDMETHODIMP CProjDef::TransformPoint3D(IUnknown *srcProj, double *x, double *y, double *z, int *success)
{
	void *psProjOther;
	IProjDef *srcProjReal;

	srcProj->QueryInterface( IID_IProjDef, (void **) &srcProjReal );
	srcProjReal->GetHandle( (long *) &psProjOther );

	if( psProjOther == NULL || psProj == NULL )
	{
		SetError( "One of projections not set." );
		*success = 0;
		return E_FAIL;
	}
	
	if( pj_is_latlong( psProjOther ) )
	{
		*x *= DEG_TO_RAD;
		*y *= DEG_TO_RAD;
	}

	*success = pj_transform( psProjOther, psProj, 1, 0, x, y, z ) == 0;
	if( ! *success )
		SetProjError( "pj_transform failed." );

	else if( pj_is_latlong( psProj ) )
	{
		*x *= RAD_TO_DEG;
		*y *= RAD_TO_DEG;
	}

	return S_OK;
}

STDMETHODIMP CProjDef::GetHandle(long *pHandle)
{
	*pHandle = (long) psProj;

	return S_OK;
}

STDMETHODIMP CProjDef::IsLatLong(int *result)
{
	if( psProj != NULL )
	{
		*result = pj_is_latlong( (projPJ) psProj );
		return S_OK;
	}
	else
	{
		SetError( "Projection is null" );
		return E_FAIL;
	}
}

STDMETHODIMP CProjDef::GetLastError( BSTR *error )
{
	*error = SysAllocString( sLastError );

	return S_OK;
}

void  CProjDef::SetError( const char *pszMessage )

{
	USES_CONVERSION;

	if( sLastError != NULL )
	{
		SysFreeString( sLastError );
		sLastError = NULL;
	}

	sLastError = SysAllocString( A2BSTR( pszMessage ) );		
}

void  CProjDef::SetProjError( const char *pszMessage )

{
	int *pj_errno = pj_get_errno_ref();

	if( *pj_errno > 0 )
		SetError( strerror( *pj_errno ) );
	else if( *pj_errno < 0 )
		SetError( pj_strerrno( *pj_errno ) );
	else
		SetError( pszMessage );
}