[prev in list] [next in list] [prev in thread] [next in thread] 

List:       r-sig-geo
Subject:    Re: [R-sig-Geo] extract X, Y coordinates from (points) POSTGIS
From:       "Steinbuch, Luc" <luc.steinbuch () wur ! nl>
Date:       2015-04-21 15:19:35
Message-ID: 85f8df2a3afb40d5bbc544c26c23b534 () scomp5292 ! wurnet ! nl
[Download RAW message or body]

Hi Marcos and Barry,

Another, low-level solution would be:

----------------

d <- as.data.frame(matrix(data=NA, nrow=4))
d$xy <- c("POINT (-59.900833 -34.869722)", "POINT (-60.009032477477234 \
-34.71350213192503)", "POINT (-60.13337136105485 -34.74472597084807)", "POINT \
(-60.199859958706185 -34.7384738077165)")

vs_numbers_and_space = substr(d$xy, start=8, stop=nchar(d$xy)-1) # remove "POINT ("  \
at beginning and ")" at end of strings # vs_ : vector of strings

ms_just_numbers =  matrix(unlist(strsplit(vs_numbers_and_space, split=" ")), nrow=4, \
byrow = TRUE) # split strings at space, and put into matrix # ms_ : matrix of strings

d$X = as.numeric(ms_just_numbers[,1])
d$Y = as.numeric(ms_just_numbers[,2])

remove(vs_numbers_and_space, ms_just_numbers)

-------------------------------

Luc


Date: Mon, 20 Apr 2015 12:01:51 +0200
From: Marcos Angelini <angelini75@gmail.com>
To: R-sig-Geo@r-project.org
Subject: [R-sig-Geo] extract X,	Y coordinates from (points) POSTGIS
	coordinates (WKT
Message-ID:
	<CAGZnXNBmrM4X5zdZC94zMryeqD1VZbXWiYR2fL4M51LWvxVdzg@mail.gmail.com>
Content-Type: text/plain; charset="UTF-8"

Hi community,

I have a data frame of points with coordinates (WKT) in one column. It is

d <- as.data.frame(NULL)
d$xy <- c("POINT (-59.900833 -34.869722)", "POINT (-60.009032477477234 \
-34.71350213192503)", "POINT (-60.13337136105485 -34.74472597084807)", "POINT \
(-60.199859958706185 -34.7384738077165)")

I want to convert this into:
d$X
-59.900833
-60.009032
-60.133371
-60.199860
d$Y
-34.869722
-34.713502
-34.744726
-34.738474

I've tried with gsub() but I'm not handy with regular expressions. Could you help me, \
please?

Marcos.

------------------------------

Message: 4
Date: Mon, 20 Apr 2015 12:24:35 +0100
From: Barry Rowlingson <b.rowlingson@lancaster.ac.uk>
To: Marcos Angelini <angelini75@gmail.com>
Cc: r-sig-geo <R-sig-Geo@r-project.org>
Subject: Re: [R-sig-Geo] extract X, Y coordinates from (points)
	POSTGIS coordinates (WKT
Message-ID:
	<CANVKczMMF=RQr1DxmkZQuG73snn9v2=rqu=S1yQpge_smXn=nw@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

If you install rgeos then you get the readWKT function, and use sp as
well to build some spatial points:

[tip: use `dput(d)` to dump sample data]

> d = structure(list(xy = c("POINT (-59.900833 -34.869722)", "POINT \
> (-60.009032477477234 -34.71350213192503)",
"POINT (-60.13337136105485 -34.74472597084807)", "POINT
(-60.199859958706185 -34.7384738077165)"
)), .Names = "xy", row.names = c(NA, -4L), class = "data.frame")

 > library(sp); library(rgeos)

 > xy = do.call(rbind,lapply(d$xy, readWKT))
 > xy
SpatialPoints:
          x         y
1 -59.90083 -34.86972
1 -60.00903 -34.71350
1 -60.13337 -34.74473
1 -60.19986 -34.73847

You can then get the separate coordinates as vectors:

 > xy$x
        1         1         1         1
-59.90083 -60.00903 -60.13337 -60.19986
 > xy$y
        1         1         1         1
-34.86972 -34.71350 -34.74473 -34.73847

[the 1s are just row labels]

If you just want a 2-column matrix:

 > coordinates(xy)
          x         y
1 -59.90083 -34.86972
1 -60.00903 -34.71350
1 -60.13337 -34.74473
1 -60.19986 -34.73847

 - thats a matrix, not a data frame, but you can easily build one!

Barry

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic