Divide irregularly shaped polygon into 3 equal parts











up vote
0
down vote

favorite












I have a polygon object of a transect line that I would like to divide into 3 equal parts that are one-after-the-other along the transect line. So far I have only been able to slice the polygon vertically, which results in non-continuous parts.



Here is a reproducible example using Harvard Forest data, available through the following link



After you have downloaded and unzipped the data, you will see a folder called HARV. In this folder is the shapefile, HARV_roads.shp



Import HARV_roads.shp



lines_HARV <- readOGR("/Your file path/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")


Get footpath lines only



footpath_HARV <- lines_HARV[lines_HARV$TYPE == "footpath",]


Plot the footpath



plot(footpath_HARV,
lwd=6,
main="NEON Harvard Forest Field Siten Footpath")


footpath



Create a thicker polygon object from the footpath line using buffer, to simulate a transect line with X width



require(raster)
footpath_buffer <- buffer(footpath_HARV,width=40)


Plot footpath_buffer



plot(footpath_buffer)


buffer



Chop the buffer into 3 equal parts using Barry Rowlingson's code (below) from this post:



makeVchopper <- function(pol){
bb = bbox(pol)
delta = (bb[2,2] - bb[2,1])/10
xmin = bb[1,1]-delta
ymin = bb[2,1]-delta
ymax = bb[2,2]+delta

choppoly = function(xmax){
readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin,
xmin,ymin))
}
choppoly
}

slicer <- function(pol, xmin, xmax){
bb = bbox(pol)
delta = (bb[2,2] - bb[2,1])/10
ymax = bb[2,2] + delta
ymin = bb[2,1] - delta
r = readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin, xmin,ymin))
gIntersection(pol,r)
}

chop_thirds <- function(pol, fractions=c(1/3, 2/3)){
chopper = makeVchopper(pol)
bb = bbox(pol)
xmin = bb[1,1]
xmax = bb[1,2]

totalArea = gArea(pol)

chopped_area = function(x){
gArea(gIntersection(chopper(x),pol))
}

edges = lapply(fractions, function(fraction){
target = totalArea * fraction
target_function = function(x){
chopped_area(x) - target
}
uniroot(target_function, lower=xmin, upper=xmax)$root
})

xdelta = (xmax-xmin)/10
chops = matrix(c(xmin-xdelta, rep(edges,rep(2,length(edges))),
xmax+xdelta), ncol=2, byrow=TRUE)
apply(chops, 1, function(edges){
slicer(pol, edges[1], edges[2])
})

}


Divide footpath_buffer into 3 equal parts:



parts.footpath <- chop_thirds(footpath_buffer)


Plot footpath_buffer and the 3 equal parts



plot(footpath_buffer)
plot(parts.footpath[[1]], add=TRUE, col=1,border=F)
plot(parts.footpath[[2]], add=TRUE, col=2,border=F)
plot(parts.footpath[[3]], add=TRUE, col=3,border=F)


3 parts



footpath_buffer is now divided into 3 equal parts, but the parts are broken up. I am hoping to achieve something like this, where each part is continuous along the transect:



