Example #1
0
 def test_qgis_raster_layer_loading(self):
     """Test that reading from QgsRasterLayer works."""
     # This line is the cause of the problem:
     qgis_layer = QgsRasterLayer(RASTER_BASE + '.tif', 'test')
     layer = Raster(data=qgis_layer)
     qgis_extent = qgis_layer.dataProvider().extent()
     qgis_extent = [qgis_extent.xMinimum(), qgis_extent.yMinimum(),
                    qgis_extent.xMaximum(), qgis_extent.yMaximum()]
     layer_exent = layer.get_bounding_box()
     self.assertListEqual(
         layer_exent, qgis_extent,
         'Expected %s extent, got %s' % (qgis_extent, layer_exent))
Example #2
0
    def test_convert_to_qgis_raster_layer(self):
        """Test that converting to QgsVectorLayer works."""
        # Create vector layer
        layer = Raster(data=RASTER_BASE + '.tif')

        # Convert to QgsRasterLayer
        qgis_layer = layer.as_qgis_native()
        qgis_extent = qgis_layer.dataProvider().extent()
        qgis_extent = [qgis_extent.xMinimum(), qgis_extent.yMinimum(),
                       qgis_extent.xMaximum(), qgis_extent.yMaximum()]
        layer_exent = layer.get_bounding_box()
        self.assertListEqual(layer_exent, qgis_extent)
Example #3
0
 def test_qgis_raster_layer_loading(self):
     """Test that reading from QgsRasterLayer works."""
     # This line is the cause of the problem:
     qgis_layer = QgsRasterLayer(RASTER_BASE + '.tif', 'test')
     layer = Raster(data=qgis_layer)
     qgis_extent = qgis_layer.dataProvider().extent()
     qgis_extent = [qgis_extent.xMinimum(), qgis_extent.yMinimum(),
                    qgis_extent.xMaximum(), qgis_extent.yMaximum()]
     layer_exent = layer.get_bounding_box()
     self.assertListEqual(
         layer_exent, qgis_extent,
         'Expected %s extent, got %s' % (qgis_extent, layer_exent))
Example #4
0
    def test_convert_to_qgis_raster_layer(self):
        """Test that converting to QgsVectorLayer works."""
        # Create vector layer
        keywords = read_keywords(RASTER_BASE + '.keywords')
        layer = Raster(data=RASTER_BASE + '.tif', keywords=keywords)

        # Convert to QgsRasterLayer
        qgis_layer = layer.as_qgis_native()
        qgis_extent = qgis_layer.dataProvider().extent()
        qgis_extent = [qgis_extent.xMinimum(), qgis_extent.yMinimum(),
                       qgis_extent.xMaximum(), qgis_extent.yMaximum()]
        layer_exent = layer.get_bounding_box()
        self.assertListEqual(
            layer_exent, qgis_extent,
            'Expected %s extent, got %s' % (qgis_extent, layer_exent))
Example #5
0
    def test_convert_to_qgis_raster_layer(self):
        """Test that converting to QgsVectorLayer works."""
        if qgis_imported:
            # Create vector layer
            keywords = read_keywords(RASTER_BASE + '.keywords')
            layer = Raster(data=RASTER_BASE + '.tif', keywords=keywords)

            # Convert to QgsRasterLayer
            qgis_layer = layer.as_qgis_native()
            qgis_extent = qgis_layer.dataProvider().extent()
            qgis_extent = [qgis_extent.xMinimum(), qgis_extent.yMinimum(),
                           qgis_extent.xMaximum(), qgis_extent.yMaximum()]
            layer_exent = layer.get_bounding_box()
            self.assertListEqual(
                layer_exent, qgis_extent,
                'Expected %s extent, got %s' % (qgis_extent, layer_exent))
