Calculate points with angle and distance from a centroid

Here is just a small r-script, which calculates the location of points around a centroid. I had problems with a dataset containing only the angle and the distance of the points and not their exact location. In my case the dataset represents a little forest survey with trees occurring in a given distance from the plots centroid.

require(rgeos);library(rgdal)

bsp_sel # my plot data
trueCentroids = gCentroid(bsp_sel,byid=TRUE)

plot(bsp_sel)
points(trueCentroids,pch=2)

centroid1

My Buffered plot and its centroid

# data structure:
# Species | Distance (in m) | Angle (in radians degree)
## For example
# Spruce | 20.5 | 66
centerPoint <- coordinates(trueCentroids)

# Function to calculate for instance the coordinates of the point in 20.5m by 66° from the coordinates of the centerpoint
retPoCordinates <- function(center,angle,distance){
x <- round(centerPoint[,1] + distance * cos(angle),0)
y <- round(centerPoint[,2] + distance * sin(angle),0)
result <- as.data.frame(cbind(x,y));names(result) <- c("x","y")
return(result)
}


pp <- retPoCordinates(centerPoint,data$angle,data$distance) #Saves the coordinates of all trees in the variable pp
coordinates(pp) <- ~x+y
points(pp,col="green4",pch=19) # Add to plot
Plot with all trees around the centroid

Plot with all trees around the centroid

#If you want you could furthermore save your SpatialPoint Object as Esri shapefile. Use the tree species as attribute-table
#and the spatial projection from your first shape
spp <- SpatialPointsDataFrame(coords=coordinates(pp),data=as.data.frame(data$Species),proj4string=CRS(proj4string(bsp_sel)))
writeOGR(spp,"shapes","treepoints",driver="ESRI Shapefile",)

# Besides you could now calculate a distance matrix between the points or make a test for spatial aggregation and so on...

library(dismo)
pointDistance(pp,longlat=F)

# ...

Go 4 it ! 😉

Advertisements

Tags: , ,

About Martin Jung

PhD researcher at the University of Sussex. Interested in nature conservation, ecology and biodiversity as well as statistics, GIS and 'big data'
Sussex Research Hive

Supporting the research community at the University of Sussex

Small Pond Science

Research, teaching, and mentorship in the sciences

Landscape Ecology 2.0

intersecting landscape ecology, open science, and R

nexus

The Research Blog of IIASA

Jörg Steinkamps Blog

Mainly things about R, Linux and vegetation modeling

Amy Whitehead's Research

the ecological musings of a conservation biologist

Michael McCarthy's Research

School of BioSciences, The University of Melbourne

The Rostrum

science, statistics, policy and more

marionpfeifer

Environmental Change - Understand, Predict, Adapt

Dynamic Ecology

Multa novit vulpes

metvurst

METeorological Visualisation Utilities using R for Science and Teaching

A Birder´s Blog

"Everybody loves what they know"

BIOFRAG

A new metric to quantify biodiversity response to fragmentation

Trust Me, I'm a Geographer

Using Technology to Explore Our World

Duncan Golicher's weblog

Research, scripts and life in Chiapas

%d bloggers like this: