I'd like to convert a shapefile (sel_stands_CMPC.shp
) to geoJSON and after to data.frame, but the the list
to data.frame
I have the error of differing number of rows. I need geoJSON and dataframe
data, then is not the case of shapefile to dataframe
simple conversion. I try to do:
library(sf)
library(geojsonsf)
library(plyr)
library(geojsonR)
# get AOI in shapefile
download.file(
"https://github.com/Leprechault/trash/raw/main/sel_stands_CMPC.zip",
zip_path <- tempfile(fileext = ".zip")
)
unzip(zip_path, exdir = tempdir())
# Open the file
setwd(tempdir())
stands_sel <- st_read("sel_stands_CMPC.shp")
# Reading layer `sel_stands_CMPC' from data source `C:\Users\fores\AppData\Local\Temp\Rtmp6l1bGx\sel_stands_CMPC.shp' using driver `ESRI Shapefile'
# Simple feature collection with 2 features and 16 fields
# Geometry type: MULTIPOLYGON
# Dimension: XY
# Bounding box: xmin: -52.3284 ymin: -30.43305 xmax: -51.21323 ymax: -30.35118
# Geodetic CRS: GCS_unknown
# Open as geoJSON
geo <- sf_geojson(stands_sel)
# Convert to data frame for *CSV export
geo_js_df <- plyr::ldply(FROM_GeoJson(geo), data.frame)
# Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, :
# arguments imply differing number of rows: 144, 62, 29
CodePudding user response:
Not sure if this is what you are looking for, but here is a suggestion:
REPREX
# Convert 'geo" into 'wkt' and then 'data.frame'
geo_js_df <- as.data.frame(geojson_wkt(geo))
And the output looks like this:
# Output structure
str(geo_js_df)
#> 'data.frame': 2 obs. of 17 variables:
#> $ SISTEMA_PR: chr "MACRO ESTACA - EUCALIPTO" "SEMENTE - EUCALIPTO"
#> $ ESPECIE : chr "SALIGNA" "DUNNI"
#> $ ID_UNIQUE : chr "BARBANEGRA159A" "CAMPOSECO016A"
#> $ CICLO : num 2 1
#> $ LOCALIDADE: chr "BARRA DO RIBEIRO" "DOM FELICIANO"
#> $ ROTACAO : num 1 1
#> $ CARACTER_1: chr "Produtivo" "Produtivo"
#> $ VLR_AREA : num 8.53 28.07
#> $ ID_REGIAO : num 11 11
#> $ CD_USO_SOL: num 2433 9053
#> $ DATA_PLANT: chr "2008/04/15" "2010/04/15"
#> $ ID_PROJETO: chr "002" "344"
#> $ CARACTERIS: chr "Plantio Comercial" "Plantio Comercial"
#> $ PROJETO : chr "BARBA NEGRA" "CAMPO SECO"
#> $ ESPACAMENT: chr "3.00 x 2.50" "3.5 x 2.14"
#> $ CD_TALHAO : chr "159A" "016A"
#> $ geometry :List of 2
#> ..$ : 'wkt' chr "MULTIPOLYGON (((-51.2142 -30.3517,-51.2143 -30.3518,-51.2143 -30.3518,-51.2143 -30.3519,-51.2143 -30.3519,-51.2"| __truncated__
#> ..$ : 'wkt' chr "MULTIPOLYGON (((-52.3214 -30.4271,-52.3214 -30.4272,-52.3214 -30.4272,-52.3215 -30.4272,-52.3215 -30.4272,-52.3"| __truncated__
#> - attr(*, "wkt_column")= chr "geometry"
Created on 2021-10-09 by the reprex package (v2.0.1)
CodePudding user response:
The same can be achieved with:
geo_df2 <-as.data.frame( sf::st_read(geo, quiet=TRUE))
str(geo_df2)
'data.frame': 2 obs. of 17 variables:
$ CD_USO_SOL: num 2433 9053
$ ID_REGIAO : num 11 11
$ ID_PROJETO: chr "002" "344"
$ PROJETO : chr "BARBA NEGRA" "CAMPO SECO"
$ CD_TALHAO : chr "159A" "016A"
$ CARACTERIS: chr "Plantio Comercial" "Plantio Comercial"
$ CARACTER_1: chr "Produtivo" "Produtivo"
$ CICLO : int 2 1
$ ROTACAO : int 1 1
$ DATA_PLANT: Date, format: "2008-04-15" "2010-04-15"
$ LOCALIDADE: chr "BARRA DO RIBEIRO" "DOM FELICIANO"
$ ESPACAMENT: chr "3.00 x 2.50" "3.5 x 2.14"
$ ESPECIE : chr "SALIGNA" "DUNNI"
$ SISTEMA_PR: chr "MACRO ESTACA - EUCALIPTO" "SEMENTE - EUCALIPTO"
$ VLR_AREA : num 8.53 28.07
$ ID_UNIQUE : chr "BARBANEGRA159A" "CAMPOSECO016A"
$ geometry :sfc_MULTIPOLYGON of length 2; first list element: List of 3
..$ :List of 1
.. ..$ : num [1:144, 1:2] -51.2 -51.2 -51.2 -51.2 -51.2 ...
..$ :List of 1
.. ..$ : num [1:62, 1:2] -51.2 -51.2 -51.2 -51.2 -51.2 ...
..$ :List of 1
.. ..$ : num [1:29, 1:2] -51.2 -51.2 -51.2 -51.2 -51.2 ...
..- attr(*, "class")= chr [1:3] "XY" "MULTIPOLYGON" "sfg"
- attr(*, "sf_column")= chr "geometry"
- attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
..- attr(*, "names")= chr [1:16] "CD_USO_SOL" "ID_REGIAO" "ID_PROJETO" "PROJETO" ...
write.csv(geo_df2, 'Barra_do_Ribeiro_Dom_Feliciano.csv')
depending on your further use. And I got all the errors you did and then some more.