TransWikia.com

Assigning individual spatial coordinates (x,y point) to different ROI spatial polygons in R

Geographic Information Systems Asked by anzellan on August 23, 2020

I am trying to track cell movement from one region to another region. Basically I’d like to assign each coordinate in the track data with a particular region it occupies. I have transformed the track data into spatial data. I have also imported multiple ROIs from imageJ using the "RImageJROI" package.

As of right now I have tried to iterate through the spatial data list and create a vector which assigns the name of the ROI the coordinate exists in, but have had no success. The vector (X) in my global environment just states "logical(empty)" and I receive a list of warnings. These warnings mostly consist of:

  1. number of items to replace is not a multiple of replacement length
  2. the condition has length > 1 and only the first element will be used

I imagine I’m just not calling the coordinates or the ROI’s correctly.
Does anyone have any ideas on different strategies to accomplish this? Current code is below.

coordinates(df) <- c("pos_x", "pos_y")
as(df, "SpatialPoints")
X=vector()
for (i in 1:nrow(df)) {
  if (df$coordinates[i] <- inside.owin(x, y, BCA)) {
    X[i] = "BCA"
  } else if (df$coordinates[i] <- inside.owin(x, y, PCS)) { 
    X[i] = "PCS" 
  } else if (df$coordinates[i] <- inside.owin(x, y, BA2)) {
    X[i] = "BA" 
  } else if (df$coordinates[i] <- inside.owin(x, y, CaDId)) {
    X[i] = "CaDId" 
  } else if (df$coordinates[i] <- inside.owin(x, y, CaDIv)) {
    X[i] = "CaDIv" 
  } else if (df$coordinates[i] <- inside.owin(x, y, MtAd)) {
    X[i] = "MtAd"
  } else if (df$coordinates[i] <- inside.owin(x, y, MtAv)) {
    X[i] = "MtAv" 
  } else if (df$coordinates[i] <- inside.owin(x, y, PHBC)) {
    X[i] = "PHBCd"
  } else if (df$coordinates[i] <- inside.owin(x, y, PHBCv)) {
    X[i] = "PHBCv"
  }
} 

One Answer

I ended up figuring it out after @Spacedman suggested I create dummy data set. I'll leave the answer posted with the dummy data.

There were many issues with the code I posted above, but the main issue is that I wasn't using inside.owin properly. Previously, I had converted my data frame to spatial coordinates and attempted to use inside.owin , but I ended up using my original data frame containing x and y positions that I could plug into inside.owin and that worked perfectly.

Here is the solution I came up with:

## create ROIs
ROI1 <- owin(xrange = c(0,2), yrange = c(3, 6))
ROI1 <- as(ROI1, "SpatialPolygons")
ROI2 <- owin(xrange = c(4,8), yrange = c(0, 2))
ROI2 <- as(ROI2, "SpatialPolygons")

## create points
pts <- data.frame("x" = c(1, 1.5, 2, 3, 3.5, 4, 5, 6.5, 7, 8), "y" = c(1, 5.5, 6, 3, 5, 1, 2, 0.5, 1, 8))


## test if pts are inside ROIs
X=vector()
for (i in 1:nrow(pts)) {
  if (inside.owin(pts$x[i], pts$y[i], ROI1)){
    X[i] = "ROI1"
  } else if (inside.owin(pts$x[i], pts$y[i], ROI2)){
    X[i] = "ROI2"
  }
}

Answered by anzellan on August 23, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP