One of the fastest and easiest possible solutions uses a short program written with the free open source program R
(the R project for statistical computing). The following code computes the distance matrix (using spherical distances) between two arrays of (lon, lat) coordinates named customers
and facilities
and stores it in an array distances
(with rows for customers and columns for facilities).
dist <- function(x, y=c(0,0), radius=6366710) {
# `x` and `y` are both (lon, lat) in radians.
# `x` is an `n` by 2 matrix.
# Returns distances on a sphere of radius `radius`; the default value
# is a spherical model in meters.
return (2 * radius * asin( sqrt(sin((x[,2]-y[2])/2)^2 +
cos(x[,2])*cos(y[2])*(sin(x[,1]-y[1])/2)^2)))
}
distances <- apply(facilities * pi/180, 1, function(y) dist(customers * pi/180, y)
It runs reasonably quickly: your problem with 150 customers and 30 facilities generates a matrix of all 150*30 = 4500 distances in 2.5 milliseconds.
To extract the coordinates from a Google Earth file, save the locations directly in KML format or unzip the KMZ file (which will produce a KML version). This is an ASCII file containing (lon, lat) coordinates.. The maptools package reads such files directly. Here is a working example that reads KML files of customers and facilities and computes all distances between them.
read.kml <- function(s) {
require(maptools)
x <- getKMLcoordinates(s, ignoreAltitude=TRUE)
return (matrix(unlist(x), ncol=2))
}
customers <- read.kml("F:/temp/customers.kml")
facilities <- read.kml("F:/temp/facilities.kml")
distances <- apply(facilities * pi/180, 1, function(y) dist(customers * pi/180, y))
At this point you can perform just about any kind of calculation you might like with the distances and--of course--you can write them to a file for post-processing on another platform.
The only way to have the same placemark appear in two folders in KML is 1) duplicate the placemark or 2) have NetworkLink in each folder reference the placemark in a separate KML file.
In both cases, if both folders are visible then the placemark is duplicated on the map which may or may not be apparent. Google Earth does not remove duplicates. If user clicks on the placemark then the icon will appear twice on the map. If the placemark is a point then the icon will only show once but the name label will appear twice (unless name is hidden via the LabelStyle).
One way to prevent the same item from being visible from two folders is making the folders use a radioFolder list style. That way only one folder and one placemark can be visible at a time.
Best Answer
If you have your placemarks in order in the kmz file, the task will be fairly simple (and can be accomplished in a text editor). Either save the placemarks as a kml file (right click and "Save place as"), or change the file extension of the kmz file to zip and extract the zip file (a kmz file is simply a zipped up kml file(s)).
Here you can open up the kml file with any text editor. After figuring out what placemarks you want to be included in each folder, simply place the following text before the first placemark that you want to be included in the folder (here is the KML reference page);
Then when you find the last placemark you want to be included in that same folder, place the closing folder tag;
Then repeat until you have all your placemarks in folders as desired. Tedious, but if a one time job and you only need a few folders, it shouldn't take more than a few minutes copying and pasting into the appropriate spots.
If your kmz file is not sorted appropriately, this won't work. If that is the case, you need to either sort the file before its creation, or utilize other software to read in the kmz file and sort it. If you have access to ArcGIS, you can use a tool to change the kml file to a shapefile, then sort the shapefile accordingly in ArcMap, and then export the KML file. ArcMap has built in functionality to do this, but this script has worked incredibly well for me in the past (Export to KML). If you don't have access to ESRI products, you will have to ask someone else besides me how to sort the placemarks in the KMZ file!