Natural Earth 10 m land polygon for Ireland#

https://www.naturalearthdata.com/downloads/10m-physical-vectors/10m-land/

import os
from datetime import datetime, timezone
from zipfile import BadZipFile, ZipFile
import geopandas as gpd
import matplotlib.pyplot as plt
import pooch
from matplotlib import ticker
import climag.climag as cplt
# base data download directory
SUB_DIR = os.path.join("data", "boundaries", "NaturalEarth")
os.makedirs(SUB_DIR, exist_ok=True)

URL = "https://naciscdn.org/naturalearth/10m/physical/ne_10m_land.zip"
KNOWN_HASH = None
FILE_NAME = "ne_10m_land.zip"

# file name for the GeoPackage where the boundary vector layers will be saved
GPKG_BOUNDARY = os.path.join("data", "boundaries", "boundaries_all.gpkg")

DATA_DIR_TEMP = os.path.join(SUB_DIR, "temp")

os.makedirs(DATA_DIR_TEMP, exist_ok=True)
# download data if necessary
if not os.path.isfile(os.path.join(SUB_DIR, FILE_NAME)):
    pooch.retrieve(
        url=URL, known_hash=KNOWN_HASH, fname=FILE_NAME, path=SUB_DIR
    )

    with open(
        os.path.join(SUB_DIR, f"{FILE_NAME[:-4]}.txt"), "w", encoding="utf-8"
    ) as outfile:
        outfile.write(
            f"Data downloaded on: {datetime.now(tz=timezone.utc)}\n"
            f"Download URL: {URL}"
        )
DATA_FILE = os.path.join(SUB_DIR, FILE_NAME)
ZipFile(DATA_FILE).namelist()
['ne_10m_land.README.html',
 'ne_10m_land.VERSION.txt',
 'ne_10m_land.cpg',
 'ne_10m_land.dbf',
 'ne_10m_land.prj',
 'ne_10m_land.shp',
 'ne_10m_land.shx']
data = gpd.read_file(f"zip://{DATA_FILE}!ne_10m_land.shp")
data.head()
featurecla scalerank min_zoom geometry
0 Land 0.0 0.0 MULTIPOLYGON (((-0.00029 -71.49903, 0.01555 -7...
1 Land 6.0 5.0 MULTIPOLYGON (((166.13697 -50.86435, 166.20525...
2 Land 5.0 3.0 MULTIPOLYGON (((-78.78897 -33.60906, -78.78038...
3 Land 6.0 6.0 MULTIPOLYGON (((163.98512 -20.04762, 163.98609...
4 Land 5.0 4.0 MULTIPOLYGON (((134.70737 -6.58904, 134.72006 ...
data.crs
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
data.plot(figsize=(8, 8), color="seagreen")
plt.tick_params(labelbottom=False, labelleft=False)
plt.show()
../_images/b967d11e1126f719d2c697be2a233c99bd1b59fa453d89f9c7b512b48435aa4e.png
# crop Ireland's boundary
ie = gpd.read_file(GPKG_BOUNDARY, layer="NUTS_RG_01M_2021_2157_IE")
data_cropped = data.to_crs(cplt.ITM_EPSG).overlay(
    gpd.GeoDataFrame(geometry=ie.buffer(12500))
)
# dissolve features
data_cropped = data_cropped.dissolve()
data_cropped = data_cropped[["geometry"]]
data_cropped
geometry
0 MULTIPOLYGON (((474445.798 543242.050, 468956....
data_cropped.plot(
    figsize=(8, 8),
    color="navajowhite",
    edgecolor="darkslategrey",
    linewidth=0.5,
)
plt.tick_params(labelbottom=False, labelleft=False)
plt.show()
../_images/408b09a5ffb6417cc9fa5dc5fbe0ca9395cf1294a37be92ce34a4951281c0454.png
data_cropped.to_file(GPKG_BOUNDARY, layer="ne_10m_land_2157_IE")
# bounding box
ie_bound = gpd.GeoDataFrame(
    geometry=data_cropped.envelope.buffer(100000).envelope
)
ie_bound
geometry
0 POLYGON ((330424.324 419708.048, 866465.646 41...
base = ie_bound.boundary.plot(color="crimson", figsize=(7.5, 7.5))
data_cropped.boundary.plot(ax=base, color="darkslategrey", linewidth=0.4)

plt.tick_params(labelbottom=False, labelleft=False)

plt.title("Boundary of Ireland")
plt.tight_layout()
plt.show()
../_images/a8a936c0654cae7e7a4200a08e8a6cfd8a63b70b9217f55e3552f49c15cfa800.png
ie_bound.to_file(GPKG_BOUNDARY, layer="ne_10m_land_2157_IE_BBOX")
# difference between bbox and land polygon
ie_bound = ie_bound.overlay(data_cropped, how="difference")
ie_bound
geometry
0 POLYGON ((330424.324 1060113.588, 866465.646 1...
base = ie_bound.plot(
    color="navajowhite",
    figsize=(7.5, 7.5),
    edgecolor="darkslategrey",
    linewidth=0.4,
)

plt.tick_params(labelbottom=False, labelleft=False)

plt.title("Boundary of Ireland")
plt.tight_layout()
plt.show()
../_images/f56a7005967e7bd05c84f7d57ab1ac9914c0ba75a0258994d6afe3153b93dd29.png
ie_bound.to_file(GPKG_BOUNDARY, layer="ne_10m_land_2157_IE_BBOX_DIFF")