MapKit通过MKOverlay奇怪的渲染问题添加栅格地图 [英] MapKit Adding Raster Map via MKOverlay Weird Rendering Issue

查看:70
本文介绍了MapKit通过MKOverlay奇怪的渲染问题添加栅格地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将NOAA雷达图像添加到我拥有的气象应用程序中.我注意到经过一些测试后,该图像无法在地图上正确绘制.图像的拐角似乎已接近校正,但是当您离开拐角时,它会偏斜.下面是相关代码.

I have added NOAA radar images to a weather app I have. I noticed that the image was not drawing correctly on the map after some testing. It seems the corners of the image were close to correct but when you move away from the corners it skews off. Below is the relevant code.

覆盖:

@implementation FMRadarOverlay

- (id)initWithImage:(UIImage*)radarImage withLowerLeftCoordinate:(CLLocationCoordinate2D)lowerLeftCoordinate withUpperRightCoordinate:(CLLocationCoordinate2D)upperRightCoordinate{
    self.radarImage = radarImage;

    MKMapPoint lowerLeft = MKMapPointForCoordinate(lowerLeftCoordinate);
    MKMapPoint upperRight = MKMapPointForCoordinate(upperRightCoordinate);
    mapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);
    return self;
}

- (CLLocationCoordinate2D)coordinate{
    return MKCoordinateForMapPoint(MKMapPointMake(MKMapRectGetMidX(mapRect), MKMapRectGetMidY(mapRect)));
}

- (MKMapRect)boundingMapRect{
    return mapRect;
}

@end

渲染器:

@implementation FMRadarOverlayRenderer

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    FMRadarOverlay *radarOverlay = (FMRadarOverlay*)self.overlay;
    CGImageRef imageReference = radarOverlay.radarImage.CGImage;

    MKMapRect theMapRect = [radarOverlay boundingMapRect];
    CGRect theRect = [self rectForMapRect:theMapRect];

    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextTranslateCTM(context, 0.0, -theRect.size.height);
    CGContextSetAlpha(context, 0.6);
    CGContextDrawImage(context, theRect, imageReference);
}

@end

还有创建图层的示例:

//using url with map and drawing transparent to check accuracy of rendering
//url for radar data only is http://radar.weather.gov/Conus/RadarImg/latest_radaronly.gif   
NSURL *radarUrl = [NSURL URLWithString:@"http://radar.weather.gov/Conus/RadarImg/latest.gif"];
NSData *radarData = [NSData dataWithContentsOfURL:radarUrl];
UIImage *rawImage = [UIImage imageWithData:radarData];
FMRadarOverlay *radarOverlay = [[FMRadarOverlay alloc] initWithImage:rawImage withLowerLeftCoordinate:CLLocationCoordinate2DMake(21.652538062803, -127.620375523875420) withUpperRightCoordinate:CLLocationCoordinate2DMake(50.406626367301044, -66.517937876818)];

如果您拍摄相同的图像并进行坐标处理,并使用kml文件将其覆盖在Google Earth上,则它们将正确呈现.就像它无法补偿地球的曲率或其他事物.我已经尝试过使用具有不同地理参考栅格图层的方法,该方法具有相似的效果.有什么想法吗?

If you take the same images and coordinates and overlay them on google earth using a kml file they render correctly. It is like it is not compensating for the curvature of the earth or something. I have tried this with a different georeferenced raster layer it had a similar effect. Any ideas?

KML示例:

<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Document><name>NWS Radar Images</name><open>1</open><Folder>  <name>National Weather Service</name> <ScreenOverlay>  <name>National Weather Service</name>   <description>National Weather Service Doppler Radar RIDGE Imagery http://radar.weather.gov</description>   <visibility>1</visibility> <Icon>  <href>http://radar.weather.gov/ridge/graphics/nws_google.gif</href>  </Icon>  <overlayXY x="0" y="1" xunits="fraction" yunits="fraction" />  <screenXY x="0" y="1" xunits="fraction" yunits="fraction" />  <rotationXY x="0" y="0" xunits="fraction" yunits="fraction" />  <size x="0" y="0" xunits="fraction" yunits="fraction" />  </ScreenOverlay><ScreenOverlay>  <name>NOAA</name>   <description>National Oceanic and Atomospheric Administration  http://www.noaa.gov</description>   <visibility>1</visibility> <Icon>  <href>http://radar.weather.gov/ridge/graphics/noaa_google.gif</href>  </Icon>  <overlayXY x=".2" y="1" xunits="fraction" yunits="fraction" />  <screenXY x=".2" y="1" xunits="fraction" yunits="fraction" />  <rotationXY x="0" y="0" xunits="fraction" yunits="fraction" />  <size x="0" y="0" xunits="fraction" yunits="fraction" />  </ScreenOverlay></Folder><Folder><name>LATEST_RADARONLY</name><Folder><name>Mosaic</name><ScreenOverlay><name>Legend</name><visibility>1</visibility><Icon><href></href><refreshMode>onInterval</refreshMode><refreshInterval>120</refreshInterval></Icon><overlayXY x="1.0" y="0.5" xunits="fraction" yunits="fraction"/><screenXY x="1.0" y="0.5" xunits="fraction" yunits="fraction"/><rotationXY x="0" y="0" xunits="fraction" yunits="fraction"/></ScreenOverlay><GroundOverlay><name>Mosaic</name><Icon><href>http://radar.weather.gov/ridge/Conus/RadarImg/latest.gif</href><refreshMode>onInterval</refreshMode><refreshInterval>120</refreshInterval></Icon><visibility>1</visibility><LatLonBox><north>50.406626367301044</north><south>21.652538062803</south><east>-66.517937876818</east><west>-127.620375523875420</west></LatLonBox></GroundOverlay></Folder></Folder></Document></kml>

渲染不良的图片:

推荐答案

问题出在图像空间投影上.我最终使用了一个名为gdalwarp的工具,以像MapKit一样在墨卡托投影中重新投影图像.我已经在MapKit中看到了很多地面叠加层的示例,但所有这些示例都忽略了这一重要信息.我认为大多数地理栅格地图都是为Google Earth或GIS应用程序设计的.

The problem was with the images spatial projection. I ended up using a tool called gdalwarp to re project the image in mercator projection like MapKit uses. I have seen a lot of examples of ground overlays in MapKit and all of them overlook this important information. I think most geo raster maps are projected for applications like Google Earth or GIS applications.

这篇关于MapKit通过MKOverlay奇怪的渲染问题添加栅格地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