build a SpatialLines object from a list

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

build a SpatialLines object from a list

R help mailing list-2
Hi all,I'm trying to build a SpatialLines object from a list that contains 124 river segments. Each segment in the list contains the x,y coordinates. I'm using the following code to create the SpatialLines object, but it just retrieves one segment. Any suggestions?

test.func = function(x){
     for (i in 1:length(x)) {        tt[[i]] <- x[i]; tt[[i]]  = Line(tt[[i]]); tt[[i]]  = Lines(list(tt[[i]] ), 'i')        tt1 = SpatialLines(list(tt[[i]]))           }    return(tt1)  }
Ashraf,
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: build a SpatialLines object from a list

Eric Berger
Hi Ashraf,
It is not obvious to me what your structures are but one problem in your
function is the assignment tt1 <- SpatialLines(list(tt[[i]])).

This will set tt1 to just have one item.

Consider the following

test.func <- function(x) {
    tt1 <- list()
    for ( i in ... )  {
       ...
       tt1[[i]] <- SpatialLines(tt[[i]])
    }
    return(tt1)
}

HTH,
Eric


On Mon, Sep 25, 2017 at 3:40 PM, Ashraf Afana via R-help <
[hidden email]> wrote:

> Hi all,I'm trying to build a SpatialLines object from a list that contains
> 124 river segments. Each segment in the list contains the x,y coordinates.
> I'm using the following code to create the SpatialLines object, but it just
> retrieves one segment. Any suggestions?
>
> test.func = function(x){
>      for (i in 1:length(x)) {        tt[[i]] <- x[i]; tt[[i]]  =
> Line(tt[[i]]); tt[[i]]  = Lines(list(tt[[i]] ), 'i')        tt1 =
> SpatialLines(list(tt[[i]]))           }    return(tt1)  }
> Ashraf,
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: build a SpatialLines object from a list

R help mailing list-2
Hi Eric,

Thanks for the help.But this will not solve the problem as it will generate a list and what I need is an object of class sp using SpatialLine function from sp package.So, I need to convert each matrix to coordinates and then to a line and then to a spatial line as figured in the code.

My data structure is a list of 141 matrices.Each matrix represents coordinates of the river lines position.

Ashraf, cheers

    On Monday, 25 September 2017, 16:56, Eric Berger <[hidden email]> wrote:
 

 Hi Ashraf,It is not obvious to me what your structures are but one problem in your function is the assignment tt1 <- SpatialLines(list(tt[[i]])).
This will set tt1 to just have one item.
Consider the following
test.func <- function(x) {    tt1 <- list()    for ( i in ... )  {       ...       tt1[[i]] <- SpatialLines(tt[[i]])    }    return(tt1)}

HTH,Eric    
On Mon, Sep 25, 2017 at 3:40 PM, Ashraf Afana via R-help <[hidden email]> wrote:

Hi all,I'm trying to build a SpatialLines object from a list that contains 124 river segments. Each segment in the list contains the x,y coordinates. I'm using the following code to create the SpatialLines object, but it just retrieves one segment. Any suggestions?

test.func = function(x){
     for (i in 1:length(x)) {        tt[[i]] <- x[i]; tt[[i]]  = Line(tt[[i]]); tt[[i]]  = Lines(list(tt[[i]] ), 'i')        tt1 = SpatialLines(list(tt[[i]]))           }    return(tt1)  }
Ashraf,
        [[alternative HTML version deleted]]

______________________________ ________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/ listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/ posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



   
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: build a SpatialLines object from a list

Eric Berger
Hi Ashraf,
In that case I think you may need to structure the code to first build the
list and only at the end supply that to the SpatialLines function,
something like

test.func <- function(x) {
    tt <- list()
    for ( i in ... )  {
       ...
       tt[[i]] <- (whatever)
    }
    return(SpatialLines(tt))
}

Eric


On Tue, Sep 26, 2017 at 12:36 PM, Ashraf Afana <[hidden email]> wrote:

