title: Length of polygon in R
body: <p>I want to calculate length of each polygon.
  -Around each polygon I created points (st_sample),
  -from combiantion of points I created all possible polyline,
  -for polylines which are inside polygon I calucalted length,
  -the longest polyline is my result (max length of poylgon).</p>
  <p><a href="https://i.stack.imgur.com/nhSHO.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nhSHO.jpg" alt="My problem of calculating length of polygon" /></a></p>
  <p>I wrote code which got me results but it is really slow. Do you have some solution for improvment of my code? I know that with two loops I cannot expect some miracle about speed but I do not know how get results another way.</p>
  <p>If nothing else mybe at least some alterntive solution for creating all polyline from combination of points for one polygon in one step without loop ? :)</p>
  <p>thank you</p>
  <pre><code>library(sf)
  library(data.table)
  
  poly=st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))
  poly=poly[1:10,]
  poly=st_cast(poly,&quot;POLYGON&quot;)
  poly$max_length=0
  
  ##Combination of 10 points, withot repetiton
  aa=CJ(1:10,1:10)
  aa=aa[!duplicated(t(apply(aa[,.(V1, V2)], 1, sort))),][V1!=V2]
  
  ##for each polygon create sample of coordinates along line, from them I create polyline and calculated length for linestring which are inside polygon
  
  for (ii in 1:nrow(poly)){
    ncl=st_cast(poly[ii,],&quot;LINESTRING&quot;)
    ##sample of point along line
    ncp=st_cast(st_sample(ncl,10, type=&quot;regular&quot;, exact=T),&quot;POINT&quot;)
    
    ##create empty sf 
    aaa=st_sf(st_sfc())
    st_crs(aaa)=&quot;NAD27&quot;
    
    ##for each combination of points create linestring and calculate length only for polylines which are inside polygon
    for (i in 1:nrow(aa)){
   aaa=rbind(aaa,st_sf(geometry=st_cast(st_union(ncp[t(aa[i])]),&quot;LINESTRING&quot;)))
    }
  poly$max_length[ii]=as.numeric(max(st_length(aaa[unlist(st_contains(poly[ii,],aaa)),])))
  }
  </code></pre>
  <p>Second attempt with running function inside data.table. One loop less but problem is probably second loop.</p>
  <pre><code>poly=st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))
  poly=poly[1:10,]
  poly=st_cast(poly,&quot;POLYGON&quot;)
  poly$max_length=0
  
  ##Combination of 10 points, withot repetiton
  aa=CJ(1:10,1:10)
  aa=aa[!duplicated(t(apply(aa[,.(V1, V2)], 1, sort))),][V1!=V2]
  
  overFun &lt;- function(x){
    
    ncl=st_cast(x[,geometry],&quot;LINESTRING&quot;)
    ##sample of point along line
    ncp=st_cast(st_sample(ncl,40, type=&quot;regular&quot;, exact=T),&quot;POINT&quot;)
    
    ##create empty sf 
    aaa=st_sf(st_sfc())
    st_crs(aaa)=&quot;NAD27&quot;
    
    ##for each combination pof points create linestring and calculate length
    for (i in 1:nrow(aa)){
      aaa=rbind(aaa,st_sf(geometry=st_cast(st_union(ncp[t(aa[i])]),&quot;LINESTRING&quot;)))
    }
    
    as.numeric(max(st_length(aaa[unlist(st_contains(x[,geometry],aaa)),])))}  
  
  setDT(poly)
  
  ##run function inside data.table
  poly[,max_length:=overFun(poly), by=seq(nrow(poly))]
  
  </code></pre>
  <p>Edit: I found some solution for my problem which is enough fast for my needs.
  Using parallel library inside data.table with function which also work on a data.table. There is still question why some polyline are excluded with function st_contains (see picture upper). Maybe some problem with precision?</p>
  <pre><code>library(sf)
  library(data.table)
  
  poly=st_read(system.file(&quot;shape/nc.shp&quot;, package=&quot;sf&quot;))
  poly=st_cast(poly,&quot;POLYGON&quot;)
  setDT(poly)
  
  ##Combination of 10 points, withot repetiton
  aa=CJ(1:10,1:10)
  aa=aa[!duplicated(t(apply(aa[,.(V1, V2)], 1, sort))),][V1!=V2]
  
  overFun &lt;- function(x){
    
  ncl=st_cast(poly[1,geometry],&quot;LINESTRING&quot;)
  ##sample of point along line
  ncp=st_cast(st_sample(ncl,10, type=&quot;regular&quot;, exact=T),&quot;POINT&quot;)
    
  df=data.table(ncp[aa[,V1]],ncp[aa[,V2]] )
    
  df[,v3:=st_cast(st_union(st_as_sf(V1),st_as_sf(V2)),&quot;LINESTRING&quot;), by=seq(nrow(df))]
  as.numeric(max(st_length(df[unlist(st_contains(poly[1,geometry], df$v3)),]$v3)))}
  
  library(parallel)
  cl &lt;- makeCluster(detectCores() - 1)
  clusterExport(cl, list(&quot;overFun&quot;,&quot;data.table&quot;,&quot;st_cast&quot;,&quot;CJ&quot;,&quot;poly&quot;,&quot;st_sample&quot;,&quot;st_sf&quot;,&quot;st_sfc&quot;,&quot;aa&quot;,&quot;st_length&quot;,&quot;st_union&quot;,
                         &quot;st_as_sf&quot;,&quot;st_contains&quot;))
  system.time(poly[,c(&quot;max_length&quot;):=.(clusterMap(cl, overFun, poly$geometry)),])
  stopCluster(cl)
  
  </code></pre>