Ejemplo n.º 1
0
    def test_add_intersections_to_domain_features(self):

        # Only run test if python >= 2.7
        if sys.hexversion < 0x02070000: return

        bounding_polygon = [[0., 0.], [0., 10.], [10., 10.], [10., 0.]]

        breakLines = {
            'bl1': [[-0.01, 5.], [10.01, 5.]],
            'bl2': [[5., -0.01], [5., 10.01]]
        }

        # We make an erronious riverwall, which is colinear with a breakLine
        # This is not permitted
        riverWalls = {
            'rw1': [[-0.01, 8., 2.], [10.01, 4., 3.]],
            'rw2': [[5., -0.01, 1.], [5., 10.01, 2.]]
        }

        def should_fail():
            # Should fail
            newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
                                    breakLines, riverWalls, point_movement_threshold=0.02,\
                                    verbose=verbose)
            return

        self.assertRaises(Exception, lambda: should_fail())

        #################################################################
        # Fix the riverwall, and it should work
        riverWalls = {'rw1': [[-0.000001, 8., 2.], [10.0000001, 4., 3.]]}
        # This should work
        newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
                                breakLines, riverWalls, point_movement_threshold=0.02,\
                                verbose=verbose)

        # There should be several new points on breakLines + riverWalls
        assert newBL['bl1'][1] == newBL['bl2'][1]
        assert newRW['rw1'][2][0:2] == newBL['bl1'][2]
        assert newRW['rw1'][1][0:2] == newBL['bl2'][2]
        # rw1 x/y coords are close to y=8-3/10*x
        #     x/z coords are close to z=2+x/10
        assert numpy.allclose(newRW['rw1'][1][2], newRW['rw1'][1][0] / 10. + 2)
        assert numpy.allclose(newRW['rw1'][2][2], newRW['rw1'][2][0] / 10. + 2)

        return
    def test_add_intersections_to_domain_features(self):

        # Only run test if python >= 2.7
        if sys.hexversion < 0x02070000: return

        bounding_polygon=[ [0., 0.], [0., 10.], [10., 10.], [10., 0.]]

        breakLines={ 'bl1': [[-0.01, 5.],[10.01, 5.]],
                     'bl2': [[5., -0.01], [5., 10.01]] }
       
        # We make an erronious riverwall, which is colinear with a breakLine
        # This is not permitted 
        riverWalls={ 'rw1': [[-0.01, 8., 2.],[10.01, 4., 3.]],
                     'rw2': [[5., -0.01, 1.], [5., 10.01, 2.]] }

        def should_fail():
            # Should fail
            newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
                                    breakLines, riverWalls, point_movement_threshold=0.02,\
                                    verbose=verbose)
            return
        self.assertRaises(Exception, lambda: should_fail())

        #################################################################
        # Fix the riverwall, and it should work
        riverWalls={ 'rw1': [[-0.000001, 8., 2.],[10.0000001, 4., 3.]]
                      }
        # This should work
        newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
                                breakLines, riverWalls, point_movement_threshold=0.02,\
                                verbose=verbose)

        # There should be several new points on breakLines + riverWalls
        assert newBL['bl1'][1]==newBL['bl2'][1]
        assert newRW['rw1'][2][0:2]==newBL['bl1'][2]
        assert newRW['rw1'][1][0:2]==newBL['bl2'][2]
        # rw1 x/y coords are close to y=8-3/10*x
        #     x/z coords are close to z=2+x/10
        assert numpy.allclose(newRW['rw1'][1][2], newRW['rw1'][1][0]/10.+2)
        assert numpy.allclose(newRW['rw1'][2][2], newRW['rw1'][2][0]/10.+2)

        return
