# 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)) {
} else if (df$coordinates[i] <- inside.owin(x, y, CaDIv)) { X[i] = "CaDIv" } else if (df$coordinates[i] <- inside.owin(x, y, 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

## Related Questions

### Python GDAL Merge Issue: gdal_merge.py

1  Asked on February 2, 2021 by terracoder

### Extents of British National Grid

0  Asked on February 2, 2021 by btl

### Survey123 Python Module Feature Report Names

0  Asked on February 2, 2021

1  Asked on February 2, 2021 by erich-purpur

### Creating polygons via snapping to circle line

0  Asked on February 2, 2021 by katharina

### Obtaining land surface temperature in Google Earth Engine using GEET and TOA error is showing

1  Asked on February 2, 2021 by geenewbie

### Finding point within box using MongoDB?

1  Asked on February 2, 2021

### Attribute join on the fly for visualization with ArcGIS JS API

0  Asked on February 2, 2021 by seth-lutske

### Calculating minimum distance in meters of two polygons (defined in lat/long)

1  Asked on February 2, 2021 by arshak

### Maximum Likelihood Classification not working correctly in ArcMap

0  Asked on February 2, 2021

### Reducing points but maintaining attributes from original shapefile

0  Asked on February 2, 2021

### How to use ground overlay google map android for polygon area to fit the map image in it?

1  Asked on February 2, 2021 by nimaazx

### Stop layer from showing up in legend on composer

3  Asked on February 2, 2021 by mortensickel

### How to convert a shapefile (shp) to raster via GEE

1  Asked on February 2, 2021 by wesleysc352

### Missing holes after exporting GeoJSON in QGIS

0  Asked on February 2, 2021

### QGIS- Missing measure line box/point during editing

1  Asked on February 2, 2021

### Writing table to memory in ArcPy gives ERROR 000354

1  Asked on February 2, 2021 by elbert-de-hon

### QGIS export circle to KML then import to Google Map

0  Asked on February 2, 2021 by julien

### Road Connectivity Analysis by field

1  Asked on February 2, 2021 by stephen-g

### Positioning columns in QGIS Mapcomposer

2  Asked on February 2, 2021 by theo