Changing Map Projection in Python¶
Map Projection and CRS¶
Map Projection¶
Map projection is a mathematical transformation of the earth’s 3D surface to a 2D representation on a map. This process involves the distortion of the map’s properties such as area, shape, direction, distance, and scale.
CRS (Coordinate Reference System)¶
CRS is a system used to represent the earth’s surface in two-dimensional form, using a set of coordinates. CRS is used to precisely locate positions on the earth’s surface and is often expressed as a combination of a map projection and a geographic or geodetic datum. A CRS also defines units of measurement, such as meters or degrees, used to describe the coordinates of a location.
Common CRS¶
- WGS 84: One of the most widely used geographic CRS, based on the WGS 84 datum, used for GPS navigation and mapping applications.
- UTM (Universal Transverse Mercator): A system of projecting the earth’s surface into 60 zones, used for accurate representation of large-scale maps.
- State Plane Coordinate System (SPCS): A system of projections used for large-scale maps in the United States, designed to minimize distortion within state borders.
Common Map Projections¶
- Mercator: A cylindrical projection often used for nautical and marine navigation, preserves angles and shapes but distorts areas away from the equator.
- Robinson: A projection designed to present the world in a visually pleasing manner, balancing distortion of area, direction, and shape.
- Equidistant Cylindrical: A projection that preserves distances along two standard parallels, often used for world maps and atlases.
- Albers Equal Area: A conic projection that preserves areas, often used for thematic maps in the United States.
An important thing to consider is that there is no perfect map projection as each projection involves trade-offs between various map properties such as area, shape, direction, distance, and scale. The choice of a map projection depends on the specific requirements of the user and the purpose of the map.
Let’s check how to plot different Map projection in python.
We will be using geopandas
to plot the shapefiles and maps. You can read how to plot shapefiles using geopandas here
import geopandas as gpd
import matplotlib.pyplot as plt
This is a python code using the libraries geopandas and matplotlib.
geopandas
is a library that extends the functionality of the popular data analysis library pandas to handle geospatial data. It allows users to manipulate and analyze geospatial data in a similar way to regular data and provides geospatial operations and manipulations for geospatial data stored in a GeoDataFrame.
matplotlib
is a plotting library for Python. It allows for creating static, animated, and interactive visualizations in Python.
In this code, geopandas is imported as geopandas and matplotlib is imported as plt. The geopandas library is used to handle and manipulate geospatial data, while matplotlib is used to create visualizations of the data.
Setting projection¶
coastlines=gpd.read_file("Coastlines/Global_coastlines_low_res.shp") # location to your shapefiles
coastlines.set_crs("EPSG:4326")
This is a Python code that uses the geopandas
library to read a shapefile of global coastlines into a GeoDataFrame and set its Coordinate Reference System (CRS).
gpd.read_file
is a method in the geopandas library used to read a shapefile into a GeoDataFrame
. The argument to read_file is the file path to the shapefile
, which in this case is "Coastlines/Global_coastlines_low_res.shp"
. The result of this line is a GeoDataFrame
stored in the variable coastlines
set_crs
is a method in the geopandas
library used to set the CRS of a GeoDataFrame. The argument to set_crs
is a string in the format "EPSG:XXXX"
where “XXXX” is the EPSG code for the desired CRS. In this case, the EPSG code 4326 is used, which corresponds to the WGS 84 CRS commonly used for GPS navigation and mapping applications. This line sets the CRS of the coastlines GeoDataFrame to WGS 84.
What is EPSG code?¶
EPSG
stands for European Petroleum Survey Group. The EPSG Geodetic Parameter Dataset is a database of Coordinate Reference Systems (CRS) that provides information on various map projections, datums, and ellipsoids used in geodetic surveys and mapping.
An EPSG code is a unique identifier assigned to a specific CRS, which can include information such as the map projection, datum, and ellipsoid used in the CRS. For example, EPSG code 4326 refers to the WGS 84 CRS, which is based on the WGS 84 datum and is commonly used for GPS navigation and mapping applications.
The EPSG Geodetic Parameter Dataset is widely used as a reference for mapping and geodetic applications and is maintained by the International Association of Oil & Gas Producers (OGP).
EPSG Codes for Common CRS and Map Projections¶
- WGS 84 (GPS navigation and mapping): EPSG 4326
- UTM (Universal Transverse Mercator): EPSG 32600-32660 (depending on the UTM zone)
- State Plane Coordinate System (SPCS): EPSG 26911-26919 (depending on the state)
- Mercator: EPSG 3395
- Robinson: EPSG 54030
- Equidistant Cylindrical: EPSG 4087
- Albers Equal Area: EPSG 102022
Note: The EPSG code provides a unique identifier for a specific CRS and includes information about the map projection, datum, and ellipsoid used in the CRS.
Plotting the maps¶
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
plt.title("WGS 84")
coastlines.plot(ax=ax,facecolor="wheat",edgecolor="black",linewidth=0.3)
<Axes:title={'center':'WGS 84'}>
Changing One Map Projection to Other¶
From WGS 84 to Equidistant Cylindrical¶
coastlines=coastlines.to_crs("EPSG:4087")
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
plt.title("Equidistant Cylindrical")
coastlines.plot(ax=ax,facecolor="wheat",edgecolor="black",linewidth=0.3)
<Axes:title={'center':'Equidistant Cylindrical'}>
mollweide_proj = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
coastlines=coastlines.to_crs(mollweide_proj)
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
plt.title("Mollweide")
coastlines.plot(ax=ax,facecolor="wheat",edgecolor="black",linewidth=0.3)
<Axes:title={'center':'Mollweide'}>
mollweide_proj = "+proj=moll +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
This line of code defines a custom Proj string for the Mollweide projection. The Proj string is a text representation of a projection definition in the PROJ format.
Each component in the Proj string represents a specific parameter of the projection. The components are separated by a space character, and each component has the form +parameter=value.
Here’s a brief explanation of each component in this Proj string:
+proj
=moll: This component specifies the projection type as Mollweide.
+lon_0
=0: This component sets the central meridian of the projection to 0 degrees longitude.
+x_0=0
: This component sets the false easting of the projection to 0 meters.
+y_0=0
: This component sets the false northing of the projection to 0 meters.
+datum=WGS84
: This component specifies the datum used for the projection as WGS 84, which is a widely used global reference system for latitude and longitude coordinates.
+units
=m: This component sets the units of the projection to meters.
+no_defs
: This component disables any default behavior of the PROJ library that might interfere with the projection definition.
By using this custom Proj string, you can define the Mollweide projection in a flexible and portable way, without relying on a specific EPSG code or Proj database.
Robinson¶
robinson_proj = "+proj=robin +lon_0=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs"
coastlines=coastlines.to_crs(robinson_proj)
fig = plt.figure(figsize=[12,8])
ax = fig.add_axes([0, 0, 1, 1])
plt.title("Robinson")
coastlines.plot(ax=ax,facecolor="wheat",edgecolor="black",linewidth=0.3)
<Axes:title={'center':'Robinson'}>