Extract BC census shapefile - r

I have downloaded the Canada census level polygon shapefiles strong text (https://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2011-eng.cfm) and imported into R using readOGR function from rgdal package.
However, I am struggling to extract the shapefiles specifically from British Columbia. I have used some of the previous explanations to extract my US state zip codes with success (Choropleth Maps in R - TIGER Shapefile issue).
I need assistance in extracting the BC shapefiles.

I don't know which Geographic area or water feature you have selected from that website. I have downloaded the Digital Boundary File of Provinces/territories. I have read and subsetted British Columbia shapefile using following command
library(rgdal)
df <- readOGR(dsn = "C:\\Users\\User\\Desktop\\gpr_000a11a_e", layer = "gpr_000a11a_e")
#To see the data head
head(df#data)
#To see a particular column of the shapefile
df#data$PRENAME
#Subsetting British Columbia from the shapefile
BC = subset(df, PRENAME=="British Columbia")
#Plotting it
plot(BC)

Related

subsetting zipcodes from shapefile and plotting in R

I have a Census Shapefile of the United States by ZCTA. Am only interested in plotting NY. I can read in the whole shapefile and plot only NY by limiting the longitude and latitude but it takes a long time and R keeps crashing.
Instead, I'd like to reduce to only zipcodes in NY. I'm reading in the shapefile, converting to dataframe and then subsetting to the zipcodes/ZCTAs in NY. When I plot the result, however, it's not a full map. Is this the right approach? My R code and plot are below:
### read in the ZCTA shapefile for US
us_modzcta <- "Data/us_zcta/tl_2018_us_zcta510.shp"
us <- shapefile(us_modzcta)
## convert to dataframe
us_df <- fortify(us,region="ZCTA5CE10")
## subset - keep only zip codes in NY - starts with 10, 11, 12, 13, 14
ny_df <- us_df[which(startsWith(us_df$id,c("11","12","13","10","14"))),]
p<- plot(ny_df)
Why so complicated path via raster and ggplot2? Please have a look on sf package:
library(sf)
library(tidyverse)
us_modzcta <- "census/tl_2018_us_zcta510.shp"
ny <- st_read(us_modzcta) |>
filter(grepl("^1[0-4]", ZCTA5CE10))
plot(ny$geometry)
Created on 2022-03-05 by the reprex package (v2.0.1)

Is it possible to create a map of the UK counties with the packages spData/tmap

I have recently used the package spData for plotting the London boroughs but I haven't found anything for plotting the counties in the UK.
Is it possible the the package spData contains only a few countries in it?
Thank you for your help,
Andrea
Could this be what you are looking for? A shp of the counties?
spData isn't a complete set of world spatial data.
You can find a complete set (with good county shapes) in the Natural Earth 10m (high resolution) cultural vectors. Here's how I was able to make a quick map of UK counties, coloured by the length of the county name:
library("tmap")
library("tmaptools")
library("rnaturalearth")
library("rnaturalearthhires")
library("sf")
data("states10")
uk_counties <- states10[which(states10$iso_a2 =="GB"),]
uk_counties = st_as_sf(uk_counties)
qtm(uk_counties, fill="name_len")
Once you have this dataframe called uk_counties, you can join it to your data & quickly make some nice maps.
choropleth of uk counties by length of county name

Quadrats in Spatstat

I am now doing exploratory analysis and the objective is to plot a quadrat map, determine if there exists complete spatial randomness (visually and using chi-square test), etc. However, I am having trouble plotting a quadrat map. I previously asked this question that led me to reproject my data.
Here is my code:
library(rgdal) #Brings Spatial Data in R
library(spatstat) # Spatial Statistics
library(maptools)
library(raster)
# Load nyc zip code boundary polygon shapefile
s <- readOGR("/Users/my_name/Documents/fproject/zip","zip")
nyc <- as(s,"owin")
### OGR data source with driver: ESRI Shapefile
#Source: "/Users/my_name/Documents/project/zip", layer: "zip"
#with 263 features
# Load nyc arrests point feature shapefile
s <- readOGR("/Users/my_name/Documents/project/nycarrests/","geo1")
### OGR data source with driver: ESRI Shapefile
#Source: "/Users/my_name/Documents/project/nycarrests", layer: "geo1"
#with 103376 features
#It has 19 fields
#Converting the dataset into a point pattern
utm <- "+proj=utm +zone=32 +datum=WGS84"
x <- spTransform(s,utm)
arrests <- as(x,"ppp")
marks(arrests) <- NULL
Window(arrests) <- nyc
plot(arrests, main="2020", cols="dark green",pch=20)
### Quadrat density 2020
Q <- quadratcount(arrests, nx= 3, ny=3)
plot(arrests, pch=20, cols="grey70", main="2020") # Plot points
plot(Q, main="2020", add=TRUE) # Add quadrat grid
Here, my data points are constrained by NYC zip code boundaries, but they do not show up on my map. What can I do to fix this?
You are not providing much information. For example you could show (print) s (each time), or crs(s) (both with the raster package loaded)
Either way, it is almost certainly because the coordinate reference systems you are using are not the same. You change one to "UTM zone 21", but you do not change the others. They are probably different. Also, zone 21 is not appropriate for New York.

Shapefile with multiple shapes but only one polygon

I am using the "King County with Natural Shoreline for Puget Sound and Lake Washington" shapefile from http://www5.kingcounty.gov/gisdataportal/. The outline for Lake Washington appears when I plot the shapefile but is not saved as an additional polygon relative to the outline of King County. I wish to color Lake Washington a different color to the rest of the county but am not sure how to do this because the shapefile only has 1 feature. I am trying to manipulate the shapefile in R.
You can extract the "hole" that is Lake Washington and then add that to the plot. This uses base graphics but it's just as easy with ggplot.
library(rgdal)
# your shapefile
kc <- readOGR("kingsh/kingsh.shp", "kingsh")
# extract the singular hole that is Lake Washington and
# make it into something plottable
lw <- SpatialPolygons(list(Polygons(list(kc#polygons[[1]]#Polygons[[2]]), ID="lw")))
plot(kc, col="steelblue")
plot(lw, col="maroon", add=TRUE)
You can find the hole by manually scanning the objects or something like:
unlist(lapply(kc#polygons, function(p) {
polys <- slot(p, "Polygons")
lapply(polys, function(q) {
slot(q, "hole")
})
}))
I had to guess at how you're reading it in since you didn't provide any code.

Overlay shapefile and rasterlayer in R?

I have a raster layer with climate data from the north of Mexico, part of Canada and the US mainland. I am trying to restrict the climate data to only the zone of the US mainland. To do this I thought it would be easy to import a US mainland map and overlay it with my raster data (of course this is turning out to be a lot more difficult than I thought). So far, using the rgdal library, I have managed to import a shapefile including the USA mainland map with states divisions. Now I want to convert this into a raster layer so that can finally overlay it with my initial climate raster layer. This is the code that I am using:
setwd ("C:/Climate_data/USA map")
ogrInfo(".", "USA_mainland_states")
usa_state = readOGR(dsn=".", layer="USA_mainland_states")
##Convert to Raster
r_usa_state <- raster()
extent(r_usa_state) <- extent(usa_state)
rasterize(usa_state,r_usa_state, fun='last')
overlay (r_usa_state, sms_av, fun='mask')
However, now I get the following error:
Error in .readCells(x, cells, 1) : no data on disk or in memory
sms_av is the climate rasterlayer (103936 elements, 823.3 kb).
Also, when I do:
hasValues(r_usa_state)
I get:
FALSE
What am I doing wrong?? Any advice would be GREATLY appreciated!!!!

Resources