0

Not sure how to do this:

I have a something stored like this (in a):

[
  "MULTIPOLYGON (((-95.0000000372529 40.999989970587194, -94.5000000372529 40.999989970587194, -94.5000000372529 40.499989970587194, -95.0000000372529 40.499989970587194)))"
]

typeof(a) # prints "character"
class(a)  # prints "json"

And I would like to convert it to a data.frame that looks like this (no need to round the numbers, it was just easier to type that)

  X1  X2
1 -95 40
2 -94 40
3 -94 40
4 -95 40

I tried

a.df<-as.data.frame(as.matrix(a))

but that prints

 V1
1 [\n  "MULTIPOLYGON (((-95.0000000372529 40.999989970587194, -94.5000000372529 40.999989970587194, -94.5000000372529 40.499989970587194, -95.0000000372529 40.499989970587194)))"\n]\n
1
  • That doesn't look like valid JSON at all. Are you sure there isn't some other name for this file format? Commented Apr 6, 2017 at 15:17

1 Answer 1

1
library(sf)
library(jsonlite)
library(tidyverse)

x <- fromJSON('[
  "MULTIPOLYGON (((-95.0000000372529 40.999989970587194, -94.5000000372529 40.999989970587194, -94.5000000372529 40.499989970587194, -95.0000000372529 40.499989970587194)))"
]')

map(x, st_as_sfc) %>%
  map(~as.data.frame(.[[1]][[1]]))
## [[1]]
##      X1       X2
## 1 -95.0 40.99999
## 2 -94.5 40.99999
## 3 -94.5 40.49999
## 4 -95.0 40.49999

Now:

wkt2geojson('[
  "MULTIPOLYGON (((-95.0000000372529 40.999989970587194, -94.5000000372529 40.999989970587194, -94.5000000372529 40.499989970587194, -95.0000000372529 40.499989970587194)))"
]')

also kinda works but tosses warnings.

Knowing the actual source that created this WKT in such a configuration wld be helpful

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.