[R-sig-Geo] Parse EWKT Polygon/MultiLineString/MultiPolygon Text
Phillip Allen
phillipjallen830 at gmail.com
Wed Nov 11 15:22:44 CET 2015
Hi all,
Years ago I wrote a short function (below) that parses a Extended Well Know
Text (EWKT) LineString field from PostGIS. The code is below. Unlike the
normal R sp functions this has XYZM values.
My problem is it was written for a simple linestring as seen below:
SRID=32611;LINESTRING(0 0 0 0,1 1 1 1,1 2 2 2 )
But now I need to parse multiLineString/multiPolygons, and Polygons all of
which have double & triple parentheses.
SRID=32611;POLYGON((0 0 0 0,4 0 0 1,4 4 2 3,0 4 2 4,0 0 3 5),(1 1 0 0, 2 1
2 2, 2 2 2 3, 1 2 3 4,1 1 0 5))
SRID=32611;MULTILINESTRING((0 0 0 0,1 1 0 1,1 2 2 2),(2 3 2 0,3 2 2 1,5 4 3
3))
SRID=32611;MULTIPOLYGON(((0 0 0 0,4 0 1 0,4 4 1 0,0 4 1 0,0 0 1 0),(1 1 1
0,2 1 1 0,2 2 1 0,1 2 1 0,1 1 0 0)), ((-1 -1 1 0,-1 -2 1 0,-2 -2 1 0,-2 -1
1 0,-1 -1 1 0)))
For the life of me I cannot think of an elegant way to parse it out without
a complicated for-each loop. Does anyone have an idea of how to parse ewkt
(xyzm)?
I am using these to plot up vertical geological cross-sections in rgl's
plot3d/segments3d with bore-hole data such as this:
>segments3d(ewkt.xyzm2matrix(xsecD$gewkt) )
I hope one day soon sp can handle 2.5D data.
regards,
Phil
ewkt.xyzm2matrix <- function(t) {
# Converts Extented-Well-Know-Text Coordinates to XYZ Coordinate matrix
t <- gsub("SRID=32611;LINESTRING", "", t)
t <- unlist(substr(t, 2, nchar(t)-1 ))
c <- 4
r <- length(unlist(strsplit(t,",")))
t <- gsub(',', ' ', t)
t <- as.double(unlist(strsplit(t," ")))
tmat <- matrix(t, r, c, byrow=TRUE)
tmat <- tmat[, 1:3]
return(tmat)
}
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list