#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2024/05/05 08:42:03 +0900> """ Output spline curve data QGISからエクスポート、エディタで編集して作成したcsvを読み込んで、 scipyでスプライン補間した値を出力する。 Usage: python dump_spline.py -i INPUT.csv > OUTPUT.csv Windows10 x64 22H2 + Python 3.10.10 64bit scipy 1.13.0 + numpy 1.26.4 """ import argparse import sys import numpy as np from scipy import interpolate def load_csv(infile, muly): """load csv file.""" ax = [] ay = [] with open(infile, "r") as file: for line in file: line = line.rstrip() x, y = line.split(",") ax.append(float(x)) ay.append(float(y) * muly) return ax, ay # B-Spline def spline1(x, y, s): tck, u = interpolate.splprep([x, y], k=3, s=0) u = np.linspace(0, 1, num=len(x) * s, endpoint=True) spline = interpolate.splev(u, tck) return spline[0], spline[1] def main(): parser = argparse.ArgumentParser() parser.add_argument("-i", "--input", required=True, help="Input csv file") parser.add_argument( "-n", "--num", type=int, default=10, help="Integer number. default=10" ) parser.add_argument( "--muly", type=float, default=1.0, help="Multiply y-coordinate. default=1.0" ) args = parser.parse_args() if args.input is None: sys.exit() x, y = load_csv(args.input, args.muly) bx, by = spline1(x, y, args.num) # output csv for i in range(len(bx)): print(f"{bx[i]},{by[i]}") if __name__ == "__main__": main()