source: inundation/pmesh/create_mesh.py @ 2378

Last change on this file since 2378 was 2376, checked in by ole, 19 years ago

Geo

File size: 4.9 KB
RevLine 
[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]6from coordinate_transforms.redfearn import redfearn
[1832]7
[2349]8def 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
26def 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]66def 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
[2172]80    Resolution is the maximal area per triangle for the bounding polygon
81    (excluding interior regions, see later)
[1832]82
[1855]83    Interior_regions is a list of tuples consisting of (polygon, resolution)
[2172]84    for each region to be separately refined.
[1855]85
86    This function does not allow segments to share points - use underlying
87    pmesh functionality for that
88
[1990]89    FIXME - FUTURE. Something like this.
[1837]90      Use class Point_set
[1784]91
[1832]92      Accept deg, min, sec, e.g.
93      [ [(-20,30,55), (116, 20, 00)], ...]
[1855]94
95
[1990]96
[1832]97   
98    """
[1784]99
[1990]100    from mesh import Mesh
[1991]101    from coordinate_transforms.redfearn import redfearn
[2351]102    from utilities.polygon import populate_polygon
[2376]103    from utilities.numerical_tools import ensure_numeric
104    from coordinate_transforms.geo_reference import Geo_reference
[1832]105
106
[2376]107    bounding_polygon = ensure_numeric(bounding_polygon)
108    #print 'refzone', refzone
109
[1855]110    #Convert to UTM
[2376]111    if not UTM:
112        bounding_polygon, refzone = convert_points_from_latlon_to_utm(bounding_polygon)
113    else:
114       assert refzone is not None, 'Refzone must be specified, got %s' %refzone 
115
116
117    #FIXME: Must give georeference here.
118    #Why can't Mesh work out the ll corners?
119
120    xllcorner = min(bounding_polygon[:,0])
121    yllcorner = min(bounding_polygon[:,1])   
[1784]122   
[2376]123    geo = Geo_reference(xllcorner = xllcorner,
124                        yllcorner = yllcorner,
125                        zone = refzone)
126
127    print 'geo reference derived from bounding polygon', geo
128    m = Mesh(geo_reference=geo)
[1855]129   
[2376]130   
131   
[1855]132    #Do bounding polygon
133    region_dict = create_region(bounding_polygon, boundary_tags, refzone)   
134    m.addVertsSegs(region_dict)
[1832]135
136
[1855]137    #Find one point inside region automatically
138    if interior_regions is not None:
139        excluded_polygons = []       
140        for P, res in interior_regions:
[2376]141            if not UTM:           
142                polygon, _ = convert_points_from_latlon_to_utm(P, refzone)
143            else:
144                polygon = P
[1855]145            excluded_polygons.append( polygon )
146    else:
147        excluded_polygons = None
[1832]148
[1855]149    from Numeric import array
[1784]150   
[1855]151    [inner_point] = populate_polygon(bounding_polygon, 1, exclude = excluded_polygons)
152    print 'I:', inner_point, resolution
153    inner = m.addRegionEN(inner_point[0], inner_point[1])
154    inner.setMaxArea(resolution)
[1784]155
156
[1855]157
158    #Do interior regions
159    if interior_regions is not None:   
160        for P, res in interior_regions:
[2376]161            if not UTM:                       
162                polygon, _ = convert_points_from_latlon_to_utm(P, refzone)
163            else:
164                polygon = P
165               
[1855]166            region_dict = create_region(polygon, None, refzone)
167
168            m.addVertsSegs(region_dict)
169
170            [inner_point] = populate_polygon(polygon, 1)
171            print 'I', inner_point, res
172            X = m.addRegionEN(inner_point[0], inner_point[1])
173            X.setMaxArea(res)
174           
175
[1784]176   
[1832]177    m.generateMesh('pzq28.0za1000000a')
178   
179    m.export_mesh_file(filename)
180   
[1784]181
182
183
184
185   
Note: See TracBrowser for help on using the repository browser.