Menu

[r5472]: / trunk / toolkits / basemap / pyshapelib / pytest.py  Maximize  Restore  History

Download this file

129 lines (105 with data), 4.5 kB

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