예제 #1
0
    def test_aggregate_vector_impact(self):
        """Test aggregation results on a vector layer.
        created from loadStandardLayers.qgs with:
        - a flood in Jakarta like in 2007
        - Essential buildings
        - be flooded
        - kabupaten_jakarta_singlepart.shp
        """
        impact_layer = Vector(data=os.path.join(
            TESTDATA, 'aggregation_test_impact_vector.shp'),
                              name='test vector impact')

        expected_results = [['JAKARTA BARAT', '87'], ['JAKARTA PUSAT', '117'],
                            ['JAKARTA SELATAN', '22'],
                            ['JAKARTA UTARA', '286'], ['JAKARTA TIMUR', '198']]
        self._aggregate(impact_layer, expected_results)

        impact_layer = Vector(data=TESTDATA +
                              '/aggregation_test_impact_vector_small.shp',
                              name='test vector impact')

        expected_results = [['JAKARTA BARAT', '2'], ['JAKARTA PUSAT', '0'],
                            ['JAKARTA SELATAN', '0'], ['JAKARTA UTARA', '1'],
                            ['JAKARTA TIMUR', '0']]

        self._aggregate(impact_layer, expected_results)
예제 #2
0
    def test_aggregate_vector_impact(self):
        """Test aggregation results on a vector layer.
        created from loadStandardLayers.qgs with:
        - a flood in Jakarta like in 2007
        - Essential buildings
        - be flodded
        - kabupaten_jakarta_singlepart.shp
        """
        myImpactLayer = Vector(
            data=os.path.join(TESTDATA, 'aggregation_test_impact_vector.shp'),
            name='test vector impact')

        myExpectedResults = [
            {0: 'JAKARTA BARAT', 1: '87'},
            {0: 'JAKARTA PUSAT', 1: '117'},
            {0: 'JAKARTA SELATAN', 1: '22'},
            {0: 'JAKARTA UTARA', 1: '286'},
            {0: 'JAKARTA TIMUR', 1: '198'}
        ]
        # self._aggregate(myImpactLayer, myExpectedResults)

        myImpactLayer = Vector(
            data=TESTDATA + '/aggregation_test_impact_vector_small.shp',
            name='test vector impact')

        myExpectedResults = [
            {0: 'JAKARTA BARAT', 1: '87'},
            {0: 'JAKARTA PUSAT', 1: '117'},
            {0: 'JAKARTA SELATAN', 1: '22'},
            {0: 'JAKARTA UTARA', 1: '286'},
            {0: 'JAKARTA TIMUR', 1: '198'}
        ]

        # TODO (MB) enable this
        self._aggregate(myImpactLayer, myExpectedResults)
    def minimum_needs(self, input_layer, population_name):
        """Compute minimum needs given a layer and a column containing pop.

        :param input_layer: InaSAFE layer object assumed to contain
            population counts
        :type input_layer: read_layer

        :param population_name: Attribute name that holds population count
        :type population_name: str

        :returns: Layer with attributes for minimum needs as per Perka 7
        :rtype: read_layer
        """

        all_attributes = []
        for attributes in input_layer.get_data():
            # Get population count
            population = attributes[population_name]
            # Clean up and turn into integer
            if population in ['-', None]:
                displaced = 0
            else:
                if type(population) is basestring:
                    population = str(population).replace(',', '')

                try:
                    displaced = int(population)
                except ValueError:
                    # noinspection PyTypeChecker,PyArgumentList
                    QtGui.QMessageBox.information(
                        None, self.tr('Format error'),
                        self.tr('Please change the value of %1 in attribute '
                                '%1 to integer format').arg(population).arg(
                                    population_name))
                    raise ValueError

            # Calculate estimated needs based on BNPB Perka 7/2008
            # minimum needs
            # weekly_needs = {
            #     'rice': int(ceil(population * min_rice)),
            #     'drinking_water': int(ceil(population * min_drinking_water)),
            #     'water': int(ceil(population * min_water)),
            #     'family_kits': int(ceil(population * min_family_kits)),
            #     'toilets': int(ceil(population * min_toilets))}

            # Add to attributes
            weekly_needs = evacuated_population_weekly_needs(displaced)

            # Record attributes for this feature
            all_attributes.append(weekly_needs)

        output_layer = Vector(geometry=input_layer.get_geometry(),
                              data=all_attributes,
                              projection=input_layer.get_projection())
        return output_layer
예제 #4
0
    def test_line_aggregation(self):
        """Test if line aggregation works
        """

        data_path = os.path.join(UNITDATA, 'impact',
                                 'aggregation_test_roads.shp')
        impact_layer = Vector(data=data_path, name='test vector impact')

        expected_results = [[u'JAKARTA BARAT', 0], [u'JAKARTA PUSAT', 4356],
                            [u'JAKARTA SELATAN', 0], [u'JAKARTA UTARA', 4986],
                            [u'JAKARTA TIMUR', 5809]]
        impact_layer_attributes = [[{
            'KAB_NAME': u'JAKARTA BARAT',
            'flooded': 0.0,
            'length': 7230.864654,
            'id': 2,
            'aggr_sum': 7230.864654
        }],
                                   [{
                                       'KAB_NAME': u'JAKARTA PUSAT',
                                       'flooded': 4356.161093,
                                       'length': 4356.161093,
                                       'id': 3,
                                       'aggr_sum': 4356.161093
                                   }],
                                   [{
                                       'KAB_NAME': u'JAKARTA SELATAN',
                                       'flooded': 0.0,
                                       'length': 3633.317287,
                                       'id': 4,
                                       'aggr_sum': 3633.317287
                                   }],
                                   [{
                                       'KAB_NAME': u'JAKARTA UTARA',
                                       'flooded': 4985.831677,
                                       'length': 4985.831677,
                                       'id': 1,
                                       'aggr_sum': 4985.831677
                                   }],
                                   [{
                                       'KAB_NAME': u'JAKARTA TIMUR',
                                       'flooded': 0.0,
                                       'length': 4503.033629,
                                       'id': 4,
                                       'aggr_sum': 4503.033629
                                   }, {
                                       'KAB_NAME': u'JAKARTA TIMUR',
                                       'flooded': 5809.142247,
                                       'length': 5809.142247,
                                       'id': 1,
                                       'aggr_sum': 5809.142247
                                   }]]
        self._aggregate(impact_layer,
                        expected_results,
                        impact_layer_attributes=impact_layer_attributes)
