Center of Gravity II

An accurate center of gravity calculator for Google Earth is now available here. All the code is Brammeleman‘s, taken from his calculator page here, save for the one final altered latitude correction, which places the center of gravity closer to the poles — at the correct position.

Abbreviated instructions: In Google Earth, collect your placemarks in a folder and save that folder to your hard drive as a KML file (not KMZ). Upload this file into the calculator and open the returned file in Google Earth. Voila.

So what does this center of gravity calculator do, precisely?

Practically speaking, the spot you get back minimizes the total distance people at placemarks will need to travel in order to meet up together. And if you calculate the center of gravity for just two placemarks, the straight line you can draw through all three points will be the shortest distance as the jumbo jet flies between your two placemarks.

newcog.jpgMathematically speaking, the code first finds the center of gravity for given points of equal mass on the surface of a sphere in three dimensional space. (The center of gravity will thus be inside the sphere.) It then finds the point on the sphere closest to the center of gravity by projecting a straight line from the center of the sphere through the center of gravity to the surface of the sphere.

The algorithm assumes Earth is a sphere, even though it is actually a geoid, so errors of tens of meters will persist. Correcting for those is something I am two PhDs short of being able to do.

The original story is three posts down, here. As a result of the correction, my family’s actual center of gravity is now 43km further north, in Bree, near the Dutch border.

25 thoughts on “Center of Gravity II”

  1. Tried it. Saved three placemarks to a new folder, saved the folder as a .KML file to my desktop, then attempted to upload it. No joy…got this error message:

    Fatal error: Call to a member function on a non-object in /home/stefan/public_html/ogleearth/index.php on line 40

    Did I do something wrong, or is the code being tweaked, or what?

  2. Forgive me for my lack of understanding, but isn’t this just marking the halfway point between any two given points? In other words, what exactly is new about this?

  3. With 2 points on a 2D Cartesian grid, it’s trivial. With 2 points on a sphere, you need to make some calculations, because you can’t just average longitude and latitude to get the midpoint.

    And while you’re at it, you might as well do it for an arbitrary number of points. So try 20 points instead of two.

  4. I checked out DeafScribe’s file. Make sure you’re just saving a folder in Google Earth, and make sure you’re saving as KML, not KMZ.

    Also, it turns out that Google Earth balks at ampersands (&) in your descriptions of your placemarks… so no ampersands!

  5. Maybe I’m crazy, but all I get is

    Warning: Division by zero in /home/stefan/public_html/ogleearth/index.php on line 82

    Warning: Division by zero in /home/stefan/public_html/ogleearth/index.php on line 86

    Warning: Division by zero in /home/stefan/public_html/ogleearth/index.php on line 87

    Warning: Division by zero in /home/stefan/public_html/ogleearth/index.php on line 88

  6. In response to Tim McCormack: Does the term “Great Circle Route” mean anything to you? The most frequently used flat maps (2D maps) distort the actual geography of the earth, particularly near the poles (make Greenland appear huge). Therefore, it is not possible to find the shortest route between two points by drawing a straight line between them on a flat map. The shortest path will actually lie on the circle going around the entire earth (Great Circle) that passes through the two points.

  7. I saved a folder with two placemarks as KML, and I’m getting the same type of “Division by zero” errors.

  8. It’s true, the code is a bit fragile. You really need to create a _new_ folder (Command-shift N), then drag new placemarks into them, then right-click on the folder and save as KML. If for some reason the folder has been a child of another object, the script treats these phantom objects as having coordinates of 0, which in turn precipitates division by zero.

  9. This is esentially useless to me, too bad: Fatal error: Call to a member function on a non-object in /home/stefan/public_html/ogleearth/index.php on line 40

  10. I did what you suggested abotu the divide by zero problem, created a new folder. But I cannot seem to create a new folder that ISN’T a child of the My Places folder. I’m on Windows, so maybe it’s a difference with that?

  11. I’m with Jason. The folder has to be a child of My Places. I just created a new folder (under My Places), drug three placemarks into it, saved it as a klm file and still got the same div by zero errors.

  12. I’ll have some time this evening to check if it’s a Mac vs. PC thing (it’d surprise me but then at least we’d know the two apps save KML differently).

    Just bear in mind that this code is a toy, a proof of concept, etc… It works for me, but mileage may vary, and the code is available to all to tinker with and improve.


  13. No problem. It sounds like an incredibly cool toy and looking forward to some success on PC if that’s truly the problem. I’m a Software QA guy by trade, so it’s hardwired into me to automatically report bugs. heh

  14. works now, must’ve tweaked something. nifty, now i just need to move so the Center of Gravity for me isn’t an abandoned industrial park in Newark.

  15. Is there a way to weight the place markers so that if 10 people live in the same place I do not have to enter the place marker 10 times to find the COG of them and 1 in another place?

  16. I was also wondering if there is an opportunity to weight the coordinates with that program?

  17. Its not a program. All you need it Google Earth Pro so that you can import data.

    Put all your address and zip codes in an excel spreadsheet. Save the spreadsheet as a .txt file not an .xls

    Make sure when you save the excel file you only have 1 worksheet (ie. delete any other tabs at the bottom)

    Then import the file into google earth. Use field type delimited tab. If you are only using zip codes make sure your excel sheet only uses 1 column. If you are using addresses, lat, long etc. just label them properly but only include what google earth can injest.

    Then plot your addresses/zips/whatever into google earth and save it as a kml file. Then go to the Center Of Gravity web site link above and upload it while your google earth is open and there you have it. A new pushpin will appear which is your COG.

  18. I came upon some posts that go back several months concerning the Center of Gravity calculator. There is a brand new free online tool

    Geographic midpoint Calculator with Maps

    You select or enter your desired locations, then your locations are displayed on a Google map/satellite/hybrid map along with the exact midpoint for those locations. The example given there is finding the midpoint for all cities that you have lived in. You can give your locations relative weights.


  19. A significant update to GeoMidpoint’s

    Geographic Midpoint Calculator that makes it more versatile.

    Before it had a 3000 city database, now it has a built-in geocoder that puts millions of world cities, towns and street addresses at your fingertips. Also multiple latitude/longitude data can now be imported into the calculator.


  20. Getting this error:

    Warning: Invalid argument supplied for foreach() in /home/stefan/public_html/ogleearth/index.php on line 144

    Any thoughts?

Comments are closed.