问题描述
我最近编写了一个函数,可以从坐标获取墨卡托投影地图上的相对点。
我使用的地图是 svg
,但未缩放。 Download SVG @ Google Drive Link
当 SVG
以位图形式呈现在 PictureBox
中时,此代码和函数预计会获得 SVG
的相对点。问题是函数输出的点与我预期的不符。
在下面将 SVG
渲染为没有缩放的位图的图像中,我标记了一个绿点,我希望它得到的点和红点,函数实际上给了我。
我编写这段代码是为了从 SVG 的经纬度获取 Private Sub fs_Load(sender As Object,e As EventArgs) Handles MyBase.Load
MsgBox(ConvertGeoToPixel(35.652832,139.839478,450,530,123.658963,145.820743,24.217586).ToString())
End Sub
Private Function ConvertGeoToPixel(ByVal latitude As Double,ByVal longitude As Double,ByVal imageWidth As Integer,ByVal imageHeight As Integer,ByVal mapLonLeft As Double,ByVal mapLonRight As Double,ByVal mapLatBottom As Double) As Point
Dim mapLatBottomrad As Double = mapLatBottom * Math.PI / 180
Dim latitudeRad As Double = latitude * Math.PI / 180
Dim mapLonDelta As Double = mapLonRight - mapLonLeft
Dim worldMapWidth As Double = (imageWidth / mapLonDelta * 360) / (2 * Math.PI)
Dim mapOffsetY As Double = worldMapWidth / 2 * Math.Log((1 + Math.Sin(mapLatBottomrad)) / (1 - Math.Sin(mapLatBottomrad)))
Dim x As Double = (longitude - mapLonLeft) * (imageWidth / mapLonDelta)
Dim y As Double = imageHeight - ((worldMapWidth / 2 * Math.Log((1 + Math.Sin(latitudeRad)) / (1 - Math.Sin(latitudeRad)))) - mapOffsetY)
Return New Point() With {
.X = Convert.ToInt32(x),.Y = Convert.ToInt32(y)
}
End Function
的相对点。
width
我知道地图的 height
是 450,123.658963
是 530。我还知道,当完全没有缩放时,地图的左经度是 45.523885
,其最高纬度是 { {1}},其右侧经度为 145.820743
,底部纬度为 24.217586
。此信息包含在我下载 SVG 地图的源中。我将这些信息合并到了 SVG 中。
这个输出很不正常(如图所示)是:
{X=329,Y=261}
这太离谱了。
我不确定发生了什么。我该怎么做才能解决这个问题?
解决方法
转换是线性转换。纬度的经度调整已经在 SVG 中为您完成。只需计算纬度和经度的比例和偏移即可。