UE4 GDAL 生成Landscape (三)

UE4 GDAL 生成Landscape (三)

在这里插入图片描述

设置仿射变换

void UGISDataComponent::SetGeoTransforms(GDALDatasetRef& DatasetRef)
{
	GeoTransform.AddZeroed(6);
	FMemory::Memcpy(GeoTransform.GetData(), GDALHelpers::GetGeoTransform(DatasetRef).Get(), 6 * sizeof(double));
	
	InvGeoTransform.AddZeroed(6);
	FMemory::Memcpy(InvGeoTransform.GetData(), GDALHelpers::GetInvertedGeoTransform(DatasetRef).Get(), 6 * sizeof(double));
}

经纬度转UE4坐标

FVector UGISDataComponent::GetWorldSpaceLocation(FVector2D GPSCoordinate)
{
	// Get Transform from lat long 
	FString WGS84_WKT = GDALHelpers::WktFromEPSG(4326);
	OGRCoordinateTransformationRef CoordTransform = GDALHelpers::CreateCoordinateTransform(WGS84_WKT, WKT);
	
	UE_LOG(LogTemp, Log, TEXT("coord %s"), *GPSCoordinate.ToString());
	
	UE_LOG(LogTemp, Log, TEXT("WKT:\n %s"), *WKT);	
	
	// LatLong to Projection
	FVector Projected;
	
	// Prior to GDAL 3.0, coordinate transformations expect WGS84 coordinates to be in (lon,lat) format instead of (lat,lon)
	// (See: https://gdal.org/tutorials/osr_api_tut.html#crs-and-axis-order)
	#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,0,0)
		GPSCoordinate = FVector2D(GPSCoordinate.Y, GPSCoordinate.X);
	#endif
	
	check(GDALHelpers::TransformCoordinate(CoordTransform, FVector(GPSCoordinate, 0), Projected))
	
	// Projection to pixel space
	FVector2D PixelLoc = GDALHelpers::ApplyGeoTransform(InvGeoTransform.GetData(), FVector2D(Projected));
	
	// Get Height at pixel from ALandscape
	ALandscape* parent = (ALandscape*)GetAttachmentRootActor();
	FVector out(PixelLoc.X, PixelLoc.Y, 0);
	
	FVector origin;
	FVector bounds;
	parent->GetActorBounds(true, origin, bounds);
	
	// Convert to normalized local space
	out /= FVector(NumPixelsX, NumPixelsY, 1.0);
	
	// Scale to Worldspace
	out *= 2.0 * bounds;
	
	// Offset by actor transform
	out += parent->GetActorLocation();
	
	UE_LOG(LogTemp, Log, TEXT("World Space %s, pixel %s, norm %s, parent_loc: %s, origin %s, bounds: %s"), *out.ToString(), *PixelLoc.ToString(), *(PixelLoc / FVector2D(NumPixelsX, NumPixelsY)).ToString(), *parent->GetActorLocation().ToString(), *origin.ToString(), *bounds.ToString());
	
	out.Z = parent->GetHeightAtLocation(out).Get(0);
	
	return out;
}

UE4坐标转经纬度

FVector2D UGISDataComponent::GetGPSLocation(const FVector& WorldSpaceCoordinate)
{
	ALandscape* parent = (ALandscape*)GetAttachmentRootActor();
	FVector origin;
	FVector bounds;
	
	parent->GetActorBounds(true, origin, bounds);
	
	// Get local position then normalize to pixel space
	FVector2D PixelCoordinate = FVector2D(parent->GetTransform().InverseTransformPosition(WorldSpaceCoordinate));
	
	// Normalize local position
	PixelCoordinate /= FVector2D(bounds * 2.0f);
	
	// Scale to pixel space
	PixelCoordinate *= FVector2D(NumPixelsX, NumPixelsY);

	// Convert PixelSpace to projection
	FVector2D Projection = GDALHelpers::ApplyGeoTransform(GeoTransform.GetData(), PixelCoordinate);
	
	// Get Transform to lat long 
	FString WGS84_WKT = GDALHelpers::WktFromEPSG(4326);
	OGRCoordinateTransformationRef CoordTransform = GDALHelpers::CreateCoordinateTransform(WKT, WGS84_WKT);
	
	FVector Out;
	check(GDALHelpers::TransformCoordinate(CoordTransform, FVector(Projection, 0), Out))
	
	// Prior to GDAL 3.0, coordinate transformations expect WGS84 coordinates to be in (lon,lat) format instead of (lat,lon)
	// (See: https://gdal.org/tutorials/osr_api_tut.html#crs-and-axis-order)
	#if GDAL_VERSION_NUM < GDAL_COMPUTE_VERSION(3,0,0)
		Out = FVector(Out.Y, Out.X, Out.Z);
	#endif
	
	return FVector2D(Out);
}

参考

  1. UE4 GDAL 生成Landscape (一)
  2. GitHub:UE4HmapsGenerator
  3. GitHub:UnrealGDAL
  4. GDAL 命令行工具
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值