r - Function returns 0x0 table -
i have function compute latitude , longitude points in order create "ring" around center location. problem results print screen not stored anywhere. function creates dataframe 0 columns , 0 rows. want able take these coordinates , use them elsewhere. able nest function well, can't nest when doesn't return anything.
my end goal create kml code. have kml code need repeat many times. kml code creates radius rings, fills them color, , adds name place. want generate files automatically using list of locations in lat/lon.
my question is, how can function return list of coordinates want may paste them in kml code accordingly? can loop using adply , printed results 3 coordinates, nothing created.
i quite new coding, please gentle. in advance.
make.ring.file=function(dist,df) { r = 6378.14 #radius of earth d = dist*1.609344 #distance of ring radius in km lat1 = df$lat*(pi/180) #current lat point converted radians lon1 = df$lon*(pi/180) #current lon point converted radians num3=0 index=seq(from=0,to=360,by=120) bear=null lat=null lon=null z=null coordlist=null for(n in 1:length(index)) { bear[n]=index[n]*(pi/180) lat[n]=(asin(sin(lat1)*cos(d/r) + cos(lat1)*sin(d/r)*cos(bear[n])))*(180/pi) lon[n]=(lon1 + atan2(sin(bear[n])*sin(d/r)*cos(lat1), cos(d/r)-sin(lat1)*sin(lat[n]*(pi/180))))*(180/pi) z[n]=0 coordlist[n]=paste(lon[n],lat[n],z[n],sep=",") } return(data.frame(cat(coordlist,"\n","\n"))) } > head(x1) lat lon 1 38.86095 -86.51672 2 30.63275 -84.41614 3 31.53697 -87.88780 > results=adply(x1,1,make.ring.file,dist=30) -86.51672,39.2946592897837,0 -86.0358241901732,38.6431079084023,0 -86.9976158098268,38.6431079084023,0 -86.51672,39.2946592897837,0 -84.41614,31.0664592897837,0 -83.9805971533182,30.4151694949636,0 -84.8516828466818,30.4151694949636,0 -84.41614,31.0664592897837,0 -87.8878,31.9706792897837,0 -87.4481292235866,31.3193631233201,0 -88.3274707764134,31.3193631233201,0 -87.8878,31.9706792897837,0 > str(results) 'data.frame': 0 obs. of 0 variables > is.data.frame(results) [1] true
i think want:
make.ring.file=function(dist,df) { r = 6378.14 #radius of earth d = dist*1.609344 #distance of ring radius in km lat1 = df$lat*(pi/180) #current lat point converted radians lon1 = df$lon*(pi/180) #current lon point converted radians num3=0 index=seq(from=0,to=360,by=120) bear=null lat=null lon=null z=null coordlist=null for(n in 1:length(index)) { bear[n]=index[n]*(pi/180) lat[n]=(asin(sin(lat1)*cos(d/r) + cos(lat1)*sin(d/r)*cos(bear[n])))*(180/pi) lon[n]=(lon1 + atan2(sin(bear[n])*sin(d/r)*cos(lat1), cos(d/r)-sin(lat1)*sin(lat[n]*(pi/180))))*(180/pi) z[n]=0 coordlist[n]=paste(lon[n],lat[n],z[n],sep=",") } return(data.frame(out=paste(coordlist,collapse=" "))) }
the key addition is:
return(data.frame(out=paste(coordlist,collapse=" ")))
the function cat
, prints console, can't assign - merely making empty data.frame , printing.
the paste command works pasting 4 lines of coordlist
, separator of " ". can modify required downstream code.
Comments
Post a Comment