Excel - 3D cartesian points - euclidean distance for a large group of points - excel

I have a large set of XYZ Cartesian points in Excel (some 40k actually) and was looking for a formula or macro to compare every point to every other point to get the distances between them.
The math to get the distance value between two 3D points is:
Distance=SQRT((X2 – X1)^2 + (Y2 – Y1)^2 + (Z2 – Z1)^2)
X1=the X value of the 1st point
X2=the X value of the 2nd point
Y1=the Y value of the 1st point
Y2=the Y value of the 2nd point
etc
Here is an example starting with 10 points:
http://i.imgur.com/U3lchMk.jpg
Would anyone know of a way to build this into Excel so that I can just copy the formula across the page to the horizontal limit? Or would you recommend a better way than using Excel?
As a secondary goal, I want to group the points into clusters that can connect by a distance lower than 2. But if I can accomplish the first goal, I can worry about the second later.

Actually, I was able to come up with the solution with a bit more research: i.imgur.com/9JL5Qni.jpg =SQRT(((INDIRECT("A"&$D2))-(INDIRECT("A"&E$1)))^2+((INDIRECT("B"&$D2))-(INDIRECT‌​("B"&E$1)))^2+((INDIRECT("C"&$D2))-(INDIRECT("C"&E$1)))^2)

Related

Calculating a route between two cells, distinguishing between "land" and "water" cells

World Map
I am using Excel and VBA for D&D and have made a world map seperating resources between different cells. I am using this to calculate the distance between the towns and the resources, so that I can calculate the price per pound of the resource.
In order to find the distance between two points (the resource and the town) I use this formula:
=SQRT(([#ROW]-$C$2)^2+([#COLUMN]-$D$2)^2)
This finds the hypotenuse between the two points, using the columns and rows difference as the other sides of the triangle.
However, I need to go one step further and have a means to tell whether the hypotenuse travels through water tiles or land.
You need a function that gives you a list of cells along your hypotenuse. Then you test each cell to see if it is land or water.
Step 1: Determine the biggest distance vertically or horizontally
Step 2: Divide the smallest distance by the largest distance. This ratio is the distance you move in the smallest direction for each unit of the largest.
Step 3: do a for for loop x to y step 1 for the largest distance. for each iteration of the loop cumulatively add the ratio from 2 to the start position of the smallest. The get the cell reference from the current largest plus the (integer +1) part of the start plus cumulative movement distance in the direction of the shortest.

Distance between straight lines

I work in the oil & gas industry and I'm seeking advice about how to calculate the minimum distance between a set of wells (the wells are drawn as straight lines on a map). My goal is for each individual well to have a unique "spacing" value (measured in feet) which is basically the straight-line horizontal distance to the closest wellbore on a map. Below is a simple example of what I'm trying to accomplish (assume the pipe | symbol is a wellbore and the dashes are the distance between the wells)
|--|---|-|
In the drawing above we have 4 wells. The 1st well (starting from the far left) would have a spacing value of 2 (since there are 2 dashes to the closest well), the 2nd well would also have a value of 2 (since the closest well is the one to the far left which is two spaces away), the 3rd well would have a value of 1, and the 4th well would have a value of 1.
Now imagine that I have hundreds of these wells (each with latitude/longitude points that describe the start & end points of each well) and I have them all mapped in TIBCO Spotfire (scattered across Texas). Do you guys know if it would even be possible to automate a calculation like the above? I would also like to build in a rule that says the max distance between wells is 2640 ft (half of a mile).
Any ideas are appreciated!
I think you should be able to do this without any R or iron python.
Within Spotfire, you can calculate the distance in miles between 2 points using the formula below (substitute 6371 for 3958.756 to get the answer in kilometres).
GreatCircleDistance([Lat 1],[Lon 1],[Lat 2],[Lon 2]) * 3958.756
For your use case, you could cross join your table of locations, so that you have a row for every possible location combination, then calculate the distance between them using the formula above. After that, it should be pretty straight forward to find each wells closest pair.

Build a geographical map from triangle points based on distance

I have 5 {x,y} points randomly placed on a grid
Each of the points do not know the {x,y} coordinates of the other points
Each of the points do know the distance of each of the other points from their {x,y} position
Each of the points exchanges this distance information with every other point
So every point knows every distance of every other point
Using this distance information every point can calculate (by finding the angles) triangles for every other point using itself as a reference point
Example, point 1 can calculate the following triangles:
1-2-3,
1-2-4,
1-2-5,
1-3-4,
1-3-5,
1-4-5,
and using the distance data recieved from the other points it can also calculate
2-3-4,
2-3-5,
2-4-5,
3-4-5
I would like to build a map of the location of every other point relative to a single point
How should I go about doing this? I am asuming it would be some kind of triangulation algorithm but these mainly seem to compute the location of a point from three other points, not the other way around where the other points {x,y} coordinates are discovered based on only the distance information.
I have tried plotting the two possible triangles for every 3 triangle points and then rotating them on a fixed known point to try and align them, but I think this avenue will end up with too many possibilities and errors
Ultimately I would like every point to end up with {x,y} coordinates of every other point relative to itself
You know the distance from one point to every other, dij. Thus, point 2 lies in a circumference of center point 1 and radius = d12. Point 3 lies in a circumference of center point 1 and R=d13 and it also lies in another circumference of center point 2 and R=d23.
See this picture:
I've set point 2 in X-axis for simplicity.
As you see, point 3 is on the intersection of two cicrcumferences centered at P1 and P2. There is a second intersection, P3a. Let's choose the one that is upwards and continue.
For P4 we can use three circumferences, centered at P1, P2 and P3. Again we get two solutions.
The same process can be done with the rest of points. For Pn you have n-1 circumferences.
I'm sure you can find the maths for circle-circle intersection.
Some remarks must be observed:
1) The construction is simpler if you first sort the points by distance to P1.
2) Not all distances generate a solution. For example, increase d13 an there's no intersection between the two circumferences for P3. Or increase d14 and now the three circumferences don't intersect in just the two expected points 4 and 4a.
3) This fact can be overworked by considering the average of intersections and the distance from each solution to this average. You can set a tolerance in these distances and tell if the average is a solution or else some dij is wrong. Since two solutions are possible, you must consider two averages.
4) The two possible triangulations are symmetric, over X-axis in the case I've drawn.
The real solution is obtained by a rotation around P1. To calculate the angle of rotation you need the {x,y} coordinates of another point.

