Coral is a library of digital cartography functions implemented in Python 3.
The examples bellow use a public domain shapefile from the Natural Earth project.
Download it and unzip its contents to a folder named data
.
- Simple map of Africa.
from coral import shapefile, tqdm, coord, proj, vector
# Choose projection and scale.
prj = proj.TransMercator()
pixsz = 5000 # meters/pixel
# Load shapefile.
sfpath = "data/ne_50m_admin_0_countries"
sf = shapefile.Reader(sfpath, encoding='latin1')
# Draw countries to canvas.
cvs = vector.Canvas()
for sr in tqdm.tqdm(sf.shapeRecords()):
# Ignore countries not in Africa.
if sr.record[54] != "Africa":
continue
points = sr.shape.points
offsets = list(sr.shape.parts) + [len(points)]
for a, b in zip(offsets[:-1], offsets[1:]):
# Ignore some small islands.
if b - a < 50:
continue
# Project points.
polygon = (prj.geo2rect(lon, lat) for lon, lat in points[a:b])
# Simplify polygons to reduce number of points.
polygon = coord.simplify(polygon, pixsz)
cvs.addpolygon(polygon, stroke=0)
cvs.save("africa.eps")
- Simple map of Europe with color.
from coral import shapefile, tqdm, coord, proj, vector
# Define region of interest.
lon0, lon1 = -25, 41
lat0, lat1 = 35, 72
# Define Albers Projection for region of interest.
latos = (lat1 - lat0) / 6
parallels = lat0 + latos, lat1 - latos
prj = proj.ConicEqualArea(parallels)
pixsz = 5000
# Define some colors
land = 0.4, 0.9, 0.2
border = 1, 1, 1 # this is white
ocean = 0.5, 0.8, 0.9
sfpath = "data/ne_50m_admin_0_countries"
sf = shapefile.Reader(sfpath, encoding='latin1')
cvs = vector.Canvas(ocean)
for sr in tqdm.tqdm(sf.shapeRecords()):
if sr.record[54] != "Europe":
continue
points = sr.shape.points
offsets = list(sr.shape.parts) + [len(points)]
for a, b in zip(offsets[:-1], offsets[1:]):
# Prevent some lands from interfering on the region of interest.
lon, lat = points[a]
outside = not lon0 < lon < lon1 or not lat0 < lat < lat1
if outside:
bb = cvs.bbox
polygon = (prj.geo2rect(lon, lat) for lon, lat in points[a:b])
polygon = coord.simplify(polygon, pixsz)
cvs.addpolygon(polygon, fill=land, stroke=border)
if outside:
cvs.bbox = bb
cvs.save("europe.eps")
Coral is released under the MIT License. See LICENSE.rst for more information.