Skip to content

Certain high precision central longitudes can cause the Mercator projection to have nonsensical bounds #2576

@mhearne-usgs

Description

@mhearne-usgs

On my system (Ubuntu 20.04.4 LTS) and on a colleagues Mac, the script below gives the following results:

Failure to create valid mercator projection boundaries with -169.40000000042403
Successfully created valid mercator projection boundaries with -169.4

I am using Cartopy 0.25.0, he is using 0.24.1.

I'm going to try reducing the precision as a workaround all the time, but I'll need to do lots of tests since I'm making maps in an automated system.

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import numpy as np

if __name__ == "__main__":
    bounds = (
        -172.32916666697352,
        -166.4708333338745,
        50.50416666824549,
        54.11250000143449,
    )
    xmin, xmax, ymin, ymax = bounds
    clon = (xmin + xmax) / 2
    clat = (ymin + ymax) / 2
    if xmax < 0 and xmax < xmin:
        clon = (xmin + (xmax + 360)) / 2

    proj = ccrs.Mercator(
        central_longitude=clon,
        max_latitude=ymax,
        globe=None,
    )
    try:
        assert (proj.boundary.bounds[2] - proj.boundary.bounds[0]) > 1
    except AssertionError:
        print(f"Failure to create valid mercator projection boundaries with {clon}")
    new_clon = np.round(clon, decimals=2)
    proj = ccrs.Mercator(
        central_longitude=new_clon,
        max_latitude=ymax,
        globe=None,
    )
    print(f"Successfully created valid mercator projection boundaries with {new_clon}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions