Use Qt+GDAL library to make longitude and latitude coordinate conversion tool

1. Function interface

insert image description here
The following is the result calculated by Global Mapper, which proves that the calculation is correct.
insert image description here

2. Function introduction

  1. Support CGCS2000/WGS84/XIAN80/BEIJING54 four common coordinate systems;
  2. Automatically judge the legitimacy of the input latitude and longitude and X/Y coordinates;
  3. Automatically calculate the central meridian according to the standard 3° zone;
  4. It supports converting latitude and longitude to projected coordinates and converting projected coordinates to latitude and longitude.

3. Realize the logic

1) Use the GDAL library to construct the basic coordinate system data.

OGRSpatialReference m_spatialReference;

if(m_Coordinate == CoordinateDialog::CGCS2000)
    {
    
    
        qDebug() << "CGCS2000";
        m_spatialReference.importFromEPSG(4490);
    }
    else if(m_Coordinate == CoordinateDialog::WGS84)
    {
    
    
        qDebug() << "WGS84";
        m_spatialReference.importFromEPSG(4326);
    }
    else if(m_Coordinate == CoordinateDialog::XIAN80)
    {
    
    
        qDebug() << "XIAN80";
        m_spatialReference.importFromEPSG(4610);
    }
    else if(m_Coordinate == CoordinateDialog::BeiJing54)
    {
    
    
        qDebug() << "BeiJing54";
        m_spatialReference.importFromEPSG(4214);
    }

2) Set the projected coordinate system and convert it

    m_spatialReference.SetTM(0.0, nMeridian, 1.0, nAreacode*1000000 + nOffsetE, nOffsetN);
	OGRSpatialReference* pLonLat = m_spatialReference.CloneGeogCS();
	
	// X、Y转经纬度
	OGRCoordinateTransformation* pXY2LonLat = OGRCreateCoordinateTransformation(&m_spatialReference, pLonLat);
	// 经纬度转X、Y
	OGRCoordinateTransformation* pXY2LonLat = OGRCreateCoordinateTransformation(&m_spatialReference, pLonLat);
	
    pXY2LonLat->Transform(1, &dLon, &dLat)

4. Source code download

Complete source code download address: Source code download

Guess you like

Origin blog.csdn.net/m0_37251750/article/details/130163795