Ejemplo n.º 3
0
 def should_fail():
     # Should fail
     newBP, newBL, newRW = su.add_intersections_to_domain_features(bounding_polygon,
                                                                   breakLines, riverWalls, point_movement_threshold=0.02,
                                                                   verbose=verbose)
     return
    def process_project_data(self):
        """Process the input data ready for ANUGA

        """

        # Print messages from the ProjectData.__init__ call
        # This allows us to log those messages without refactoring
        # (Consider refactoring though)
        if myid == 0:
            for p in self.print_info:
                print p
            print ''
            print '---------------------'
            print 'PROCESS_PROJECT_DATA'
            print '---------------------'
            print ''
            # Record the time and broadcast to other processers
            time_number = time.time()
            if numprocs > 1:
                for i in range(1, numprocs):
                    send(time_number, i)
        else:
            time_number = receive(0)

        # We can either use interior regions, or breaklines

        if not self.interior_regions_data == []:
            assert self.pt_areas is None, \
                'Cannot define both ptAreas and non-empty interior regions'

        bounding_polygon_and_tags = \
            read_boundary_tags_line_shapefile(
                self.bounding_polygon_and_tags_file,
                self.boundary_tags_attribute_name)
        self.bounding_polygon = bounding_polygon_and_tags[0]
        self.boundary_tags = bounding_polygon_and_tags[1]

        self.breaklines = su.readListOfBreakLines(self.breakline_files)
        (self.riverwalls, self.riverwall_par) = \
            su.readListOfRiverWalls(self.riverwall_csv_files)

        if self.pt_areas is not None:
            self.region_point_areas = su.readRegionPtAreas(
                self.pt_areas,
                convert_length_to_area=self.region_resolutions_from_length)
        else:
            self.region_point_areas = None

        # Hack to override resolution
        # region_point_areas=\
        # [ region_point_areas[i][0:2]+[150*150*0.5] for i in \
        #                      range(len(region_point_areas))]

        # Redefine interior_regions to contain the polygon data + resolutions

        self.interior_regions = [[su.read_polygon(ir[0]), ir[1]]
                                 for ir in self.interior_regions_data]

        # Deal with intersections in the bounding polygon / breaklines /
        # riverwalls. At the moment we cannot add points to the bounding
        # polygon because the boundary tags are not adjusted -- so check that
        # the length of the bounding polygon doesn't change
        lbp = len(self.bounding_polygon)
        if type(self.break_line_intersect_point_movement_threshold) is not str:
            (self.bounding_polygon, self.breaklines, self.riverwalls) = \
                su.add_intersections_to_domain_features(
                    self.bounding_polygon,
                    self.breaklines,
                    self.riverwalls,
                    point_movement_threshold=self.break_line_intersect_point_movement_threshold,
                    verbose=True)

        msg = 'Bounding polygon had points added or dropped because of ' + \
              'intersections --' + \
              'This is not yet properly supported.  Please add ' + \
              ' the intersection points to the bounding polygon'
        assert lbp == len(self.bounding_polygon), msg

        # Here we make a unique ID based on the all the mesh geometry inputs
        # This tells us if we need to regenerate partitions, or use old ones
        mesh_dependency_information = [
            self.bounding_polygon, self.interior_regions, self.riverwalls,
            self.breaklines, self.region_point_areas, self.default_res,
            self.boundary_tags
        ]

        if not self.use_existing_mesh_pickle:
            # Append the time to the mesh dependency so we don't reuse old
            # meshes
            mesh_dependency_information.append([time_number])

        self.mesh_id_hash = hashlib.md5(
            json.dumps(mesh_dependency_information)).hexdigest()

        # Fix the output tif bounding polygon
        if self.output_tif_bounding_polygon is None:
            self.output_tif_bounding_polygon = self.bounding_polygon
        else:
            self.output_tif_bounding_polygon = \
                su.read_polygon(self.output_tif_bounding_polygon)

        # Make proj4string from projection information
        #

        if isinstance(self.projection_information, int):

            # projection_information describes a UTM zone
            # e.g. '+units=m +ellps=WGS84 +zone=47 +south=False +proj=utm '

            if self.projection_information < 0:
                self.proj4string = '+proj=utm +zone=' \
                    + str(abs(self.projection_information)) \
                    + ' +south +datum=WGS84 +units=m +no_defs'
            else:
                self.proj4string = '+proj=utm +zone=' \
                    + str(self.projection_information) \
                    + ' +datum=WGS84 +units=m +no_defs'
        elif isinstance(self.projection_information, str):
            self.proj4string = self.projection_information
        else:
            msg = 'Invalid projection information ' + \
                ' --  must be a proj4string, or an integer' + \
                ' defining a UTM zone [positive for northern hemisphere,' + \
                ' negative for southern hemisphere]'
            raise Exception(msg)

        # Set up directories etc

        self.partition_basedir = 'PARTITIONS/'
        self.partition_dir = self.partition_basedir + 'Mesh_' +\
            str(self.mesh_id_hash)
        self.meshname = self.output_dir + '/mesh.tsh'
 def should_fail():
     # Should fail
     newBP, newBL, newRW=su.add_intersections_to_domain_features(bounding_polygon,\
                             breakLines, riverWalls, point_movement_threshold=0.02,\
                             verbose=verbose)
     return