> Hi Eric,
>
> Thanks for the help.But this will not solve the problem as it will
> generate a list and what I need is an object of class sp using SpatialLine
> function from sp package.So, I need to convert each matrix to coordinates
> and then to a line and then to a spatial line as figured in the code.
>
> My data structure is a list of 141 matrices.Each matrix represents
> coordinates of the river lines position.
>
> Ashraf, cheers
>
>
> On Monday, 25 September 2017, 16:56, Eric Berger <[hidden email]>
> wrote:
>
>
> Hi Ashraf,
> It is not obvious to me what your structures are but one problem in your
> function is the assignment tt1 <- SpatialLines(list(tt[[i]])).
>
> This will set tt1 to just have one item.
>
> Consider the following
>
> test.func <- function(x) {
>     tt1 <- list()
>     for ( i in ... )  {
>        ...
>        tt1[[i]] <- SpatialLines(tt[[i]])
>     }
>     return(tt1)
> }
>
> HTH,
> Eric
>
>
> On Mon, Sep 25, 2017 at 3:40 PM, Ashraf Afana via R-help <
> [hidden email]> wrote:
>
> Hi all,I'm trying to build a SpatialLines object from a list that contains
> 124 river segments. Each segment in the list contains the x,y coordinates.
> I'm using the following code to create the SpatialLines object, but it just
> retrieves one segment. Any suggestions?
>
> test.func = function(x){
>      for (i in 1:length(x)) {        tt[[i]] <- x[i]; tt[[i]]  =
> Line(tt[[i]]); tt[[i]]  = Lines(list(tt[[i]] ), 'i')        tt1 =
> SpatialLines(list(tt[[i]]))           }    return(tt1)  }
> Ashraf,
>         [[alternative HTML version deleted]]
>
> ______________________________ ________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/ listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/
> posting-guide.html <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>
>
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: build a SpatialLines object from a list

MacQueen, Don
In reply to this post by R help mailing list-2
Have you tried following the example in

  ?'SpatialLines-class'

You'll probably get better help from R-sig-geo

And please don't send html email, it makes your email hard to read.

--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
Lab cell 925-724-7509
 
 

On 9/26/17, 2:36 AM, "R-help on behalf of Ashraf Afana via R-help" <[hidden email] on behalf of [hidden email]> wrote:

    Hi Eric,
   
    Thanks for the help.But this will not solve the problem as it will generate a list and what I need is an object of class sp using SpatialLine function from sp package.So, I need to convert each matrix to coordinates and then to a line and then to a spatial line as figured in the code.
   
    My data structure is a list of 141 matrices.Each matrix represents coordinates of the river lines position.
   
    Ashraf, cheers
   
        On Monday, 25 September 2017, 16:56, Eric Berger <[hidden email]> wrote:
     
   
     Hi Ashraf,It is not obvious to me what your structures are but one problem in your function is the assignment tt1 <- SpatialLines(list(tt[[i]])).
    This will set tt1 to just have one item.
    Consider the following
    test.func <- function(x) {    tt1 <- list()    for ( i in ... )  {       ...       tt1[[i]] <- SpatialLines(tt[[i]])    }    return(tt1)}
   
    HTH,Eric    
    On Mon, Sep 25, 2017 at 3:40 PM, Ashraf Afana via R-help <[hidden email]> wrote:
   
    Hi all,I'm trying to build a SpatialLines object from a list that contains 124 river segments. Each segment in the list contains the x,y coordinates. I'm using the following code to create the SpatialLines object, but it just retrieves one segment. Any suggestions?
   
    test.func = function(x){
         for (i in 1:length(x)) {        tt[[i]] <- x[i]; tt[[i]]  = Line(tt[[i]]); tt[[i]]  = Lines(list(tt[[i]] ), 'i')        tt1 = SpatialLines(list(tt[[i]]))           }    return(tt1)  }
    Ashraf,
            [[alternative HTML version deleted]]
   
    ______________________________ ________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/ listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/ posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.
   
   
   
       
    [[alternative HTML version deleted]]
   
    ______________________________________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    https://stat.ethz.ch/mailman/listinfo/r-help
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.