[R-sig-Geo] extract X, Y coordinates from (points) POSTGIS

Steinbuch, Luc luc.steinbuch at wur.nl
Tue Apr 21 17:19:35 CEST 2015


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 at gmail.com>
To: R-sig-Geo at r-project.org
Subject: [R-sig-Geo] extract X,	Y coordinates from (points) POSTGIS
	coordinates (WKT
Message-ID:
	<CAGZnXNBmrM4X5zdZC94zMryeqD1VZbXWiYR2fL4M51LWvxVdzg at 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 at lancaster.ac.uk>
To: Marcos Angelini <angelini75 at gmail.com>
Cc: r-sig-geo <R-sig-Geo at 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 at 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



More information about the R-sig-Geo mailing list