Home Machine Learning Dissolving map boundaries in QGIS and Python | by Himalaya Bir Shrestha | Might, 2024

Dissolving map boundaries in QGIS and Python | by Himalaya Bir Shrestha | Might, 2024

0
Dissolving map boundaries in QGIS and Python | by Himalaya Bir Shrestha | Might, 2024

[ad_1]

In an empty QGIS undertaking, by typing world within the coordinate area within the backside of the web page, I may name an in-built map of the world with administrative boundaries of all of the international locations as proven under.

Getting a world map in QGIS. Picture by Writer.

Subsequent, through the use of the choose characteristic, I chosen the 8 international locations of South Asia as highlighted within the map under. QGIS presents the choice to pick out international locations by hand, by polygon, by radius, and by individually choosing or deselecting international locations with a mouse click on.

Choosing international locations from the world map. Picture by Writer.

Clipping in QGIS

Clipping these international locations off of the world map is simple in QGIS. One must go to Vector within the menu-> Choose Geoprocessing instruments -> Choose Clip. Within the choices, I ticked on the test field for the Chosen options solely within the Enter layer and ran the method.

Working Clipping algorithm. Picture by Writer.

The clipping motion was accomplished in 7.24 seconds alone and I acquired a brand new layer known as “Clipped”. That is depicted by the brown coloration within the screenshot under. By going to Properties of the layer, one can use completely different coloring choices in QGIS within the Symbology possibility.

New Clipped layer is created. Picture by Writer.

Dissolving boundaries in QGIS

Subsequent, I wished to dissolve the boundaries between international locations in South Asia. For this, I chosen all of the international locations in South Asia. I went to the Vector Menu -> Choose Geoprocessing Instruments ->Dissolve. Just like the earlier step, I chosen “Chosen featured solely” within the enter layer and ran the algorithm which took simply 0.08 seconds. A brand new layer known as “Dissolved” was created the place the executive boundaries between international locations had been dissolved and appeared as a single unit as proven under:

New Dissolved layer is created. Picture by Writer.

Visualizing each the world layer and Dissolved layer on the identical time appears as proven under:

Dissolved layer and world layer. Picture by Writer.

On this part, I’m going to reveal how I may the identical goal in Python utilizing the geopandas package deal.

In step one, I learn the in-built dataset of the world map inside the geopandas package deal. It accommodates the vector knowledge of the world with the administative boundaries of all counntries. That is obtained from the Pure Earth dataset, which is free to make use of.

import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt
import numpy as np

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
world.plot(coloration = "lightgrey")

Plotting world map in geopandas. Picture by Writer.

Clipping with geopandas

In my very first put up, I demonstrated how it’s potential to clip off a customized Polygon geometry as a masks from the unique geopandas dataframe or layer. Nonetheless, for simplicity, I simply used the filter choices to acquire the required layers for Asia and South Asia.

asia = world[world.continent == "Asia"]
asia.plot(coloration = "lightgrey")
Filtering Asia continent from world. Picture by Writer.

To filter the South Asia area, I used an inventory containing the identify of every nation 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()

Filtering South Asia area from Asia. Picture by Writer.

Dissolve boundaries between international locations in South Asia utilizing geopandas

To dissolve the boundaries between international locations in South Asia, I used the dissolve characteristic in geopandas. I handed None as an argument, and specified parameters to use sure mixture features, wherein the inhabitants and GDP within the ensuing dissolved dataframe would sum up the inhabitants and GDP in all international locations in South Asia. I’m but to determine how the mixture operate can be utilized in QGIS.

south_asia_dissolved = south_asia.dissolve(by = None,
aggfunc = {"pop_est":"sum",
"gdp_md_est":"sum"})
south_asia_dissolved.plot(coloration = "lightgrey"
Administrative boundaries between international locations in South Asia are dissolved. Picture by Writer.

Dissolving boundaries between international locations inside a continent on the planet

Utilizing the identical process as above, I wished to dissolve the boundaries between international locations inside a continent and present completely different continents distinct from one another in a world map primarily based on the variety of international locations in every continent.

For this function, first I added a brand new column known as num_countries within the world geodataframe containing 1 as a price. Then I dissolved the world map utilizing the continent column as a reference.

world["num_countries"] = 1

continents_dissolved = world.dissolve(by = "continent",
aggfunc = {"pop_est":"sum",
"gdp_md_est":"sum",
"num_countries":"rely"}).reset_index()

continents_dissolved

I used the mixture operate to sum up the inhabitants and GDP in all international locations within the continent and rely the variety of international locations in every continent. The ensuing geodataframe continents_dissolved look as proven:

Ensuing continents_dissolved geopandas dataframe.

We see that Asia has the most important inhabitants and GDP of all continents. Equally, we see that Africa has essentially the most international locations (51) adopted by Asia (47), Europe (39), North America (18), South America (13), and Oceania (7). Antarctica and Seven seas (open ocean) are additionally considered continents on this dataset.

Lastly, I wished to plot the world map highlighting the variety of international locations in every continent with the assistance of a coloration map. I achieved this utilizing the next code:

map = continents_dissolved.plot(column = "num_countries",
cmap = "Greens")

# Get the present axes
ax = plt.gca()

# Add a horizontal colorbar
cbar = plt.colorbar(map.get_children()[0],
ax=ax,
orientation='horizontal',
side = 30 #management the width of coloration bar. increased worth= decrease width.
)

# Set a label for the colorbar
cbar.set_label('Variety of International locations')

plt.title("Continents of the world primarily based on variety of international locations")

plt.savefig("Continents dissolved.jpeg",
bbox_inches = "tight",
dpi = 300)

# Present the plot
plt.present()

The ensuing map seems as proven under:

Map of the world the place the colour displays variety of international locations in every continent. Picture by Writer.

Conclusion

On this put up, I described methods to dissolve map boundaries utilizing QGIS and geopandas in Python. Within the course of, I additionally defined the clipping course of and the potential of utilizing mixture operate whereas dissolving the map boundaries in geopandas. These processes might be very helpful for the manipulation, processing, and transformation of geographical maps within the type of vector datasets. The code and the QGIS undertaking file for this put up can be found on this GitHub repository. Thanks for studying!

[ad_2]