Loading [MathJax]/extensions/tex2jax.js
DM_shape_file_read.py
1 ## @package python.demo.DM_shape_file_read
2 #
3 # \brief read a polyline shape file including an used-defined attribute table.
4 #
5 # Shape may contain points, polyline or polygon geometry objects. Furthermore, the corresponding
6 # dbf file contains an arbitrary number of attributes per geometry object which are imported as
7 # attribute. Per default all attributes are imported into an appropriate addinfo layout. Nevertheless,
8 # it is possible to import only a subset of attributes into a user-defined addinfo layout by the
9 # setAttributeMapping function as shown below.
10 #
11 import sys, os
12 from opals import pyDM
13 
14 def DM_read_shapefile(filename):
15 
16  # Create polygon and layout factory objects
17  pf = pyDM.PolygonFactory()
18  lf = pyDM.AddInfoLayoutFactory()
19  lf.addColumn(pyDM.ColumnType.uint32, '_shapeId') # internal shape object identifier. It's automatically set by the shape export
20  layout = lf.getLayout()
21 
22  # Read table definition for the shape file
23  tableDef = pyDM.ShapeTableDefinition.read(filename)
24 
25  # output table definition
26  print(f"Shape file {filename} contains {tableDef.columns()} attributes:")
27  print(f"idx name type width decimals")
28  for i in range(tableDef.columns()):
29  print(f" {i}\t{tableDef.name(i):16}{tableDef.type(i):10}{tableDef.width(i):10}{tableDef.decimales(i):10}")
30 
31  # Create shape export with the
32  print(f"Open shape file '{filename}'")
33  imp = pyDM.ImportShape(filename)
34 
35  # create mapping between dbf attributes and the defined layout (optional)
36  # per default all attributes will be imported
37  mapping = {tableDef.name(0): 0, } # import dbf attribute Id as column 0 of layout (_shapeId)
38  imp.setAttributeMapping(layout, mapping) # use attribute column 0 as automatically filled counter
39 
40  print("Read files...")
41  for idx, obj in enumerate(imp):
42  print(f" Geometry {idx} is a {obj.type()} and contains {obj.sizePoint()} points (_shapeId={obj.info().get(0)})")
43  for pt_idx, pt in enumerate(obj.points()):
44  print(f" {pt_idx:3}.point {pt.x:12.3f}, {pt.y:12.3f}, {pt.z:9.3f}")
45  print("Done")
46 
47 
48 if len(sys.argv) < 1:
49  print("shape file parameter missing")
50  sys.exit(-1)
51 
52 DM_read_shapefile(sys.argv[1])