# Lat, Lon and elevation Conversation into unity coordinates

I have came across a very vague project problem and its feasibility report or communication procedures I have to define. And here is the current scenario:

1. I have two applications

2. One is built on Unity3d (which is under my control)

3. The second is also an application (which i don't know, and not under my control-) but it is related to a map service app.

Now the actual thing I want to do is that: If a user click in the map application than, the touch should be recognize in the unity application too.

Suppose I have able to communicate with both applications and the map application providing me the latitude, longitude and elevation information on-click. Now the question is how do I convert its lat, lon and elev into my project work-space/unity coordinates?. I found 3D coordinates on a sphere to Latitude and Longitude, and its about sphere. I don't have sphere I have an environment where different models are placed precisely according to map (there is no dummy models, all are accurate) and the map projection is based on snake-grid.

• Every map is a projection of a sphere(or a section) into a plane there are many ways to do this depending of which one was used to create your "flat" environment you must be able to find the corresponding formula. – Westside Tony Sep 28 '17 at 14:26
• As Westside Tony notes below, without knowing what map projection was used to lay out your environment, this question is under-specified. Please edit the question to add any information you have about the projection being used, or the method used to build/source this environment content. – DMGregory Sep 28 '17 at 16:21
• ok i will share the details soon. at the moment i dont have any detail about the environment. maybe it has model using google maps – Muhammad Faizan Khan Sep 29 '17 at 6:46
• @DMGregory the map projection is based on snake grid. – Muhammad Faizan Khan May 20 at 10:10
• By Snake Grid, do you mean this map projection? If so, it looks unlikely that we'll be able to help you. This appears to be a proprietary mapping method with all information about the formula locked behind paywalls or in black box parameter files. You might want to consult GIS experts for help with that. More conventional map projections like Web Mercator have enough public information for gamedevs to help with, but this one not so much. – DMGregory May 20 at 11:16

Since the map projection is dependent on complex parameters not exposed to us, a formula-based exact solution seems unlikely.

Instead, I'd recommend the game developer's favourite tool: piecewise approximation! This is how we handle textures, 3D models, time and physics, and it works for maps too.

To simplify the problem, we'll assume y = elevation (you can add a scaling or offset factor here as needed), and focus on how (lat, long) map to (x, z) in your scene.

Our strategy will be to cover our map in a web of known points. Then for any arbitrary input within the span of that web, we can interpolate the position of nearby known points.

Start by manually picking feature points that you can clearly identify in both your mapping application and your Unity scene built from that map. Make sure you have good coverage in your main areas of interest, and have at least some points all the way out to the outer bounds of your Unity scene / outside the expected area of play.

For each point, note both:

• the latitude & longitude coordinates that your mapping app reports for that point

• the x & z position of the corresponding point in your Unity scene

Use Delaunay triangulation to turn this point cloud in the 2D lat-long plane into a triangle mesh.

Now it's effectively an inverse texture mapping problem: we have a set of vertices (x, z) with texture coordinates (lat, long), forming a triangle mesh over your Unity scene. Given a texture coordinate (lat, long), we want to know where it sits on one of your mesh's triangles.

We can use a Binary Space Partition to divide the lat-long plane into polygons along the triangle edges, each touching three of your feature points. You can then walk the tree to find which polygon a particular input (lat, long) pair falls into, in logarithmic time, and get the three feature points for that region.

Next you can find the barycentric coordinates of this (lat, long) pair compared against its feature point triangle's (lat, long) positions. Use these barycentric coordinates as interpolation weights to blend the feature points' (x, z) positions, then substitute the y you got from your elevation, and the result is a 3D position in your Unity scene.

The position you get out will exactly match your map at the feature points, and use an affine approximation in between. You can add more closely spaced feature points if you find it's inaccurate in any problem regions.