예제 #5
0
    def test_aggregate_vector_impact(self):
        """Test aggregation results on a vector layer.
        created from loadStandardLayers.qgs with:
        - a flood in Jakarta like in 2007
        - Essential buildings
        - be flooded
        - kabupaten_jakarta_singlepart.shp
        """

        # Aggregation in sum mode
        impact_layer = Vector(data=os.path.join(
            TESTDATA, 'aggregation_test_impact_vector.shp'),
                              name='test vector impact')
        expected_results = [['JAKARTA BARAT', '87'], ['JAKARTA PUSAT', '117'],
                            ['JAKARTA SELATAN', '22'],
                            ['JAKARTA UTARA', '286'], ['JAKARTA TIMUR', '198']]
        self._aggregate(impact_layer, expected_results)
        impact_layer = Vector(data=TESTDATA +
                              '/aggregation_test_impact_vector_small.shp',
                              name='test vector impact')
        expected_results = [  # Count of inundated polygons:
            ['JAKARTA BARAT', '2'], ['JAKARTA PUSAT', '0'],
            ['JAKARTA SELATAN', '0'], ['JAKARTA UTARA', '1'],
            ['JAKARTA TIMUR', '0']
        ]
        self._aggregate(impact_layer, expected_results)
        expected_results = [['Entire area', '3']]
        self._aggregate(impact_layer, expected_results, use_aoi_mode=True)

        # Aggregation in class_count mode
        data_path = os.path.join(
            UNITDATA, 'impact',
            'aggregation_test_impact_vector_class_count.shp')
        impact_layer = Vector(data=data_path, name='test vector impact')
        expected_results = [['Entire area', '2', '3', '0']]
        self._aggregate(impact_layer, expected_results, use_aoi_mode=True)
        expected_results = [['JAKARTA BARAT', '1', '2', '0'],
                            ['JAKARTA PUSAT', '1', '0', '0'],
                            ['JAKARTA SELATAN', '0', '0', '0'],
                            ['JAKARTA UTARA', '0', '1', '0'],
                            ['JAKARTA TIMUR', '0', '0', '0']]
        impact_layer_attributes = [
            [  # JAKARTA BARAT
                {
                    'INUNDATED': 1,
                    'depth': 2.0,
                    'type': None,
                    'name': None,
                    'osm_id': None
                }, {
                    'INUNDATED': 1,
                    'depth': 2.0,
                    'type': None,
                    'name': None,
                    'osm_id': None
                }, {
                    'INUNDATED': 0,
                    'depth': None,
                    'type': None,
                    'name': None,
                    'osm_id': None
                }
            ],
            [  # JAKARTA PUSAT
                {
                    'INUNDATED': 0,
                    'depth': None,
                    'type': None,
                    'name': None,
                    'osm_id': None
                }
            ],
            [
                # JAKARTA SELATAN
            ],
            [  # JAKARTA UTARA
                {
                    'INUNDATED': 1,
                    'depth': None,
                    'type': None,
                    'name': None,
                    'osm_id': None
                }
            ],
            [
                # JAKARTA TIMUR
            ]
        ]
        self._aggregate(impact_layer,
                        expected_results,
                        impact_layer_attributes=impact_layer_attributes)
예제 #6
0
    def minimum_needs(self, input_layer, population_name):
        """
            Args
                input_layer: InaSAFE layer object assumed to contain
                             population counts
                population_name: Attribute name that holds population count
            Returns
                InaSAFE layer with attributes for minimum needs as per Perka 7


        """

        needs_attributes = []
        for attributes in input_layer.get_data():
            attribute_dict = attributes

            # Get population count
            population = attributes[population_name]
            # Clean up and turn into integer
            if population in ['-', None]:
                displaced = 0
            else:
                population = str(population).replace(',', '')
                try:
                    displaced = int(population)
                except ValueError:
                    QtGui.QMessageBox.information(
                        None, self.tr('Format error'),
                        self.tr('Please change the value of %s in '
                                'attribute %s to integer format') %
                        (population, population_name))
                    raise ValueError

            # Calculate estimated needs based on BNPB Perka 7/2008
            # minimum bantuan

            # 400g per person per day
            rice = int(displaced * 2.8)
            # 2.5L per person per day
            drinking_water = int(displaced * 17.5)
            # 15L per person per day
            water = int(displaced * 105)
            # assume 5 people per family (not in perka)
            family_kits = int(displaced / 5)
            # 20 people per toilet
            toilets = int(displaced / 20)

            # Add to attributes

            attribute_dict['Beras'] = rice
            attribute_dict['Air minum'] = drinking_water
            attribute_dict['Air bersih'] = water
            attribute_dict['Kit keluarga'] = family_kits
            attribute_dict['Jamban'] = toilets

            # Record attributes for this feature
            needs_attributes.append(attribute_dict)

        output_layer = Vector(geometry=input_layer.get_geometry(),
                              data=needs_attributes,
                              projection=input_layer.get_projection())
        return output_layer