wellknown - convert WKT to GeoJSON and vice versa.

Inspiration partly comes from Python’s geomet/geomet - and the name from Javascript’s wellknown (it’s a good name).

Different interfaces

WKT from R stuctures

There’s a family of functions that make it easy to go from familiar R objects like lists and data.frames to WKT, including:

  • point() - make a point, e.g., POINT (-116 45)
  • multipoint() - make a multipoint, e.g., MULTIPOINT ((100 3), (101 2))
  • linestring() - make a linestring, e.g., LINESTRING (100 0, 101 1)
  • polygon() - make a polygon, e.g., POLYGON ((100 0), (101 0), (101 1), (100 0))
  • multipolygon() - make a multipolygon, e.g., MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))

The above currently accept (depending on the fxn) numeric, list, and data.frame (and character for special case of EMPTY WKT objects).

Geojson to WKT and vice versa

geojson2wkt() and wkt2geojson() cover a subset of the various formats available:

  • Point
  • MultiPoint
  • Polygon
  • MultiPolygon
  • LineString
  • MultilineString
  • Geometrycollection

Geojson to WKT

geojson2wkt() converts any geojson as a list to a WKT string (the same format )

WKT to Geojson

wkt2geojson() converts any WKT string into geojson as a list. This list format for geojson can be used downstream e.g., in the leaflet package.

WKT to WKB, and vice versa

wkt_wkb() converts WKT to WKB, while wkb_wkt() converts WKB to WKT

Install

Stable version

install.packages("wellknown")

Dev version

install.packages("devtools")
devtools::install_github("ropensci/wellknown")
library("wellknown")

GeoJSON to WKT

Multipolygon

mpoly <- list(
  MultiPolygon = list(
    list(
      matrix(c(100, 101, 101, 100, 0.001, 0.001, 1.001, 0.001), ncol = 2),
      matrix(c(100.2, 100.8, 100.8, 100.2, 0.2, 0.2, 0.8, 0.2), ncol = 2)
    ),
    list(
      matrix(c(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 1.0), ncol = 3),
      matrix(c(9.0, 10.0, 11.0, 12.0, 1.0, 2.0, 3.0, 4.0, 9.0), ncol = 3)
    )
  )
)
geojson2wkt(mpoly, fmt=1)
#> [1] "MULTIPOLYGON Z(((100.000 0.001 0.000, 101.000 0.001 0.000, 101.000 1.001 0.000, 100.000 0.001 0.000), (100.2 0.2 0.0, 100.8 0.2 0.0, 100.8 0.8 0.0, 100.2 0.2 0.0)), ((1.0 4.0 7.0, 2.0 5.0 8.0, 3.0 6.0 1.0), (9.0 12.0 3.0, 10.0 1.0 4.0, 11.0 2.0 9.0)))"

GeometryCollection

gmcoll <- list(
 GeometryCollection = list(
   list(type = 'Point', coordinates = c(0.0, 1.0)),
   list(type = 'LineString', coordinates = matrix(c(0.0, 2.0, 4.0, 5.0,
                           0.0, 1.0, 2.0, 4.0),
                           ncol = 2)),
   list(type = 'Polygon', coordinates = list(
     matrix(c(100.001, 101.1, 101.001, 100.001, 0.001, 0.001, 1.001, 0.001),
       ncol = 2),
     matrix(c(100.201, 100.801, 100.801, 100.201, 0.201, 0.201, 0.801, 0.201),
       ncol = 2)
  ))
 )
)
geojson2wkt(gmcoll, fmt=0)
#> [1] "GEOMETRYCOLLECTION (POINT (0 1), LINESTRING (0 0, 2 1, 4 2, 5 4), POLYGON ((100.001 0.001, 101.100 0.001, 101.001 1.001, 100.001 0.001), (100.201 0.201, 100.801 0.201, 100.801 0.801, 100.201 0.201)))"

Convert json or character objects

You can convert directly from an object of class json, which is output from jsonlite::toJSON().

library("jsonlite")
(json <- toJSON(list(Point = c(-105, 39)), auto_unbox = TRUE))
#> {"Point":[-105,39]}

And you can convert from a geojson character string:

lint WKT

lint("POINT (1 2)")
#> [1] TRUE
lint("LINESTRING EMPTY")
#> [1] TRUE
lint("MULTIPOINT ((1 2), (3 4), (-10 100))")
#> [1] TRUE
lint("POLYGON((20.3 28.6, 20.3 19.6, 8.5 19.6, 8.5 28.6, 20.3 28.6))")
#> [1] TRUE
lint("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, 40 10, 10 20, 5 10, 15 5)))")
#> [1] TRUE
lint("POINT (1 2 3 4 5)")
#> [1] FALSE
lint("LINESTRING (100)")
#> [1] FALSE
lint("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, a b, 10 20, 5 10, 15 5)))")
#> [1] FALSE

Meta

ropensci_footer