Esempio n. 1
0
    def test_concat_fail(self):
        """Test failing concat function with fake data."""

        with self.assertRaises(TypeError):
            Exposures.concat([
                self.dummy, self.dummy.gdf, self.dummy.gdf.values, self.dummy
            ])
Esempio n. 2
0
    def test_concat_pass(self):
        """Test concat function with fake data."""

        self.dummy.check()

        catexp = Exposures.concat([self.dummy, self.dummy.gdf, pd.DataFrame(self.dummy.gdf.values, columns=self.dummy.gdf.columns), self.dummy])
        self.assertEqual(self.dummy.gdf.shape, (10,5))
        self.assertEqual(catexp.gdf.shape, (40,5))
        self.assertTrue(u_coord.equal_crs(catexp.crs, 'epsg:3395'))
Esempio n. 3
0
    def set_countries(self,
                      countries,
                      ref_year=2016,
                      res_km=None,
                      from_hr=None,
                      admin_file='admin_0_countries',
                      **kwargs):
        """ Model countries using values at reference year. If GDP or income
        group not available for that year, consider the value of the closest
        available year.

        Parameters:
            countries (list or dict): list of country names (admin0 or subunits)
                or dict with key = admin0 name and value = [admin1 names]
            ref_year (int, optional): reference year. Default: 2016
            res_km (float, optional): approx resolution in km. Default:
                nightlights resolution.
            from_hr (bool, optional): force to use higher resolution image,
                independently of its year of acquisition.
            admin_file (str): file name, admin_0_countries or admin_0_map_subunits
            kwargs (optional): 'gdp' and 'inc_grp' dictionaries with keys the
                country ISO_alpha3 code. 'poly_val' list of polynomial coefficients
                [1,x,x^2,...] to apply to nightlight (DEF_POLY_VAL used if not
                provided). If provided, these are used.
        """
        admin_key_dict = {
            'admin_0_countries': ['ADMIN', 'ADM0_A3'],
            'admin_0_map_subunits': ['SUBUNIT', 'SU_A3']
        }

        shp_file = shapereader.natural_earth(resolution='10m',
                                             category='cultural',
                                             name=admin_file)
        shp_file = shapereader.Reader(shp_file)

        cntry_info, cntry_admin1 = country_iso_geom(countries, shp_file,
                                                    admin_key_dict[admin_file])
        fill_econ_indicators(ref_year, cntry_info, shp_file, **kwargs)

        nightlight, coord_nl, fn_nl, res_fact, res_km = get_nightlight(
            ref_year, cntry_info, res_km, from_hr)

        tag = Tag(file_name=fn_nl)
        bkmrbl_list = []

        for cntry_iso, cntry_val in cntry_info.items():

            bkmrbl_list.append(
                self._set_one_country(cntry_val, nightlight, coord_nl,
                                      res_fact, res_km,
                                      cntry_admin1[cntry_iso], **kwargs).gdf)
            tag.description += ("{} {:d} GDP: {:.3e} income group: {:d} \n").\
                format(cntry_val[1], cntry_val[3], cntry_val[4], cntry_val[5])

        Exposures.__init__(self,
                           data=Exposures.concat(bkmrbl_list).gdf,
                           crs=DEF_CRS,
                           ref_year=ref_year,
                           tag=tag,
                           value_unit='USD')

        rows, cols, ras_trans = pts_to_raster_meta(
            (self.gdf.longitude.min(), self.gdf.latitude.min(),
             self.gdf.longitude.max(), self.gdf.latitude.max()),
            (coord_nl[0, 1], -coord_nl[0, 1]))
        self.meta = {
            'width': cols,
            'height': rows,
            'crs': self.crs,
            'transform': ras_trans
        }