source: production/karratha_2005/create_mesh.py @ 1989

Last change on this file since 1989 was 1989, checked in by ole, 18 years ago

Renaming of mesh generation and new refinements

File size: 4.4 KB
Line 
1"""Create mesh for lwru2 validation of the Oshiri island tsunami
2"""
3
4#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5# Assume that the root AnuGA dir is included in your pythonpath
6#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7
8
9from pyvolution.coordinate_transforms.geo_reference import Geo_reference
10from pyvolution.coordinate_transforms.redfearn import redfearn
11
12
13def convert_points_from_latlon_to_utm(polygon, refzone):
14    points = []
15    for point in polygon:
16        zone, easting, northing  = redfearn(point[0], point[1])
17        #FIXME: Use point.latitude etc once we have a proper point set
18        assert zone == refzone
19        points.append([easting, northing])
20
21    return points   
22
23
24
25
26
27def create_region(polygon, tags, refzone):
28    """Create dictionary representation of region bounded by given polygon for use with pmesh
29    """
30
31
32    #
33    dict = {}
34    dict['points'] = polygon
35
36    #Create segments
37    #E.g. [[0,1], [1,2], [2,3], [3,0]]
38    segments = []
39    N = len(polygon)
40    for i in range(N):
41        lo = i
42        hi = (lo + 1) % N
43        segments.append( [lo, hi] ) 
44       
45       
46    dict['segments'] = segments
47
48
49    #Create tags
50    #E.g. ['wall', 'wall', 'ocean', 'wall']
51
52    if tags is not None:
53        segment_tags = [0]*N
54        for key in tags:
55            indices = tags[key]
56            for i in indices:
57                segment_tags[i] = key
58   
59        dict['segment_tags'] = segment_tags
60
61
62
63    return dict
64
65
66
67def create_mesh_from_regions(bounding_polygon,
68                             boundary_tags,
69                             resolution,
70                             filename = None,
71                             interior_regions = None):
72    """Create mesh from bounding polygon, tags for all segments and resolution.
73
74    Polygon is a list of points in latitude and longitudes - decimal degrees
75
76
77    Tags is a list of symbolic tags
78
79    Resolution is the maximal area
80
81    Interior_regions is a list of tuples consisting of (polygon, resolution)
82
83    This function does not allow segments to share points - use underlying
84    pmesh functionality for that
85
86    FIXME - FUTURE:
87      Use class Point_set
88      Take multiple polygons
89
90      Accept deg, min, sec, e.g.
91      [ [(-20,30,55), (116, 20, 00)], ...]
92
93
94    FIXME: This whole thing needs to be completely rethought
95   
96    """
97
98    from pmesh.mesh import Mesh
99    from pyvolution.coordinate_transforms.redfearn import redfearn
100    from pyvolution.util import populate_polygon
101   
102
103    #Make georef
104    #FIXME: Pass in geo or create automatically somehow
105    import project
106    refzone = project.refzone  #FIXME
107    mesh_origin = project.mesh_origin
108    geo = Geo_reference(xllcorner = mesh_origin[1],  #From dem
109                        yllcorner = mesh_origin[2],
110                        zone = refzone)
111
112
113
114   
115    print "***********************"
116    print "geo ref", geo
117    print "***********************"
118   
119    m = Mesh(geo_reference=geo)
120
121
122
123    #Convert to UTM
124    bounding_polygon = convert_points_from_latlon_to_utm(bounding_polygon, refzone)   
125   
126   
127    #Do bounding polygon
128    region_dict = create_region(bounding_polygon, boundary_tags, refzone)   
129    m.addVertsSegs(region_dict)
130
131
132    #Find one point inside region automatically
133    if interior_regions is not None:
134        excluded_polygons = []       
135        for P, res in interior_regions:
136            polygon = convert_points_from_latlon_to_utm(P, refzone)           
137            excluded_polygons.append( polygon )
138    else:
139        excluded_polygons = None
140
141    from Numeric import array
142   
143    [inner_point] = populate_polygon(bounding_polygon, 1, exclude = excluded_polygons)
144    print 'I:', inner_point, resolution
145    inner = m.addRegionEN(inner_point[0], inner_point[1])
146    inner.setMaxArea(resolution)
147
148
149
150    #Do interior regions
151    if interior_regions is not None:   
152        for P, res in interior_regions:
153            polygon = convert_points_from_latlon_to_utm(P, refzone)                       
154            region_dict = create_region(polygon, None, refzone)
155
156            m.addVertsSegs(region_dict)
157
158            [inner_point] = populate_polygon(polygon, 1)
159            print 'I', inner_point, res
160            X = m.addRegionEN(inner_point[0], inner_point[1])
161            X.setMaxArea(res)
162           
163
164   
165    m.generateMesh('pzq28.0za1000000a')
166   
167    m.export_mesh_file(filename)
168   
169
170
171
172
173   
Note: See TracBrowser for help on using the repository browser.