Example #6
0
def start(west,
          north,
          east,
          south,
          since,
          until=None,
          data_dir=None,
          population=None):

    bbox = (west, north, east, south)

    year, month, day = [int(x) for x in since.split('-')]
    since = datetime.date(year, month, day)

    if not isinstance(until, datetime.date):
        year, month, day = [int(x) for x in until.split('-')]
        until = datetime.date(year, month, day)
    else:
        until = until

    # Make sure the inputs are divisible by 10.
    for item in bbox:
        msg = "%d is not divisible by 10." % item
        assert int(item) % 10 == 0, msg

    the_viewports = viewports(bbox)
    the_timespan = timespan(since, until)

    data_dir = os.path.abspath(data_dir)

    if not os.path.exists(data_dir):
        os.mkdir(data_dir)

    print 'Downloading layers per day'
    # Download the layers for the given viewport and timespan.
    download(the_viewports, the_timespan, data_dir)

    print 'Merging layers per day'
    merged_files = merge(the_timespan, data_dir)

    flood_filename = os.path.join(data_dir, 'flood_severity.tif')

    if not os.path.exists(flood_filename):
        if len(merged_files) > 0:
            # Add all the pixels with a value higher than 3.
            #accumulate(merged_files, flood_filename, threshold=3)
            flooded = _flood_severity(merged_files)
            flooded.write_to_file(flood_filename)

            subprocess.call([
                'gdal_merge.py', '-co', 'compress=packbits', '-o',
                'flood_severity_compressed.tif', '-ot', 'Byte', flood_filename
            ],
                            stdout=open(os.devnull, 'w'))
            os.remove(flood_filename)
            os.rename('flood_severity_compressed.tif', flood_filename)
        else:
            raise Exception('No merged files found for %s' % the_timespan)

    population_file = os.path.join(data_dir, population)
    population_object = Raster(population_file)
    # get population bbox
    pop_bbox = population_object.get_bounding_box()

    # get resolutions and pick the best
    pop_resolution = population_object.get_resolution()[0]

    hazard_object = Raster(flood_filename)
    hazard_resolution = hazard_object.get_resolution()[0]
    hazard_bbox = hazard_object.get_bounding_box()

    if pop_bbox[0] > bbox[0] and pop_bbox[1] > bbox[1] and pop_bbox[2] < bbox[
            2] and pop_bbox[3] < bbox[3]:
        hazard_file = clip(flood_filename, pop_bbox, cellSize=pop_resolution)
        exposure_layer = population_file
    else:
        hazard_file = clip(flood_filename,
                           hazard_bbox,
                           cellSize=pop_resolution)
        exposure_layer = clip(population_file, hazard_bbox, cellSize=None)

    basename, ext = os.path.splitext(hazard_file)
    keywords_file = basename + '.keywords'

    if not os.path.exists(keywords_file):
        with open(keywords_file, 'w') as f:
            f.write(FLOOD_KEYWORDS)

    impact = calculate(hazard_file, exposure_layer)

    impact.write_to_file('impact.tif')

    count = impact.keywords['count']
    pretty_date = until.strftime('%a %d, %b %Y')
    print pretty_date, "|", "People affected: %s / %s" % (
        count, impact.keywords['total'])
Example #7
0
def start(west,north,east,south, since, until=None, data_dir=None, population=None):
    
    bbox = (west, north, east, south)

    year, month, day = [int(x) for x in since.split('-')]
    since = datetime.date(year, month, day)

    if not isinstance(until, datetime.date):
        year, month, day = [int(x) for x in until.split('-')]
        until = datetime.date(year, month, day)
    else:
        until = until

    # Make sure the inputs are divisible by 10.
    for item in bbox:
        msg = "%d is not divisible by 10." % item
        assert int(item) % 10 == 0, msg

    the_viewports = viewports(bbox)
    the_timespan = timespan(since, until)

    data_dir = os.path.abspath(data_dir)

    if not os.path.exists(data_dir):
        os.mkdir(data_dir)

    print 'Downloading layers per day'
    # Download the layers for the given viewport and timespan.
    download(the_viewports, the_timespan, data_dir)

    print 'Merging layers per day'
    merged_files = merge(the_timespan, data_dir)

    flood_filename = os.path.join(data_dir, 'flood_severity.tif')

    if not os.path.exists(flood_filename):
        if len(merged_files) > 0:
            # Add all the pixels with a value higher than 3.
            #accumulate(merged_files, flood_filename, threshold=3)
            flooded = _flood_severity(merged_files)
            flooded.write_to_file(flood_filename)

            subprocess.call(['gdal_merge.py',
                     '-co', 'compress=packbits',
                     '-o', 'flood_severity_compressed.tif',
                     '-ot', 'Byte',
                     flood_filename], stdout=open(os.devnull, 'w'))
            os.remove(flood_filename)
            os.rename('flood_severity_compressed.tif', flood_filename)
        else:
            raise Exception('No merged files found for %s' % the_timespan)
    
    population_file = os.path.join(data_dir, population)
    population_object = Raster(population_file)
    # get population bbox
    pop_bbox = population_object.get_bounding_box()

    # get resolutions and pick the best
    pop_resolution = population_object.get_resolution()[0]

    hazard_object = Raster(flood_filename)
    hazard_resolution = hazard_object.get_resolution()[0]
    hazard_bbox = hazard_object.get_bounding_box()

    if pop_bbox[0] > bbox[0] and pop_bbox[1] > bbox[1] and pop_bbox[2] < bbox[2] and pop_bbox[3] < bbox[3]:
        hazard_file = clip(flood_filename, pop_bbox, cellSize=pop_resolution)
        exposure_layer = population_file
    else:
        hazard_file = clip(flood_filename, hazard_bbox, cellSize=pop_resolution)
        exposure_layer = clip(population_file, hazard_bbox, cellSize=None)    

    basename, ext = os.path.splitext(hazard_file)
    keywords_file = basename + '.keywords'

    if not os.path.exists(keywords_file):
        with open(keywords_file, 'w') as f:
            f.write(FLOOD_KEYWORDS)

    impact = calculate(hazard_file, exposure_layer)

    impact.write_to_file('impact.tif')

    count = impact.keywords['count']
    pretty_date = until.strftime('%a %d, %b %Y')
    print pretty_date, "|", "People affected: %s / %s" % (count, impact.keywords['total'])