Changeset 5207


Ignore:
Timestamp:
Apr 11, 2008, 4:18:26 PM (16 years ago)
Author:
duncan
Message:

Adding regional tags when importing polygons

Location:
anuga_core
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/documentation/user_manual/anuga_user_manual.tex

    r5144 r5207  
    17041704
    17051705
    1706 \begin{methoddesc}  {import_ungenerate_file}{self,ofile, tag=None}
     1706\begin{methoddesc}  {import_ungenerate_file}{self,ofile, tag=None,
     1707region_tag=None}
    17071708Module: \module{pmesh.mesh},  Class: \class{Mesh}
    17081709
    1709 This method is used to import a polygon file in the ungenerate
    1710 format, which is used by arcGIS. The polygons from the file are converted to
     1710This method is used to import a polygon file in the ungenerate format,
     1711which is used by arcGIS. The polygons from the file are converted to
    17111712vertices and segments. \code{ofile} is the name of the polygon file.
    17121713\code{tag} is the tag given to all the polygon's segments.
     1714\code{region_tag} is the tag given to all the polygon's segments.  If
     1715it is a string the one value will be assigned to all regions.  If it
     1716is a list the first value in the list will be applied to the first
     1717polygon etc.
    17131718
    17141719This function can be used to import building footprints.
  • anuga_core/source/anuga/pmesh/mesh.py

    r5188 r5207  
    583583        return h
    584584   
    585     def add_region(self, x,y, geo_reference=None):
     585    def add_region(self, x,y, geo_reference=None, tag=None):
    586586        """
    587587        adds a point, which represents a region.
     
    594594        [[x,y]] = self.geo_reference.change_points_geo_ref([x,y],
    595595                                                 points_geo_ref=geo_reference)
    596         return self._addRegion(x, y)
     596        region =  self._addRegion(x, y)
     597        if tag is not None:
     598            region.setTag(tag)
     599        return region
    597600
    598601    def build_grid(self,  vert_rows, vert_columns):
     
    680683        so if the user whats to modify it they can.     
    681684        """
    682         if max_triangle_area is None:
     685        if max_triangle_area is None and region_tag is None:
    683686            create_region = False
    684687        else:
     
    779782        # Due to this default this method is too connected to
    780783        # _add_area_from_polygon
    781        
    782784        segment_tags = ['']*number_of_segs
    783785        if tags is not None:
     
    18811883
    18821884
    1883     def import_ungenerate_file(self,ofile, tag=None):
     1885    def import_ungenerate_file(self,ofile, tag=None, region_tag=None):
    18841886        """
    18851887        Imports an ungenerate file, from arcGIS into mesh.
    18861888
    18871889        ofile is the name of the ungenerated file.
    1888         Tag is a string name to be taggged on each segment.
     1890        Tag is a string name to be taggged on each segment.
     1891       
     1892        region_tag is the tag applied to the building regions.
     1893        if it is a string the one value will be assigned to all regions
     1894        if it is a list the first value in the list will be applied to the first polygon etc.
     1895        WARNING: size of list and number of polygons isn't checked
    18891896
    18901897        WARNING values are assumed to be absolute.
     
    18961903        if tag is not None:
    18971904            Segment.set_default_tag(str(tag))
    1898         self.addVertsSegs(dict)
     1905       
     1906        if region_tag is None:
     1907            self.addVertsSegs(dict)
     1908        else:           
     1909            if not isinstance(region_tag, list):
     1910                region_tag = [region_tag]*len(dict['polygons'])
     1911            for a_tag,polygon in map(None, region_tag, dict['polygons']):
     1912                segment_tags = {tag:range(len(polygon))}
     1913                self.add_region_from_polygon(polygon,segment_tags=segment_tags,
     1914                                region_tag=a_tag)
     1915           
     1916       
    18991917        Segment.set_default_tag(default_tag)
    19001918
    1901         # change the tag back to  it's default
     1919        # change the tag back to it's default
    19021920   
    19031921       
     
    21642182    last line:  "END"
    21652183    """
     2184   
    21662185    END_DELIMITER = 'END'
    21672186   
    21682187    points = []
    21692188    segments = []
     2189    polygons = []
    21702190   
    21712191    isEnd = False
    21722192    line = fd.readline() #not used <# of polynomial> <x> <y>
    21732193    while not isEnd:
     2194        poly = []
    21742195        line = fd.readline()
    21752196        fragments = line.split()
    2176         vert = [float(fragments.pop(0)),float(fragments.pop(0))]
    2177         points.append(vert)
     2197        x = float(fragments.pop(0))
     2198        y = float(fragments.pop(0))
     2199        points.append([x,y])
     2200        poly.append([x,y])
    21782201        PreviousVertIndex = len(points)-1
    21792202        firstVertIndex = PreviousVertIndex
     
    21832206            #print "line >" + line + "<"
    21842207            fragments = line.split()
    2185             vert = [float(fragments.pop(0)),float(fragments.pop(0))]
    2186             points.append(vert)
     2208            x = float(fragments.pop(0))
     2209            y = float(fragments.pop(0))
     2210            points.append([x,y])
     2211            poly.append([x,y])
    21872212            thisVertIndex = len(points)-1
    21882213            segment = [PreviousVertIndex,thisVertIndex]
     
    21902215            PreviousVertIndex = thisVertIndex
    21912216            line = fd.readline() #Read the next line
    2192             i =+ 1
    21932217        # If the last and first segments are the same,
    21942218        # Remove the last segment and the last vertex
     
    22022226            points.pop()
    22032227            segments.pop()
     2228            poly.pop()
    22042229            thisVertIndex = len(points)-1
    2205             segments.append([thisVertIndex, firstVertIndex])
     2230        segments.append([thisVertIndex, firstVertIndex])
    22062231       
    22072232        line = fd.readline() # read <# of polynomial> <x> <y> OR END
    22082233        #print "line >>" + line + "<<"
     2234        # do poly stuff here
     2235        polygons.append(poly)
    22092236        if line.startswith(END_DELIMITER):
    22102237            isEnd = True
     
    22152242    ungenerated_dict['points'] = points
    22162243    ungenerated_dict['segments'] = segments
     2244    ungenerated_dict['polygons'] = polygons
    22172245    return ungenerated_dict
    22182246
  • anuga_core/source/anuga/pmesh/test_mesh.py

    r4955 r5207  
    12821282                        'wrong tag.')
    12831283       
    1284     def test_ungenerateFileLoadingII(self):
     1284    def test_import_ungenerate_file(self):
    12851285       
    12861286        fileName = tempfile.mktemp(".txt")
    12871287        file = open(fileName,"w")
    12881288        file.write("         1       ??      ??\n\
    1289        0.0       0.0\n\
    1290        1.0       0.0\n\
    1291        1.0       1.0\n\
    1292        0.0       1.0\n\
    1293        0.0       0.0\n\
     1289       10.0       10.0\n\
     1290       11.0       10.0\n\
     1291       11.0       11.0\n\
     1292       10.0       11.0\n\
     1293       10.0       10.0\n\
    12941294END\n\
    12951295         2      ?? ??\n\
    1296        10.0       10.0\n\
    1297        10.0       20.0\n\
    12981296       20.0       20.0\n\
     1297       20.0       30.0\n\
     1298       30.0       30.0\n\
    12991299END\n\
    13001300END\n")
     
    13201320        m.addVertsSegs(dict)
    13211321
    1322         self.failUnless(len(m.userSegments) ==10,
     1322        self.failUnless(len(m.userSegments) ==11,
    13231323                        'Wrong segment list length.')
    13241324        self.failUnless(len(m.userVertices) == 11,
     
    13401340        initial_tag = "PIG"
    13411341        Segment.set_default_tag(initial_tag)
    1342         m.import_ungenerate_file(fileName, tag=tag)
     1342        m.import_ungenerate_file(fileName, tag=tag, region_tag="swamp")
    13431343
    13441344        os.remove(fileName)
     
    13461346        self.failUnless(Segment.get_default_tag() == initial_tag,
    13471347                        'Wrong segment list length.')
    1348        
    1349        
    1350         self.failUnless(len(m.userSegments) ==10,
     1348        m.export_mesh_file("swamp.tsh")
     1349        #print "m.userSegments",m.userSegments
     1350        self.failUnless(len(m.userSegments) ==11,
    13511351                        'Wrong segment list length.')
    13521352        self.failUnless(len(m.userVertices) == 11,
    13531353                        'Wrong vertex list length.')
     1354        self.failUnless(len(m.regions) == 2,
     1355                        'Wrong regions list length.')
     1356        self.failUnless(m.regions[0].getTag() == "swamp",
     1357                        'Wrong regions tag.')
     1358        self.failUnless(m.regions[1].getTag() == "swamp",
     1359                        'Wrong regions 1 tag.')
     1360       
     1361        # have to reset this , since it's a class attribute
     1362        Segment.set_default_tag("")
     1363       
     1364       
     1365    def test_import_ungenerate_file_different_region_tags(self):
     1366       
     1367        fileName = tempfile.mktemp(".txt")
     1368        file = open(fileName,"w")
     1369        file.write("         1       ??      ??\n\
     1370       10.0       10.0\n\
     1371       11.0       10.0\n\
     1372       11.0       11.0\n\
     1373       10.0       11.0\n\
     1374       10.0       10.0\n\
     1375END\n\
     1376         2      ?? ??\n\
     1377       20.0       20.0\n\
     1378       20.0       30.0\n\
     1379       30.0       30.0\n\
     1380END\n\
     1381END\n")
     1382        file.close()
     1383       
     1384       
     1385        a = Vertex (0.0, 0.0) #, attributes = [1.1])
     1386        b = Vertex (0.0, 40.0) #, attributes = [1.2])
     1387        c = Vertex (40.0,40.0) #, attributes = [1.3])
     1388        d = Vertex (40.0,0.0) #, attributes = [1.4])
     1389   
     1390        s1 = Segment(a,b)
     1391        s2 = Segment(b,c)
     1392        s3 = Segment(c,d)
     1393        s4 = Segment(d,a)
     1394     
     1395        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
     1396        dict = importUngenerateFile(fileName)
     1397        #os.remove(fileName)
     1398
     1399        tag = "DSG"
     1400        Segment.set_default_tag(tag)
     1401        m.addVertsSegs(dict)
     1402
     1403        self.failUnless(len(m.userSegments) ==11,
     1404                        'Wrong segment list length.')
     1405        self.failUnless(len(m.userVertices) == 11,
     1406                        'Wrong vertex list length.')
     1407
     1408        # Test the method
     1409        a = Vertex (0.0, 0.0) #, attributes = [1.1])
     1410        b = Vertex (0.0, 40.0) #, attributes = [1.2])
     1411        c = Vertex (40.0,40.0) #, attributes = [1.3])
     1412        d = Vertex (40.0,0.0) #, attributes = [1.4])
     1413   
     1414        s1 = Segment(a,b)
     1415        s2 = Segment(b,c)
     1416        s3 = Segment(c,d)
     1417        s4 = Segment(d,a)
     1418     
     1419        m = Mesh(userVertices=[a,b,c,d], userSegments=[s1,s2,s3,s4])
     1420        tag = "DSG"       
     1421        initial_tag = "PIG"
     1422        Segment.set_default_tag(initial_tag)
     1423        m.import_ungenerate_file(fileName, tag=tag, region_tag=["swamp","coastalp"])
     1424
     1425        os.remove(fileName)
     1426
     1427        self.failUnless(Segment.get_default_tag() == initial_tag,
     1428                        'Wrong segment list length.')
     1429        m.export_mesh_file("swamp.tsh")
     1430        #print "m.userSegments",m.userSegments
     1431        self.failUnless(len(m.userSegments) ==11,
     1432                        'Wrong segment list length.')
     1433        self.failUnless(len(m.userVertices) == 11,
     1434                        'Wrong vertex list length.')
     1435        self.failUnless(len(m.regions) == 2,
     1436                        'Wrong regions list length.')
     1437        self.failUnless(m.regions[0].getTag() == "swamp",
     1438                        'Wrong regions tag.')
     1439        self.failUnless(m.regions[1].getTag() == "coastalp",
     1440                        'Wrong regions 1 tag.')
     1441       
    13541442       
    13551443        # have to reset this , since it's a class attribute
     
    21992287    #suite = unittest.makeSuite(meshTestCase,'test_asciiFile')
    22002288    #suite = unittest.makeSuite(meshTestCase,'test_mesh2IO')
    2201     #suite = unittest.makeSuite(meshTestCase,'testgenerateMesh')
     2289    #suite = unittest.makeSuite(meshTestCase,'test_import_ungenerate_file')
    22022290    runner = unittest.TextTestRunner() #verbosity=2)
    22032291    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.