Ejemplo n.º 6
0
    def process_project_data(self):
        """Process the input data ready for ANUGA

        """

        # Print messages from the ProjectData.__init__ call
        # This allows us to log those messages without refactoring
        # (Consider refactoring though)
        if myid == 0:
            for p in self.print_info:
                print p
            print ''
            print '---------------------'
            print 'PROCESS_PROJECT_DATA'
            print '---------------------'
            print ''
            # Record the time and broadcast to other processers
            time_number = time.time()
            if numprocs > 1:
                for i in range(1, numprocs):
                    send(time_number, i)
        else:
            time_number = receive(0)

        # We can either use interior regions, or breaklines

        if not self.interior_regions_data == []:
            assert self.pt_areas is None, \
                'Cannot define both ptAreas and non-empty interior regions'

        bounding_polygon_and_tags = \
            read_boundary_tags_line_shapefile(
                self.bounding_polygon_and_tags_file,
                self.boundary_tags_attribute_name)
        self.bounding_polygon = bounding_polygon_and_tags[0]
        self.boundary_tags = bounding_polygon_and_tags[1]

        self.breaklines = su.readListOfBreakLines(self.breakline_files)
        (self.riverwalls, self.riverwall_par) = \
            su.readListOfRiverWalls(self.riverwall_csv_files)

        if self.pt_areas is not None:
            self.region_point_areas = su.readRegionPtAreas(
                self.pt_areas,
                convert_length_to_area=self.region_resolutions_from_length)
        else:
            self.region_point_areas = None

        # Hack to override resolution
        # region_point_areas=\
        # [ region_point_areas[i][0:2]+[150*150*0.5] for i in \
        #                      range(len(region_point_areas))]

        # Redefine interior_regions to contain the polygon data + resolutions

        self.interior_regions = [[su.read_polygon(ir[0]), ir[1]] for ir in
                                 self.interior_regions_data]

        # Deal with intersections in the bounding polygon / breaklines /
        # riverwalls. At the moment we cannot add points to the bounding
        # polygon because the boundary tags are not adjusted -- so check that
        # the length of the bounding polygon doesn't change
        lbp = len(self.bounding_polygon)
        if type(self.break_line_intersect_point_movement_threshold) is not str:
            (self.bounding_polygon, self.breaklines, self.riverwalls) = \
                su.add_intersections_to_domain_features(
                    self.bounding_polygon,
                    self.breaklines,
                    self.riverwalls,
                    point_movement_threshold=self.break_line_intersect_point_movement_threshold,
                    verbose=True)

        msg = 'Bounding polygon had points added or dropped because of ' + \
              'intersections --' + \
              'This is not yet properly supported.  Please add ' + \
              ' the intersection points to the bounding polygon'
        assert lbp == len(self.bounding_polygon), msg

        # Here we make a unique ID based on the all the mesh geometry inputs
        # This tells us if we need to regenerate partitions, or use old ones
        mesh_dependency_information = [
                self.bounding_polygon,
                self.interior_regions,
                self.riverwalls,
                self.breaklines,
                self.region_point_areas,
                self.default_res,
                self.boundary_tags
            ]

        if not self.use_existing_mesh_pickle:
            # Append the time to the mesh dependency so we don't reuse old
            # meshes
            mesh_dependency_information.append([time_number])

        self.mesh_id_hash = hashlib.md5(json.dumps(mesh_dependency_information)).hexdigest()

        # Fix the output tif bounding polygon
        if self.output_tif_bounding_polygon is None:
            self.output_tif_bounding_polygon = self.bounding_polygon
        else:
            self.output_tif_bounding_polygon = \
                su.read_polygon(self.output_tif_bounding_polygon)

        # Make proj4string from projection information
        #

        if isinstance(self.projection_information, int):

            # projection_information describes a UTM zone
            # e.g. '+units=m +ellps=WGS84 +zone=47 +south=False +proj=utm '

            if self.projection_information < 0:
                self.proj4string = '+proj=utm +zone=' \
                    + str(abs(self.projection_information)) \
                    + ' +south +datum=WGS84 +units=m +no_defs'
            else:
                self.proj4string = '+proj=utm +zone=' \
                    + str(self.projection_information) \
                    + ' +datum=WGS84 +units=m +no_defs'
        elif isinstance(self.projection_information, str):
            self.proj4string = self.projection_information
        else:
            msg = 'Invalid projection information ' + \
                ' --  must be a proj4string, or an integer' + \
                ' defining a UTM zone [positive for northern hemisphere,' + \
                ' negative for southern hemisphere]'
            raise Exception(msg)

        # Set up directories etc

        self.partition_basedir = 'PARTITIONS/'
        self.partition_dir = self.partition_basedir + 'Mesh_' +\
            str(self.mesh_id_hash)
        self.meshname = self.output_dir + '/mesh.tsh'