Skip to content

Instantly share code, notes, and snippets.

@balazsdukai
Last active May 30, 2018 12:49
Show Gist options
  • Save balazsdukai/ecb5983a1e909d3150e4e26e2198bef4 to your computer and use it in GitHub Desktop.
Save balazsdukai/ecb5983a1e909d3150e4e26e2198bef4 to your computer and use it in GitHub Desktop.
Subset a LAS/LAZ by a set of polygons
#! /bin/bash
# Crop a LAS file by multiple polygons. Implemented for lastools.
# example:
# subset_las.sh <path to LAS file> <path to polygons directory>
LAS_IN=$1
DIR=$2
for f in $(dir $DIR)
do
file=$DIR/$f
filename=$(basename "$file")
las_out="${filename%.*}".laz
re_lead='(?<=Extent: \()'
re_float='[0-9]{6}[.][0-9]{6}'
min_x="$(ogrinfo -so -al $file | grep -P -o "$re_lead""$re_float")"
re_lead="(?<=Extent: \("$re_float", )"
min_y="$(ogrinfo -so -al $file | grep -P -o "$re_lead""$re_float")"
re_lead="(?<=Extent: \("$re_float", "$re_float"\) - \()"
max_x="$(ogrinfo -so -al $file | grep -P -o "$re_lead""$re_float")"
re_lead="(?<=Extent: \("$re_float", "$re_float"\) - \("$re_float", )"
max_y="$(ogrinfo -so -al $file | grep -P -o "$re_lead""$re_float")"
echo "Saving $las_out"
las2las -i $LAS_IN -keep_xy $min_x $min_y $max_x $max_y -keep_every_nth 5 -cores 4 -o $(pwd)/$las_out
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment