2

I'm working in R and I am new to maps and GIS and I have no idea how it works. But I am trying to return which part of the city specific coordinates are located. I got the shapefile from here:

https://opendata.arcgis.com/datasets/ac4e22c0d8444b8b81a0ee9af3273f40_2.zip

I read the file and transform it:

library(rgdal)
library(mapview)    
nhood <- readOGR(dsn = "hverfahlutar.shp")
nhood2 = spTransform(nhood, CRS("+proj=longlat +datum=WGS84"))

the dataframe I am working with is this:

schools <- structure(list(school = skolar <- c("Vesturbæjarskóli", "Landakotsskóli", 
                      "Barnaskóli Hjallastefnunnar í Reykjavík ", "Tjarnarskóli", "Austurbæjarskóli", 
                      "Langholtsskóli", "Vogaskóli", "Suðurhlíðarskóli", "Brúarskóli", 
                      "Hlíðaskóli"), long = c(64.1497778, 64.1478234, 64.1287022, 64.1406399, 
                                              64.1417735, 64.1380849, 64.131696, 64.1216781, 64.1256253, 64.130929
                      ), lat = c(-21.959836, -21.9517121, -21.9311303, -21.9365311, 
                                 -21.9237807, -21.8623288, -21.8606524, -21.9040609, -21.9130514, 
                                 -21.9122735)), .Names = c("school", "long", "lat"), row.names = c(NA, 
                                                                                                   -10L), class = c("tbl_df", "tbl", "data.frame"))

I can plot it with points so the data seems to be okay:

plot(nhood2)
points(schools$long ~ schools$lat, col = "red", cex = 1)

What to do next I do not know. I think I need to use the over function but it has not been working for me.

What I need to do is, for each coordinates in each row of school. I need to return the corresponding value in nhood2$HEITI (the name of the neighbourhood.)

I think I need to prepare the data in the schools dataframe for this, but I don't understand how... or why.

1

Your schools isn't a data frame, its a "tibble". You should convert it to a real data frame, and then assign its coordinates to create a Spatial Points Data Frame. Also you've got your lat-long the wrong way round, so I'll rename the columns:

library(sp)
schools = data.frame(schools)
names(schools) = c("school","lat","long")
coordinates(schools)=~long+lat

Your points and polygons are using the same coordinate system now, so set the schools projection to the same as nhood2 - there's no transformation done here, its just marking the coordinate system:

proj4string(schools) = proj4string(nhood2)

Don't use tibbles if you are working with spatial data and the sp package (which is what rgdal also returns).

Then you can overlay points with polygons:

> over(schools, nhood2)
   OBJECTID                 DAGS_INN  NOTANDI     HEITI DAGS_UPPR DAGS_LEIDR
1      4664 2002-07-30T12:48:16.000Z LUK_GOGN   Grandar      <NA>       <NA>
2      4671 2002-07-30T12:48:16.000Z LUK_GOGN Vesturbær      <NA>       <NA>
3      4694 2002-07-30T12:48:16.000Z LUK_GOGN Vatnsmýri      <NA>       <NA>
4      4709 2002-07-30T12:48:16.000Z LUK_GOGN Austurbær      <NA>       <NA>
5      4709 2002-07-30T12:48:16.000Z LUK_GOGN Austurbær      <NA>       <NA>
6      4700 2002-07-30T12:48:16.000Z LUK_GOGN    Heimar      <NA>       <NA>
7      4700 2002-07-30T12:48:16.000Z LUK_GOGN    Heimar      <NA>       <NA>
8      4681 2002-07-30T12:48:16.000Z LUK_GOGN Öskjuhlíð      <NA>       <NA>
9      4674 2002-07-30T12:48:16.000Z LUK_GOGN    Hlíðar      <NA>       <NA>
10     4674 2002-07-30T12:48:16.000Z LUK_GOGN    Hlíðar      <NA>       <NA>

This has the HEITI column from the polygons for each point, as well as all the other columns from the polygons.

You might also want to look into how to do this using sf class objects, a newer system for spatial data.

  • Thank you very much. I had forgotten about it being tibble as I was working with a older dataset (and the addiction to using the dplyr package). I plan to just join the dataframes to get the name of the school in the corresponding row... but, is there a way to add the school column from the school variable to the new overlay dataframe? – Atius Feb 26 '18 at 15:05
2

Here is a solution with sf. In this case, you can leave it as a tibble.

library(sf)
# since you mentioned in the comment you used dplyr I thought you wouldn't mind this dependency, used for filter
library(dplyr)

nhood <- st_read('input/HVERFAHEITI.shp')
names(schools) = c("school","lat","long")
schools <-  st_as_sf(schools, coords = c("long", "lat"), crs = 4326)

# this shows which neighbourhoods (HEITI) intersects each school
st_join(schools, nhood['HEITI'], join = st_intersects)

>#                                    school     HEITI                   geometry
># 1                          Vesturbæjarskóli   Grandar POINT (-21.95984 64.14978)
># 2                            Landakotsskóli Vesturbær POINT (-21.95171 64.14782)
># 3  Barnaskóli Hjallastefnunnar í Reykjavík  Vatnsmýri  POINT (-21.93113 64.1287)
># 4                              Tjarnarskóli Austurbær POINT (-21.93653 64.14064)
># 5                          Austurbæjarskóli Austurbær POINT (-21.92378 64.14177)
># 6                            Langholtsskóli    Heimar POINT (-21.86233 64.13808)
># 7                                 Vogaskóli    Heimar  POINT (-21.86065 64.1317)
># 8                          Suðurhlíðarskóli Öskjuhlíð POINT (-21.90406 64.12168)
># 9                                Brúarskóli    Hlíðar POINT (-21.91305 64.12563)
># 10                               Hlíðaskóli    Hlíðar POINT (-21.91227 64.13093)

I'm not totally clear on what you are requesting in your comment, but if you would like to subset which nhood polygons have a school in it and add the school variable (i.e. add school to nhood object, rather than nhood to school object), this should work:

st_join(nhood['HEITI'], schools, join = st_intersects) %>% 
  filter(!is.na(school)) %>% 
  as.data.frame()

>#         HEITI                                   school                       geometry
># 1  Austurbær                             Tjarnarskóli MULTIPOLYGON (((-21.92646 6...
># 2  Austurbær                         Austurbæjarskóli MULTIPOLYGON (((-21.92646 6...
># 3     Heimar                           Langholtsskóli MULTIPOLYGON (((-21.8594 64...
># 4     Heimar                                Vogaskóli MULTIPOLYGON (((-21.8594 64...
># 5    Grandar                         Vesturbæjarskóli MULTIPOLYGON (((-21.96228 6...
># 6  Vesturbær                           Landakotsskóli MULTIPOLYGON (((-21.94349 6...
># 7     Hlíðar                               Brúarskóli MULTIPOLYGON (((-21.9185 64...
># 8     Hlíðar                               Hlíðaskóli MULTIPOLYGON (((-21.9185 64...
># 9  Öskjuhlíð                         Suðurhlíðarskóli MULTIPOLYGON (((-21.91542 6...
># 10 Vatnsmýri Barnaskóli Hjallastefnunnar í Reykjavík  MULTIPOLYGON (((-21.94523 6...

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.