Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GetCoords and Mercator Projection ? #422

Open
alfalf99 opened this issue Jan 23, 2021 · 2 comments
Open

GetCoords and Mercator Projection ? #422

alfalf99 opened this issue Jan 23, 2021 · 2 comments

Comments

@alfalf99
Copy link

alfalf99 commented Jan 23, 2021

Hello,

I have a SOUTHAMERICA map (png) from a mercator projection, and want to use it in Excel by applying on it an X/Y chart.

I want to use your very interesting getCoords function to translate the long/lat into X/Y coords (but I see it is for EQUIRECTANGULAR translation, not Mercator)

When I apply the factors and offsets calculated, points are not well positioned :(
For example for Bazil points :
image

Below my setting (I have set x2 and Y2 with Width/ Height of my map image):
image

For example , for rio de Janeiro, I have the following long/lat : -43,23 / -22,9
If I apply factor, I get : 406,41 / 403,18

Is it normal ? Is there a way to get the right factor/offset to help on plot on a mercator projection ?
Thank your for your valuable help.

@neveldo
Copy link
Owner

neveldo commented Jan 25, 2021

Hi, I think this is because the tutorial is for the maps using equi-rectangular projection only, it can be applied for other projections. For the mercator, you will have to define the proper algorithm to convert the coordinates.

Feel free to take a look at this pull-request that includes a map with a mercator projection to see how the getCoords() function is built : #169

@alfalf99
Copy link
Author

Thank you.

I have tried to translate the mercator getcoords function (from the link provided) in VBA.
Coordinates of my south america submap are hardcoded in the VBA script.

But when I test RIO DE JANEIRO, lat, long = (-43.23, -22.9)
I have the result :
x= -0,753617936081045
y = -0,689338045886754
which is wrong, but perhaps I have missed something :(
If it returns x, y I would expect values that are based on dim of my map: width = 510 and height : 792,25.
so something like that : x= 400 and y = 600

Below the VBA code :
`Option Explicit

'// Width of the map, in pixel
Public Const WWwidth As Double = 1008.77
'// Height of the map, in pixel
Public Const WWheight As Double = 651.44
'// Longitude of the left side of the map, in degree
'leftLongitude: -169.6,
'// Longitude of the right side of the map, in degree
'rightLongitude: 190.25,
'// Latitude of the top of the map, in degree
'topLatitude: 83.68,
'// Latitude of the bottom of the map, in degree
'bottomLatitude: -55.55,

Public Sub teste()

Call getCoords(-43.23, -22.9) 'RIO DE JANEIRO lat, lon

End Sub

'*
' * Get x,y point from lat,lon coordinate
' *
' * Principle:
' * (lat, lon) are inputs
' * Projection(lat, lon) = (x', y')
' * Transformation(x', y') = (x, y)
' *
' * Source: http://jkwiens.com/2009/01/23/miller-projection/
' *
' * @param lat latitude value in degree
' * @param lon longitude value in degree
' * @return {x, y} coordinate in pixel
' *
Sub getCoords(lat As Double, lon As Double)

Dim xLeftPrime As Double, xRightPrime As Double, yTopPrime As Double, yBottomPrime As Double
Dim leftLongitude As Double, rightLongitude As Double, topLatitude As Double, bottomLatitude As Double

Dim xPrime As Double, yPrime As Double
Dim x As Double, y As Double

'---------------INPUT  MAP INFO MANUALLY
xLeftPrime = 0
bottomLatitude = -86.66

xRightPrime = 510.9827 '-- sub map width
rightLongitude = -32.16

yTopPrime = 0
topLatitude = 13.5

yBottomPrime = 792.25 '-- sub map height
bottomLatitude = -58
'----------------------------------------


'Project map boundaries with projection (only once for performance)
'If (xLeftPrime Is Empty) Then xLeftPrime = projectLongitude(leftLongitude)
'If (xRightPrime Is Empty) Then xRightPrime = projectLongitude(rightLongitude)
'If (yTopPrime Is Empty) Then yTopPrime = projectLatitude(topLatitude)
'If (yBottomPrime Is Empty) Then yBottomPrime = projectLatitude(bottomLatitude)

'Compute x' and y' (projection-specific value)
xPrime = projectLongitude(lon)
yPrime = projectLatitude(lat)

'Compute x and y
x = (xPrime - xLeftPrime) * (WWwidth / (xRightPrime - xLeftPrime))
y = (yTopPrime - yPrime) * (WWheight / (yTopPrime - yBottomPrime))

Debug.Print x
Debug.Print y
'return {x: x, y: y}

End Sub

' * Transform a longitude coordinate into projection-specific x' coordinate
' * Note: this is not in pixel
' *
' * @param lon longitude value in degree
' * @return x' projection-specific value
' *
Public Function projectLongitude(lon As Double)

'Compute longitude in radian
projectLongitude = lon * Excel.WorksheetFunction.Pi / 180
End Function

'*
' * Transform a latitude coordinate into projection-specific y' coordinate
' * Note: this is not in pixel
' *
' * @param lat latitude value in degree
' * @return y' projection-specific value
' *
Public Function projectLatitude(lat As Double)
'Compute latitude in radian and get its SINUS
Dim latRadSinus As Double
latRadSinus = Math.Sin(lat * Excel.WorksheetFunction.Pi / 180)
projectLatitude = 0.5 * Math.Log((1 + latRadSinus) / (1 - latRadSinus))
End Function
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants