/[thuban]/branches/WIP-pyshapelib-bramz/libraries/pyshapelib/pytest.py
ViewVC logotype

Annotation of /branches/WIP-pyshapelib-bramz/libraries/pyshapelib/pytest.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1611 - (hide annotations)
Tue Aug 19 21:24:20 2003 UTC (21 years, 6 months ago) by jan
Original Path: trunk/thuban/libraries/pyshapelib/pytest.py
File MIME type: text/x-python
File size: 3407 byte(s)
These files have been moved here from thuban/extensions/pyshapelib/
See there in the Attic for the older history.

1 jan 1611 import shapelib, dbflib, shptree
2    
3     #
4     # The the shapefile module
5     #
6    
7     def make_shapefile(filename):
8     obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
9     [[(10, 10), (20, 10), (20, 20), (10, 10)]])
10     print obj.extents()
11     print obj.vertices()
12     outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)
13     outfile.write_object(-1, obj)
14     del outfile
15    
16     def read_shapefile(filename):
17     # open the shapefile
18     shp = shapelib.ShapeFile(filename)
19    
20     # the info method returns a tuple (num_shapes, type, min, max) where
21     # num_shapes is the number of shapes, type is the type code (one of
22     # the SHPT* constants defined in the shapelib module) and min and
23     # max are 4-element lists with the min. and max. values of the
24     # vertices.
25     print shp.info()
26    
27     # read_object reads a shape
28     obj = shp.read_object(0)
29    
30     # The vertices method returns the shape as a list of lists of tuples.
31     print obj.vertices()[0][:10]
32    
33     # The extents returns a tuple with two 4-element lists with the min.
34     # and max. values of the vertices.
35     print obj.extents()
36    
37     # The type attribute is the type code (one of the SHPT* constants
38     # defined in the shapelib module)
39     print obj.type
40    
41     # The id attribute is the shape id
42     print obj.id
43    
44     # the cobject method returns a PyCObject containing the shapelib
45     # SHPHandle. This is useful for passing shapefile objects to
46     # C-Python extensions.
47     print shp.cobject()
48    
49     # build a quad tree from the shapefile. The first argument must be
50     # the return value of the shape file object's cobject method (this
51     # is currently needed to access the shape file at the C-level). The
52     # second argument is the dimension and the third the maximum depth.
53     # 0 means to guess an appropriate depth
54     tree = shptree.SHPTree(shp.cobject(), 2, 0)
55    
56     # Retrieve the ids for a region. Here we just use the extents of the
57     # object previously read from the shapefile
58     minima, maxima = obj.extents()
59     print tree.find_shapes(minima[:2], maxima[:2])
60    
61    
62     make_shapefile("testfile")
63     read_shapefile("testfile")
64    
65     #
66     # Test the DBF file module.
67     #
68    
69     def make_dbf(file):
70     # create a new dbf file and add three fields.
71     dbf = dbflib.create(file)
72     dbf.add_field("NAME", dbflib.FTString, 20, 0)
73     dbf.add_field("INT", dbflib.FTInteger, 10, 0)
74     dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
75    
76     def add_dbf_records(file):
77     # add some records to file
78     dbf = dbflib.open(file, "r+b")
79     # Records can be added as a dictionary...
80     dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535})
81     # ... or as a sequence
82     dbf.write_record(1, ("Ogg", 2, -1000.1234))
83    
84     def list_dbf(file):
85     # print the contents of a dbf file to stdout
86     dbf = dbflib.DBFFile(file)
87     print "%d records, %d fields" % (dbf.record_count(), dbf.field_count())
88     format = ""
89     for i in range(dbf.field_count()):
90     type, name, len, decc = dbf.field_info(i)
91     if type == 0:
92     format = format + " %%(%s)%ds" % (name, len)
93     elif type == 1:
94     format = format + " %%(%s)%dd" % (name, len)
95     elif type == 2:
96     format = format + " %%(%s)%dg" % (name, len)
97     print format
98     for i in range(dbf.record_count()):
99     print format % dbf.read_record(i)
100    
101    
102     make_dbf("testfile")
103     add_dbf_records("testfile")
104     list_dbf("testfile")

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26