Geographic maps and their Mapping in Python
Have you ever wondered how those stunning maps of the world are made? The interactive data visualisations on Our World in Data cover a wide range of topics, including population growth, violence, political power, education, culture, and many other issues that are important to us. These representations aid in our comprehension of the causes and effects of recent global change. This quantity of knowledge piqued my interest and gave me the desire to learn more.
When defining a physical object’s position or that of any other entity, such as a town, a city, a street, a structure, etc, We refer to this type of data as “spatial data,” also known as geospatial data, GIS data, or geodata, since it uses a geographic coordinate system. Using spatial data, you may ascertain an object’s length, size, area, and shape in addition to its location. The GeoPandas and GeoPlot libraries are required to interact with geographical data in Python.
Of course, when we talk about geographic data, we think of a data point’s coordinates, which are its longitude and latitude. They are merely the X and Y coordinates for a particular location on the map, which is correct. Other geographic data types, such as polygon and line data, do exist. Here, a map’s point data will be the primary subject of attention. With TechVidvan let’s start and learn about Mapping Geographic Data in Python.
What is Spatial Data?
Any piece of information that, either directly or indirectly, refers to a specific geographic region or location is referred to as “spatial data.” Spatial data is a numerical representation of a physical object in a geographical coordinate system. Although there are many other kinds of spatial data, geometric and geographic data are the most prevalent. Let’s attempt to comprehend these two types of facts.
1. Geometric Data:
A two-dimensional flat surface represents geometric data, a type of spatial data. For instance, floor plans require geometric data. Geometric information is used by Google Maps, a navigation app, to produce accurate directions.
2. Geographic Data:
Information that has been plotted around a sphere is referred to as “geographical data.” The globe is typically Earth in most cases. Geographical information highlights the connection between latitude and longitude to a certain object or region. A well-known example of geographic data is the Global Positioning System (GPS).
Prerequisites for Mapping Geographic Data in Python
- Numpy
- Pandas (version 0.24 or later)
- Shapely (interface to GEOS)
- Fiona (interface to GDAL)
- Pyproj (interface to PROJ; +2.2.0v)
Installation:
Anaconda can be used for installation:
Syntax:
conda install geopandas conda install geoplot
A community project called conda-forge offers conda packages for a variety of software. In addition to the “defaults” channel that Anaconda offers, it provides the conda-forge package channel for conda from which packages can be installed.
Examining a Shapefile:
Using the variable “world data,” we will first import the GeoPandas library before reading our shapefile. Using the command: Geopandas can read nearly any vector-based spatial data format, including ESRI shapefiles, GeoJSON files, and more.
Syntax:
import geopandas as gpds # Reading the data worlds_data = gpds.read_file(r'worlds.shp') worlds_data
Steps involved in Mapping Geographic Data in Python :
An open-source project called GeoPandas aims to simplify the handling of geographical data in Python. GeoPandas adds support for spatial operations on geometric types to the Pandas data types. Shapely geometric processes are carried out. Geopandas also use Matplotlib for charting and Fiona for file access. A sizable stack of open-source geospatial libraries is what GeoPandas rely on for its spatial capability (GEOS, GDAL, and PROJ).
Step 1: Libraries and data are being loaded:
Let’s begin by loading the libraries first.
import numpy import pandas import matplotlib pyplot as plty
Step 2: dataset is loaded
df = pd.read_csv(‘C:/.. …/DataSet.txt’) df.head()
Step 3: Establish the Bounding Box
The Bounding Box must now be defined. The area that will encompass all spatial points is known as the Bounding Box, and it is specified by two longitudes and two latitudes.
BBox = ((df.longitude.min(), df.longitude.max(),df.latitude.min(), df.latitude.max()) > (46.5691,46.8398, 24.6128, 24.8256)
Step 4: Find Your Map
Enter the bounding box information before exporting the desired map as an image.
Step 5: Plotting geographic data in python
Finally, create scatter points on the ‘ruh m’ map picture using the ‘df.longitude’ and ‘df.latitude’ values. Keep in mind that the X-axis and Y-axis must be set up according to the bounding box, or “BBox.”
fig, ax = plt.subplots(figsize = (8,7))
ax.scatter(df.longitude, df.latitude, zorder=1, alpha= 0.2, c='b', s=10)
ax.set_title('Plotting Spatial Data on Riyadh Map')
ax.set_xlim(BBox[0],BBox[1])
ax.set_ylim(BBox[2],BBox[3])
ax.imshow(ruh_m, zorder=0, extent = BBox, aspect= 'equal')
Other methods to graph geograph data:
1. Reference System for Coordinates:
Geopandas’ CRS, or Coordinates Reference System, allows us to examine the coordinate system we are currently using. Additionally, we may transform it into a system for project coordination—the pyproj.CRS object represents the Coordinate Reference System (CRS). We can use the following syntax to check the current CRS.
Syntax:
GeoDataFrame.crs
The to_crs() method converts geometry to a different coordinate reference system. Change the system of coordinates used to reference all of the geometries in a geometry column that is active. The series at hand must have the CRS attribute configured. One of two output formats, CRS or ePSG, may be used. All points in all objects will be altered using this technique. It doesn’t have any idea about projecting whole geometries.
In the current projection, all segment joining points—not geodesics—are taken to be lined up. Crossing the dateline (or another projection barrier) will cause objects to act in an unfavorable manner.
2. Inserting a legend
Next, we will convert the area into square kilometers by dividing it by 106 (i.e. (1000000). The “world data” variable in the variable explorer displays the output.
Using plot() parameters, we can add a legend and a label to our global map.
name: bool (default False). Set up a legend, ignored if no column or color is specified
dict legend kwds (default None). Matplotlib.pyplot.legend() and Matplotlib.pyplot.colorbar keyword arguments (). Additional words that are acceptable when a scheme is specified:
string, format: a formatting guideline for the classes’ bin borders in the legend. For instance, do not use decimals: {“fmt”: “{:.0f}”}.
3. Geoplot’s Sankey
An information network’s information flow is shown on a Sankey diagram. It helps see the volumes of data that are being processed by a system. The Sankey diagram is given a geospatial context in this picture, which makes it useful for tracking, for instance, travel volumes between airports or traffic loads on a road network.
A GeoDataFrame with line strings or multipoint geometries is necessary for a basic Sankey. Hue gives the map a gradation of color. To control the colormap, use matplotlib’s cmap function. Indicate the color scheme for a category colormap. A legend turns a story on or off. Here, Mollweide projection is being used.
Conclusion:
We learned about Various data visualization tools, including Matplotlib, Pandas, Plotly, Seaborn, Bokeh, etc., that can be used to accomplish Mapping Geographic Data in Python. These tools are all frequently used with tabular data. Similarly, we can use a Python module called GeoPandas to visualize geospatial data or draw a map of any geographic place and show some of the noteworthy information. Geospatial data manipulation and graphing are the only uses for this package. In this essay, we will talk about the following issues in contrast to GeoPandas:
We have examined spatial data and its corresponding properties in this article. Later, we talked about how GeoPandas, which is entirely based on Pandas, may be used to manage geospatial data. We have expertly produced the satellite view of our map by analyzing the map from the internet using contextual data and coordinates from GeoPandas.
