0

I have created a settlement plan on ArcGIS Pro, on which every polygon corresponds to a specific room in the settlement. All the polygons/rooms have some features (E.g. Fire Place, Bench etc.). I have extracted a csv file where all attributes and X and Y information is stored.

What I want to do is to plot in R only some of the rooms according to their features (E.g. only the rooms with a fire place).

Here is my initial code:

setwd("C:\\Users\\HP\\Desktop\\myrtos")
getwd()
library(rgdal)
library(raster)
library(Rcpp)
library(maptools)
library(spatstat)
rooms <- readOGR (dsn="shp", layer="rooms")
rooms <- as(rooms,"owin")
roomattr <- read.csv (file="rooms.csv", header=TRUE)
coordinates(roomattr) <- ~XCoord+YCoord

I tried to plot something like this, but apparently didn't work, there was no error message, but when I plotted the data there was nothing on the screen

constructions <- roomattr[which(roomattr$Rooms_csv.Bench == 'Yes'& roomattr$Rooms_csv.Platform == 'Yes'& roomattr$Rooms_csv.Ledge == 'Yes' & roomattr$Rooms_csv.Slab == 'Yes'),names(roomattr) %in% c("XCoord","YCoord")]
pppconstructions <- ppp(x =coordinates(constructions)[,1], y =coordinates(constructions)[,2], window=rooms)

I also tryied combining the files rooms = cbind(rooms, roomattr) and then plotting it plot(rooms[rooms$Rooms_csv.Stairs=="Yes",])

This method works only if I haven't run the rooms <- as(rooms,"owin") first. If I have run it I get the error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 966, 84

The thing is that I need rooms shape file as window of analysis for plotting the rest of my data (points)

Is there any way to make it work?

I am also attaching a link of the data (csv and shp file)

11
  • 1
    What didn't work? Did the code run and not do what you thought or did it give an error message? What does your data look like? What does summary of it show? What R packages have you got attached? Where did the ppp function come from? Please edit your question and give more fuller details so we can understand and help you.
    – Spacedman
    Sep 12 at 18:26
  • You are totally right! I added some more information.
    – MaryM
    Sep 12 at 18:53
  • Instead of an image of the data file, could you either make it shareable via a cloud service like Dropbox or Google Drive, or at least put a few rows into the question as text so we can make a data frame like it easily? We probably also need the shapefile that rooms came from otherwise we can't try and plot it.
    – Spacedman
    Sep 12 at 19:03
  • I did, many thanks
    – MaryM
    Sep 12 at 19:13
  • 1
    Instead of commenting, you need to edit your question to show what code led to what error message, giving all the code and the error message details. Now we have your data we should be able to reproduce this.
    – Spacedman
    Sep 13 at 8:22

2 Answers 2

1

Select the polygons you want first from the augmented rooms object, then create an owin with the subset.

> roomattr <- read.csv (file="rooms.csv", header=TRUE)
> rooms = cbind(rooms, roomattr)

Now we have rooms with all the polygons and attributes. Suppose we only want the "stairs" rooms, then:

> stairs = rooms[rooms$Rooms_csv.Stairs=="Yes",]

That's the subset of stairs only. There's...

> nrow(stairs)
[1] 11

eleven of them. Want to use spatstat so need to make an owin? Do that:

> stairwin = as(stairs, "owin")
> plot(stairwin)

enter image description here

2
  • Many, many thanks!!
    – MaryM
    Sep 13 at 16:49
  • One more question to that. I want to plot only some of the rooms (E.g. R81 and R82). I tried this: ` rr = rooms[rooms$rooms.Rooms =="R81" , rooms$rooms.Rooms == "R82"] rr <- as(rr,"owin") plot(rr)` But I only get R81 and not the second one. I also tried to put a "&" between the two variables but it din't work again. Any idea?
    – MaryM
    Sep 21 at 16:31
1

Below is how I would geo join with {tigris} and an example of filtering with {tmap}. I'm interested to hear what others have to say about these methods:

library(dplyr)
library(sf)
library(tmap)
library(tigris)

#read in data
rooms <- st_read(file.path("rooms.shp"))
rooms.csv<-read.csv(file="rooms.csv",header=TRUE)

#rename index column in csv to match that in shapefile
colnames(rooms.csv)[3]<- c("Rooms")

#perform geo join of csv and shapefile
rooms.join<-geo_join(rooms,rooms.csv,by="Rooms",how = "left")

#interactive map
tmap_mode("view")
tm_shape(dplyr::filter(rooms.join,Rooms_csv.Stairs=="Yes"))+tm_polygons()

#static map
tmap_mode("plot")
tm_shape(dplyr::filter(rooms.join,Rooms_csv.Stairs=="Yes"))+tm_polygons()+tm_layout("Rooms with Stairs")

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.