importing shapefiles in Python

Python is a very common scripting language which seems like a swiss knife for programming. This is the reason to use it as a framework for the program “where are your customers“.
In this context I need to import the shapefile into Python. Therefore the guys at geospatialpython present a nice module to import shapefiles into python.
First install the file using easy-install/pip. Therefore open the terminal:

sudo apt-get install python-pip
sudo easy_install pyshp

If you already installed python-pip you can skip the first line.
Now we can import our module and import our whole shapefile (in this example called ‘Countries’) right away:

import shapefile
ctr = shapefile.Reader('/home/rikl/Dokumente/Python/shapefile/Countries')

So, lets get to the geometry and the attributes

geomet = ctr.shapeRecords() #will store the geometry separately
first = geomet[0] #will extract the first polygon to a new object
first.shape.points #will show you the points of the polygon
first.record #will show you the attributes

This is so cool, I love it! Now we can go ahead and try to figure out, how to create new instances like attributes to a point shapefile. Each point will then represent a customer on our webmap…

4 2 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
Sowmya Rajendran
Sowmya Rajendran
7 years ago

I am getting an error message like “No module named shapefile”