def __init__(self): from cartopy.io import shapereader fname = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_1_states_provinces') _ = shapereader.Reader(fname) self._records = {x.attributes["iso_3166_2"].replace('\x00', ''): x for x in _.records()}
async def add_country(message, _): resolution = '10m' category = 'cultural' name = 'admin_0_countries' shpfilename = shapereader.natural_earth(resolution, category, name) country = message.content.title() df = geopandas.read_file(shpfilename) result = df.loc[df['ADMIN'] == country] if len(result): data = {} with open("countries.json", "r") as f: data = json.load(f) author = str(message.author) country = message.content.title() print(country) if author in data["people"]: data["countries"][data["people"][author]].remove(author) data["people"][author] = country if country in data["countries"]: data["countries"][country].append(author) else: data["countries"][country] = [author] with open("countries.json", "w") as f: json.dump(data, f) await boxed_message( message.channel, f"Country {country} successfully registered for user {author}") else: await boxed_message( message.channel, "Could not find this country in the database, please check if you spelled it correctly" )
def plotMap(line): plt.figure(num=None, figsize=(12, 9), dpi=150, facecolor='w', edgecolor='k') # Plot AT centerline ax = plt.axes(projection=cartopy.crs.PlateCarree()) ax.set_extent([-88, -66.5, 20, 50]) # US East Coast ax.add_feature(cartopy.feature.LAND) ax.add_feature(cartopy.feature.OCEAN) ax.add_feature(cartopy.feature.COASTLINE) ax.add_feature(cartopy.feature.BORDERS, linestyle=':') ax.add_feature(cartopy.feature.LAKES, alpha=0.5) ax.add_feature(cartopy.feature.RIVERS) shapename = 'admin_1_states_provinces_lakes_shp' states_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename) # convert to Shapely type track = sgeom.LineString(line) for state in shpreader.Reader(states_shp).geometries(): # pick a default color for the land with a black outline, # this will change if the storm intersects with our track facecolor = [0.9375, 0.9375, 0.859375] edgecolor = 'black' if state.intersects(track): facecolor = [0.9, 0.9, 0.92] ax.add_geometries([state], ccrs.PlateCarree(), facecolor=facecolor, edgecolor=edgecolor) # Plot AT Course ax.add_geometries([track], ccrs.PlateCarree(),facecolor='none', edgecolor='red', linewidth=2)
def _nat_earth_shp(resolution='10m', category='cultural', name='admin_0_countries'): shp_file = shapereader.natural_earth(resolution=resolution, category=category, name=name) return shapereader.Reader(shp_file)
def draw_map(ax): men_data = get_data("data/running/men_100m.tsv") women_data = get_data("data/running/women_100m.tsv") shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() # ax.add_feature(cfeature.OCEAN, color=p.blue(shade=30)) ax.add_feature(cfeature.BORDERS, alpha=0.1) ax.add_feature(cfeature.COASTLINE, alpha=0.5) data = Counter([(x[1], x[2]) for x in men_data[:100]]) data.update([(x[1], x[2]) for x in women_data[:100]]) tmp = defaultdict(int) for d in data: tmp[d[1]] += 1 data = {x: tmp[x] / max(tmp.values()) for x in tmp} for country in countries: ct = country.attributes['ADM0_A3'] if ct in data: ax.add_geometries([country.geometry], crs.PlateCarree(), fc=cm(data[ct]), alpha=0.8) ax.set_title("Countries with most records", fontsize=9)
def add_cntry_names(axis, extent, proj=ccrs.PlateCarree()): """Add country names. Parameters: axis (cartopy.mpl.geoaxes.GeoAxesSubplot): cartopy axis. extent (list): geographical limits [min_lon, max_lon, min_lat, max_lat] proj (cartopy.crs projection, optional): geographical projection, PlateCarree default. """ shp_file = shapereader.natural_earth(resolution='10m', \ category='cultural', name='admin_0_countries') shp = shapereader.Reader(shp_file) ext_pts = list( box(extent[0], extent[2], extent[1], extent[3]).exterior.coords) ext_trans = [ccrs.PlateCarree().transform_point(pts[0], pts[1], proj) \ for pts in ext_pts] for rec, point in zip(shp.records(), shp.geometries()): point_x = point.centroid.xy[0][0] point_y = point.centroid.xy[1][0] if ext_trans[2][0] < point_x <= ext_trans[0][0]: if ext_trans[0][1] < point_y <= ext_trans[1][1]: axis.text(point_x, point_y, rec.attributes['NAME'], \ horizontalalignment='center', verticalalignment='center', \ transform=ccrs.PlateCarree(), fontsize=14)
def plot_suli_state(statecode): fig = plt.figure() class LowerThreshold(ccrs.Mercator): @property def threshold(self): return 1 if statecode == 'HI': ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal()) ax.set_extent([-165, -70, 20, 35], ccrs.Geodetic()) elif statecode == 'AK': ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal()) ax.set_extent([-140, -66, 20, 77], ccrs.Geodetic()) else: ax = fig.add_axes([0, 0, 1, 1], projection=LowerThreshold()) ax.set_extent([-126, -66, 24.5, 46], ccrs.Geodetic()) shapename = 'admin_1_states_provinces_lakes_shp' states_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename) for state in shpreader.Reader(states_shp).records(): # pick a default color for the land with a black outline, # this will change if the storm intersects with our track facecolor = [0.9375, 0.9375, 0.859375] edgecolor = 'black' if state.attributes['postal'] == statecode: ax.add_geometries([state.geometry], ccrs.PlateCarree(), facecolor='Gold', edgecolor=edgecolor) else: ax.add_geometries([state.geometry], ccrs.PlateCarree(), facecolor=facecolor, edgecolor=edgecolor) these_students = suli_students[suli_students['State'] == statecode] if len(these_students) < 20: alpha = 1 elif len(these_students) < 80: alpha = .5 else: alpha = .25 unique_colleges = these_students['College'].unique() college_counts = these_students.groupby('College').count().reset_index() for idx in range(len(these_students)): student = these_students.iloc[idx] ax.plot([student['Longitude'],student['Lab Longitude']],[student['Latitude'],student['Lab Latitude']],color='Blue',transform=ccrs.Geodetic(),alpha=alpha) for index, college in college_counts.iterrows(): student = these_students[these_students['College'] == college['College']].reset_index().loc[0] popularity = 1.5*college['Name'] + 5 ax.plot(student['Longitude'],student['Latitude'],color='Blue',transform=ccrs.Geodetic(),markersize=popularity,marker='.',alpha=0.5) for lab in these_students['Host Lab'].unique(): this_lab = natlabs[natlabs['Host Lab'] == lab] ax.plot(this_lab['Lab Longitude'],this_lab['Lab Latitude'],transform=ccrs.Geodetic(),marker='*',markersize=10,color='Gold',markeredgewidth=1,markeredgecolor='Black') ax.set_title('Host National Laboratories for '+str(len(these_students))+' '+statecode+' SULI/CCI students (2014-2016)') fig.savefig('/Users/mbaumer/side_projects/us_hep_funding/docs/_img/'+statecode+'.png',format='png',bbox_inches='tight') plt.close(fig)
def available_data_map(country_market): ax = plt.axes(projection=ccrs.PlateCarree()) ax.add_feature(cartopy.feature.OCEAN) ax.set_extent([-150, 60, -25, 60]) shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() for country in countries: name = country.attributes['sovereignt'] if name in country_market: ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0,1,0), label=country.attributes['adm0_a3']) else: ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor='0.75', label=country.attributes['adm0_a3']) ## mm/dd/yyyy format date = time.strftime("%m/%d/%Y") ax.annotate('Source: Quandl at https://www.quandl.com/c/markets/global-stock-markets', (0,0), (0, -20), xycoords='axes fraction', textcoords='offset points', va='top') ax.set_title("Available National Stock Market Data\n(as of %s)" % date) plt.savefig('available_markets.pdf', bbox_inches='tight')
def get_geometries_ocean(name0): """ Get an iterable of Shapely geometries corrresponding to given countries. """ # Using the Natural Earth feature interface provided by cartopy. # You could use a different source, all you need is the geometries. print(name0) shape_records = Reader( shpreader.natural_earth(resolution='110m', category='Physical', name='ocean')).records() geoms = [] names = [] for country in shape_records: try: geoms += country.geometry except TypeError: geoms.append(country.geometry) if name0 == 'MPac': geoms = add_country(geoms, 'Taiwan') if name0 == 'SCSPhi': geoms = add_country(geoms, 'Philippines') for i in range(len(geoms)): names.append(name0) return geoms, ccrs.PlateCarree()._as_mpl_transform, names
def country_iso_geom(countries, shp_file, admin_key=['ADMIN', 'ADM0_A3']): """ Get country ISO alpha_3, country id (defined as the United Nations Statistics Division (UNSD) 3-digit equivalent numeric codes and 0 if country not found) and country's geometry shape. Parameters ---------- countries : list or dict list of country names (admin0) or dict with key = admin0 name and value = [admin1 names] shp_file : cartopy.io.shapereader.Reader shape file admin_key: str key to find admin0 or subunit name Returns ------- cntry_info : dict key = ISO alpha_3 country, value = [country id, country name, country geometry], cntry_admin1 : dict key = ISO alpha_3 country, value = [admin1 geometries] """ countries_shp = {} list_records = list(shp_file.records()) for info_idx, info in enumerate(list_records): countries_shp[info.attributes[admin_key[0]].title()] = info_idx cntry_info = dict() cntry_admin1 = dict() if isinstance(countries, list): countries = {cntry: [] for cntry in countries} admin1_rec = list() else: admin1_rec = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_1_states_provinces') admin1_rec = shapereader.Reader(admin1_rec) admin1_rec = list(admin1_rec.records()) for country_name, prov_list in countries.items(): country_idx = countries_shp.get(country_name.title()) if country_idx is None: options = [country_opt for country_opt in countries_shp if country_name.title() in country_opt] if not options: options = list(countries_shp.keys()) LOGGER.error('Country %s not found. Possible options: %s', country_name, options) raise ValueError iso3 = list_records[country_idx].attributes[admin_key[1]] try: cntry_id = int(iso_cntry.get(iso3).numeric) except KeyError: cntry_id = 0 cntry_info[iso3] = [cntry_id, country_name.title(), list_records[country_idx].geometry] cntry_admin1[iso3] = _fill_admin1_geom(iso3, admin1_rec, prov_list) return cntry_info, cntry_admin1
def add_pref(ax, linestyle='-', facecolor='none', edgecolor='k', linewidth=0.8): # 10mの解像度のデータ shpfilename = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_1_states_provinces') # # 都道府県のみ取得 provinces = shapereader.Reader(shpfilename).records() provinces_of_japan = filter( lambda province: province.attributes['admin'] == 'Japan', provinces) # # 都道府県境の追加 for province in provinces_of_japan: #print(province.attributes['name']) geometry = province.geometry ax.add_geometries([geometry], ccrs.PlateCarree(), facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, linestyle=linestyle)
def transformToCountry(self, x: float, y: float) -> str: geoms = fiona.open( shpreader.natural_earth(resolution='50m', category='physical', name='land')) land_geom = sgeom.MultiPolygon( [sgeom.shape(geom['geometry']) for geom in geoms]) land = prep(land_geom) if x is not None and y is not None: on = land.contains(sgeom.Point(x, y)) if on: result = rg.search((y, x)) country_full = self.cc.convert(names=result[0]["cc"], to="name_short") country = self.cc.convert(names=result[0]["cc"], to="ISO3") print("Starting in", country_full) plt.title("Starting in " + str(country_full), fontsize=50) plt.scatter(x, y, color="black", marker=".", transform=ccrs.Geodetic()) self.go = True return country else: print("Mer") plt.title("Please choose a location on land !", fontsize=50) return ""
def setMap(self, mapFileName): cmap = mpl.cm.Blues shapename = 'admin_0_countries' countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename) fig, ax = plt.subplots(figsize=(12,6), subplot_kw={'projection': ccrs.Robinson()}) plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.) countriesRecords = shpreader.Reader(countries_shp).records() countryMap = self.createDict(countriesRecords) maxQuantity = max(countryMap.values()) for country in shpreader.Reader(countries_shp).records(): countryName = country.attributes['name_long'] quantity = countryMap[countryName] ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=cmap(quantity / float(maxQuantity), 1), label=countryName) norm = mpl.colors.Normalize(vmin = 1, vmax = maxQuantity) cax = fig.add_axes([0.91, 0.1, 0.02, 0.8]) mpl.colorbar.ColorbarBase(cax, cmap=cmap, norm=norm ) print(maxQuantity) plt.savefig(mapFileName, dpi = 200)
def draw_map(): # Generate counts before drawing the map data = get_data() data = {x[1]: x for x in data if x[2] <= 186} plt.figure(figsize=(11, 9)) ax = plt.subplot(111, projection=crs.Miller()) shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() # ax.add_feature(cfeature.OCEAN, color=p.blue(shade=30)) ax.add_feature(cfeature.BORDERS, alpha=0.5) ax.add_feature(cfeature.COASTLINE, alpha=0.5) for country in countries: ct = country.attributes['ADM0_A3'] if ct in data.keys(): ax.add_geometries([country.geometry], crs.PlateCarree(), fc=p.red(shade=40), alpha=0.8) else: ax.add_geometries([country.geometry], crs.PlateCarree(), fc=p.gray(shade=20), alpha=0.8) plt.title("Economic inequality in the World") plt.savefig("plot.png", dpi=300) plt.show()
def plot(self): ax = plt.axes([0, 0, 1, 1], projection=self.projection_type) ax.background_patch.set_visible(False) ax.outline_patch.set_visible(False) ax.set_extent([-100, -160, 20, 100], ccrs.Geodetic()) shape_filename = shpreader.natural_earth(resolution='110m', category='cultural', name=self.shape_name) reader = shpreader.Reader(shape_filename) states = reader.records() for state in states: name = state.attributes[self.attr] try: color = self.color_map( self.norm_color( self.df.loc[state.attributes[self.attr]][self.title])) ax.add_geometries(state.geometry, ccrs.PlateCarree(), facecolor=color, label=state.attributes[self.attr], edgecolor=self.edge_color, linewidth=.05) except LookupError: print('Could not find \'{}\' in data frame.'.format(name)) plt.savefig(self.save_filename, bbox_inches='tight', pad_inches=0.2, dpi=300)
def _get_geoms(self, _f, attname): """Get Shapely geometries of locations from Natural Earth. Parameters ---------- _f : str Natural Earth file name attname : str Expected attribute name Returns ------- self.geoms : list of geometries """ filename = shapereader.natural_earth(resolution='10m', category='cultural', name=_f) reader = shapereader.Reader(filename) records = list(reader.records()) for _loc in records: if _loc.attributes[attname] in self.loc_names: if _loc.attributes[attname] == 'France': # Only want mainland France. Not its territories try: self.geoms += _loc.geometry[-1] except TypeError: self.geoms.append(_loc.geometry[-1]) else: try: self.geoms += _loc.geometry except TypeError: self.geoms.append(_loc.geometry) return self
def add_populated_places(axis, extent, proj=ccrs.PlateCarree()): """Add city names. Parameters: axis (cartopy.mpl.geoaxes.GeoAxesSubplot): cartopy axis. extent (list): geographical limits [min_lon, max_lon, min_lat, max_lat] proj (cartopy.crs projection, optional): geographical projection, PlateCarree default. """ shp_file = shapereader.natural_earth(resolution='110m', \ category='cultural', name='populated_places_simple') shp = shapereader.Reader(shp_file) ext_pts = list( box(extent[0], extent[2], extent[1], extent[3]).exterior.coords) ext_trans = [ccrs.PlateCarree().transform_point(pts[0], pts[1], proj) \ for pts in ext_pts] for rec, point in zip(shp.records(), shp.geometries()): if ext_trans[2][0] < point.x <= ext_trans[0][0]: if ext_trans[0][1] < point.y <= ext_trans[1][1]: axis.plot(point.x, point.y, 'ko', markersize=7, transform=ccrs.PlateCarree(), markerfacecolor='None') axis.text(point.x, point.y, rec.attributes['name'], \ horizontalalignment='right', verticalalignment='bottom', \ transform=ccrs.PlateCarree(), fontsize=14)
def get_coastlines(bounds=None, resolution=110): """ Get Polygones of coast intersecting given bounds Parameter: bounds (tuple): min_lon, min_lat, max_lon, max_lat in EPSG:4326 resolution (float, optional): 10, 50 or 110. Resolution in m. Default: 110m, i.e. 1:110.000.000 Returns: GeoDataFrame """ resolution = nat_earth_resolution(resolution) shp_file = shapereader.natural_earth(resolution=resolution, category='physical', name='coastline') coast_df = gpd.read_file(shp_file) coast_df.crs = NE_CRS if bounds is None: return coast_df[['geometry']] ex_box = box(bounds[0], bounds[1], bounds[2], bounds[3]) tot_coast = list() for row, line in coast_df.iterrows(): if line.geometry.envelope.intersects(ex_box): tot_coast.append(row) if not tot_coast: ex_box = box(bounds[0] - 20, bounds[1] - 20, bounds[2] + 20, bounds[3] + 20) for row, line in coast_df.iterrows(): if line.geometry.envelope.intersects(ex_box): tot_coast.append(row) return coast_df.iloc[tot_coast][['geometry']]
def get_NL_contour(): shpfilename = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_0_countries') reader = shapereader.Reader(shpfilename) countries = reader.records() for country in countries: if country.attributes['GEOUNIT'] == 'Netherlands': return country.geometry
def test_cut_nasa_esp_pass(self): """Test load_nightlight_nasa function.""" shp_fn = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_0_countries') shp_file = shapereader.Reader(shp_fn) list_records = list(shp_file.records()) for info_idx, info in enumerate(list_records): if info.attributes['ADM0_A3'] == 'AIA': bounds = info.bounds req_files = nl_utils.check_required_nl_files(bounds) files_exist, _ = nl_utils.check_nl_local_file_exists(req_files) nl_utils.download_nl_files(req_files, files_exist) try: nightlight, coord_nl = load_nightlight_nasa( bounds, req_files, 2016) except TypeError: print('MemoryError caught') return self.assertTrue(coord_nl[0, 0] < bounds[1]) self.assertTrue(coord_nl[1, 0] < bounds[0]) self.assertTrue(coord_nl[0, 0] + (nightlight.shape[0] - 1) * coord_nl[0, 1] > bounds[3]) self.assertTrue(coord_nl[1, 0] + (nightlight.shape[1] - 1) * coord_nl[1, 1] > bounds[2])
def drawMapFromOneFile(self, folderPath): thing = "choroMap_" SHAPENAME = 'admin_0_countries' SHP = shpreader.natural_earth(resolution='110m', category='cultural', name=SHAPENAME) ax = plt.axes(projection=ccrs.PlateCarree(), ) ax.figure.set_size_inches(11, 12) selectedCountryData = self.fh.getOne(folderPath) maxScore = self.getMaxScore(selectedCountryData) color_patch = self.getPatches(COLORS1, maxScore) for country in shpreader.Reader(SHP).records(): ax.add_geometries([country.geometry], ccrs.PlateCarree(), facecolor=EARTHCOLOR, edgecolor='black') for scoredCountry in selectedCountryData: if country.attributes['NAME_LONG'] == scoredCountry[4]: ax.add_geometries([country.geometry], ccrs.PlateCarree(), facecolor=self.get_color( int(scoredCountry[1]), maxScore, COLORS1), edgecolor='black', label=country.attributes['NAME_LONG']) plt.legend(handles=color_patch, loc='lower left', bbox_to_anchor=(0, 0)) plt.savefig(self.outFileName(folderPath, thing), format='png', bbox_inches='tight', dpi=300) plt.show()
def prepare(self): shpfilename = shpreader.natural_earth( resolution='50m', category='physical', name='ocean') reader = shpreader.Reader(shpfilename) oceans = reader.geometries() oceans = sgeom.MultiPolygon(oceans) self.geoms = oceans
def __init__(self): self.fig = plt.figure(figsize=(15, 10)) self.ax = self.fig.add_subplot( 1, 1, 1, projection=ccrs.PlateCarree(central_longitude=0)) self.ax.set_extent((-80, -30, -40, 15)) self.ax.coastlines() # Fonte: https://gis.stackexchange.com/questions/88209/python-mapping-in-matplotlib-cartopy-color-one-country shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() for country in countries: if country.attributes['NAME_EN'] == 'Brazil': self.ax.add_geometries([country.geometry], ccrs.PlateCarree(), facecolor='#C8D5B9', label=country.attributes['NAME_EN']) self.ax.add_feature(cfeature.BORDERS) self.ax.add_feature(cfeature.OCEAN)
def get_coastlines(extent=None, resolution=110): """Get latitudes and longitudes of the coast lines inside extent. All earth if no extent. Parameters: extent (tuple, optional): (min_lon, max_lon, min_lat, max_lat) resolution (float, optional): 10, 50 or 110. Resolution in m. Default: 110m, i.e. 1:110.000.000 Returns: np.array (lat, lon coastlines) """ resolution = nat_earth_resolution(resolution) shp_file = shapereader.natural_earth(resolution=resolution, category='physical', name='coastline') with fiona.open(shp_file) as shp: coast_lon, coast_lat = [], [] for line in shp: tup_lon, tup_lat = zip(*line['geometry']['coordinates']) coast_lon += list(tup_lon) coast_lat += list(tup_lat) coast = np.array([coast_lat, coast_lon]).transpose() if extent is None: return coast in_lon = np.logical_and(coast[:, 1] >= extent[0], coast[:, 1] <= extent[1]) in_lat = np.logical_and(coast[:, 0] >= extent[2], coast[:, 0] <= extent[3]) return coast[np.logical_and(in_lon, in_lat)].reshape(-1, 2)
def coastline_geometries( resolution: Optional[str] = COASTLINE_RESOLUTION, ) -> List[np.ndarray]: """ Fetch the Natural Earth shapefile coastline geometries for the required resolution. If the geometries are not already available within the cartopy cache, then they will be downloaded. The 2D longitude (φ) and latitude (λ) xy coastline geometries will be unpacked as 3D xy0 coordinates i.e., φλ0. Parameters ---------- resolution : str, default=COASTLINE_RESOLUTION The resolution of the Natural Earth coastlines, which may be either ``110m``, ``50m`` or ``10m``. Returns ------- List[np.ndarray] A list containing one or more coastline xy0 geometries. Notes ----- .. versionadded:: 0.1.0 """ lines, multi_lines = [], [] category, name = "physical", "coastline" # load in the shapefiles fname = shp.natural_earth(resolution=resolution, category=category, name=name) reader = shp.Reader(fname) def unpack(geometries): for geometry in geometries: if isinstance(geometry, MultiLineString): multi_lines.extend(list(geometry.geoms)) else: xy = np.array(geometry.coords[:], dtype=np.float32) x = xy[:, 0].reshape(-1, 1) y = xy[:, 1].reshape(-1, 1) z = np.zeros_like(x) xyz = np.hstack((x, y, z)) lines.append(xyz) unpack(reader.geometries()) if multi_lines: unpack(multi_lines) logger.debug( "loaded %s geometries", len(lines), ) return lines
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' polynomial transformation [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() 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)) 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, gpd.GeoDataFrame( pd.concat(bkmrbl_list, ignore_index=True)), crs=DEF_CRS) # set metadata self.ref_year = ref_year self.tag = tag self.tag.file_name = fn_nl self.value_unit = 'USD' rows, cols, ras_trans = pts_to_raster_meta( (self.longitude.min(), self.latitude.min(), self.longitude.max(), self.latitude.max()), (coord_nl[0, 1], -coord_nl[0, 1])) self.meta = {'width': cols, 'height': rows, 'crs': self.crs, 'transform': ras_trans}
def plot_countries(df, projection, colors, title, edgecolor='grey', annotation=None): ax = plt.axes(projection=projection) ax.add_feature(cartopy.feature.OCEAN, facecolor='#add8e6') ax.outline_patch.set_edgecolor(edgecolor) values = sorted(list(df.unique())) shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() for country in countries: attribute = 'NAME_EN' # get classification try: classification = df[country.attributes[attribute]] #ici print(country.attributes) break ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(colors[values.index(classification)]), label=country.attributes[attribute], edgecolor='#000000', linewidth=.25) except: # print(country.attributes[attribute]) pass # legend import matplotlib.patches as mpatches handles = [] values = ['< à ' + str(v) for v in values] for i in range(len(values)): handles.append(mpatches.Rectangle((0, 0), 1, 1, facecolor=colors[i])) plt.legend(handles, values, loc='lower left', bbox_to_anchor=(0.025, -0.0), fancybox=True, frameon=False, fontsize=5) plt.title(title, fontsize=8) title = 'maps/' + title + '.png' plt.savefig(title, bbox_inches='tight', pad_inches=.2, dpi=300) print('Saved: {}'.format(title))
def main(): ax = plt.axes([0.01, 0.01, 0.98, 0.98], projection=ccrs.PlateCarree()) ax.set_xlim([-125, -66.5]) ax.set_ylim([20, 50]) states_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_1_states_provinces_shp') lons, lats = sample_data() # to get the effect of having just the states without a map "background" # turn off the outline and background patches ax.background_patch.set_visible(False) ax.outline_patch.set_visible(False) plt.title('US States which intersect the track ' 'of Hurricane Katrina (2005)') # turn the lons and lats into a shapely LineString track = sgeom.LineString(zip(lons, lats)) # buffer the linestring by two degrees (note: this is a non-physical # distance) track_buffer = track.buffer(2) for state in shpreader.Reader(states_shp).geometries(): # pick a default color for the land with a black outline, # this will change if the storm intersects with our track facecolor = [0.9375, 0.9375, 0.859375] edgecolor = 'black' if state.intersects(track): facecolor = 'red' elif state.intersects(track_buffer): facecolor = '#FF7E00' ax.add_geometries([state], ccrs.PlateCarree(), facecolor=facecolor, edgecolor=edgecolor) ax.add_geometries([track_buffer], ccrs.PlateCarree(), facecolor='#C8A2C8', alpha=0.5) ax.add_geometries([track], ccrs.PlateCarree(), facecolor='none') # make two proxy artists to add to a legend direct_hit = mpatches.Rectangle((0, 0), 1, 1, facecolor="red") within_2_deg = mpatches.Rectangle((0, 0), 1, 1, facecolor="#FF7E00") labels = [ 'State directly intersects\nwith track', 'State is within \n2 degrees of track' ] plt.legend([direct_hit, within_2_deg], labels, loc=3, fancybox=True) plt.show()
def test_on_land(): shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') r = reader_shape.Reader.from_shpfiles(shpfilename) assert r.__on_land__ (np.array([10]), np.array([60])) == [ True ] assert r.__on_land__ (np.array([5]), np.array([60])) == [ False]
def coastlines(self, resolution='110m', **kwargs): import cartopy.io.shapereader as shapereader coastline_path = shapereader.natural_earth(resolution=resolution, category='physical', name='coastline') shapereader.mpl_axes_plot(self, shapereader.Reader(coastline_path).geometries(), **kwargs)
def makeMap(countryData, output, maxSpecs): #Define panel names levels = ["Number of species", "BOLD public", "BOLD total", "Total"] #Load details of map shapename = 'admin_0_countries' countries_shp = shpreader.natural_earth(resolution='10m', category='cultural', name=shapename) #Load colourmap cmap = plt.get_cmap("plasma_r") #Make figure plt.figure(figsize=(20, 14)) for r in range(0, 4): ax = plt.subplot(2, 2, r + 1, projection=ccrs.PlateCarree()) for country in shpreader.Reader(countries_shp).records(): code = country.attributes["ADM0_A3_US"] value = countryData.get(code, ["NA", "NA", "NA", "NA"]) if value[1] == "NA": ax.add_geometries(country.geometry, ccrs.PlateCarree(), lw=1, facecolor=(0.9, 0.9, 0.9), edgecolor=(0, 0, 0, 1), alpha=1) else: if r == 0: col = list(cmap(value[r] / maxSpecs)) else: col = list(cmap(value[r])) ax.add_geometries(country.geometry, ccrs.PlateCarree(), lw=1, facecolor=col, edgecolor=(0, 0, 0, 1), alpha=1) #Make lables if r == 0: sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(0, maxSpecs)) else: sm = plt.cm.ScalarMappable(cmap=cmap, norm=plt.Normalize(0, 1)) sm._A = [] cb = plt.colorbar(sm) cb.ax.set_yticklabels(cb.ax.get_yticklabels(), fontsize=8) #Map size ax.set_extent([-25, 52, 30, 70]) ax.set_aspect('auto', adjustable="box") ax.set_title(levels[r], fontsize=10) #Save plt.savefig(output) plt.savefig(output[:-3] + "pdf") plt.close()
def get_country_geometries(country_names=None, extent=None, resolution=10): """Returns a gpd GeoSeries of natural earth multipolygons of the specified countries, resp. the countries that lie within the specified extent. If no arguments are given, simply returns the whole natural earth dataset. Take heed: we assume WGS84 as the CRS unless the Natural Earth download utility from cartopy starts including the projection information. (They are saving a whopping 147 bytes by omitting it.) Same goes for UTF. Parameters: country_names (list, optional): list with ISO3 names of countries, e.g ['ZWE', 'GBR', 'VNM', 'UZB'] extent (tuple, optional): (min_lon, max_lon, min_lat, max_lat) assumed to be in the same CRS as the natural earth data. resolution (float, optional): 10, 50 or 110. Resolution in m. Default: 10m Returns: GeoDataFrame """ resolution = nat_earth_resolution(resolution) shp_file = shapereader.natural_earth(resolution=resolution, category='cultural', name='admin_0_countries') nat_earth = gpd.read_file(shp_file, encoding='UTF-8') if not nat_earth.crs: nat_earth.crs = NE_CRS for idx in nat_earth.index: # fill gaps in nat_earth if nat_earth.loc[idx].ISO_A3=='-99': nat_earth.loc[idx, 'ISO_A3'] = nat_earth.loc[idx].ADM0_A3 if nat_earth.loc[idx].ISO_N3=='-99': for col in ['ISO_A3', 'ADM0_A3', 'NAME']: try: nat_earth.loc[idx, 'ISO_N3'] = iso_cntry.get(nat_earth.loc[idx, col]).numeric except KeyError: nat_earth.loc[idx, 'ISO_N3'] = '-99' else: break if country_names: if isinstance(country_names, str): country_names = [country_names] out = nat_earth[nat_earth.ISO_A3.isin(country_names)] elif extent: bbox = Polygon([ (extent[0], extent[2]), (extent[0], extent[3]), (extent[1], extent[3]), (extent[1], extent[2]) ]) bbox = gpd.GeoSeries(bbox, crs=nat_earth.crs) bbox = gpd.GeoDataFrame({'geometry': bbox}, crs=nat_earth.crs) out = gpd.overlay(nat_earth, bbox, how="intersection") else: out = nat_earth return out
def get_geo_data(self): """Get Geo data from natural earth.""" # Get all countries and create a dictionary by name countries_shp = shpreader.natural_earth( resolution='10m', category='cultural', name='admin_0_countries', ) self.countries = list(shpreader.Reader(countries_shp).records()) self.countries_by_name = {} self.countries_by_iso_a2 = {} for country in shpreader.Reader(countries_shp).records(): self.countries_by_name[country.attributes['NAME_LONG']] = country self.countries_by_iso_a2[country.attributes['ISO_A2']] = country # Get all states and create a dictionary by name states_provinces_shp = shpreader.natural_earth( resolution='50m', category='cultural', name='admin_1_states_provinces', ) # full_list = list(shpreader.Reader(states_provinces_shp).records()) # self.states = [x for x in full_list if x.attributes['type_en'] == 'State'] self.states = list(shpreader.Reader(states_provinces_shp).records()) self.states_by_name = {} for state in self.states: self.states_by_name[state.attributes['name']] = state # Get all timezones and create a dictionary by name timezones_shp = shpreader.natural_earth( resolution='10m', category='cultural', name='time_zones', ) self.timezones = list(shpreader.Reader(timezones_shp).records()) self.timezones_by_name = {} for timezone in shpreader.Reader(timezones_shp).records(): # Try to get the actual name. Something like `Europe/Berlin` timezone_name = timezone.attributes['tz_name1st'] # If there is no name, we default to the utc offset name `-5` `+4.5` if timezone_name == '': timezone_name = timezone.attributes['name'] if timezone_name not in self.timezones_by_name.keys(): self.timezones_by_name[timezone_name] = timezone
def plot_world(txtinfile, pdfoutfile): """ Plot the cartopy map based on Robinson projection. """ cite_count = get_country_counts(txtinfile) RED, YELLOW, GREEN, BLUE = '#FF4500', '#FFFF00', '#26B81C', '#0000FF' shapename = 'admin_0_countries' countries_shp = shpreader.natural_earth(resolution='110m', category='cultural', name=shapename) ax = plt.axes(projection=ccrs.Robinson()) mapped_countries = set() for i, country in enumerate(shpreader.Reader(countries_shp).records()): country_name = country.attributes['name_long'] if country_name not in cite_count: # Do not assign color to the country not in input file ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor='none') continue mapped_countries.add(country_name) country_count = cite_count[country_name] if country_count > 50: gcolor = BLUE elif country_count > 10: gcolor = RED elif country_count > 5: gcolor = YELLOW else: gcolor = GREEN # Add the country to be colored ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=gcolor) plt.title('Geographic distribution of citations') handles, labels = ax.get_legend_handles_labels() # Build legend ge50 = mpatches.Rectangle((0,0), 1, 1, facecolor=BLUE) label_ge50 = 'Citations > 50' ge25 = mpatches.Rectangle((0,0), 1, 1, facecolor=RED) label_ge25 = '10 < Citations <= 50' ge5 = mpatches.Rectangle((0,0), 1, 1, facecolor=YELLOW) label_ge5 = '5 < Citations <= 10' lt5 = mpatches.Rectangle((0,0), 1, 1, facecolor=GREEN) label_lt5 = 'Citations <= 5' handles = [ge50, ge25, ge5, lt5] labels = [label_ge50, label_ge25, label_ge5, label_lt5] # Fix the legend to <loc> ax.legend(handles, labels, loc='lower left', prop={'size': 8}) ax.coastlines() plt.savefig(pdfoutfile) plt.close() return
def load_us_shape(): us_shp = [] shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() for country in countries: if country.attributes['adm0_a3'] == 'USA': us_shp.append(country) us_shp = us_shp[0].geometry return {"us":us_shp}
def iter_country_shapes(resolution): """ Get an iterator of shapereader Records for all Natural Earth countries :param resolution: an instance of Resolutions to iterate countries at. """ geodata = shapereader.natural_earth(resolution=resolution.value, category='cultural', name='admin_0_countries') georeader = shapereader.Reader(geodata) return georeader.records()
def main(): ax = plt.axes([0.01, 0.01, 0.98, 0.98], projection=ccrs.PlateCarree()) ax.set_xlim([-125, -66.5]) ax.set_ylim([20, 50]) states_shp = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_1_states_provinces_shp') lons, lats = sample_data() # to get the effect of having just the states without a map "background" # turn off the outline and background patches ax.background_patch.set_visible(False) ax.outline_patch.set_visible(False) plt.title('US States which intersect the track ' 'of Hurricane Katrina (2005)') # turn the lons and lats into a shapely LineString track = sgeom.LineString(zip(lons, lats)) # buffer the linestring by two degrees (note: this is a non-physical # distance) track_buffer = track.buffer(2) for state in shpreader.Reader(states_shp).geometries(): # pick a default color for the land with a black outline, # this will change if the storm intersects with our track facecolor = [0.9375, 0.9375, 0.859375] edgecolor = 'black' if state.intersects(track): facecolor = 'red' elif state.intersects(track_buffer): facecolor = '#FF7E00' ax.add_geometries([state], ccrs.PlateCarree(), facecolor=facecolor, edgecolor=edgecolor) ax.add_geometries([track_buffer], ccrs.PlateCarree(), facecolor='#C8A2C8', alpha=0.5) ax.add_geometries([track], ccrs.PlateCarree(), facecolor='none') # make two proxy artists to add to a legend direct_hit = mpatches.Rectangle((0, 0), 1, 1, facecolor="red") within_2_deg = mpatches.Rectangle((0, 0), 1, 1, facecolor="#FF7E00") labels = ['State directly intersects\nwith track', 'State is within \n2 degrees of track'] plt.legend([direct_hit, within_2_deg], labels, loc=3, fancybox=True) plt.show()
def _test_natural_earth_available(): """ Test whether we can download the natural earth cartographies. :return: Can we access natural earth? """ from cartopy.io.shapereader import natural_earth from six.moves.urllib.error import HTTPError try: natural_earth_available = natural_earth() except HTTPError: natural_earth_available = False return natural_earth_available
def coastlines_land(self, facecolor=colors['land'], **kwargs): import cartopy.io.shapereader as shapereader land_path = shapereader.natural_earth(resolution='110m', category='physical', name='land') paths = [] for geom in shapereader.Reader(land_path).geometries(): paths.extend(patch.geos_to_path(self.projection.project_geometry(geom))) self.add_collection(mcollections.PathCollection(paths, facecolor=facecolor, **kwargs), autolim=False)
def geometries(self): key = (self.name, self.category, self.scale) if key not in _NATURAL_EARTH_GEOM_CACHE: path = shapereader.natural_earth(resolution=self.scale, category=self.category, name=self.name) geometries = tuple(shapereader.Reader(path).geometries()) _NATURAL_EARTH_GEOM_CACHE[key] = geometries else: geometries = _NATURAL_EARTH_GEOM_CACHE[key] return iter(geometries)
def create_shp_list(shapename_state): states_shp = shpreader.natural_earth(resolution='110m',category='cultural', name=shapename_state) states_shp_list = [] geo_state_list = [] geo_state = [] for item in shpreader.Reader(states_shp).records(): states_shp_list.append(str(item.attributes['name'])) geo_state_list.append(item.geometry) for item in geo_state_list: geo_state.append(item) geo_state_shp = zip(states_shp_list, geo_state) return geo_state_shp
def setup_class(self): RIVERS_PATH = shp.natural_earth(resolution='110m', category='physical', name='rivers_lake_centerlines') self.reader = shp.Reader(RIVERS_PATH) names = [record.attributes['name'] for record in self.reader.records()] # Choose a nice small river self.river_name = 'Peace' self.river_index = names.index(self.river_name) self.test_river_geometry = \ list(self.reader.geometries())[self.river_index] self.test_river_record = list(self.reader.records())[self.river_index]
def plot(countries,values,label='',clim=None,verbose=False): """ Usage: worldmap.plot(countries, values [, label] [, clim]) """ countries_shp = shpreader.natural_earth(resolution='110m',category='cultural', name='admin_0_countries') ## Create a plot fig = plt.figure() ax = plt.axes(projection=ccrs.PlateCarree()) ## Create a colormap cmap = plt.get_cmap('RdYlGn_r') if clim: vmin = clim[0] vmax = clim[1] else: val = values[np.isfinite(values)] mean = val.mean() std = val.std() vmin = mean-2*std vmax = mean+2*std norm = Normalize(vmin=vmin,vmax=vmax) smap = ScalarMappable(norm=norm,cmap=cmap) ax2 = fig.add_axes([0.3, 0.18, 0.4, 0.03]) cbar = ColorbarBase(ax2,cmap=cmap,norm=norm,orientation='horizontal') cbar.set_label(label) ## Add countries to the map for country in shpreader.Reader(countries_shp).records(): countrycode = country.attributes['adm0_a3'] countryname = country.attributes['name_long'] ## Check for country code consistency if countrycode == 'SDS': #South Sudan countrycode = 'SSD' elif countrycode == 'ROU': #Romania countrycode = 'ROM' elif countrycode == 'COD': #Dem. Rep. Congo countrycode = 'ZAR' elif countrycode == 'KOS': #Kosovo countrycode = 'KSV' if countrycode in countries: val = values[countries==countrycode] if np.isfinite(val): color = smap.to_rgba(val) else: color = 'grey' else: color = 'w' if verbose: print("No data available for "+countrycode+": "+countryname) ax.add_geometries(country.geometry,ccrs.PlateCarree(),facecolor=color,label=countryname) plt.show()
def market_change_map(country_market): """ Takes a dictionary of format Country: Return and prints a color gradient map ranging from 5% to -5% as blue to red, respectively """ # Getting the base figure set up fig, ax = plt.subplots(figsize=(12,6), subplot_kw={'projection': ccrs.PlateCarree()}) ax.add_feature(cartopy.feature.OCEAN) ax.set_extent([-150, 60, -25, 60]) shpfilename = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') # Reader and countries both are "generator objects" which can be iterated reader = shpreader.Reader(shpfilename) countries = reader.records() for country in countries: # 'soverignt' is the attribute with full country names name = country.attributes['sovereignt'] # So it is used to match the key of country name in the dictionary if name in country_market: # facecolor is normalized to a 0,1 gradient assuming daily returns capped at -5,5 ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=mpl.cm.bwr_r((country_market[name]+5)/10), label=country.attributes['adm0_a3']) else: ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor='0.75', label=country.attributes['adm0_a3']) # mm/dd/yyyy format and hour:minute for time stamps to indicate when the map was created/what # data was used date = time.strftime("%m/%d/%Y") hour = time.strftime("%H:%M") # Add the color bar for a legend norm = mpl.colors.Normalize(vmin=-5, vmax=5) cax = fig.add_axes([0.95, 0.2, 0.02, 0.6]) cb = mpl.colorbar.ColorbarBase(cax, cmap=mpl.cm.bwr_r, norm=norm, spacing='proportional') cb.set_label('One Day % Change', rotation=270, labelpad=16) cb.ax.yaxis.set_ticks_position('left') ax.set_title("Daily Change in National Stock Markets\n(Taken on %s at %s)" % (date, hour)) ax.annotate('Source: Quandl at https://www.quandl.com/c/markets/global-stock-markets', (0,0), (0, -20), xycoords='axes fraction', textcoords='offset points', va='top') plt.savefig('Daily_Change_%s.pdf' % time.strftime("%m_%d_%Y"), bbox_inches='tight')
def test_robust(self): COASTLINE_PATH = shp.natural_earth() # Make sure all the coastlines can be projected without raising any # exceptions. projection = cartopy.crs.TransverseMercator(central_longitude=-90) reader = shp.Reader(COASTLINE_PATH) all_geometries = list(reader.geometries()) geometries = [] geometries += all_geometries # geometries += all_geometries[48:52] # Aus & Taz # geometries += all_geometries[72:73] # GB # for geometry in geometries: for geometry in geometries[93:]: projection.project_geometry(geometry)
def create_geo_shp_country(shapename): """fixed a bug with encoding""" countries_shp = shpreader.natural_earth(resolution='110m',category='cultural', name=shapename) geo_shp_list = [] geo = [] countries_shp_list = [] for item in shpreader.Reader(countries_shp).records(): geo_shp_list.append(item.geometry) countries_shp_list.append(str(item.attributes['name_long'])) for item in geo_shp_list: geo.append(item) for n,i in enumerate(countries_shp_list): if i=="C\xf4te d'Ivoire": countries_shp_list[n]=str('d Ivoire') geo_country_shp = zip(countries_shp_list, geo) return geo_country_shp
def geometries(self): """ Returns the shapely geometries defined by this Natural Earth dataset. """ key = (self.name, self.category, self.scale) if key not in _NATURAL_EARTH_GEOM_CACHE: path = shapereader.natural_earth(resolution=self.scale, category=self.category, name=self.name) geometries = tuple(shapereader.Reader(path).geometries()) _NATURAL_EARTH_GEOM_CACHE[key] = geometries else: geometries = _NATURAL_EARTH_GEOM_CACHE[key] return iter(geometries)
def create_map(geo_state_shp,output_file): cmap = mpl.cm.Purples shapename_state = 'admin_1_states_provinces_lakes_shp' states_shp = shpreader.natural_earth(resolution='110m',category='cultural', name=shapename_state) ax = plt.axes([0, 0, 1, 1],projection=ccrs.LambertConformal()) ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic()) for item in geo_state_shp: name = item[0] if name in unique_states: i = unique_states.index(name) number = unique_counts[i] ax.add_geometries(item[1], ccrs.PlateCarree(), facecolor=cmap(number/float(maxi), 1), label=name) else: ax.add_geometries(item[1], ccrs.PlateCarree(),facecolor='#FAFAFA',label=name) plt.draw() plt.savefig(output_file)
def mapping(outfile): cmap = mpl.cm.Blues countries_shp = shpreader.natural_earth(resolution='110m',category='cultural', name=shapename) ax = plt.axes(projection=ccrs.Robinson()) for item in geo_country_shp: name = item[0] if name in unique_country: i = unique_country.index(name) number = unique_count[i] ax.add_geometries(item[1], ccrs.PlateCarree(), facecolor=cmap(number/float(maxi), 1), label=name) else: ax.add_geometries(item[1], ccrs.PlateCarree(), facecolor='#FAFAFA', label=name) plt.show() #if you want to save the picture add: plt.draw() plt.savefig(outfile)
def natural_earth_shp(self, name='land', resolution='110m', category='physical', **kwargs): """ Adds the geometries from the specified Natural Earth shapefile to the Axes as a :class:`~matplotlib.collections.PathCollection`. ``**kwargs`` are passed through to the :class:`~matplotlib.collections.PathCollection` constructor. Returns the created :class:`~matplotlib.collections.PathCollection`. .. note:: Currently no clipping is done on the geometries before adding them to the axes. This means, if very high resolution geometries are being used, performance is likely to be severely effected. This should be resolved transparently by v0.5. """ import cartopy.io.shapereader as shapereader kwargs.setdefault('edgecolor', 'face') kwargs.setdefault('facecolor', _colors['land']) key = (name, category, resolution) if key not in _NATURAL_EARTH_GEOM_CACHE: coastline_path = shapereader.natural_earth(resolution=resolution, category=category, name=name) geoms = tuple(shapereader.Reader(coastline_path).geometries()) # put the geoms in the cache _NATURAL_EARTH_GEOM_CACHE[key] = geoms else: geoms = _NATURAL_EARTH_GEOM_CACHE[key] return self.add_geometries(geoms, ccrs.Geodetic(), **kwargs)
def draw_map(self, area, ax=None): if ax is None: ax = plt.gca() if area == 'japan': ax.coastlines(resolution='10m', lw=0.5) else: # acquire the provinces dataset from Natural Earth shpfilename = shapereader.natural_earth(resolution='10m', category='cultural', name='admin_1_states_provinces_lines') # get shapefile records reader = shapereader.Reader(shpfilename) provinces = reader.records() # filter provinces of Japan provinces_of_japan = filter(lambda province: province.attributes['adm0_name'] == 'Japan', provinces) # plot for province in provinces_of_japan: geometry = province.geometry ax.add_geometries(geometry, ccrs.PlateCarree(), facecolor='none', linestyle='-', lw=0.5) ax.coastlines(resolution='10m', lw=0.5)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with cartopy. If not, see <https://www.gnu.org/licenses/>. from __future__ import (absolute_import, division, print_function) import numpy as np from numpy.testing import assert_array_almost_equal import cartopy.io.shapereader as shp LAKES_PATH = shp.natural_earth(resolution='110m', category='physical', name='lakes') RIVERS_PATH = shp.natural_earth(resolution='110m', category='physical', name='rivers_lake_centerlines') class TestLakes(object): def setup_class(self): self.reader = shp.Reader(LAKES_PATH) def _assert_geometry(self, geometry): assert geometry.type == 'MultiPolygon' assert len(geometry) == 1 polygon = geometry[0]
new_d14c_cube.data = d14c_data_for_cube new_dpco2_cube.data = dpco2_data_for_cube new_d14c_cube.coord('time').points = (d14c_cube.coord('time').points-11)/100 new_dpco2_cube.coord('time').points = (d14c_cube.coord('time').points-11)/100 new_d14c_cube.long_name = d14c_cube.long_name new_dpco2_cube.long_name = dpco2_cube.long_name new_d14c_cube.standard_name = d14c_cube.standard_name new_dpco2_cube.standard_name = dpco2_cube.standard_name new_d14c_cube.units = d14c_cube.units new_dpco2_cube.units = dpco2_cube.units new_d14c_cube.var_name = d14c_cube.var_name new_dpco2_cube.var_name = dpco2_cube.var_name shpfilename = natural_earth(resolution='110m', category='physical', name='land') reader = Reader(shpfilename) continents = reader.records() new_d14c_cube.coord('latitude').guess_bounds() new_d14c_cube.coord('longitude').guess_bounds() new_dpco2_cube.coord('latitude').guess_bounds() new_dpco2_cube.coord('longitude').guess_bounds() continent_geometries = reader.geometries() # NB. Switched from using records() all_continents_geometry = cascaded_union(list(continent_geometries)) area_weights = geometry_area_weights(new_d14c_cube, all_continents_geometry) land_mask = np.where(area_weights > 0, True, False) new_d14c_cube.data = np.ma.array(new_d14c_cube.data, mask=land_mask) area_weights = geometry_area_weights(new_dpco2_cube, all_continents_geometry) land_mask = np.where(area_weights > 0, True, False)
'Michigan':'g', 'Mississippi':'b', 'Missouri':'g', 'New Hampshire':'g', 'New York':'g', 'North Carolina':'g', 'Rhode Island':'g', 'South Carolina':'g', 'Tennessee':'b', 'Vermont':'g', 'Virginia':'g', 'West Virginia':'g', 'District of Columbia':'g' } plt.figure() statesShape = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_1_states_provinces_shp') ax = plt.axes([0.01, 0.01, 0.98, 0.98], projection=ccrs.PlateCarree()) ax.set_xlim([-175, -66.5]) ax.set_ylim([15, 80]) cmap = mpl.cm.Blues # finish the map for state in shpreader.Reader(statesShape).records(): name = state.attributes['name'] #print '%s\t%s' % (name, state.attributes['name_alt']) #see if key exists if name in states: ax.add_geometries(state.geometry, ccrs.PlateCarree(), facecolor='b', alpha=0.4, label=name) else:
def create_world_map(): """ This function reads all the data for the countries concerning the main tourist theme (from file). It creates a world map of the tested countries colored to each theme's color. """ # read the dictionary with the countries and the main theme countries_theme_dict = dict() with open('country_main_themes.txt') as main_themes_file: for entry in main_themes_file: country, theme = entry.replace('\n', '').split(':') countries_theme_dict[country] = theme # read all the themes list themes_list = [] counter = 0 with open(create_matrices.TRAIN_MATRICES_EXPORT_PATH + 'class_themes.txt')\ as class_labels: for theme in class_labels: themes_list.append(theme.replace('\n','')) counter += 1 # get unique color for each theme color_list = get_colors(len(themes_list)) # map each color to the theme string theme_color = dict() for i in range(len(themes_list)): theme_color[themes_list[i]] = color_list[i] # create a mapping between country name and its ISO 3-digit code countries_name_dict = dict() for country in countries_theme_dict: # filter out any name mismatch try: country_code = iso3166.countries_by_name[country.upper()][2] countries_name_dict[country_code] = country except KeyError: # print country pass # create the world map fig = plt.Figure() fig.set_canvas(plt.gcf().canvas) ax = plt.axes(projection=ccrs.PlateCarree()) ax.add_feature(cartopy.feature.LAND, linewidth=0.5, edgecolor='white') ax.add_feature(cartopy.feature.OCEAN) ax.set_extent([-150,60,-25,60]) shpf = shpreader.natural_earth(resolution='110m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpf) countries = reader.records() for country in countries: if country.attributes['adm0_a3'] in countries_name_dict.keys(): ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=theme_color[ countries_theme_dict[ countries_name_dict[ country.attributes['adm0_a3']]]], linewidth=0.5, edgecolor='white', label=country.attributes['adm0_a3']) else: ax.add_geometries(country.geometry, ccrs.PlateCarree(), facecolor=(0.3,0.3,0.3), linewidth=0.5, edgecolor='white', label=country.attributes['adm0_a3']) plt.title('World map of countries colored in main tourist theme') # === create the legend === # create list with rectangle colors for the legend legend_color_list = [] for color in color_list: temp = mpatches.Rectangle((0, 0), 1, 1, facecolor=color) legend_color_list.append(temp) legend_color_list.append(mpatches.Rectangle((0, 0), 1, 1, facecolor=(0.3,0.3,0.3))) # create labels for the legend legend_labels_list = [] for theme in themes_list: legend_labels_list.append(theme) legend_labels_list.append('not tested') ncol = 4 plt.legend(legend_color_list, legend_labels_list, loc='lower left', bbox_to_anchor=(0.025, -0.3), fancybox=True, prop={'size':6}, ncol=ncol) # save figure to file fig.savefig("world_map.png", format='png', dpi=200, bbox_inches='tight')
#earth_colors = itertools.cycle(earth_colors) # # #count = [] #ax = plt.axes(projection=ccrs.PlateCarree()) #for country in shpreader.Reader(countries_shp).records(): # count.append(country.attributes[u'name_long']) # print country.attributes['name_long'], earth_colors.next() # ax.add_geometries(country.geometry, ccrs.PlateCarree(), # facecollsor=earth_colors.next())#, # #label=country.attributes['name_long']) # #plt.show() shpfilename = shpreader.natural_earth(resolution='50m', category='cultural', name='admin_0_countries') reader = shpreader.Reader(shpfilename) countries = reader.records() #country = next(countries) #print type(country.attributes) #print sorted(country.attributes.keys()) norm = mpl.colors.Normalize(vmin=0, vmax=1338612970.0) cmap = plt.cm.RdYlBu_r fig, ax = plt.subplots(subplot_kw={'projection': ccrs.PlateCarree()}) #plt.figure()
__author__ = 'mattdyer' #start here when the script is launched if (__name__ == '__main__'): #set up the option parser parser = OptionParser() #add the options to parse parser.add_option('-c', '--csv', dest='csv', help='The CSV file') parser.add_option('-o', '--output', dest='output', help='The output directory') (options, args) = parser.parse_args() #start to build the map countries = {} shapeName = 'admin_0_countries' countriesShape = shpreader.natural_earth(resolution='110m', category='cultural', name=shapeName) plt.figure(figsize=(11, 8), dpi=150) ax = plt.axes(projection=ccrs.Robinson()) #we will build a quick hash to store valid country names to check against input for country in shpreader.Reader(countriesShape).records(): name = country.attributes['name_long'] print name countries[name] = 1 #read in the CSV data max = 0.0; values = {} fileHandle = open(options.csv, 'r')
def _obtain_ne(resolution, category, name, title, names='name', abbrevs='postal' , numbers='index', coords='geometry', query=None, combine_coords=False): """ create Regions_cls from natural_earth data http://www.naturalearthdata.com Parameters ---------- resolution : string Resolution of the dataset ('10m', '50m' or '110m'). category : string Natural earth categories ('cultural', 'physical'). name : string Name of natural earth dataset. title : string Displayed text in Regions_cls. names : string or list Names of the single regions (Region_cls). If string, obtains them from the geopandas DataFrame, else uses the provided list. abbrevs : string or list Abbreviations of the single regions (Region_cls). If string obtains them from the geopandas DataFrame, else uses the provided list. numbers : string or list Numbers of the single regions (Region_cls). If string obtains them from the geopandas DataFrame, else uses the provided list. coords : string or list Coordinates of the single regions (Region_cls). If string obtains them from the geopandas DataFrame, else uses the provided list. query : None or string If given, the geopandas DataFrame is subset with df.query(query). Optional, default None. combine_coords : bool If False, uses the coords as is, else combines them all to a shapely MultiPolygon (used to combine all land Polygons). Optional, default False. """ from cartopy.io import shapereader import geopandas # maybe download natural_earth feature and return filename shpfilename = shapereader.natural_earth(resolution, category, name) # read the file with geopandas df = geopandas.read_file(shpfilename) # subset the whole dataset if necessary if query is not None: df = df.query(query).reset_index(drop=True) # get necessary data for Regions_cls numbers = _maybe_get_column(df, numbers) names = _maybe_get_column(df, names) abbrevs = _maybe_get_column(df, abbrevs) coords = _maybe_get_column(df, coords) # create one MultiPolygon of all Polygons (used for land) if combine_coords: from shapely import geometry coords = [geometry.MultiPolygon([p for p in coords])] # make sure numbers is a list numbers = np.array(numbers) return Regions_cls(title, numbers, names, abbrevs, coords)
# Locate gridpoint inside US fn = Dataset(PATH_AOD+'GOCART_output_20020701_daily_average.nc4',mode='r') lona = fn.variables['Longitude'][:] lata = fn.variables['Latitude' ][:] lata = lata.T lona = lona.T fn.close() mask = (lona>-125) & (lona<-65) & (lata>24) & (lata<50) LN=lona[mask] LT=lata[mask] code = ('AL','AZ','AR','CA','CO','CT','DE','FL','GA','ID','IL','IN','IA','KS','KY','LA','ME','MD','MA','MI','MN','MS','MO','MT','NE','NV','NH','NJ','NM','NY','NC','ND','OH','OK','OR','PA','RI','SC','SD','TN','TX','UT','VT','VA','WA','WV','WI','WY') cord = sgeom.MultiPoint(list(zip(LN,LT))) shpfilename = shpreader.natural_earth(category='cultural', resolution='110m', name='admin_1_states_provinces_lakes') states = shpreader.Reader(shpfilename).records() CORD=np.empty([LN.size,2])*0. k=0 for state in states: name = state.attributes['postal'] if name in code: print name for i in range(0,LN.size): if state.geometry.contains(cord[i]): CORD[k,:]=[cord[i].x, cord[i].y] k=k+1 CORD=zip((CORD[CORD[:,0]!=0,0]),(CORD[CORD[:,1]!=0,1])) CORDS=pd.DataFrame(CORD) CORDS=CORDS.drop_duplicates()