Finding Distance From List Of Coordnates

looking to see if someone can suggest a site or excel method to find the distance of multiple long,lat coordinates from a one point
example: I have a starting point and 7 other coordinates, is there a way to find how far away (in KM/MI) each point is from the starting point?
Starting loction : 33.17261,-117.14571
list of coordinates
32.75827,-117.17577
32.76079,-117.18589
32.76444,-117.20174
32.59815,-117.01685
32.66387,-117.05577
32.59811,-117.01681
32.66381,-117.05571
lets assume the column they are stored in is column A starting in row 2 (assuming you have a header row). The first thing you are going to want to do is split them into their own columns so you can work with the numbers. There are a couple of ways to do this. The simplest is using the built in feature Text-to-Columns located on the Data ribbon.
Have the whole column selected when you start the process and on the first page, select Delimited.
On the second step choose "," as a delimeter and then press finish. You do not need the third step.
once that is done your data should now be sitting in two columns.
I will place the point you are referring to in C2 and D2, you can use text to columns for this part too or just type it in.
So base on information on another site (assuming its correct), take the earth as a sphere with radius, 6371 km. Place this value in E2.
Next convert your list into X and Y values using the following equations:
F2
=$E$2*COS(RADIANS($B2))*COS(RADIANS($A2))
G2
=$E$2*SIN(RADIANS($B2))*SIN(RADIANS($A2))
note that the degrees were converted to radians for use in excels trig functions
Repeat the process for you starting point coordinates and place the formulas in H2 and I2.
H2
=$E$2*COS(RADIANS($D2))*COS(RADIANS($C2))
I2
=$E$2*SIN(RADIANS($D2))*SIN(RADIANS($C2))
Finally in J2 use the following formula:
=SQRT((F2-$H$2)^2+(G2-$I$2)^2)
Copy the formulas in F2, G2, and J2 down as for as your source list goes. The values in J represent the distance between the X Y points. It does not the curvature of the earth though. Apparently there are many different models to predict this. You need one that works for your area if you want something more refined.
For geodesic grade accuracy (fraction of a mm) you can use my Excel add-in available on GitHub: https://github.com/tdjastrzebski/Vincenty-Excel, in particular VincentyInvDistance() function. The solution implements Vincenty's formulae,
Otherwise use Haversine formula but it does not provide geodesic grade results.
The topic is not that trivial, see Geodesics on an ellipsoid for more details.

