# [R] Generating time progressing line for Google Earth

fbielejec fbielejec at gmail.com
Thu Jan 20 14:01:45 CET 2011

```Dear,

I am trying to visualise a time-progressing line (it's supposed to

The idea is to have a function which takes start and end point
geographic coordinates, as well as number of intervals to chop the path
up, and returns the collection of points segmenting this line.

Unfortunately my calculations fail for large distances, as the
generated lines are nowhere near being straigth (run the code to see the
example).

My R code so far:

############
#---LIBS---#
############
library(brew)

###############################
#---GREAT CIRCULAR DISTANCE---#
###############################
}

return(degree)
}

# Calculates the distance between two points using the
# Spherical Law of Cosines
gcd.slc <- function(lon1, lat1, lon2, lat2) {

R = 6371.0 # Earth mean radius [km]

d = acos(sin(lat1)*sin(lat2) + cos(lat1)*cos(lat2) *
cos(lon2-lon1)) * R
return(d) # Distance in km
}

########################
#---PROGRESSING LINE---#
########################
GenerateLineSegments <- function(start_point.Long, start_point.Lat,
end_point.Long, end_point.Lat, numberOfIntervals) {

coords <- matrix(NA, numberOfIntervals, 2)

full_distance = gcd.slc(lat1 = start_point.Lat, lon1 =
start_point.Long, lat2 = end_point.Lat, lon2 = end_point.Long)
distance_slice = full_distance / numberOfIntervals

for(i in 1 : numberOfIntervals) {

distance = i * distance_slice

ang_dist = distance / 6371.0

# First calculate the bearing
bearing = atan2( sin(lon_diff) * cos(lat_2), (cos(lat_1) * sin(lat_2))
- (sin(lat_1) * cos(lat_2) * cos(lon_diff)) );

# Then use the bearing and the start point to find the destination
new_lat_rad = asin(sin(lat_1) * cos(ang_dist) + cos(lat_1) *
sin(ang_dist) * cos(bearing)); new_lon_rad = lon_1 +
atan2( sin(bearing) * sin(ang_dist) * cos(lat_1), cos(ang_dist) -
sin(lat_1) * sin(lat_2) );

# Convert from radians to degrees

coords[i, 2] = new_lat
coords[i, 1] = new_lon
}

return(coords)
}

###################
#---BREWING KML---#
###################
#A	39.5	-4.5
#E	44.75	-107.5

startLong = -4.5
startLat = 39.5

endLong = -107.5
endLat = 44.75

numberOfIntervals = 100

coords <- GenerateLineSegments(startLong, startLat, endLong, endLat,
numberOfIntervals) coords <- as.data.frame(coords)
names(coords) <- c("Long", "Lat")

seg <- data.frame(matrix(NA, nrow(coords) - 1, 5))
names(seg) <- c("x", "y", "xend", "yend", "segment")

for (i in 1 : (nrow(coords) - 1 ) ) {
seg[i, ]\$x = coords[i, ]\$Long
seg[i, ]\$y = coords[i, ]\$Lat
seg[i, ]\$xend = coords[i + 1, ]\$Long
seg[i, ]\$yend = coords[i + 1, ]\$Lat
seg[i, ]\$segment = paste(i)
}

#seg

brew(file = "LinesTemplate.xml", output = "RKMLoutput.kml" )

...and the kml template file to be used with brewer:

<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>

<Folder><name>lines</name>

<% for(i in 1:nrow(seg)){ %>
<Placemark>
<TimeSpan>
<begin><%= paste(1980+i, "01", "01", sep="-")%></begin>
</TimeSpan>
<name><%=paste(seg\$x[i], ",", seg\$y[i]," : ", seg\$xend[i],
",", seg\$yend[i], sep="")%></name>
<Style><LineStyle><color>7f9bffff</color><width>3.5</width></LineStyle></Style>
<LineString> <tessellate>1</tessellate>
<coordinates><%=seg[i,]\$x%>, <%=seg[i,]\$y%>, 0.0
<%=seg[i,]\$xend%>, <%=seg[i,]\$yend%>, 0.0
</coordinates></LineString>
</Placemark>
<% } %>

</Folder>

</Document></kml>

What is going wrong here?

--
while(!succeed) { try(); }

```