3 continuous parts transect










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a polygon object of a transect line that I would like to divide into 3 equal parts that are one-after-the-other along the transect line. So far I have only been able to slice the polygon vertically, which results in non-continuous parts.



    Here is a reproducible example using Harvard Forest data, available through the following link



    After you have downloaded and unzipped the data, you will see a folder called HARV. In this folder is the shapefile, HARV_roads.shp



    Import HARV_roads.shp



    lines_HARV <- readOGR("/Your file path/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")


    Get footpath lines only



    footpath_HARV <- lines_HARV[lines_HARV$TYPE == "footpath",]


    Plot the footpath



    plot(footpath_HARV,
    lwd=6,
    main="NEON Harvard Forest Field Siten Footpath")


    footpath



    Create a thicker polygon object from the footpath line using buffer, to simulate a transect line with X width



    require(raster)
    footpath_buffer <- buffer(footpath_HARV,width=40)


    Plot footpath_buffer



    plot(footpath_buffer)


    buffer



    Chop the buffer into 3 equal parts using Barry Rowlingson's code (below) from this post:



    makeVchopper <- function(pol){
    bb = bbox(pol)
    delta = (bb[2,2] - bb[2,1])/10
    xmin = bb[1,1]-delta
    ymin = bb[2,1]-delta
    ymax = bb[2,2]+delta

    choppoly = function(xmax){
    readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
    xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin,
    xmin,ymin))
    }
    choppoly
    }

    slicer <- function(pol, xmin, xmax){
    bb = bbox(pol)
    delta = (bb[2,2] - bb[2,1])/10
    ymax = bb[2,2] + delta
    ymin = bb[2,1] - delta
    r = readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
    xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin, xmin,ymin))
    gIntersection(pol,r)
    }

    chop_thirds <- function(pol, fractions=c(1/3, 2/3)){
    chopper = makeVchopper(pol)
    bb = bbox(pol)
    xmin = bb[1,1]
    xmax = bb[1,2]

    totalArea = gArea(pol)

    chopped_area = function(x){
    gArea(gIntersection(chopper(x),pol))
    }

    edges = lapply(fractions, function(fraction){
    target = totalArea * fraction
    target_function = function(x){
    chopped_area(x) - target
    }
    uniroot(target_function, lower=xmin, upper=xmax)$root
    })

    xdelta = (xmax-xmin)/10
    chops = matrix(c(xmin-xdelta, rep(edges,rep(2,length(edges))),
    xmax+xdelta), ncol=2, byrow=TRUE)
    apply(chops, 1, function(edges){
    slicer(pol, edges[1], edges[2])
    })

    }


    Divide footpath_buffer into 3 equal parts:



    parts.footpath <- chop_thirds(footpath_buffer)


    Plot footpath_buffer and the 3 equal parts



    plot(footpath_buffer)
    plot(parts.footpath[[1]], add=TRUE, col=1,border=F)
    plot(parts.footpath[[2]], add=TRUE, col=2,border=F)
    plot(parts.footpath[[3]], add=TRUE, col=3,border=F)


    3 parts



    footpath_buffer is now divided into 3 equal parts, but the parts are broken up. I am hoping to achieve something like this, where each part is continuous along the transect:



    3 continuous parts transect










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a polygon object of a transect line that I would like to divide into 3 equal parts that are one-after-the-other along the transect line. So far I have only been able to slice the polygon vertically, which results in non-continuous parts.



      Here is a reproducible example using Harvard Forest data, available through the following link



      After you have downloaded and unzipped the data, you will see a folder called HARV. In this folder is the shapefile, HARV_roads.shp



      Import HARV_roads.shp



      lines_HARV <- readOGR("/Your file path/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")


      Get footpath lines only



      footpath_HARV <- lines_HARV[lines_HARV$TYPE == "footpath",]


      Plot the footpath



      plot(footpath_HARV,
      lwd=6,
      main="NEON Harvard Forest Field Siten Footpath")


      footpath



      Create a thicker polygon object from the footpath line using buffer, to simulate a transect line with X width



      require(raster)
      footpath_buffer <- buffer(footpath_HARV,width=40)


      Plot footpath_buffer



      plot(footpath_buffer)


      buffer



      Chop the buffer into 3 equal parts using Barry Rowlingson's code (below) from this post:



      makeVchopper <- function(pol){
      bb = bbox(pol)
      delta = (bb[2,2] - bb[2,1])/10
      xmin = bb[1,1]-delta
      ymin = bb[2,1]-delta
      ymax = bb[2,2]+delta

      choppoly = function(xmax){
      readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
      xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin,
      xmin,ymin))
      }
      choppoly
      }

      slicer <- function(pol, xmin, xmax){
      bb = bbox(pol)
      delta = (bb[2,2] - bb[2,1])/10
      ymax = bb[2,2] + delta
      ymin = bb[2,1] - delta
      r = readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
      xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin, xmin,ymin))
      gIntersection(pol,r)
      }

      chop_thirds <- function(pol, fractions=c(1/3, 2/3)){
      chopper = makeVchopper(pol)
      bb = bbox(pol)
      xmin = bb[1,1]
      xmax = bb[1,2]

      totalArea = gArea(pol)

      chopped_area = function(x){
      gArea(gIntersection(chopper(x),pol))
      }

      edges = lapply(fractions, function(fraction){
      target = totalArea * fraction
      target_function = function(x){
      chopped_area(x) - target
      }
      uniroot(target_function, lower=xmin, upper=xmax)$root
      })

      xdelta = (xmax-xmin)/10
      chops = matrix(c(xmin-xdelta, rep(edges,rep(2,length(edges))),
      xmax+xdelta), ncol=2, byrow=TRUE)
      apply(chops, 1, function(edges){
      slicer(pol, edges[1], edges[2])
      })

      }


      Divide footpath_buffer into 3 equal parts:



      parts.footpath <- chop_thirds(footpath_buffer)


      Plot footpath_buffer and the 3 equal parts



      plot(footpath_buffer)
      plot(parts.footpath[[1]], add=TRUE, col=1,border=F)
      plot(parts.footpath[[2]], add=TRUE, col=2,border=F)
      plot(parts.footpath[[3]], add=TRUE, col=3,border=F)


      3 parts



      footpath_buffer is now divided into 3 equal parts, but the parts are broken up. I am hoping to achieve something like this, where each part is continuous along the transect:



      3 continuous parts transect










      share|improve this question















      I have a polygon object of a transect line that I would like to divide into 3 equal parts that are one-after-the-other along the transect line. So far I have only been able to slice the polygon vertically, which results in non-continuous parts.



      Here is a reproducible example using Harvard Forest data, available through the following link



      After you have downloaded and unzipped the data, you will see a folder called HARV. In this folder is the shapefile, HARV_roads.shp



      Import HARV_roads.shp



      lines_HARV <- readOGR("/Your file path/NEON-DS-Site-Layout-Files/HARV/HARV_roads.shp")


      Get footpath lines only



      footpath_HARV <- lines_HARV[lines_HARV$TYPE == "footpath",]


      Plot the footpath



      plot(footpath_HARV,
      lwd=6,
      main="NEON Harvard Forest Field Siten Footpath")


      footpath



      Create a thicker polygon object from the footpath line using buffer, to simulate a transect line with X width



      require(raster)
      footpath_buffer <- buffer(footpath_HARV,width=40)


      Plot footpath_buffer



      plot(footpath_buffer)


      buffer



      Chop the buffer into 3 equal parts using Barry Rowlingson's code (below) from this post:



      makeVchopper <- function(pol){
      bb = bbox(pol)
      delta = (bb[2,2] - bb[2,1])/10
      xmin = bb[1,1]-delta
      ymin = bb[2,1]-delta
      ymax = bb[2,2]+delta

      choppoly = function(xmax){
      readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
      xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin,
      xmin,ymin))
      }
      choppoly
      }

      slicer <- function(pol, xmin, xmax){
      bb = bbox(pol)
      delta = (bb[2,2] - bb[2,1])/10
      ymax = bb[2,2] + delta
      ymin = bb[2,1] - delta
      r = readWKT(sprintf("POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))",
      xmin,ymin, xmin,ymax, xmax,ymax, xmax,ymin, xmin,ymin))
      gIntersection(pol,r)
      }

      chop_thirds <- function(pol, fractions=c(1/3, 2/3)){
      chopper = makeVchopper(pol)
      bb = bbox(pol)
      xmin = bb[1,1]
      xmax = bb[1,2]

      totalArea = gArea(pol)

      chopped_area = function(x){
      gArea(gIntersection(chopper(x),pol))
      }

      edges = lapply(fractions, function(fraction){
      target = totalArea * fraction
      target_function = function(x){
      chopped_area(x) - target
      }
      uniroot(target_function, lower=xmin, upper=xmax)$root
      })

      xdelta = (xmax-xmin)/10
      chops = matrix(c(xmin-xdelta, rep(edges,rep(2,length(edges))),
      xmax+xdelta), ncol=2, byrow=TRUE)
      apply(chops, 1, function(edges){
      slicer(pol, edges[1], edges[2])
      })

      }


      Divide footpath_buffer into 3 equal parts:



      parts.footpath <- chop_thirds(footpath_buffer)


      Plot footpath_buffer and the 3 equal parts



      plot(footpath_buffer)
      plot(parts.footpath[[1]], add=TRUE, col=1,border=F)
      plot(parts.footpath[[2]], add=TRUE, col=2,border=F)
      plot(parts.footpath[[3]], add=TRUE, col=3,border=F)


      3 parts



      footpath_buffer is now divided into 3 equal parts, but the parts are broken up. I am hoping to achieve something like this, where each part is continuous along the transect:



      3 continuous parts transect







      shapefile sp rgeo-shapefile






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 10:35









      Val

      2,9422826




      2,9422826










      asked Nov 9 at 21:25









      Carrie Perkins

      425




      425





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233454%2fdivide-irregularly-shaped-polygon-into-3-equal-parts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233454%2fdivide-irregularly-shaped-polygon-into-3-equal-parts%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check