1 | """Create mesh for lwru2 validation of the Oshiri island tsunami |
---|
2 | """ |
---|
3 | |
---|
4 | # Assume that the root AnuGA dir (inundation) is included in your pythonpath |
---|
5 | |
---|
6 | from coordinate_transforms.redfearn import redfearn |
---|
7 | |
---|
8 | def convert_points_from_latlon_to_utm(polygon, refzone=None): |
---|
9 | points = [] |
---|
10 | for point in polygon: |
---|
11 | zone, easting, northing = redfearn(point[0], point[1]) |
---|
12 | #FIXME: Use point.latitude etc once we have a proper point set |
---|
13 | |
---|
14 | if refzone is None: |
---|
15 | refzone = zone |
---|
16 | else: |
---|
17 | assert zone == refzone |
---|
18 | |
---|
19 | points.append([easting, northing]) |
---|
20 | |
---|
21 | return points, zone |
---|
22 | |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | def create_region(polygon, tags, refzone): |
---|
27 | """Create dictionary representation of region bounded by given polygon for use with pmesh |
---|
28 | """ |
---|
29 | |
---|
30 | |
---|
31 | # |
---|
32 | dict = {} |
---|
33 | dict['points'] = polygon |
---|
34 | |
---|
35 | #Create segments |
---|
36 | #E.g. [[0,1], [1,2], [2,3], [3,0]] |
---|
37 | segments = [] |
---|
38 | N = len(polygon) |
---|
39 | for i in range(N): |
---|
40 | lo = i |
---|
41 | hi = (lo + 1) % N |
---|
42 | segments.append( [lo, hi] ) |
---|
43 | |
---|
44 | |
---|
45 | dict['segments'] = segments |
---|
46 | |
---|
47 | |
---|
48 | #Create tags |
---|
49 | #E.g. ['wall', 'wall', 'ocean', 'wall'] |
---|
50 | |
---|
51 | if tags is not None: |
---|
52 | segment_tags = [0]*N |
---|
53 | for key in tags: |
---|
54 | indices = tags[key] |
---|
55 | for i in indices: |
---|
56 | segment_tags[i] = key |
---|
57 | |
---|
58 | dict['segment_tags'] = segment_tags |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | return dict |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | def create_mesh_from_regions(bounding_polygon, |
---|
67 | boundary_tags, |
---|
68 | resolution, |
---|
69 | filename = None, |
---|
70 | interior_regions = None, |
---|
71 | UTM = False, |
---|
72 | refzone = None): |
---|
73 | """Create mesh from bounding polygon, tags for all segments and resolution. |
---|
74 | |
---|
75 | Polygon is a list of points in latitude and longitudes - decimal degrees |
---|
76 | |
---|
77 | Boundary tags is a dictionary of symbolic tags. For every tag there is a list of |
---|
78 | indices referring to segments associated with that tag |
---|
79 | |
---|
80 | FIXME (Howard): Not clear what the keys and values of the dictionary are. |
---|
81 | |
---|
82 | Resolution is the maximal area per triangle for the bounding polygon |
---|
83 | (excluding interior regions, see later) |
---|
84 | |
---|
85 | Interior_regions is a list of tuples consisting of (polygon, resolution) |
---|
86 | for each region to be separately refined. |
---|
87 | |
---|
88 | This function does not allow segments to share points - use underlying |
---|
89 | pmesh functionality for that |
---|
90 | |
---|
91 | FIXME - FUTURE. Something like this. |
---|
92 | Use class Point_set |
---|
93 | |
---|
94 | Accept deg, min, sec, e.g. |
---|
95 | [ [(-20,30,55), (116, 20, 00)], ...] |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | """ |
---|
101 | print "OBSOLETE - USE MESH_INTERFACE.PY" |
---|
102 | from mesh import Mesh |
---|
103 | from coordinate_transforms.redfearn import redfearn |
---|
104 | from utilities.polygon import populate_polygon |
---|
105 | from utilities.numerical_tools import ensure_numeric |
---|
106 | from coordinate_transforms.geo_reference import Geo_reference |
---|
107 | |
---|
108 | |
---|
109 | bounding_polygon = ensure_numeric(bounding_polygon) |
---|
110 | #print 'refzone', refzone |
---|
111 | |
---|
112 | #Convert to UTM |
---|
113 | if not UTM: |
---|
114 | bounding_polygon, refzone = convert_points_from_latlon_to_utm(bounding_polygon) |
---|
115 | else: |
---|
116 | assert refzone is not None, 'Refzone must be specified, got %s' %refzone |
---|
117 | |
---|
118 | |
---|
119 | #FIXME: Must give georeference here. |
---|
120 | #Why can't Mesh work out the ll corners? |
---|
121 | |
---|
122 | xllcorner = min(bounding_polygon[:,0]) |
---|
123 | yllcorner = min(bounding_polygon[:,1]) |
---|
124 | |
---|
125 | geo = Geo_reference(xllcorner = xllcorner, |
---|
126 | yllcorner = yllcorner, |
---|
127 | zone = refzone) |
---|
128 | |
---|
129 | print 'geo reference derived from bounding polygon', geo |
---|
130 | m = Mesh(geo_reference=geo) |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | #Do bounding polygon |
---|
135 | region_dict = create_region(bounding_polygon, boundary_tags, refzone) |
---|
136 | m.addVertsSegs(region_dict) |
---|
137 | |
---|
138 | |
---|
139 | #Find one point inside region automatically |
---|
140 | if interior_regions is not None: |
---|
141 | excluded_polygons = [] |
---|
142 | for P, res in interior_regions: |
---|
143 | if not UTM: |
---|
144 | polygon, _ = convert_points_from_latlon_to_utm(P, refzone) |
---|
145 | else: |
---|
146 | polygon = P |
---|
147 | excluded_polygons.append( polygon ) |
---|
148 | else: |
---|
149 | excluded_polygons = None |
---|
150 | |
---|
151 | from Numeric import array |
---|
152 | |
---|
153 | [inner_point] = populate_polygon(bounding_polygon, 1, exclude = excluded_polygons) |
---|
154 | print 'I:', inner_point, resolution |
---|
155 | inner = m.addRegionEN(inner_point[0], inner_point[1]) |
---|
156 | inner.setMaxArea(resolution) |
---|
157 | |
---|
158 | |
---|
159 | |
---|
160 | #Do interior regions |
---|
161 | if interior_regions is not None: |
---|
162 | for P, res in interior_regions: |
---|
163 | if not UTM: |
---|
164 | polygon, _ = convert_points_from_latlon_to_utm(P, refzone) |
---|
165 | else: |
---|
166 | polygon = P |
---|
167 | |
---|
168 | region_dict = create_region(polygon, None, refzone) |
---|
169 | |
---|
170 | m.addVertsSegs(region_dict) |
---|
171 | |
---|
172 | [inner_point] = populate_polygon(polygon, 1) |
---|
173 | print 'I', inner_point, res |
---|
174 | X = m.addRegionEN(inner_point[0], inner_point[1]) |
---|
175 | X.setMaxArea(res) |
---|
176 | |
---|
177 | |
---|
178 | |
---|
179 | m.generateMesh('pzq28.0za1000000a') |
---|
180 | |
---|
181 | m.export_mesh_file(filename) |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|