Geography Reference
In-Depth Information
Creating a Shapefile from Delimited Text
While the GDAL/OGR utilities provide you with a lot of capability,
sometimes you may need to dig a little deeper. In this example, we'll
use Python with OGR to create a shapefile from the volcanoes dataset.
In Section 8.2 , Importing Data, on page 122 , we used the delimited text
plugin to import the volcano data into QGIS and display it and then
save it to a shapefile. That works well, but suppose you have a lot of
data to process. In that case, writing a script to do the work is not only
quicker but more flexible.
Before we get started, we need to make sure that the Python bindings
for GDAL/OGR are present. This is easy to test using the Python Inter-
preter:
$ python
Python 2.5.1 (r251:54863, Oct 5 2007, 13:50:07)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ogr
>>>
If you get the prompt back with no errors, you are good to go. If not, it
means that your GDAL/OGR install doesn't include the Python bind-
ings. If you built from source, you'll have to go back and recompile with
the --with-python option. If you don't have them, a quick way to get the
needed bindings for Linux or Windows is to use FWTools. 9
Our script will take the following steps to get from delimited text to the
shapefile:
1. Import the needed modules.
2. Open the delimited text file.
3. Create the shapefile.
4. Add the fields to the shapefile.
5. Read the text file and populate the attributes and geometry for
each row.
6. Close the shapefile.
9.
http://fwtools.maptools.org
 
 
Search WWH ::




Custom Search