#!/bin/bash

for i in /work/Boundaries/Wilderness/*.geojson ; do
    new=$(echo $i | sed -e 's/\.geojson$//')
    base=$(basename ${new})
    echo "Processing ${base}..."
    ogr2ogr -progress -nln trails -t_srs EPSG:4326 -makevalid -explodecollections ${base}_Wilderness_Trails.geojson -clipsrc $i /work/SourceData/USFS_Trails-out.geojson
    features=$(ogrinfo -ro -al ${base}_Wilderness_Trails.geojson | grep "^Feature Count:" | cut -d ' ' -f 3)
    if test x"${features}" = x"0"; then
	ogr2ogr -progress -nln trails -t_srs EPSG:4326 -makevalid -explodecollections ${base}_Wilderness_Trails.geojson -clipsrc $i /work/SourceData/BLM_Public_Managed_Trails.geojson
    fi
done
