java utm坐标转经纬度,utm坐标和经纬度相互转换

项目中用到经纬度相互转换,自己写感觉太麻烦,查询后发现利用geos和proj4可以完成坐标转换,现在记录一下方便以后自己查询。

//经纬度转utm坐标

int convert_lonlat_utm(const new3s_PointXYZ &lon_lat_coord, new3s_PointXYZ &utm_coord)

{

OGRSpatialReference *RefSource = new OGRSpatialReference;

RefSource->SetWellKnownGeogCS("WGS84");

OGRSpatialReference *RefTarget = new OGRSpatialReference;

RefTarget = RefSource->CloneGeogCS();

int utmzone = lon_lat_coord.get_x() / 6 + 31;

RefTarget->SetProjCS("UTM(WGS84) in northern hemisphere.");

RefTarget->SetUTM(utmzone, TRUE);

OGRCoordinateTransformation *poTransform = OGRCreateCoordinateTransformation(RefSource, RefTarget);

double tempX = lon_lat_coord.get_x();

double tempY = lon_lat_coord.get_y();

double tempZ = lon_lat_coord.get_z();

poTransform->Transform(1, &tempX, &tempY, &tempZ);

utm_coord.set_x(tempX);

utm_coord.set_y(tempY);

utm_coord.set_z(tempZ);

return utmzone;

}

这里返回的是条带数,因为在下面UTM坐标转经纬度的时需要条带数。

//utm转经纬度

void convert_utm_lonlat(const new3s_PointXYZ &utm_coord, const int &utmzone, new3s_PointXYZ &lon_lat_coord)

{

//建立投影坐标系到经纬度坐标系的转换

OGRSpatialReference *RefSource = new OGRSpatialReference;

RefSource->SetWellKnownGeogCS("WGS84");

RefSource->SetProjCS("UTM(WGS84) in northern hemisphere.");

RefSource->SetUTM(utmzone, TRUE);

OGRSpatialReference *RefTarget = new OGRSpatialReference;

RefTarget = RefSource->CloneGeogCS();

OGRCoordinateTransformation *poTranform = OGRCreateCoordinateTransformation(RefSource, RefTarget);

OGRPoint *poPoint = new OGRPoint();

double tempx = utm_coord.get_x();

double tempy = utm_coord.get_y();

double tempz = utm_coord.get_z();

poTranform->Transform(1, &tempx, &tempy, NULL);

lon_lat_coord = new3s_PointXYZ(tempx, tempy, tempz);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值