1 | |
---|
2 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
3 | # Assume that the root AnuGA dir (inundation) is included in your pythonpath |
---|
4 | #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
---|
5 | |
---|
6 | from coordinate_transforms.geo_reference import Geo_reference,DEFAULT_ZONE |
---|
7 | from utilities.polygon import populate_polygon |
---|
8 | from utilities.numerical_tools import ensure_numeric |
---|
9 | from Numeric import Float |
---|
10 | from utilities.polygon import inside_polygon |
---|
11 | |
---|
12 | # This is due to pmesh being a package and a module and |
---|
13 | # the current dir being unknown |
---|
14 | try: |
---|
15 | from pmesh.mesh import Mesh |
---|
16 | except ImportError: |
---|
17 | from mesh import Mesh |
---|
18 | |
---|
19 | |
---|
20 | def create_mesh_from_regions(bounding_polygon, |
---|
21 | boundary_tags, |
---|
22 | maximum_triangle_area, |
---|
23 | filename=None, |
---|
24 | interior_regions=None, |
---|
25 | poly_geo_reference=None, |
---|
26 | mesh_geo_reference=None, |
---|
27 | minimum_triangle_angle=28.0): |
---|
28 | """Create mesh from bounding polygons, and resolutions. |
---|
29 | |
---|
30 | bounding_polygon is a list of points in Eastings and Northings, |
---|
31 | relative to the poly_geo_reference. |
---|
32 | |
---|
33 | Boundary tags is a dictionary of symbolic tags. For every tag there |
---|
34 | is a list of indices referring to segments associated with that tag |
---|
35 | |
---|
36 | Resolution is the maximal area per triangle for the bounding polygon |
---|
37 | (excluding interior regions, see later) |
---|
38 | |
---|
39 | Interior_regions is a list of tuples consisting of (polygon, resolution) |
---|
40 | for each region to be separately refined. |
---|
41 | |
---|
42 | This function does not allow segments to share points - use underlying |
---|
43 | pmesh functionality for that |
---|
44 | |
---|
45 | poly_geo_reference is the geo_reference of the polygons. |
---|
46 | If none, assume absolute. Please pass one though, since absolute |
---|
47 | references have a zone. |
---|
48 | |
---|
49 | mesh_geo_reference is the geo_reference of the mesh to be created. |
---|
50 | If none is given one will be automatically generated. It was use |
---|
51 | the lower left hand corner of bounding_polygon (absolute) |
---|
52 | as the x and y values for the geo_ref. |
---|
53 | |
---|
54 | Returns the mesh instance if no finename is given |
---|
55 | |
---|
56 | Note, interior regions should be fully nested, as overlaps may cause |
---|
57 | unintended resolutions. |
---|
58 | |
---|
59 | """ |
---|
60 | #FIXME (OLE-DSG) |
---|
61 | # To do make maximum_triangle_area optional? |
---|
62 | # check the segment indexes - throw an error if they are out of bounds |
---|
63 | # |
---|
64 | #In addition I reckon the polygons could be of class Geospatial_data |
---|
65 | |
---|
66 | |
---|
67 | # First check that interior polygons are fully contained in bounding polygon |
---|
68 | |
---|
69 | # FIXME (Ole): This causes the tests to fail because coordinates have been |
---|
70 | # converted to relative coordinates (I think). How can we simplify this? |
---|
71 | # |
---|
72 | #FIXME (DSG-DSG) this code does not take into account geo-referencing |
---|
73 | #if interior_regions is not None: |
---|
74 | |
---|
75 | # for interior_polygon, res in interior_regions: |
---|
76 | # indices = inside_polygon(interior_polygon, bounding_polygon, |
---|
77 | # closed = True, verbose = False) |
---|
78 | |
---|
79 | # if len(indices) <> len(interior_polygon): |
---|
80 | # msg = 'Interior polygon %s is outside bounding polygon: %s'\ |
---|
81 | # %(str(interior_polygon), str(bounding_polygon)) |
---|
82 | # raise msg |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | # Resolve geo referencing |
---|
87 | if mesh_geo_reference is None: |
---|
88 | bounding_polygon = ensure_numeric(bounding_polygon, Float) |
---|
89 | xllcorner = min(bounding_polygon[:,0]) |
---|
90 | yllcorner = min(bounding_polygon[:,1]) |
---|
91 | # |
---|
92 | if poly_geo_reference is None: |
---|
93 | zone = DEFAULT_ZONE |
---|
94 | else: |
---|
95 | zone = poly_geo_reference.get_zone() |
---|
96 | [(xllcorner,yllcorner)] = poly_geo_reference.get_absolute( \ |
---|
97 | [(xllcorner,yllcorner)]) |
---|
98 | # create a geo_ref, based on the llc of the bounding_polygon |
---|
99 | mesh_geo_reference = Geo_reference(xllcorner = xllcorner, |
---|
100 | yllcorner = yllcorner, |
---|
101 | zone = zone) |
---|
102 | |
---|
103 | m = Mesh(geo_reference=mesh_geo_reference) |
---|
104 | |
---|
105 | # Do bounding polygon |
---|
106 | m.add_region_from_polygon(bounding_polygon, |
---|
107 | tags=boundary_tags, |
---|
108 | geo_reference=poly_geo_reference) |
---|
109 | |
---|
110 | # Find one point inside region automatically |
---|
111 | if interior_regions is not None: |
---|
112 | excluded_polygons = [] |
---|
113 | for polygon, res in interior_regions: |
---|
114 | excluded_polygons.append( polygon ) |
---|
115 | else: |
---|
116 | excluded_polygons = None |
---|
117 | |
---|
118 | from Numeric import array |
---|
119 | |
---|
120 | |
---|
121 | # Convert bounding poly to absolute values |
---|
122 | # this sort of thing can be fixed with the geo_points class |
---|
123 | if poly_geo_reference is not None: |
---|
124 | bounding_polygon_absolute = \ |
---|
125 | poly_geo_reference.get_absolute(bounding_polygon) |
---|
126 | else: |
---|
127 | bounding_polygon_absolute = bounding_polygon |
---|
128 | |
---|
129 | [inner_point] = populate_polygon(bounding_polygon_absolute, |
---|
130 | 1, exclude = excluded_polygons) |
---|
131 | inner = m.add_region(inner_point[0], inner_point[1]) |
---|
132 | inner.setMaxArea(maximum_triangle_area) |
---|
133 | |
---|
134 | # Do interior regions |
---|
135 | if interior_regions is not None: |
---|
136 | for polygon, res in interior_regions: |
---|
137 | #polygon = convert_points_from_latlon_to_utm(P, refzone) |
---|
138 | m.add_region_from_polygon(polygon, |
---|
139 | geo_reference=poly_geo_reference) |
---|
140 | # convert bounding poly to absolute values |
---|
141 | if poly_geo_reference is not None: |
---|
142 | polygon_absolute = \ |
---|
143 | poly_geo_reference.get_absolute(polygon) |
---|
144 | else: |
---|
145 | polygon_absolute = polygon |
---|
146 | [inner_point] = populate_polygon(polygon_absolute, 1) |
---|
147 | region = m.add_region(inner_point[0], inner_point[1]) |
---|
148 | region.setMaxArea(res) |
---|
149 | |
---|
150 | |
---|
151 | if filename is None: |
---|
152 | return m |
---|
153 | else: |
---|
154 | m.generate_mesh(minimum_triangle_angle=minimum_triangle_angle, |
---|
155 | maximum_triangle_area=maximum_triangle_area) |
---|
156 | m.export_mesh_file(filename) |
---|
157 | |
---|
158 | |
---|