def test_get_country_geometries_country_norway_pass(self): """ test correct numeric ISO3 for country Norway """ iso_countries = ['NOR'] extent = [10, 11, 55, 60] res1 = get_country_geometries(iso_countries) res2 = get_country_geometries(extent=extent) self.assertEqual(res1.ISO_N3.values[0], '578') self.assertIn('578', res2.ISO_N3.values) self.assertIn('NOR', res2.ISO_A3.values) self.assertIn('Denmark', res2.NAME.values) self.assertIn('Norway', res2.NAME.values) self.assertNotIn('Sweden', res2.NAME.values)
def test_get_country_geometries_country_pass(self): """ get_country_geometries with selected countries. issues with the natural earth data should be caught by test_get_land_geometry_* since it's very similar """ iso_countries = ['NLD', 'VNM'] res = get_country_geometries(iso_countries, resolution=110) self.assertIsInstance(res, geopandas.geodataframe.GeoDataFrame)
def _set_geometry(self, extent=None, geometry=None, country=None): self.meta = {} if extent is not None: self.meta['extent'] = extent else: extent = (-180, 180, -90, 90) lon_min, lon_max, lat_min, lat_max = extent extent_poly = gpd.GeoSeries(Polygon([(lon_min, lat_min), (lon_min, lat_max), (lon_max, lat_max), (lon_max, lat_min)]), crs=NE_CRS) self.geometry = gpd.GeoDataFrame({'geometry': extent_poly}, crs=NE_CRS) if country is not None: self.meta['country'] = country if country == "all": country = None elif not isinstance(country, list): country = [country] country_geom = get_country_geometries(country_names=country) self.geometry = gpd.overlay(self.geometry, country_geom, how="intersection") if geometry is not None: self.meta['geometry'] = repr(geometry) self.geometry = gpd.overlay(self.geometry, geometry, how="intersection")
def test_get_country_geometries_extent_pass(self): """get_country_geometries by selecting by extent""" lat = np.array([28.203216, 28.555994, 28.860875]) lon = np.array([-16.567489, -18.554130, -9.532476]) res = get_country_geometries(extent=( np.min(lon), np.max(lon), np.min(lat), np.max(lat) )) self.assertIsInstance(res, geopandas.geodataframe.GeoDataFrame) self.assertTrue( np.allclose(res.bounds.iloc[1, 1], lat[0]) ) self.assertTrue( np.allclose(res.bounds.iloc[0, 0], -11.800084333105298) or np.allclose(res.bounds.iloc[1, 0], -11.800084333105298) ) self.assertTrue( np.allclose(res.bounds.iloc[0, 2], np.max(lon)) or np.allclose(res.bounds.iloc[1, 2], np.max(lon)) ) self.assertTrue( np.allclose(res.bounds.iloc[0, 3], np.max(lat)) or np.allclose(res.bounds.iloc[1, 3], np.max(lat)) )
def set_region_id(self): """ Set region_id attribute for every pixel or point """ lon_ne, lat_ne = self._ne_crs_xy() LOGGER.debug('Setting region_id %s points.', str(self.lat.size)) countries = get_country_geometries(extent=(lon_ne.min(), lon_ne.max(), lat_ne.min(), lat_ne.max())) self.region_id = np.zeros(lon_ne.size, dtype=int) for geom in zip(countries.geometry, countries.ISO_N3): select = contains(geom[0], lon_ne, lat_ne) self.region_id[select] = int(geom[1])
def set_region_id(self): """ Set the region_id to the adm0 ISO_N3 (country) code as indicated by natural earth data. Currently simply iterates over all countries in extent given by Centroids instance. Not terribly efficient; could implement a raster burn method if centroids lie on regular grid. Could also employ parallelization. Take heed: the natural earth dataset has errors and misclassifies, among others, Norway, Somaliland, and Kosovo, using -99 instead of their assigned codes. Have a look at the natural earth shapefiles and attribute tables. """ countries = get_country_geometries(extent=self.extent) self.region_id = np.zeros(self.size, dtype=int) for geom in zip(countries.geometry, countries.ISO_N3): select = shapely.vectorized.contains(geom[0], self.lon, self.lat) self.region_id[select] = geom[1]
def test_get_country_geometries_all_pass(self): """get_country_geometries with no countries or extent; i.e. the whole earth""" res = get_country_geometries(resolution=110) self.assertIsInstance(res, geopandas.geodataframe.GeoDataFrame) self.assertAlmostEqual(res.area[0], 1.639510995900778)