I am looking to create a map using this dataset: data
With the shape file of shapefile
Here is the following R code:
library("tidyverse")
library("dplyr")
library("sf")
library("ggplot2")
library("tmap")
library("tmaptools")
library("RColorBrewer")
data = readr::read_csv("C:/Users/amanm/Desktop/annualincomedata.csv")
localauthorities2 <- read_sf("C:/Users/amanm/Desktop/localauthorities2.shp")
map_and_data <- merge(localauthorities,data)
str(map_and_data)
ggplot(map_and_data) =
geom_sf(oes(fill = Percentage), color = NA) +
scale_filla_viridis_c()
I am given a error whilst trying to run the code:
> localauthorities2 <- read_sf("C:/Users/amanm/Desktop/localauthorities2.shp")
Error: Cannot open "C:\Users\amanm\Desktop\localauthorities2.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 4: Unable to open C:\Users\amanm\Desktop\localauthorities2.shx or C:\Users\amanm\Desktop\localauthorities2.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
Any advice would be useful. Thanks
A shapefile consists of a bunch of files with different extensions - .shp, .dbf and .shx being the most essential. It looks like you've not got the corresponding .shx file:
GDAL Error 4: Unable to open C:\Users\amanm\Desktop\localauthorities2.shx or C:\Users\amanm\Desktop\localauthorities2.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
You can set config options in GDAL as environment variables. I made a shapefile called foo
with all its components and deleted the .shx. Reading it produces the error:
> st_read("foo.shp")
Error: Cannot open "foo.shp"; The source could be corrupt or not supported. See `st_drivers()` for a list of supported formats.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 4: Unable to open foo.shx or foo.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it.
setting the option in the environment fixes it:
> Sys.setenv(SHAPE_RESTORE_SHX="YES")
> st_read("foo.shp")
Reading layer `foo' from data source `foo.shp' using driver `ESRI Shapefile'
Simple feature collection with 10 features and 1 field
Geometry type: POINT
Dimension: XY
Bounding box: xmin: 1 ymin: 1 xmax: 10 ymax: 10
CRS: NA
However if you've not got the corresponding .dbf
file nothing is going to regenerate that from scratch since it contains the attributes. Chances are you'll find that in the same place as the .shx
and then you don't need this hack.