source: inundation/pmesh/create_mesh.py @ 2299

Last change on this file since 2299 was 2178, checked in by sexton, 19 years ago

Improved doc string

File size: 4.6 KB
Line 
1"""Create mesh for lwru2 validation of the Oshiri island tsunami
2"""
3
4#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
5# Assume that the root AnuGA dir (inundation) is included in your pythonpath
6#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7
8#FIXME (DSG): move the coordinate transforms package to the root AnuGA dir
9from coordinate_transforms.geo_reference import Geo_reference
10from 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    Boundary tags is a dictionary of symbolic tags. For every tag there is a list of
77    indices referring to segments associated with that tag
78
79    Resolution is the maximal area per triangle for the bounding polygon
80    (excluding interior regions, see later)
81
82    Interior_regions is a list of tuples consisting of (polygon, resolution)
83    for each region to be separately refined.
84
85    This function does not allow segments to share points - use underlying
86    pmesh functionality for that
87
88    FIXME - FUTURE. Something like this.
89      Use class Point_set
90
91      Accept deg, min, sec, e.g.
92      [ [(-20,30,55), (116, 20, 00)], ...]
93
94
95
96   
97    """
98
99    #from pmesh.mesh import Mesh
100    from mesh import Mesh
101    from coordinate_transforms.redfearn import redfearn
102    from pyvolution.util import populate_polygon
103   
104
105    #Make georef
106    #FIXME: Pass in geo or create automatically somehow
107    import project
108    refzone = project.refzone  #FIXME
109    mesh_origin = project.mesh_origin
110    geo = Geo_reference(xllcorner = mesh_origin[1],  #From dem
111                        yllcorner = mesh_origin[2],
112                        zone = refzone)
113
114
115
116   
117    print "***********************"
118    print "geo ref", geo
119    print "***********************"
120   
121    m = Mesh(geo_reference=geo)
122
123
124
125    #Convert to UTM
126    bounding_polygon = convert_points_from_latlon_to_utm(bounding_polygon, refzone)   
127   
128   
129    #Do bounding polygon
130    region_dict = create_region(bounding_polygon, boundary_tags, refzone)   
131    m.addVertsSegs(region_dict)
132
133
134    #Find one point inside region automatically
135    if interior_regions is not None:
136        excluded_polygons = []       
137        for P, res in interior_regions:
138            polygon = convert_points_from_latlon_to_utm(P, refzone)           
139            excluded_polygons.append( polygon )
140    else:
141        excluded_polygons = None
142
143    from Numeric import array
144   
145    [inner_point] = populate_polygon(bounding_polygon, 1, exclude = excluded_polygons)
146    print 'I:', inner_point, resolution
147    inner = m.addRegionEN(inner_point[0], inner_point[1])
148    inner.setMaxArea(resolution)
149
150
151
152    #Do interior regions
153    if interior_regions is not None:   
154        for P, res in interior_regions:
155            polygon = convert_points_from_latlon_to_utm(P, refzone)                       
156            region_dict = create_region(polygon, None, refzone)
157
158            m.addVertsSegs(region_dict)
159
160            [inner_point] = populate_polygon(polygon, 1)
161            print 'I', inner_point, res
162            X = m.addRegionEN(inner_point[0], inner_point[1])
163            X.setMaxArea(res)
164           
165
166   
167    m.generateMesh('pzq28.0za1000000a')
168   
169    m.export_mesh_file(filename)
170   
171
172
173
174
175   
Note: See TracBrowser for help on using the repository browser.