Functions for reading and writing Well Known Text (WKT)

readWKT(text, id = NULL, p4s = NULL)
  writeWKT(spgeom, byid = FALSE)

Arguments

text

character string of WKT

id

character vector of unique ids to label geometries. Length must match the number of subgeometries in the WKT

p4s

Either a character string or an object of class CRS

spgeom

sp object as defined in package sp

byid

Logical determining if the function should be applied across subgeometries (TRUE) or the entire object (FALSE)

Details

readWKT processes the given WKT string and returns an appropriate sp geometry object. If id is not specified then geometries will be labeled by their index position. Because no sp Spatial object may be empty, readWKT is not permitted to create an empty object.

writeWKT converts an sp geometry object to a GEOS C object which is then written out as a WKT string. If byid is TRUE then each subgeometry is individually converted to a WKT string.

References

Additional information on WKT Simple Feature Specification can be found at the following locations:

https://www.ogc.org/standard/sfs/

https://en.wikipedia.org/wiki/Well-known_text

https://en.wikipedia.org/wiki/Simple_Features

Author

Colin Rundel

Examples

g1=readWKT("POINT(6 10)")
g2=readWKT("LINESTRING(3 4,10 50,20 25)")
g3=readWKT("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))")
g4=readWKT("MULTIPOINT((3.5 5.6),(4.8 10.5))")
g5=readWKT("MULTILINESTRING((3 4,10 50,20 25),(-5 -8,-10 -8,-15 -4))")
g6=readWKT("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2)),((6 3,9 2,9 4,6 3)))")
try(readWKT("POINT EMPTY"))
#> Error : Unable to parse: POINT EMPTY
#> GEOS reported: "rgeos_geospoint2SpatialPoints: empty point found"
try(readWKT("MULTIPOLYGON EMPTY"))
#> Error : Unable to parse: MULTIPOLYGON EMPTY
#> GEOS reported: "rgeos_geospolygon2Polygons: empty Polygons object"
g9=readWKT("GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))")

writeWKT(g1)
#> [1] "POINT (6.0000000000000000 10.0000000000000000)"
writeWKT(g2)
#> [1] "LINESTRING (3.0000000000000000 4.0000000000000000, 10.0000000000000000 50.0000000000000000, 20.0000000000000000 25.0000000000000000)"
writeWKT(g3)
#> [1] "POLYGON ((1.0000000000000000 1.0000000000000000, 1.0000000000000000 5.0000000000000000, 5.0000000000000000 5.0000000000000000, 5.0000000000000000 1.0000000000000000, 1.0000000000000000 1.0000000000000000), (2.0000000000000000 2.0000000000000000, 3.0000000000000000 2.0000000000000000, 3.0000000000000000 3.0000000000000000, 2.0000000000000000 3.0000000000000000, 2.0000000000000000 2.0000000000000000))"
writeWKT(g4)
#> [1] "MULTIPOINT (3.5000000000000000 5.5999999999999996, 4.7999999999999998 10.5000000000000000)"
writeWKT(g5)
#> [1] "MULTILINESTRING ((3.0000000000000000 4.0000000000000000, 10.0000000000000000 50.0000000000000000, 20.0000000000000000 25.0000000000000000), (-5.0000000000000000 -8.0000000000000000, -10.0000000000000000 -8.0000000000000000, -15.0000000000000000 -4.0000000000000000))"
writeWKT(g6)
#> [1] "MULTIPOLYGON (((1.0000000000000000 1.0000000000000000, 1.0000000000000000 5.0000000000000000, 5.0000000000000000 5.0000000000000000, 5.0000000000000000 1.0000000000000000, 1.0000000000000000 1.0000000000000000), (2.0000000000000000 2.0000000000000000, 3.0000000000000000 2.0000000000000000, 3.0000000000000000 3.0000000000000000, 2.0000000000000000 3.0000000000000000, 2.0000000000000000 2.0000000000000000)), ((6.0000000000000000 3.0000000000000000, 9.0000000000000000 4.0000000000000000, 9.0000000000000000 2.0000000000000000, 6.0000000000000000 3.0000000000000000)))"
writeWKT(g9,byid=FALSE)
#> [1] "GEOMETRYCOLLECTION (POINT (4.0000000000000000 6.0000000000000000), LINESTRING (4.0000000000000000 6.0000000000000000, 7.0000000000000000 10.0000000000000000))"
writeWKT(g9,byid=TRUE)
#> [1] "POINT (4.0000000000000000 6.0000000000000000)"                                             
#> [2] "LINESTRING (4.0000000000000000 6.0000000000000000, 7.0000000000000000 10.0000000000000000)"