I have 3409 Electoral Divisions from the Irish census for the Republic of Ireland. I want to calculate 400 metre distances from the centroid of every electoral division to the spatial polygon boundary in the direction of north, south, east and west. I've figured out how to calculate 400 metres in each direction with the output as long/lat coordinates, but not how to iteratively do this until the boundary is reached.
Here's an example of the code calculating 400 metres from the centroid going north:
library(geosphere)
pn <- cbind(DF$long, DF$lat)
bn <- 360
dn <- 400
an <- 6378137
fn <- 1/298.257223563
DF$North <- destPoint(pn, bn, dn, an, fn)
How can I do this using R to code for iteratively calculating long/lat coordinates of 400 metres from the centroid until the boundary of each 3409 electoral divisions is reached? Please note the columns that I have in the spatial polygon dataframe is centroid_x, centroid_y, long_centroid, lat_centroid, shape_area, shape_length.
Any tips or pointers for code would be most appreciative.
Best Answer
Here's what I have. First a function to generate the points going out in the cardinal directions:
Then this function which shifts a set of points to be centred at a given point. We'll use this to move a set of points generated by
cardinal
to each centroid:To use, you first have to create a set of points in the cardinal directions to span the largest of your polygons. If its too small you'll miss points, but it will be slower if you have too many. This function should compute the number of steps of size
w
needed to span the biggest bounding box in the set, which should be sufficient in the worst case scenario that one of your regions is a rectangle:Okay that's the setup. Now...
That is a list of the same length as the number of features in
ed
, so to plot an ED and then show the points:Note that some EDs have no points in them. Region 80 is very small:
Note how with this method points don't stop when the first NSEW direction meets the border (region 1209):
The northern points skip one point where the edge jinks in, and the points continue. Not sure if this is what you want or not. If not, then some rewriting is needed.