SCM

[#2283] kml_layer.SpatialLines to work with multiple lines within 'Lines' object

Date:
2012-09-20 23:00
Priority:
3
State:
Open
Submitted by:
Anthony Fischach (afischbach)
Assigned to:
Nobody (None)
Product:
None
Operating System:
other
Component:
None
Summary:
kml_layer.SpatialLines to work with multiple lines within 'Lines' object

Detailed description
Tom,
the example provided for SpatialLines* is working on lines that all contain only one line for each set of lines. I have tried the plotKML method with an example dataset that involves more than one line in each 'Lines' object within the SpatialLines* object. This builds a kml file with only the first line in each 'Lines' object. I see that your example for SpatialLines* involves SpatialLines* that have only one line in each 'Lines' object. To help you work through this problem, I have attached a tmp file with a SpatialLinesDataFrame that has more than one line in each 'Lines' object and have attached the .kml file produced from this SpatialLinesDataFrame object.

Here is a quick view of the tmp object showing how it differs from your example eberg_contours.

> str(tmp)
Formal class 'SpatialLinesDataFrame' [package "sp"] with 4 slots
..@ data :'data.frame': 78 obs. of 5 variables:
.. ..$ id : chr [1:78] "All06c50" "All06c95" "For06c10" "For06c20" ...
.. ..$ Area_sq_Km: num [1:78] 3319 23614 289 753 1376 ...
.. ..$ Method : chr [1:78] "Occupancy" "Occupancy" "Foraging" "Foraging" ...
.. ..$ Month : int [1:78] 6 6 6 6 6 6 6 6 6 6 ...
.. ..$ Contour : num [1:78] 50 95 10 20 30 40 50 60 70 80 ...
..@ lines :List of 78
.. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
.. .. .. ..@ Lines:List of 3
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slots
.. .. .. .. .. .. ..@ coords: num [1:158, 1:2] 186 186 186 186 186 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slots
.. .. .. .. .. .. ..@ coords: num [1:140, 1:2] 194 194 194 194 194 ...
.. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slots
.. .. .. .. .. .. ..@ coords: num [1:76, 1:2] 198 197 197 197 198 ...
.. .. .. ..@ ID : chr "001"

Comments:

Message  ↓
Date: 2016-04-22 18:00
Sender: Anthony Fischach

Here is a modest function that I wrote to side step the problem.
It restructures the SpatialLines to ensure that each 'Lines' object has one and only 'line', which is then assigned an arbitrary id.
Running this prior to offering a SpatialLines object to plotKML::kml_layer solves the problem.
This function would need to be extended to address data.frame objects.

## Reconstruct the spatialLines to ensure that each Line has an ID
MakeUniqueIds.sl<-function(SL){
listOfLines<-list()
n<-0
nLines<-length(slot(SL, 'lines'))
for(i in 1:nLines){
for(j in 1:length(SL@lines[[i]]@Lines)){
n<-n+1
listOfLines[n]<-SL@lines[[i]]@Lines[j]
}
}
ids<-seq(1:length(listOfLines))
LL<-list()
for(i in ids){
LL[i]<-Lines(listOfLines[i], i)
}
return(SpatialLines(LL, proj4string=prj.StudyArea))
}

Attached Files:

Attachments:
Size Name Date By Download
88 KiBtmp.RData2012-09-20 23:00afischbachtmp.RData
68 KiBtmp.kml2012-09-20 23:00afischbachtmp.kml

Changes

Field Old Value Date By
File Added265: tmp.kml2012-09-20 23:00afischbach
File Added264: tmp.RData2012-09-20 23:00afischbach
Thanks to:
Vienna University of Economics and Business Powered By FusionForge