Loading [MathJax]/extensions/tex2jax.js
DM_geometry_ogr_wkt.py
1 ## @package python.demo.DM_geometry_ogr_wkt
2 #
3 # Example script showing the bidirectional conversion of pyDM and OGR geometry objects. This can be used to get WKT
4 # representation (https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry) of pyDM geometry objects.
5 #
6 
7 import random, sys
8 
9 from opals import pyDM
10 from osgeo import ogr
11 
12 
13 def point_conversion():
14  print("-- point conversion test ---------------------------------")
15  # create ogr point
16  ogrPt = ogr.Geometry(ogr.wkbPoint)
17  ogrPt.AddPoint(*[23.12, -5.21, 2.02])
18  print(ogrPt) # prints wkt representation
19 
20  dmPt = pyDM.OGRConverter.convert(ogrPt) # convert ogr point to pyDM.Point
21  print(f"{type(dmPt)} ({dmPt.x} {dmPt.y} {dmPt.z})")
22 
23  ogrPtConv = pyDM.OGRConverter.convert(dmPt) # convert pyDM.Point to ogr point
24  print(ogrPtConv) # print wkt representation again
25 
26 
27 def polyine_conversion():
28 
29  def ouput(titel, line):
30  print(f"{titel} {type(line)} has {line.sizePoint()} point(s) in {line.sizePart()} part(s).")
31 
32  print("-- polyine conversion test -------------------------------")
33 
34  # create ogr linestring and multi linestring object
35  # whereas simple lines are represented as LINESTRING, multi part lines need to be described as MULTILINESTRING
36  # for more detail see https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry
37  # In pyDM both cases are represented by the Polyline object class
38  ogrLine = ogr.CreateGeometryFromWkt("LINESTRING (0 0, 5 5, 20 -3)")
39  ogrMultiLine = ogr.CreateGeometryFromWkt("MULTILINESTRING ((0 0, 5 5, 20 -3), (0 -3, -5 -3))")
40 
41  # prints wkt representation
42  print(ogrLine)
43  print(ogrMultiLine)
44 
45  # convert ogr line and multiline objects
46  dmLine = pyDM.OGRConverter.convert(ogrLine) # convert ogr line to pyDM.Polyline
47  dmMultiLine = pyDM.OGRConverter.convert(ogrMultiLine) # convert ogr multiline to pyDM.Polyline
48 
49  ouput("dmLine: ", dmLine)
50  ouput("dmMultiLine: ", dmMultiLine)
51 
52  ogrLineConv = pyDM.OGRConverter.convert(dmLine) # convert pyDM.Polyline or ogr line
53  ogrMultiLineConv = pyDM.OGRConverter.convert(dmMultiLine) # convert pyDM.Polyline or ogr multiline
54 
55  # print wkt representation of converted objects
56  print(ogrLineConv)
57  print(ogrMultiLineConv)
58 
59 def polygon_conversion():
60 
61  def output(titel, poly):
62  print(f"{titel} {type(poly)} has {poly.sizePoint()} point(s) in {poly.sizeFace()} faces(s).")
63 
64  print("-- polygon conversion test -------------------------------")
65 
66  # create ogr polygon and mulipolygon object
67  # an ogr polygon object describes a connected region in space that may have an arbitrary number of holes.
68  # multiple non-connected regions or nested polygons (island within hole) require multi polygon representation.
69  # in pyDM all cases are represented by the Polygon object class
70  ogrPolySimple = ogr.CreateGeometryFromWkt("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))")
71  ogrPolyWithHoles = ogr.CreateGeometryFromWkt("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0),(2 2, 4 2, 4 4, 2 4, 2 2),"\
72  "(5 5, 8 5, 8 8, 5 8, 5 5))")
73  ogrPolyNested = ogr.CreateGeometryFromWkt("MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0),(2 2, 4 2, 4 4, 2 4, 2 2),"\
74  "(5 5, 8 5, 8 8, 5 8, 5 5)), ((6 6, 7 6, 7 7, 6 7, 6 6)))")
75  ogrPolyComplex = ogr.CreateGeometryFromWkt("MULTIPOLYGON (((0 0, 10 0, 10 10, 0 10, 0 0),(5 5, 8 5, 8 8, 5 8, 5 5)),"\
76  "((6 6, 7 6, 7 7, 6 7, 6 6)), ((-3 -5, -1 -5, -1 5, -3 5, -3 -5)))")
77 
78  # prints wkt representation
79  print(ogrPolySimple)
80  print(ogrPolyWithHoles)
81  print(ogrPolyNested)
82  print(ogrPolyComplex)
83 
84  # convert ogr (multi)polygon to pyDM.Polygon
85  dmPolySimple = pyDM.OGRConverter.convert(ogrPolySimple)
86  dmPolyWithHoles = pyDM.OGRConverter.convert(ogrPolyWithHoles)
87  dmPolyNested = pyDM.OGRConverter.convert(ogrPolyNested)
88  dmPolyComplex = pyDM.OGRConverter.convert(ogrPolyComplex)
89 
90  output("dmPolySimple: ", dmPolySimple)
91  output("dmPolyWithHoles: ", dmPolyWithHoles)
92  output("dmPolyNested: ", dmPolyNested)
93  output("dmPolyComplex: ", dmPolyComplex)
94 
95  # convert pyDM.Polygon to ogr (multi)polygon
96  ogrPolySimpleConv = pyDM.OGRConverter.convert(dmPolySimple)
97  ogrPolyWithHolesConv = pyDM.OGRConverter.convert(dmPolyWithHoles)
98  ogrPolyNestedConv = pyDM.OGRConverter.convert(dmPolyNested)
99  ogrPolyComplexConv = pyDM.OGRConverter.convert(dmPolyComplex)
100 
101  # prints wkt representation
102  print(ogrPolySimpleConv)
103  print(ogrPolyWithHolesConv)
104  print(ogrPolyNestedConv)
105  print(ogrPolyComplexConv)
106 
107 
108 # convert different geometries
109 point_conversion()
110 polyine_conversion()
111 polygon_conversion()