Last active
November 17, 2016 14:53
-
-
Save kbevers/008895dc334a8419fde2b24d790016c9 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
''' | |
Rename las/laz files that are tiled in the Danish Kvadratnet. | |
''' | |
from __future__ import print_function | |
import os | |
import shutil | |
import glob | |
import argparse | |
import laspy | |
import kvadratnet | |
def rename_file(filepath, prefix='', postfix=''): | |
''' | |
Rename a las-file in accordance to the Kvadratnet naming scheme. | |
Uses information in the header to determine which kvadratnet cell the tile | |
belongs to. | |
''' | |
with laspy.file.File(filepath, mode='r') as las: | |
(x_min, y_min) = las.header.min[0:2] | |
(x_max, y_max) = las.header.max[0:2] | |
min_1km = kvadratnet.name_from_point(y_min, x_min) | |
max_1km = kvadratnet.name_from_point(y_max, x_max) | |
if min_1km <> max_1km: | |
msg = '[ERROR] {filename}: Not all points inside the same kvadratnet cell' | |
print(msg.format(filename=filepath)) | |
return None | |
(_, ext) = os.path.splitext(filepath) | |
folder = os.path.dirname(filepath) | |
newpath = os.path.join(folder, prefix+min_1km+postfix+ext) | |
shutil.move(filepath, newpath) | |
return newpath | |
def main(): | |
''' | |
Command line interface wrapped up in a box | |
''' | |
parser = argparse.ArgumentParser(description='Rename pointcloud tiles after Kvadratnet schema') | |
parser.add_argument('filespec', help='Pattern of files to rename. Uses globbing.') | |
parser.add_argument( | |
'--prefix', | |
default='', | |
help='Whatever you want before the tile name, e.g. prefix_1km_6372_549.las' | |
) | |
parser.add_argument( | |
'--postfix', | |
default='', | |
help='Whatever you want after the tile name, e.g. 1km_6372_549_postfix.las' | |
) | |
parser.add_argument('-v', '--verbose', action='store_true', help='Be verbose') | |
args = parser.parse_args() | |
files = glob.glob(args.filespec) | |
for f in files: | |
new_name = rename_file(f, prefix=args.prefix, postfix=args.postfix) | |
if args.verbose and new_name is not None: | |
print('Renaming {old} to {new}'.format(old=f, new=new_name)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment