Geography Reference
In-Depth Information
# Set the feature geometry using the point
-
feature.SetGeometry(point)
-
# Create the feature in the layer (shapefile)
20
layer.CreateFeature(feature)
-
# Destroy the feature to free resources
-
feature.Destroy()
-
-
25
# Destroy the data source to free resources
data_source.Destroy()
-
In line 2, we began reading the text file to create the features, one for
each line in the file. For each line we must create a feature object (line 4)
and then set the values for each of the fields in lines 6 through 10. Since
we chose to use the DictReader , we can access the values for each field
by name to set the values. This takes care of the attributes—all that's
left is to create the geometry from the latitude and longitude values.
To create the geometry, we create a WKT representation of the point
using the values of the latitude and longitude fields. In line 13, the
Python string-formatting feature is used to easily create the WKT for
the point in the form of POINT(x y) . Using the WKT, the point feature
is created in line 16. The last step to get the entire feature ready is
to set the geometry, as shown in line 19. The feature (attributes and
geometry) is now complete and can be added to the shapefile using
CreateFeature ( ), as shown in line 21. The last step before moving on to
the next line in the text file is to destroy the local feature object to free
up resources (line 23).
Once all the lines in the text file are processed, the data source is
“destroyed” to close everything up cleanly. When it's run, the script
produces the following files:
volcanoes.dbf
volcanoes.prj
volcanoes.shp
volcanoes.shx
If you look at the . prj file, you'll see that it contains the projection infor-
mation for WGS84. Just to prove it works, the volcano shapefile dis-
played over the world_borders shapefile is shown in Figure 13.4 , on the
next page.
You can probably envision even more clever and perhaps complicated
scripts using both the GDAL/OGR utilities and bindings. There are
GDAL/OGR bindings for Perl, Python, Ruby, Java, and C#/.NET, plus
a couple of other languages. This gives you the option of writing scripts
 
Search WWH ::




Custom Search