-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_geotiff.py
90 lines (76 loc) · 3.07 KB
/
move_geotiff.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import numpy as np
import os
from pathlib import Path, PureWindowsPath
import gdal
import rasterio
from rasterio.plot import show_hist
import shutil
import pandas as pd
import pdb
from os import listdir
from os.path import isfile, join
from os import walk
# Create folders
if not os.path.isdir('./mpt_outs'):
os.makedirs('./mpt_outs')
if not os.path.isdir('./mpt_outs/tiffs'):
os.makedirs('./mpt_outs/tiffs')
if not os.path.isdir('./mpt_outs/moved_geotiffs'):
os.makedirs('./mpt_outs/moved_geotiffs')
if not os.path.isdir('./mpt_outs/moved_geotiffs/other'):
os.makedirs('./mpt_outs/moved_geotiffs/other')
collection_df = pd.read_csv('./csvs/georef_collections.csv')
collections = list(set(collection_df['collection']))
for c in collections:
c_path = './mpt_outs/moved_geotiffs/' + c
if not os.path.isdir(c_path):
os.makedirs(c_path)
# For external drive find path ls -la /Volumes
# Add into '/Volumes/***/'
# '/Volumes/FAT32/'
paths = []
for (dirpath, dirnames, filenames) in walk('./georef_geotiffs'):
for f in filenames:
if f[-3:] == 'tif':
paths.append(os.path.join(dirpath, f))
# Is this a geotiff?
geotiff_paths = []
for p in paths:
info = gdal.Info(p, format='json')
if info['coordinateSystem']['wkt'] == '':
# copy file to mpt_outs
shutil.copy(p, './mpt_outs/tiffs/' + os.path.basename(p))
else:
geotiff_paths.append(p)
id_path_df = pd.read_csv('./csvs/klokan_id_path.csv')
# Deal with Windows path PureWindowsPath(p).name to get name from original
id_path_df['path'] = [PureWindowsPath(p).as_posix() for p in id_path_df['path']]
id_path_df['filename'] = [Path(p).name for p in id_path_df['path']]
# Align Geotiff filenames with Klokan_id_Path filenames
geotiff_path_df = pd.DataFrame({'path_geotiff': geotiff_paths})
geotiff_path_df['filename'] = [Path(p).name for p in geotiff_path_df['path_geotiff']]
id_path_df = id_path_df.merge(geotiff_path_df, how='left', on='filename')
# Copy to folder named after collection
collection = 'other'
for i, row in id_path_df.iterrows():
# Find collection for geotiff
collection_row = collection_df.loc[collection_df['id'] == row['id']]
id_collections = collection_row['collection'].values
if len(id_collections) == 1:
collection = id_collections[0]
# Does filename match Klokan ID?
if isinstance(row['path_geotiff'], str):
if Path(row['path_geotiff']).stem == row['id']:
filename = row['filename']
else:
if not os.path.isdir('./mpt_outs/moved_geotiffs/' + collection + '/id_changed'):
os.makedirs('./mpt_outs/moved_geotiffs/' + collection + '/id_changed')
filename = 'id_changed/' + row['id'] + '.tif'
dst_path = './mpt_outs/moved_geotiffs/' + collection + '/' + filename
shutil.copyfile(row['path_geotiff'], dst_path)
# Remove unused collection folders
for c in collections:
c_path = './mpt_outs/moved_geotiffs/' + c
if len(os.listdir(c_path)) == 0: # Check if the folder is empty
shutil.rmtree(c_path)
# id_path_df.to_csv('./id_path_df.csv', index = False)