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…

Published by Riccardo

In 2008 I finished my degree in geography and mathematics at the FU Berlin in Berlin. After a short stay in science as a research assistant I am a project manager in the geo-industry.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Sowmya Rajendran Recent comment authors

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

  Subscribe  
newest oldest most voted
Notify of
Sowmya Rajendran
Guest
Sowmya Rajendran

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