In an empty QGIS project, typing world
In the coordinate space at the bottom of the page, you could call up a built-in world map with the administrative boundaries of all countries as shown below.
Next, using the select function, I selected the 8 South Asian countries as highlighted on the map below. QGIS offers the option to select countries by hand, by polygon, by radius and by selecting or deselecting countries individually with a mouse click.
Clipping in QGIS
Removing these countries from the world map is easy in QGIS. You need to go to Vector in the menu -> Select Geoprocessing Tools -> Select Clip. In the options, I checked the checkbox for selected features only on the input layer and ran the process.
The clipping action completed in just 7.24 seconds and I got a new layer called “Cropped”. This is shown with the brown color in the following screenshot. By going to Layer Properties, different coloring options can be used in QGIS in the Symbology option.
Dissolving boundaries in QGIS
Next, he wanted to dissolve the borders between South Asian countries. To do this, I selected all the countries in South Asia. I went to Vector menu -> Select Geoprocessing Tools -> Dissolve. Similar to the previous step, I selected “Selected Highlights Only” in the input layer and ran the algorithm which took only 0.08 seconds. A new layer called “Dissolved” was created where the administrative boundaries between countries were dissolved and appeared as a single unit as shown below:
Displaying the world layer and the dissolved layer at the same time looks like this:
In this section, I am going to demonstrate how I can achieve the same goal in Python using the geopandas package.
In the first step, I read the built-in world map dataset within the geopandas package. Contains the vector data of the world with the administrative boundaries of all countries. This is obtained from the natural land dataset, which is free to use.
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as npworld = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot(color = "lightgrey")
Cropping with geopandas
In my first mail, I demonstrated how it is possible to clip a custom polygon geometry as a mask of the original data frame or geopandas layer. However, for simplicity, I used the filter options to get the layers needed for Asia and South Asia.
asia = world(world.continent == "Asia")
asia.plot(color = "lightgrey")
To filter the South Asia region, I used a list containing the name of each country as a reference.
south_asia_countries = ("Afghanistan", "Bangladesh", "Bhutan", "India",
"Maldives", "Nepal", "Pakistan", "Sri Lanka")south_asia = asia(asia.name.isin(south_asia_countries))
south_asia.plot()
Dissolving borders between South Asian countries using geopandas
To dissolve the borders between South Asian countries, I used the dissolve
feature in geopandas. I passed None as an argument and specified parameters to apply certain aggregate functions, in which the population and GDP in the resulting dissolved data frame would summarize the population and GDP in all South Asian countries. I have yet to figure out how the aggregate function can be applied in QGIS as well.
south_asia_dissolved = south_asia.dissolve(by = None,
aggfunc = {"pop_est":"sum",
"gdp_md_est":"sum"})
south_asia_dissolved.plot(color = "lightgrey"
Dissolving borders between countries within a continent in the world.
Using the same procedure as above, I wanted to dissolve the boundaries between countries within a continent and show different continents distinct from each other on a world map based on the number of countries on each continent.
To do this, I first added a new column called num_countries
in it world
geodataframe that contains 1 as a value. I then dissolved the world map using the continent
column for reference.
world("num_countries") = 1continents_dissolved = world.dissolve(by = "continent",
aggfunc = {"pop_est":"sum",
"gdp_md_est":"sum",
"num_countries":"count"}).reset_index()
continents_dissolved
I used the aggregate function to summarize the population and GDP of all countries on the continent and count the number of countries on each continent. The resulting geodataframe continents_dissolved
look how it is shown:
We see that Asia has the largest population and GDP of all continents. Similarly, we see that Africa has the most countries (51), followed by Asia (47), Europe (39), North America (18), South America (13), and Oceania (7). Antarctica and the Seven Seas (open ocean) are also considered continents in this data set.
Finally, I wanted to draw the world map highlighting the number of countries in each continent with the help of a color map. I achieved this using the following code:
map = continents_dissolved.plot(column = "num_countries",
cmap = "Greens")# Get the current axes
ax = plt.gca()
# Add a horizontal colorbar
cbar = plt.colorbar(map.get_children()(0),
ax=ax,
orientation='horizontal',
aspect = 30 #control the width of color bar. higher value= lower width.
)
# Set a label for the colorbar
cbar.set_label('Number of Countries')
plt.title("Continents of the world based on number of countries")
plt.savefig("Continents dissolved.jpeg",
bbox_inches = "tight",
dpi = 300)
# Show the plot
plt.show()
The resulting map appears as shown below:
Conclusion
In this post, I described ways to dissolve map boundaries using QGIS and geopandas in Python. In the process, I also explained the cropping process and the possibility of using the aggregate function while dissolving map boundaries in geopandas. These processes could be very useful for the manipulation, processing and transformation of geographic maps in the form of vector data sets. The code and QGIS project file for this post are available on this GitHub repository. Thank you for reading!