Shaepfile isn't working I keep getting an exception and I unsure on what is the error

43 Views Asked by At

Hi All I am trying to do map plotting using python and I am using a shapefile of the UK ITL3 regions obtained from the ONS geo portal and I want to build it in python however the issue I am getting is that an exception error which I will attach now:

Traceback (most recent call last):
  File "C:\Users\\PycharmProjects\pythonProject3\Map2.py", line 15, in <module>
    sf = shp.Reader(shp_path)
  File "C:\Users\AppData\Roaming\Python\Python39\site-packages\shapefile.py", line 1048, in __init__
    self.load(path)
  File "C:\Users\AppData\Roaming\Python\Python39\site-packages\shapefile.py", line 1193, in load
    raise ShapefileException("Unable to open %s.dbf or %s.shp." % (shapeName, shapeName))
shapefile.ShapefileException: Unable to open \ITL3_JAN_2021_UK_BFE_V3.dbf or \ITL3_JAN_2021_UK_BFE_V3.shp.

The code I am using is this one:

import numpy as np
import pandas as pd
import shapefile as shp
import matplotlib.pyplot as plt
import seaborn as sns


sns.set(style="whitegrid", palette="pastel", color_codes=True)
sns.mpl.rc("figure", figsize=(10,6))

#opening vector map

shp_path ="\\ITL3_JAN_2021_UK_BFE_V3.shp"

sf = shp.Reader(shp_path)

len(sf.shapes())

Any help to try and resolve this would be much appreciated so I can view the records that are contained in the shapefile.

2

There are 2 best solutions below

0
Ahmed Amin Shahin On

I see you did not specify the right path for your file, The error is indicating that Unable to open \ITL3_JAN_2021_UK_BFE_V3.dbf or \ITL3_JAN_2021_UK_BFE_V3.shp.

try to save the file in this path for example

path = "C:\myfolder\ITL3_JAN_2021_UK_BFE_V3.shp"

or try this

path = r"C:\myfolder\path_to_your_file\ITL3_JAN_2021_UK_BFE_V3.shp"

Make sure you can access the path to your file location by pasting it in your windows browser. adjust your code accordingly.

Hope this helps.

3
Timeless On

I understand you're using pyshp to read this particular shapefile (that you unzipped apparently). So, the ShapefileException means that both files (.dbf & .shp) are missing in the current directory of your terminal :

┣━━ ITL3_JAN_2021_UK_BFE_V3.cpg
┣━━ ITL3_JAN_2021_UK_BFE_V3.dbf # >> missing
┣━━ ITL3_JAN_2021_UK_BFE_V3.prj
┣━━ ITL3_JAN_2021_UK_BFE_V3.shp # >> missing
┗━━ ITL3_JAN_2021_UK_BFE_V3.shx

Or maybe the files are available and you just need to remove the \\ in your shapefile path :

shp_path ="\\ITL3_JAN_2021_UK_BFE_V3.shp"
shp_path = "ITL3_JAN_2021_UK_BFE_V3.shp"

To circumvent such issues, I suggest you pass the zipfile abs-path (btw, pyshp can read archives) :

import shapefile as shp

# replace ... with the actual path
shp_path = ".../International_Territorial_Level_3_January_2021_UK_BFE_V3_2022.zip"

sf = shp.Reader(shp_path)

len(sf.shapes()) # 179

With that said, if your main goal is doing plots, then I'd use :

import geopandas as gpd
import seaborn as sns

gdf = gpd.read_file(shp_path)

sns.set(style="whitegrid", palette="pastel", color_codes=True)
sns.mpl.rc("figure", figsize=(10, 6))

gdf.plot(column="BNG_E")

Output : enter image description here