-
Notifications
You must be signed in to change notification settings - Fork 3
/
cleanup.py
66 lines (55 loc) · 1.59 KB
/
cleanup.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
#!/usr/bin/env python
u"""
cleanup.py
Yara Mohajerani (08/2020)
Use a static coast line to clean up the GL shapefile
"""
import os
import sys
import getopt
import pandas as pd
import geopandas as gpd
#-- main function
def main():
#-- Read the system arguments listed after the program
long_options=['THRESHOLD=','MASK_DIR=','INFILE=']
optlist,arglist = getopt.getopt(sys.argv[1:],'T:M:I:',long_options)
#-- Set default settings
thresh = 15e3
MASK_DIR = '/DFS-L/DATA/gl_ml/auxiliary/'
INFILE = '/DFS-L/DATA/gl_ml/SENTINEL1_2018/combined_AllTracks.shp'
for opt, arg in optlist:
if opt in ("-T","--THRESHOLD"):
thresh = int(arg)
elif opt in ("-M","--MASK_DIR"):
MASK_DIR = os.path.expanduser(arg)
elif opt in ("-I","--INFILE"):
INFILE = os.path.expanduser(arg)
#-- read mask file
mask = gpd.read_file(os.path.join(MASK_DIR,'ais_mask_%ikm.shp'%(thresh/1e3)))
#-- read GLS
gls = gpd.read_file(INFILE)
#-- now go through and get list of GLs to delete
n = len(mask)
remove_ind = []
for i in range(len(gls)):
if i%1000 == 0:
print('%i/%i'%(i,len(gls)))
#-- start assuming GL is noise
rm = True
j = 0
#-- as soon as one intersecting element is found, stop and move on.
while rm and j < n:
if mask['geometry'][j].intersects(gls['geometry'][i]):
rm = False
j += 1
if rm:
remove_ind.append(i)
print("deleting %i lines"%len(remove_ind))
#-- remove extra elements
gls_out = gls.drop(remove_ind)
#-- save to file
gls_out.to_file(INFILE.replace('.shp','_cleaned_%ikm.shp'%(thresh/1e3)),driver='ESRI Shapefile')
#-- run main program
if __name__ == '__main__':
main()