[GRASS-stats] rgrass7 read/write SpatialPolygonsDataFrames errors

Roger Bivand Roger.Bivand at nhh.no
Thu Oct 8 14:57:40 PDT 2015


Sorry this has taken a long time; please try installing the development version available from:

http://win-builder.r-project.org/8Y3Ihj451Clx

(rgrass7_0.1-3.zip). The problem was the use of "_" instead of " " in the name of the OGR shapefile driver - in OGR and R it is "ESRI Shapefile", in GRASS it is "ESRI_Shapefile". Because the new long-name work-around worked on systems not needing to use this driver, it wasn't properly tested - and on Windows the GRASS standalone has OGR with the SQLite driver, but the Windows rgdal binary does not have SQLite. You may wish to install the R RSQLite package to move data if you use long field/column names.

Before installing this development version, and after library(rgrass7), run example(readVECT) in the basic nc location from:

https://grass.osgeo.org/sampledata/north_carolina/nc_basic_spm_grass7.zip

it should fail. Then restart R, install the development version of rgrass7 from the first link, and repeat - it works for me and should resolve the problem.

Hope this helps,

Roger

 
--
Roger Bivand
Norwegian School of Economics
Helleveien 30, 5045 Bergen, Norway
Roger.Bivand at nhh.no





Fra: grass-stats-bounces at lists.osgeo.org [grass-stats-bounces at lists.osgeo.org] på vegne av Eduardo Diez [eduardodiez at gmx.com]
Sendt: 24. september 2015 9:11 
Til: grass-stats at lists.osgeo.org
Emne: [GRASS-stats] rgrass7 read/write SpatialPolygonsDataFrames errors


Hi, I'm a user of R mostly for spatial information processing. Besides sp, maptools, etc. i also use functions from GRASS, at first through spgrass6 and then rgrass7. A few days ago, I started having trouble reading and writing SpatialPolygonsDataFrames using readVECT and writeVECT. I tried both reinstalling both grass (6.4.4, 7.0.0 and 7.0.1) and the R packages (spgrass6 0.8-6 and rgrass7 0.1-2), and also with different R objects but i kept getting the same errors.


