Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
added wkb wkt converter using a node module, fix #5
Browse files Browse the repository at this point in the history
  • Loading branch information
sckott committed Dec 3, 2015
1 parent ff12433 commit a8058a0
Show file tree
Hide file tree
Showing 29 changed files with 5,803 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.Rhistory
.RData
.DS_Store
.V8history
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -2,6 +2,9 @@ language: r

sudo: required

before_install:
- sudo apt-get install libv8-dev

r_binary_install:
- leaflet

Expand Down
6 changes: 4 additions & 2 deletions DESCRIPTION
Expand Up @@ -4,7 +4,7 @@ Description: Convert 'WKT' to 'GeoJSON' and 'GeoJSON' to 'WKT'. Functions
included for converting between 'GeoJSON' to 'WKT', creating both
'GeoJSON' features, and non-features, creating WKT from R objects
(e.g., lists, data.frames, vectors), and linting 'WKT'.
Version: 0.1.0.9000
Version: 0.1.1.9000
Authors@R: person("Scott", "Chamberlain", role = c("aut", "cre"),
email = "myrmecocystus@gmail.com")
License: MIT + file LICENSE
Expand All @@ -16,8 +16,10 @@ Imports:
methods,
utils,
jsonlite (>= 0.9.17),
magrittr
magrittr,
V8 (>= 0.9)
Suggests:
knitr,
leaflet (>= 1.0.0),
testthat
RoxygenNote: 5.0.1
5 changes: 4 additions & 1 deletion NAMESPACE
@@ -1,4 +1,4 @@
# Generated by roxygen2 (4.1.1): do not edit by hand
# Generated by roxygen2: do not edit by hand

S3method(as_json,geojson)
S3method(circularstring,character)
Expand Down Expand Up @@ -52,8 +52,11 @@ export(multipolygon)
export(point)
export(polygon)
export(properties)
export(wkb_wkt)
export(wkt2geojson)
export(wkt_wkb)
export(wktview)
importFrom(V8,new_context)
importFrom(jsonlite,toJSON)
importFrom(magrittr,"%>%")
importFrom(methods,is)
Expand Down
7 changes: 7 additions & 0 deletions R/onLoad.R
@@ -0,0 +1,7 @@
#' @importFrom V8 new_context
sh <- NULL
.onLoad <- function(libname, pkgname){
sh <<- new_context();
sh$source(system.file("js/wkx.js", package = pkgname))
sh$source(system.file("js/buffer.js", package = pkgname))
}
52 changes: 52 additions & 0 deletions R/wkb.R
@@ -0,0 +1,52 @@
#' Convert WKT to WKB
#'
#' @export
#' @name wkb
#' @param x A \code{character} string representing a WKT object, or an object
#' of class \code{raw}, representing a WKB object
#' @return \code{wkt_wkb} returns an object of class \code{raw}, a WKB reprsentation.
#' \code{wkb_wkt} returns an object of class \code{character}, a WKT representation
#' @examples
#' # WKT to WKB
#' ## point
#' wkt_wkb("POINT (-116.4 45.2)")
#'
#' ## linestring
#' wkt_wkb("LINESTRING (-116.4 45.2, -118.0 47.0)")
#'
#' ## multipoint
#' ### only accepts the below format, not e.g., ((1 2), (3 4))
#' wkt_wkb("MULTIPOINT (100.000 3.101, 101.00 2.10, 3.14 2.18)")
#'
#' ## polygon
#' wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))")
#'
#' # WKB to WKT
#' ## point
#' (x <- wkt_wkb("POINT (-116.4 45.2)"))
#' wkb_wkt(x)
#'
#' ## linestring
#' (x <- wkt_wkb("LINESTRING (-116.4 45.2, -118.0 47.0)"))
#' wkb_wkt(x)
#'
#' ## multipoint
#' (x <- wkt_wkb("MULTIPOINT (100.000 3.101, 101.00 2.10, 3.14 2.18)"))
#' wkb_wkt(x)
#'
#' ## polygon
#' (x <- wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))"))
#' wkb_wkt(x)