Excel formula to calculate the distance between multiple points using lat/lon coordinates

I'm currently drawing up a mock database schema with two tables: Booking and Waypoint.
Booking stores the taxi booking information.
Waypoint stores the pickup and drop off points during the journey, along with the lat lon position. Each sequence is a stop in the journey.
How would I calculate the distance between the different stops in each journey (using the lat/lon data) in Excel?
Is there a way to programmatically define this in Excel, i.e. so that a formula can be placed into the mileage column (Booking table), lookup the matching sequence (via bookingId) for that journey in the Waypoint table and return a result?
Example 1:
A journey with 2 stops:
1 1 1 MK4 4FL, 2, Levens Hall Drive, Westcroft, Milton Keynes 52.002529 -0.797623
2 1 2 MK2 2RD, 55, Westfield Road, Bletchley, Milton Keynes 51.992571 -0.72753
4.1 miles according to Google, entry made in mileage column in Booking table where id = 1
Example 2:
A journey with 3 stops:
6 3 1 MK7 7DT, 2, Spearmint Close, Walnut Tree, Milton Keynes 52.017486 -0.690113
7 3 2 MK18 1JL, H S B C, Market Hill, Buckingham 52.000674 -0.987062
8 3 1 MK17 0FE, 1, Maids Close, Mursley, Milton Keynes 52.040622 -0.759417
27.7 miles according to Google, entry made in mileage column in Booking table where id = 3
If you want to find the distance between two points just use this formula and you will get the result in Km, just convert to miles if needed.
Point A: LAT1, LONG1
Point B: LAT2, LONG2
ACOS(COS(RADIANS(90-Lat1)) *COS(RADIANS(90-Lat2)) +SIN(RADIANS(90-Lat1)) *SIN(RADIANS(90-lat2)) *COS(RADIANS(long1-long2)))*6371
Regards
Until quite recently, accurate maps were constructed by triangulation, which in essence is the application of Pythagoras’s Theorem. For the distance between any pair of co-ordinates take the square root of the sum of the square of the difference in x co-ordinates and the square of the difference in y co-ordinates. The x and y co-ordinates must however be in the same units (eg miles) which involves factoring the latitude and longitude values. This can be complicated because the factor for longitude depends upon latitude (walking all round the North Pole is less far than walking around the Equator) but in your case a factor for 52o North should serve. From this the results (which might be checked here) are around 20% different from the examples you give (in the second case, with pairing IDs 6 and 7 and adding that result to the result from pairing IDs 7 and 8).
Since you say accuracy is not important, and assuming distances are small (say less than 1000 miles) you can use the loxodromic distance.
For this, compute the difference of latitutes (dlat) and difference of longitudes (dlon). If there were any chance (unlikely) that you're crossing meridian 180º, take modulo 360º to ensure the difference of longitudes is between -180º and 180º. Also compute average latitude (alat).
Then compute:
distance= 60*sqrt(dlat^2 + (dlon*cos(alat))^2)
This distance is in nautical miles. Apply conversions as needed.
EXPLANATION: This takes advantage of the fact that one nautical mile is, by definition, always equal to one minute-arc of latitude. The cosine corresponds to the fact that meridians get closer to each other as they approach the poles. The rest is just application of Pythagoras theorem -- which requires that the relevant portion of the globe be flat, which is of course only a good approximation for small distances.
It all depends on what the distance is and what accuracy you require. Calculations based on "Earth locally flat" model will not provide great results for long distances but for short distance they may be ok. Models assuming Earth is a perfect sphere (e.g. Haversine formula) give better accuracy but they still do not produce geodesic grade results.
See Geodesics on an ellipsoid for more details.
One of the high accuracy (fraction of a mm) solutions is known as Vincenty's formulae. For my Excel VBA implementation look here https://github.com/tdjastrzebski/Vincenty-Excel

Resources