Country Stickers

I would like to build a trajectory on the base map and specify the labels (names) of the country as an overlay.

Here is the current code and the map they create:

import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap path = "path\\to\\data" animal_data = pd.DataFrame.from_csv(path, header=None) animal_data.columns = ["date", "time", "gps_lat", "gps_long"] # data cleaning omitted for clarity params = { 'projection':'merc', 'lat_0':animal_data.gps_lat.mean(), 'lon_0':animal_data.gps_long.mean(), 'resolution':'h', 'area_thresh':0.1, 'llcrnrlon':animal_data.gps_long.min()-10, 'llcrnrlat':animal_data.gps_lat.min()-10, 'urcrnrlon':animal_data.gps_long.max()+10, 'urcrnrlat':animal_data.gps_lat.max()+10 } map = Basemap(**params) map.drawcoastlines() map.drawcountries() map.fillcontinents(color = 'coral') map.drawmapboundary() x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values) map.plot(x, y, 'b-', linewidth=1) plt.show() 

The result is a map: Migration

This is a map of the trajectory of a migratory bird. Although this is a very good map (!), I need country names, so it’s easy to identify the countries in which the bird flies.

Is there a direct way to add country names?

+7
python matplotlib gis matplotlib-basemap
source share
1 answer

My decision depends on an external data file, which may or may not be available in the future. However, similar data can be found elsewhere, so this should not be a big problem.

Firstly, the code for printing the names of country names:

 import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap class MyBasemap(Basemap): def printcountries(self, d=3, max_len=12): data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt", sep=";", skiprows=28 ) data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)] for ix, country in data.iterrows(): plt.text(*self(country.longitude, country.latitude), s=country.BGN_name[:max_len]) 

All you need to do is download the country database from here , then select the countries that are currently on the map and mark them.

Full code:

 import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.basemap import Basemap class MyBasemap(Basemap): def printcountries(self, d=3, max_len=12): data = pd.io.parsers.read_csv("http://opengeocode.org/cude/download.php?file=/home/fashions/public_html/opengeocode.org/download/cow.txt", sep=";", skiprows=28 ) data = data[(data.latitude > self.llcrnrlat+d) & (data.latitude < self.urcrnrlat-d) & (data.longitude > self.llcrnrlon+d) & (data.longitude < self.urcrnrlon-d)] for ix, country in data.iterrows(): plt.text(*self(country.longitude, country.latitude), s=country.BGN_name[:max_len]) path = "path\\to\\data" animal_data = pd.DataFrame.from_csv(path, header=None) animal_data.columns = ["date", "time", "gps_lat", "gps_long"] params = { 'projection':'merc', 'lat_0':animal_data.gps_lat.mean(), 'lon_0':animal_data.gps_long.mean(), 'resolution':'h', 'area_thresh':0.1, 'llcrnrlon':animal_data.gps_long.min()-10, 'llcrnrlat':animal_data.gps_lat.min()-10, 'urcrnrlon':animal_data.gps_long.max()+10, 'urcrnrlat':animal_data.gps_lat.max()+10 } plt.figure() map = MyBasemap(**params) map.drawcoastlines() map.fillcontinents(color = 'coral') map.drawmapboundary() map.drawcountries() map.printcountries() x, y = map(animal_data.gps_long.values, animal_data.gps_lat.values) map.plot(x, y, 'b-', linewidth=1) plt.show() 

and finally the result:

labeld-map

It is clear that this is not as carefully labeled as one might hope, and some heuristics regarding the size of the country, the length of the name and the size of the map must be implemented to make it perfect, but this is a good starting point.

+6
source share

All Articles