wkt_wkb <- function(x) {
sh$eval(sprintf("var tt = wkx.Geometry.parse('%s').toWkb();", x))
as.raw(sh$get("tt")$data)
}

#' @export
#' @rdname wkb
wkb_wkt <- function(x) {
sh$eval(sprintf("var w = new buffer('%s', 'hex')", paste0(x, collapse = "")))
sh$eval("var tt = wkx.Geometry.parse(w).toWkt();")
sh$get("tt")
}
31 changes: 29 additions & 2 deletions README.Rmd
Expand Up @@ -57,8 +57,7 @@ There's a family of functions that make it easy to go from familiar R objects li
* `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).
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

Expand All @@ -80,6 +79,10 @@ case of `EMPTY` WKT objects).

`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

```{r eval=FALSE}
Expand Down Expand Up @@ -251,6 +254,30 @@ lint("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, a b, 10 20, 5 10, 15
#> [1] FALSE
```

## WKT <--> WKB

WKT to WKB

```{r}
## point
wkt_wkb("POINT (-116.4 45.2)")
## polygon
wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))")
```

WKB to WKT

```{r}
## point
(x <- wkt_wkb("POINT (-116.4 45.2)"))
wkb_wkt(x)
## polygon
(x <- wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))"))
wkb_wkt(x)
```

## Meta

* Please [report any issues or bugs](https://github.com/ropensci/wellknown/issues).
Expand Down
45 changes: 43 additions & 2 deletions README.md
Expand Up @@ -25,8 +25,7 @@ There's a family of functions that make it easy to go from familiar R objects li
* `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).
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

Expand All @@ -48,6 +47,10 @@ case of `EMPTY` WKT objects).

`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


Expand Down Expand Up @@ -311,6 +314,44 @@ lint("MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)), ((15 5, a b, 10 20, 5 10, 15
#> [1] FALSE
```

## WKT <--> WKB

WKT to WKB


```r
## point
wkt_wkb("POINT (-116.4 45.2)")
#> [1] 01 01 00 00 00 9a 99 99 99 99 19 5d c0 9a 99 99 99 99 99 46 40

## polygon
wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))")
#> [1] 01 03 00 00 00 01 00 00 00 04 00 00 00 00 00 00 00 00 00 59 40 00 00
#> [24] 00 00 00 00 00 00 66 66 66 66 66 46 59 40 00 00 00 00 00 00 00 00 00
#> [47] 00 00 00 00 40 59 40 00 00 00 00 00 00 f0 3f 00 00 00 00 00 00 59 40
#> [70] 00 00 00 00 00 00 00 00
```

WKB to WKT


```r
## point
(x <- wkt_wkb("POINT (-116.4 45.2)"))
#> [1] 01 01 00 00 00 9a 99 99 99 99 19 5d c0 9a 99 99 99 99 99 46 40
wkb_wkt(x)
#> [1] "POINT(-116.4 45.2)"

## polygon
(x <- wkt_wkb("POLYGON ((100.0 0.0, 101.1 0.0, 101.0 1.0, 100.0 0.0))"))
#> [1] 01 03 00 00 00 01 00 00 00 04 00 00 00 00 00 00 00 00 00 59 40 00 00
#> [24] 00 00 00 00 00 00 66 66 66 66 66 46 59 40 00 00 00 00 00 00 00 00 00
#> [47] 00 00 00 00 40 59 40 00 00 00 00 00 00 f0 3f 00 00 00 00 00 00 59 40
#> [70] 00 00 00 00 00 00 00 00
wkb_wkt(x)
#> [1] "POLYGON((100 0,101.1 0,101 1,100 0))"
```

## Meta

* Please [report any issues or bugs](https://github.com/ropensci/wellknown/issues).
Expand Down

0 comments on commit a8058a0

Please sign in to comment.