[1784] | 1 | """Create mesh for lwru2 validation of the Oshiri island tsunami |
---|
| 2 | """ |
---|
| 3 | |
---|
[1990] | 4 | # Assume that the root AnuGA dir (inundation) is included in your pythonpath |
---|
[1784] | 5 | |
---|
[1991] | 6 | from coordinate_transforms.redfearn import redfearn |
---|
[1832] | 7 | |
---|
[2349] | 8 | def convert_points_from_latlon_to_utm(polygon, refzone=None): |
---|
[1855] | 9 | points = [] |
---|
| 10 | for point in polygon: |
---|
[1989] | 11 | zone, easting, northing = redfearn(point[0], point[1]) |
---|
| 12 | #FIXME: Use point.latitude etc once we have a proper point set |
---|
[2349] | 13 | |
---|
| 14 | if refzone is None: |
---|
[2376] | 15 | refzone = zone |
---|
[2349] | 16 | else: |
---|
| 17 | assert zone == refzone |
---|
| 18 | |
---|
[1855] | 19 | points.append([easting, northing]) |
---|
[1796] | 20 | |
---|
[2349] | 21 | return points, zone |
---|
[1855] | 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 | |
---|
[1989] | 66 | def create_mesh_from_regions(bounding_polygon, |
---|
| 67 | boundary_tags, |
---|
| 68 | resolution, |
---|
| 69 | filename = None, |
---|
[2376] | 70 | interior_regions = None, |
---|
| 71 | UTM = False, |
---|
| 72 | refzone = None): |
---|
[1832] | 73 | """Create mesh from bounding polygon, tags for all segments and resolution. |
---|
[1784] | 74 | |
---|
[1832] | 75 | Polygon is a list of points in latitude and longitudes - decimal degrees |
---|
[1784] | 76 | |
---|
[2178] | 77 | Boundary tags is a dictionary of symbolic tags. For every tag there is a list of |
---|
[2172] | 78 | indices referring to segments associated with that tag |
---|
[1832] | 79 | |
---|
[2596] | 80 | FIXME (Howard): Not clear what the keys and values of the dictionary are. |
---|
| 81 | |
---|
[2172] | 82 | Resolution is the maximal area per triangle for the bounding polygon |
---|
| 83 | (excluding interior regions, see later) |
---|
[1832] | 84 | |
---|
[1855] | 85 | Interior_regions is a list of tuples consisting of (polygon, resolution) |
---|
[2172] | 86 | for each region to be separately refined. |
---|
[1855] | 87 | |
---|
| 88 | This function does not allow segments to share points - use underlying |
---|
| 89 | pmesh functionality for that |
---|
| 90 | |
---|
[1990] | 91 | FIXME - FUTURE. Something like this. |
---|
[1837] | 92 | Use class Point_set |
---|
[1784] | 93 | |
---|
[1832] | 94 | Accept deg, min, sec, e.g. |
---|
| 95 | [ [(-20,30,55), (116, 20, 00)], ...] |
---|
[1855] | 96 | |
---|
| 97 | |
---|
[1990] | 98 | |
---|
[1832] | 99 | |
---|
| 100 | """ |
---|
[2606] | 101 | print "OBSOLETE - USE MESH_INTERFACE.PY" |
---|
[1990] | 102 | from mesh import Mesh |
---|
[1991] | 103 | from coordinate_transforms.redfearn import redfearn |
---|
[2351] | 104 | from utilities.polygon import populate_polygon |
---|
[2376] | 105 | from utilities.numerical_tools import ensure_numeric |
---|
| 106 | from coordinate_transforms.geo_reference import Geo_reference |
---|
[1832] | 107 | |
---|
| 108 | |
---|
[2376] | 109 | bounding_polygon = ensure_numeric(bounding_polygon) |
---|
| 110 | #print 'refzone', refzone |
---|
| 111 | |
---|
[1855] | 112 | #Convert to UTM |
---|
[2376] | 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]) |
---|
[1784] | 124 | |
---|
[2376] | 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) |
---|
[1855] | 131 | |
---|
[2376] | 132 | |
---|
| 133 | |
---|
[1855] | 134 | #Do bounding polygon |
---|
| 135 | region_dict = create_region(bounding_polygon, boundary_tags, refzone) |
---|
| 136 | m.addVertsSegs(region_dict) |
---|
[1832] | 137 | |
---|
| 138 | |
---|
[1855] | 139 | #Find one point inside region automatically |
---|
| 140 | if interior_regions is not None: |
---|
| 141 | excluded_polygons = [] |
---|
| 142 | for P, res in interior_regions: |
---|
[2376] | 143 | if not UTM: |
---|
| 144 | polygon, _ = convert_points_from_latlon_to_utm(P, refzone) |
---|
| 145 | else: |
---|
| 146 | polygon = P |
---|
[1855] | 147 | excluded_polygons.append( polygon ) |
---|
| 148 | else: |
---|
| 149 | excluded_polygons = None |
---|
[1832] | 150 | |
---|
[1855] | 151 | from Numeric import array |
---|
[1784] | 152 | |
---|
[1855] | 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) |
---|
[1784] | 157 | |
---|
| 158 | |
---|
[1855] | 159 | |
---|
| 160 | #Do interior regions |
---|
| 161 | if interior_regions is not None: |
---|
| 162 | for P, res in interior_regions: |
---|
[2376] | 163 | if not UTM: |
---|
| 164 | polygon, _ = convert_points_from_latlon_to_utm(P, refzone) |
---|
| 165 | else: |
---|
| 166 | polygon = P |
---|
| 167 | |
---|
[1855] | 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 | |
---|
[1784] | 178 | |
---|
[1832] | 179 | m.generateMesh('pzq28.0za1000000a') |
---|
| 180 | |
---|
| 181 | m.export_mesh_file(filename) |
---|
| 182 | |
---|
[1784] | 183 | |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | |
---|
| 187 | |
---|