/[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 2734 - (hide annotations)
Thu Mar 1 12:42:59 2007 UTC (18 years ago) by bramz
File MIME type: text/x-python
File size: 4482 byte(s)
made a copy
1 jan 1611 import shapelib, dbflib, shptree
2    
3     #
4     # The the shapefile module
5     #
6    
7     def make_shapefile(filename):
8 bh 2637 # Create a shapefile with polygons
9     outfile = shapelib.create(filename, shapelib.SHPT_POLYGON)
10    
11     # Create one very simple polygon and write it to the shapefile. The
12     # vertices should be given in clockwise order to comply with the
13     # shapefile specification.
14 jan 1611 obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
15 bh 2637 [[(10, 10), (10, 20), (20, 20), (10, 10)]])
16 jan 1611 print obj.extents()
17     print obj.vertices()
18     outfile.write_object(-1, obj)
19    
20 bh 2637 # Create a polygon with a hole. Note that according to the
21     # shapefile specification, the vertices of the outer ring have to be
22     # in clockwise order and the inner rings have to be in counter
23     # clockwise order.
24     #
25     # There's an optional fourth parameter which when given must be a
26     # list of part types, one for each part of the shape. For polygons,
27     # the part type is always shapelib.SHPP_RING, though. The part
28     # types are only relevant for SHPT_MULTIPATCH shapefiles.
29     obj = shapelib.SHPObject(shapelib.SHPT_POLYGON, 1,
30     [[(0, 0), (0, 40), (40, 40), (40, 0), (0, 0)],
31     [(10, 10), (20, 10), (20, 20), (10, 20),(10, 10)],
32     ])
33     print obj.extents()
34     print obj.vertices()
35     outfile.write_object(-1, obj)
36    
37     # close the file.
38     outfile.close()
39    
40 jan 1611 def read_shapefile(filename):
41     # open the shapefile
42     shp = shapelib.ShapeFile(filename)
43    
44     # the info method returns a tuple (num_shapes, type, min, max) where
45     # num_shapes is the number of shapes, type is the type code (one of
46     # the SHPT* constants defined in the shapelib module) and min and
47     # max are 4-element lists with the min. and max. values of the
48     # vertices.
49     print shp.info()
50    
51     # read_object reads a shape
52     obj = shp.read_object(0)
53    
54     # The vertices method returns the shape as a list of lists of tuples.
55     print obj.vertices()[0][:10]
56    
57     # The extents returns a tuple with two 4-element lists with the min.
58     # and max. values of the vertices.
59     print obj.extents()
60    
61     # The type attribute is the type code (one of the SHPT* constants
62     # defined in the shapelib module)
63     print obj.type
64    
65     # The id attribute is the shape id
66     print obj.id
67    
68     # the cobject method returns a PyCObject containing the shapelib
69     # SHPHandle. This is useful for passing shapefile objects to
70     # C-Python extensions.
71     print shp.cobject()
72    
73     # build a quad tree from the shapefile. The first argument must be
74     # the return value of the shape file object's cobject method (this
75     # is currently needed to access the shape file at the C-level). The
76     # second argument is the dimension and the third the maximum depth.
77     # 0 means to guess an appropriate depth
78     tree = shptree.SHPTree(shp.cobject(), 2, 0)
79    
80     # Retrieve the ids for a region. Here we just use the extents of the
81     # object previously read from the shapefile
82     minima, maxima = obj.extents()
83     print tree.find_shapes(minima[:2], maxima[:2])
84    
85    
86     make_shapefile("testfile")
87     read_shapefile("testfile")
88    
89     #
90     # Test the DBF file module.
91     #
92    
93     def make_dbf(file):
94     # create a new dbf file and add three fields.
95     dbf = dbflib.create(file)
96     dbf.add_field("NAME", dbflib.FTString, 20, 0)
97     dbf.add_field("INT", dbflib.FTInteger, 10, 0)
98     dbf.add_field("FLOAT", dbflib.FTDouble, 10, 4)
99    
100     def add_dbf_records(file):
101     # add some records to file
102     dbf = dbflib.open(file, "r+b")
103     # Records can be added as a dictionary...
104     dbf.write_record(0, {'NAME': "Weatherwax", "INT":1, "FLOAT":3.1415926535})
105     # ... or as a sequence
106     dbf.write_record(1, ("Ogg", 2, -1000.1234))
107    
108     def list_dbf(file):
109     # print the contents of a dbf file to stdout
110     dbf = dbflib.DBFFile(file)
111     print "%d records, %d fields" % (dbf.record_count(), dbf.field_count())
112     format = ""
113     for i in range(dbf.field_count()):
114     type, name, len, decc = dbf.field_info(i)
115     if type == 0:
116     format = format + " %%(%s)%ds" % (name, len)
117     elif type == 1:
118     format = format + " %%(%s)%dd" % (name, len)
119     elif type == 2:
120     format = format + " %%(%s)%dg" % (name, len)
121     print format
122     for i in range(dbf.record_count()):
123     print format % dbf.read_record(i)
124    
125    
126     make_dbf("testfile")
127     add_dbf_records("testfile")
128     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