title: st_union (sf) polygons with data.table
body: <p>In near past I used a library sf with library data.table without special problems. But now I tried one of my code which finished with error. When I merge polygons by id within data.table the results I cannot ploted or write to shp. Workaround is transformation to sp and back to sf but with a lot polygons is this quite time consuming. Small example:</p>
  <pre><code>library(data.table)
  library(sf)
  
  a=structure(list(Id = c(1L, 2L, 3L, 3L, 1L, 1L, 2L), geometry = structure(list(
    structure(list(structure(c(455311.710673953, 455314.345317508, 
                               455315.716716433, 455316.87150159, 455312.03568616, 455311.710673953, 
                               376691.889842887, 376694.12745275, 376692.828258414, 376689.327648062, 
                               376689.255443473, 376691.889842887), .Dim = c(6L, 2L))), class = c(&quot;XY&quot;, 
                                                                                                  &quot;POLYGON&quot;, &quot;sfg&quot;)), structure(list(structure(c(455316.87150159, 
                                                                                                                                                 455315.716716433, 455319.072917605, 455321.382671023, 455316.87150159, 
                                                                                                                                                 376689.327648062, 376692.828258414, 376693.694408316, 376690.518443961, 
                                                                                                                                                 376689.327648062), .Dim = c(5L, 2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, 
                                                                                                                                                                                                  &quot;sfg&quot;)), structure(list(structure(c(455312.93790784, 455316.87150159, 
                                                                                                                                                                                                                                      455317.485088015, 455313.370891238, 455312.93790784, 376686.224010367, 
                                                                                                                                                                                                                                      376685.646434684, 376682.57880773, 376682.542858023, 376686.224010367
                                                                                                                                                                                                  ), .Dim = c(5L, 2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;)), 
    structure(list(structure(c(455318.062480594, 455322.068278933, 
                               455321.815715457, 455314.634074832, 455318.062480594, 376684.52766027, 
                               376682.903819937, 376679.980419059, 376681.027049918, 376684.52766027
    ), .Dim = c(5L, 2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;)), 
    structure(list(structure(c(455321.346477176, 455323.548076297, 
                               455324.414104129, 455322.104472781, 455321.346477176, 376688.028453727, 
                               376688.569652457, 376686.404430289, 376685.466014762, 376688.028453727
    ), .Dim = c(5L, 2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;)), 
    structure(list(structure(c(455319.302470828, 455321.743510867, 
                               455323.90891614, 455321.382671023, 455319.302470828, 376693.379039664, 
                               376694.27186193, 376691.456859488, 376690.518443961, 376693.379039664
    ), .Dim = c(5L, 2L))), class = c(&quot;XY&quot;, &quot;POLYGON&quot;, &quot;sfg&quot;)), 
    structure(list(structure(c(455315.538127566, 455316.87150159, 
                               455318.105693484, 455318.856486941, 455315.35587659, 455315.538127566, 
                               376689.307811637, 376689.327648062, 376689.653453727, 376687.667430777, 
                               376687.667430777, 376689.307811637), .Dim = c(6L, 2L))), class = c(&quot;XY&quot;, 
                                                                                                  &quot;POLYGON&quot;, &quot;sfg&quot;))), n_empty = 0L, crs = structure(list(input = NA_character_, 
                                                                                                                                                          wkt = NA_character_), class = &quot;crs&quot;), class = c(&quot;sfc_POLYGON&quot;, 
                                                                                                                                                                                                          &quot;sfc&quot;), precision = 0, bbox = structure(c(xmin = 455311.710673953, 
                                                                                                                                                                                                                                                    ymin = 376679.980419059, xmax = 455324.414104129, ymax = 376694.27186193
                                                                                                                                                                                                          ), class = &quot;bbox&quot;))), row.names = c(NA, -7L), class = c(&quot;sf&quot;, 
                                                                                                                                                                                                                                                                  &quot;data.frame&quot;), sf_column = &quot;geometry&quot;, agr = structure(c(Id = NA_integer_), class = &quot;factor&quot;, .Label = c(&quot;constant&quot;, 
                                                                                                                                                                                                                                                                                                                                                                           &quot;aggregate&quot;, &quot;identity&quot;)))
  
  
  setDT(a)
  
  
  ################original data
  #############################
  
  #plot work ok
  plot(a$geometry)
  
  ##write to shp work ok
  st_write(a,&quot;ab.shp&quot;, delete_layer=T)
  
  
  ################union polygons by id
  ####################################
  aa&lt;-a[,.(geometry=st_union(geometry)), by=Id] %&gt;% sf::st_as_sf()
  
  ##plot error:  &quot;Error in CPL_geos_is_empty(st_geometry(x)) : Not a matrix.&quot;
  plot(aa$geometry)
  
  ## write error: &quot;Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  :Not a matrix.&quot;
  st_write(aa,&quot;abc.shp&quot;, delete_layer=T)
  
  
  #####################transform from data.table to sf
  ####################################################
  aa=st_as_sf(aa)
  
  ##plot error  &quot;Error in CPL_geos_is_empty(st_geometry(x)) : Not a matrix.&quot;
  plot(aa)
  
  ## write to shp with error: &quot;Error in CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  :Not a matrix.&quot;
  st_write(aa,&quot;abc.shp&quot;, delete_layer=T)
  
  
  ############################transform sf to sp and back
  #######################################################
  aa=st_as_sf(as(st_as_sf(aa),&quot;Spatial&quot;))
  
  ##plot work ok
  plot(aa)
  
  ##write to shp work ok
  st_write(a,&quot;ab.shp&quot;, delete_layer=T)
  </code></pre>
  <p>EDIT:</p>
  <p>I found part solution base by <a href="https://github.com/Rdatatable/data.table/issues/4681" rel="nofollow noreferrer">https://github.com/Rdatatable/data.table/issues/4681</a> . If after st_union  I add line with code like aa=aa[1:nrow(aa),] ploting and writing working as expected.</p>
  <pre><code>setDT(a)
  aa&lt;-a[,.(geometry=st_union(geometry)), by=Id] %&gt;% sf::st_as_sf()
  aa=aa[1:nrow(aa),]
  plot(aa)
  st_write(aa,&quot;aa.shp&quot;)
  </code></pre>