The "mult.r" object is a RasterLayer with 4 zones and the objective is to convert it to polygons and "clean" small areas with v.clean rmarea (as i don't currently know a way of doing this in plain R).



The first thing that came out strange was that when executing writeVECT it would ask me for the driver to use (in the way of rgdal's writeOGR, it didn't ask for it before). I figured out that object to be written should be projected (not in lat-lon, don't know why). After doing this i could write the vector and execute v.clean. The problem came again when trying to read the cleaned polygons back to R.
Below i paste an extract of my R console with the errors and some information.



Thanks in advance,

Eduardo


> mult.rp <- projectRaster(mult.r, crs = prj.crs, method = "ngb")
> mult.pol <- rasterToPolygons(mult.rp, dissolve = T)

> initGRASS(gisBase = "c:/Program Files (x86)/GRASS GIS 7.0.1", home = tempdir(), override = T)
gisdbase    C:/Users/ediez1/AppData/Local/Temp/RtmpegbyiQ 
location    file2f7c6dc070e6 
mapset      file2f7c30eb3f52 
rows        1 
columns     1 
north       1 
south       0 
west        0 
east        1 
nsres       1 
ewres       1 
projection  NA 
> writeVECT(mult.pol, "pol1", v.in.ogr_flags = "o")
Error: !is.null(driver) is not TRUE
> writeVECT(mult.pol, "pol1", v.in.ogr_flags = "o", driver = "ESRI Shapefile")

Over-riding projection check
Check if OGR layer <r848659> contains polygons...


Importing 4 features (OGR layer <r848659>)...


-----------------------------------------------------
Registering primitives...
3005 primitives registered
15587 vertices registered
Number of nodes: 2691
Number of primitives: 3005
Number of points: 0
Number of lines: 0
Number of boundaries: 3005
Number of centroids: 0
Number of areas: -
Number of isles: -
-----------------------------------------------------
Cleaning polygons
-----------------------------------------------------
Breaking polygons...
Breaking polygons (pass 1: select break points)...


Breaking polygons (pass 2: break at selected points)...


-----------------------------------------------------
Removing duplicates...


-----------------------------------------------------
Breaking boundaries...


-----------------------------------------------------
Removing duplicates...


-----------------------------------------------------
Cleaning boundaries at nodes...


-----------------------------------------------------
Merging boundaries...


-----------------------------------------------------
Removing dangles...


-----------------------------------------------------
Building areas...


95 areas built
8 isles built
Number of nodes: 2771
Number of primitives: 7307
Number of points: 0
Number of lines: 0
Number of boundaries: 7307
Number of centroids: 0
Number of areas: 95
Number of isles: 8
-----------------------------------------------------
Removing bridges...


-----------------------------------------------------
Registering primitives...
231 primitives registered
7594 vertices registered
Building areas...


95 areas built
8 isles built
Attaching islands...


Number of nodes: 144
Number of primitives: 231
Number of points: 0
Number of lines: 0
Number of boundaries: 231
Number of centroids: 0
Number of areas: 95
Number of isles: 8
-----------------------------------------------------
Finding centroids for OGR layer <r848659>...


-----------------------------------------------------
Writing centroids...


-----------------------------------------------------
95 input polygons
Total area: 1.28981E+006 (95 areas)
-----------------------------------------------------
Copying features...


WARNING: Unable to delete file
         'C:/Users/ediez1/AppData/Local/Temp/Rtmpg10lvi/filec2473b0126/filec24129b63ac/.tmp/unknown/vector/tmp_10484/coor'
Building topology for vector map <r848659 at filec24129b63ac>...
Registering primitives...
326 primitives registered
7689 vertices registered
Building areas...


95 areas built
8 isles built
Attaching islands...


Attaching centroids...


Number of nodes: 144
Number of primitives: 326
Number of points: 0
Number of lines: 0
Number of boundaries: 231
Number of centroids: 95
Number of areas: 95
Number of isles: 8


> execGRASS("v.clean", flags = c("overwrite"), input = "pol1",
+ output = "clnpol", tool = "rmarea", threshold = 3000)
--------------------------------------------------
Tool: Threshold
Remove small areas: 3000
--------------------------------------------------
Copying features...


Rebuilding parts of topology...
Building topology for vector map <k1f9f75 at filec24129b63ac>...
Registering primitives...
326 primitives registered
7689 vertices registered
Building areas...


95 areas built
8 isles built
Attaching islands...


Attaching centroids...


Number of nodes: 144
Number of primitives: 326
Number of points: 0
Number of lines: 0
Number of boundaries: 231
Number of centroids: 95
Number of areas: 95
Number of isles: 8
--------------------------------------------------
Tool: Remove small areas


55 areas of total size 29487.1 removed
--------------------------------------------------
Rebuilding topology for output vector map...
Building topology for vector map <k1f9f75 at filec24129b63ac>...
Registering primitives...
212 primitives registered
6840 vertices registered
Building areas...


40 areas built
4 isles built
Attaching islands...


Attaching centroids...


Number of nodes: 136
Number of primitives: 212
Number of points: 0
Number of lines: 0
Number of boundaries: 172
Number of centroids: 40
Number of areas: 40
Number of isles: 4
> cln.pol <- readVECT("clnpol")
Error: !is.null(driver) is not TRUE
> cln.pol <- readVECT("clnpol", driver = "ESRI Shapefile")
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  : 
  line 4 did not have 2 elements


> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1


locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    


attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     


other attached packages:
[1] rgrass7_0.1-2   classInt_0.1-22 maptools_0.8-36 ggplot2_1.0.1   XML_3.98-1.3    raster_2.4-20   sp_1.2-0       


loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1      magrittr_1.5     MASS_7.3-43      munsell_0.4.2    colorspace_1.2-6 lattice_0.20-33  stringr_1.0.0    plyr_1.8.3      
 [9] tools_3.2.2      parallel_3.2.2   rgdal_1.0-7      grid_3.2.2       gtable_0.1.2     e1071_1.6-7      class_7.3-13     rgeos_0.3-12    
[17] digest_0.6.8     reshape2_1.4.1   labeling_0.3     stringi_0.5-5    scales_0.3.0     foreign_0.8-65   proto_0.3-10    


More information about the grass-stats mailing list