Created
June 13, 2021 18:31
-
-
Save CatherineH/98bc201cef1149541bc07e554429153e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# generates an svg topographical map from a GeoTIFF file. | |
import rasterio | |
from shapely.geometry import Point, LineString | |
from skimage.measure import find_contours | |
from numpy import rot90, fliplr, flipud | |
# this data was downloaded from https://maps.canada.ca/czs/index-en.html | |
fp = r'dataset/DEM.tif' | |
dataset = rasterio.open(fp) | |
_min = None | |
_max = None | |
simplify_tolerance = 2 | |
levels = 5 | |
colors = ["264653", "2a9d8f", "e9c46a", "f4a261", "e76f51"] | |
ofh = open(f"halifax_{levels}_{simplify_tolerance}.svg", "w") | |
ofh.write("<svg>") | |
band1 = dataset.read(1) | |
band1 = rot90(band1) | |
band1 = flipud(band1) | |
for i in range(0, levels): | |
path = "" | |
shapes = find_contours(band1, i*150.0/levels) | |
for shape in shapes: | |
if len(shape) <= 3: | |
continue | |
ply = LineString([Point(_point) for _point in shape]) | |
ply = ply.simplify(simplify_tolerance) | |
for point_i,point in enumerate(ply.coords): | |
if point_i==0: | |
path += f"M {point[0]} {point[1]} " | |
else: | |
path += f"L {point[0]} {point[1]} " | |
path += "z" | |
_svg_str = f"<path fill=\"none\" stroke=\"#{colors[i % len(colors)]}\" d=\"{path}\" />\n" | |
print(_svg_str) | |
ofh.write(_svg_str) | |
ofh.write("</svg>") | |
ofh.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment