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")
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)
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)
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:
shapefile sp rgeo-shapefile
add a comment |
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")
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)
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)
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:
shapefile sp rgeo-shapefile
add a comment |
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")
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)
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)
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:
shapefile sp rgeo-shapefile
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")
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)
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)
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:
shapefile sp rgeo-shapefile
shapefile sp rgeo-shapefile
edited Nov 10 at 10:35
Val
2,9422826
2,9422826
asked Nov 9 at 21:25
Carrie Perkins
425
425
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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