Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Lozac'h Loic
AgriSoilMoisture
Commits
0f26fb31
Commit
0f26fb31
authored
Feb 07, 2020
by
Lozac'h Loic
Browse files
ADD:Batch polygonize mv
parent
9c19234c
Changes
1
Show whitespace changes
Inline
Side-by-side
python/SoilMoistureBatchGdalPolygonize.py
0 → 100755
View file @
0f26fb31
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""
Batch gdal_tranlate to extract an AOI define with -projwin
Auteur: Loïc Lozac'h
"""
import
os
,
argparse
from
subprocess
import
Popen
,
PIPE
def
search_files
(
directory
=
'.'
,
matchnaming
=
'MV'
,
extension
=
'TIF'
,
fictype
=
'f'
):
images
=
[]
matchnaming
=
matchnaming
.
upper
()
extension
=
extension
.
upper
()
fictype
=
fictype
.
lower
()
for
dirpath
,
dirnames
,
files
in
os
.
walk
(
directory
):
if
fictype
!=
'd'
:
for
name
in
files
:
# print(os.path.join(dirpath, name) + " test")
if
name
.
upper
().
find
(
matchnaming
)
==
0
and
name
.
upper
().
endswith
(
extension
):
# print(os.path.join(dirpath, name) + " OK")
abspath
=
os
.
path
.
abspath
(
os
.
path
.
join
(
dirpath
,
name
))
images
.
append
(
abspath
)
elif
fictype
==
'd'
:
for
dirname
in
dirnames
:
# print(os.path.join(dirpath, name) + " test")
if
dirname
.
upper
().
find
(
matchnaming
)
>=
0
:
# print(os.path.join(dirpath, name) + " OK")
abspath
=
os
.
path
.
abspath
(
os
.
path
.
join
(
dirpath
,
dirname
))
images
.
append
(
abspath
)
else
:
print
(
"search_files type error"
)
exit
return
images
def
process_command
(
cmd
):
print
(
"Starting : "
+
" "
.
join
(
cmd
))
p
=
Popen
(
cmd
,
stdout
=
PIPE
)
# p.wait()
output
=
p
.
communicate
()[
0
]
if
p
.
returncode
!=
0
:
print
(
"process failed %d : %s"
%
(
p
.
returncode
,
output
))
print
(
"#################################################"
)
if
__name__
==
"__main__"
:
# Make parser object
parser
=
argparse
.
ArgumentParser
(
description
=
"""
Batch gdal_polygonize.py on Soil Moisture geotiff files and recursively on mvdir.
It will create a "SHP" dir on each level where MV files are found
"""
)
parser
.
add_argument
(
'-mvdir'
,
action
=
'store'
,
required
=
True
,
help
=
'Directory containing SoilMoisture geotiff to process'
)
args
=
parser
.
parse_args
()
mvfiles
=
[]
mvfiles
=
search_files
(
args
.
mvdir
)
fieldname
=
"MVx5"
for
file
in
mvfiles
:
outdir
=
os
.
path
.
join
(
os
.
path
.
dirname
(
file
),
"SHP"
)
if
not
os
.
path
.
exists
(
outdir
):
os
.
mkdir
(
outdir
)
destfile
=
os
.
path
.
join
(
outdir
,
os
.
path
.
basename
(
file
)[:
-
3
]
+
"shp"
)
layer
=
os
.
path
.
basename
(
file
)[:
-
4
]
cmd
=
[
"gdal_polygonize.py"
,
file
,
"-f"
,
"ESRI Shapefile"
,
destfile
,
layer
,
fieldname
]
process_command
(
cmd
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment