0

Trying to learn to use Python to map some geographic data by following some tutorial and I can't seem to get them to work. I'm hoping this is just a simple error on my part.

First, the link(s) to the tutorials:

https://towardsdatascience.com/mapping-with-matplotlib-pandas-geopandas-and-basemap-in-python-d11b57ab5dac

https://towardsdatascience.com/mapping-geograph-data-in-python-610a963d2d7f

I am unable to get past the steps using code for the importing of the files.

My code is

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 the vector map
shp_path = "C:\\Users\\Jack\\.spyder-py3\\District_Boundary.shp"
#reading the shape file by using reader function of the shape lib
sf = shp.Reader(shp_path)

len(sf.shapes())

My error output is:


  File "C:\Users\Jack\Anaconda3\lib\site-packages\shapefile.py", line 646, in load
    raise ShapefileException("Unable to open %s.dbf or %s.shp." % (shapeName, shapeName))

ShapefileException: Unable to open C:\Users\Jack\.spyder-py3\District_Boundary.dbf or C:\Users\Jack\.spyder-py3\District_Boundary.shp.

closed as off-topic by user2856, Fran Raga, Ian Turton Jun 25 at 7:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – user2856, Fran Raga, Ian Turton
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Do the files exist where you think they do? Try import os then do os.path.exists("/your/path"). If it returns False, then your path is wrong. We have no way of telling if your file is actually there, so this is the first thing to try. – alphabetasoup Jun 24 at 23:20
  • I can read District_Boundary.shp just fine with pyshp and geopandas. You've got your path wrong I suggest. Where did you download and unzip District_Boundary.zip to? – user2856 Jun 24 at 23:35
  • Do you have the dbf and the shx files also in the folder? – mazinga Jun 25 at 0:30
  • Thanks guys. ID-10T error. Forgot the file name was also the folder name and didn't include it. – JDubs Jun 25 at 0:34
  • @RichardLaw Nice troubleshooting help. Would you be willing to include your comment as an answer so we can consider this question resolved? – Aaron Jun 25 at 1:40
1

Make sure that the files exist where you think they do. This is generally part of good script writing: before trying to read a file, assert that it exists.

Here's one possible adaptation of your code, using assert to test the return value of os.path.exists with your proposed file path as input:

import os

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 the vector map
shp_path = "C:\\Users\\Jack\\.spyder-py3\\District_Boundary.shp"
assert os.path.exists(shp_path), "Input file does not exist."

# reading the shape file by using reader function of the shape lib
sf = shp.Reader(shp_path)

len(sf.shapes())

Now if you run this you will see an error:

Traceback (most recent call last):
  File "<input>", line 14, in <module>
AssertionError: Input file does not exist.

Incidentally, the original error was also OK. I'd probably not even bother with the assertion. It was telling you that it was unable to read the files it expected to find. That's an expected result when the file doesn't exist.

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