11import argparse
2+ from itertools import repeat
3+ from multiprocessing import Pool
4+
25import pandas as pd
36from tqdm import tqdm
4- from multiprocessing import Pool
5- from ..vector .polygon import geojson_to_px_gdf
6- from ..vector .polygon import georegister_px_df
7+
78from ..utils .cli import _func_wrapper
8- from itertools import repeat
9+ from .. vector . polygon import geojson_to_px_gdf , georegister_px_df
910
1011
1112def main ():
1213
1314 parser = argparse .ArgumentParser (
14- description = 'Interconvert footprints between pixel and geographic ' +
15- 'coordinate systems.' , argument_default = None )
15+ description = "Interconvert footprints between pixel and geographic "
16+ + "coordinate systems." ,
17+ argument_default = None ,
18+ )
1619
17- parser .add_argument ('--source_file' , '-s' , type = str ,
18- help = 'Full path to file to transform' )
19- parser .add_argument ('--reference_image' , '-r' , type = str ,
20- help = 'Full path to a georegistered image in the same' +
21- ' coordinate system (for conversion to pixels) or in' +
22- ' the target coordinate system (for conversion to a' +
23- ' geographic coordinate reference system).' )
24- parser .add_argument ('--output_path' , '-o' , type = str ,
25- help = 'Full path to the output file for the converted' +
26- 'footprints.' )
27- parser .add_argument ('--to_pixel' , '-p' , action = 'store_true' , default = False ,
28- help = 'Use this argument if you wish to convert' +
29- ' footprints in --source-file to pixel coordinates.' )
30- parser .add_argument ('--to_geo' , '-g' , action = 'store_true' , default = False ,
31- help = 'Use this argument if you wish to convert' +
32- ' footprints in --source-file to a geographic' +
33- ' coordinate system.' )
34- parser .add_argument ('--geometry_column' , '-c' , type = str ,
35- default = 'geometry' , help = 'The column containing' +
36- ' footprint polygons to transform. If not provided,' +
37- ' defaults to "geometry".' )
38- parser .add_argument ('--decimal_precision' , '-d' , type = int ,
39- help = 'The number of decimals to round to in the' +
40- ' final footprint geometries. If not provided, they' +
41- ' will be rounded to float32 precision.' )
42- parser .add_argument ('--batch' , '-b' , action = 'store_true' , default = False ,
43- help = 'Use this flag if you wish to operate on' +
44- ' multiple files in batch. In this case,' +
45- ' --argument-csv must be provided. See help' +
46- ' for --argument_csv and the codebase docs at' +
47- ' https://solaris.readthedocs.io for more info.' )
48- parser .add_argument ('--argument_csv' , '-a' , type = str ,
49- help = 'The reference file for variable values for' +
50- ' batch processing. It must contain columns to pass' +
51- ' the source_file and reference_image arguments, and' +
52- ' can additionally contain columns providing the' +
53- ' geometry_column and decimal_precision arguments' +
54- ' if you wish to define them differently for items' +
55- ' in the batch. These columns must have the same' +
56- ' names as the corresponding arguments. See the ' +
57- ' usage recipes at https://cw-geodata.readthedocs.io' +
58- ' for examples.' )
59- parser .add_argument ('--workers' , '-w' , type = int , default = 1 ,
60- help = 'The number of parallel processing workers to' +
61- ' use. This should not exceed the number of CPU' +
62- ' cores available.' )
20+ parser .add_argument (
21+ "--source_file" , "-s" , type = str , help = "Full path to file to transform"
22+ )
23+ parser .add_argument (
24+ "--reference_image" ,
25+ "-r" ,
26+ type = str ,
27+ help = "Full path to a georegistered image in the same"
28+ + " coordinate system (for conversion to pixels) or in"
29+ + " the target coordinate system (for conversion to a"
30+ + " geographic coordinate reference system)." ,
31+ )
32+ parser .add_argument (
33+ "--output_path" ,
34+ "-o" ,
35+ type = str ,
36+ help = "Full path to the output file for the converted" + "footprints." ,
37+ )
38+ parser .add_argument (
39+ "--to_pixel" ,
40+ "-p" ,
41+ action = "store_true" ,
42+ default = False ,
43+ help = "Use this argument if you wish to convert"
44+ + " footprints in --source-file to pixel coordinates." ,
45+ )
46+ parser .add_argument (
47+ "--to_geo" ,
48+ "-g" ,
49+ action = "store_true" ,
50+ default = False ,
51+ help = "Use this argument if you wish to convert"
52+ + " footprints in --source-file to a geographic"
53+ + " coordinate system." ,
54+ )
55+ parser .add_argument (
56+ "--geometry_column" ,
57+ "-c" ,
58+ type = str ,
59+ default = "geometry" ,
60+ help = "The column containing"
61+ + " footprint polygons to transform. If not provided,"
62+ + ' defaults to "geometry".' ,
63+ )
64+ parser .add_argument (
65+ "--decimal_precision" ,
66+ "-d" ,
67+ type = int ,
68+ help = "The number of decimals to round to in the"
69+ + " final footprint geometries. If not provided, they"
70+ + " will be rounded to float32 precision." ,
71+ )
72+ parser .add_argument (
73+ "--batch" ,
74+ "-b" ,
75+ action = "store_true" ,
76+ default = False ,
77+ help = "Use this flag if you wish to operate on"
78+ + " multiple files in batch. In this case,"
79+ + " --argument-csv must be provided. See help"
80+ + " for --argument_csv and the codebase docs at"
81+ + " https://solaris.readthedocs.io for more info." ,
82+ )
83+ parser .add_argument (
84+ "--argument_csv" ,
85+ "-a" ,
86+ type = str ,
87+ help = "The reference file for variable values for"
88+ + " batch processing. It must contain columns to pass"
89+ + " the source_file and reference_image arguments, and"
90+ + " can additionally contain columns providing the"
91+ + " geometry_column and decimal_precision arguments"
92+ + " if you wish to define them differently for items"
93+ + " in the batch. These columns must have the same"
94+ + " names as the corresponding arguments. See the "
95+ + " usage recipes at https://cw-geodata.readthedocs.io"
96+ + " for examples." ,
97+ )
98+ parser .add_argument (
99+ "--workers" ,
100+ "-w" ,
101+ type = int ,
102+ default = 1 ,
103+ help = "The number of parallel processing workers to"
104+ + " use. This should not exceed the number of CPU"
105+ + " cores available." ,
106+ )
63107
64108 args = parser .parse_args ()
65109 # check that the necessary set of arguments are provided.
66110 if args .batch and args .argument_csv is None :
67111 raise ValueError (
68- 'To perform batch processing, you must provide both --batch and' +
69- ' --argument_csv.' )
112+ "To perform batch processing, you must provide both --batch and"
113+ + " --argument_csv."
114+ )
70115 if args .argument_csv is None and args .source_file is None :
71116 raise ValueError (
72- 'You must provide a source file using either --source_file or' +
73- ' --argument_csv.' )
117+ "You must provide a source file using either --source_file or"
118+ + " --argument_csv."
119+ )
74120 if args .argument_csv is None and args .reference_image is None :
75121 raise ValueError (
76- 'You must provide a reference image using either' +
77- ' --reference_image or --argument_csv.' )
122+ "You must provide a reference image using either"
123+ + " --reference_image or --argument_csv."
124+ )
78125 if args .to_pixel == args .to_geo :
79126 raise ValueError (
80- 'One, and only one, of --to_pixel and --to_geo must be specified.' )
127+ "One, and only one, of --to_pixel and --to_geo must be specified."
128+ )
81129
82130 if args .argument_csv is not None :
83131 arg_df = pd .read_csv (args .argument_csv )
@@ -87,45 +135,53 @@ def main():
87135 if args .batch :
88136 # add values from individual arguments to the argument df
89137 if args .source_file is not None :
90- arg_df [' source_file' ] = args .source_file
138+ arg_df [" source_file" ] = args .source_file
91139 if args .reference_image is not None :
92- arg_df [' reference_image' ] = args .reference_image
140+ arg_df [" reference_image" ] = args .reference_image
93141 if args .geometry_column is not None :
94- arg_df [' geometry_column' ] = args .geometry_column
142+ arg_df [" geometry_column" ] = args .geometry_column
95143 if args .decimal_precision is not None :
96- arg_df [' decimal_precision' ] = args .decimal_precision
144+ arg_df [" decimal_precision" ] = args .decimal_precision
97145 else :
98146 # add values from individual arguments to the argument df
99147 if args .source_file is not None :
100- arg_df [' source_file' ] = [args .source_file ]
148+ arg_df [" source_file" ] = [args .source_file ]
101149 if args .reference_image is not None :
102- arg_df [' reference_image' ] = [args .reference_image ]
150+ arg_df [" reference_image" ] = [args .reference_image ]
103151 if args .geometry_column is not None :
104- arg_df [' geometry_column' ] = [args .geometry_column ]
152+ arg_df [" geometry_column" ] = [args .geometry_column ]
105153 if args .decimal_precision is not None :
106- arg_df [' decimal_precision' ] = [args .decimal_precision ]
154+ arg_df [" decimal_precision" ] = [args .decimal_precision ]
107155 if args .output_path is not None :
108- arg_df [' output_path' ] = [args .output_path ]
156+ arg_df [" output_path" ] = [args .output_path ]
109157
110158 if args .to_pixel :
111159 # rename argument columns for compatibility with the target func
112- arg_df = arg_df .rename (columns = {'source_file' : 'geojson' ,
113- 'reference_image' : 'im_path' ,
114- 'decimal_precision' : 'precision' ,
115- 'geometry_column' : 'geom_col' })
160+ arg_df = arg_df .rename (
161+ columns = {
162+ "source_file" : "geojson" ,
163+ "reference_image" : "im_path" ,
164+ "decimal_precision" : "precision" ,
165+ "geometry_column" : "geom_col" ,
166+ }
167+ )
116168 arg_dict_list = arg_df [
117- [' geojson' , ' im_path' , ' precision' , ' geom_col' , ' output_path' ]
118- ].to_dict (orient = ' records' )
169+ [" geojson" , " im_path" , " precision" , " geom_col" , " output_path" ]
170+ ].to_dict (orient = " records" )
119171 func_to_call = geojson_to_px_gdf
120172 elif args .to_geo :
121173 # rename argument columns for compatibility with the target func
122- arg_df = arg_df .rename (columns = {'source_file' : 'df' ,
123- 'reference_image' : 'im_path' ,
124- 'decimal_precision' : 'precision' ,
125- 'geometry_column' : 'geom_col' })
174+ arg_df = arg_df .rename (
175+ columns = {
176+ "source_file" : "df" ,
177+ "reference_image" : "im_path" ,
178+ "decimal_precision" : "precision" ,
179+ "geometry_column" : "geom_col" ,
180+ }
181+ )
126182 arg_dict_list = arg_df [
127- ['df' , ' im_path' , ' precision' , ' geom_col' , ' output_path' ]
128- ].to_dict (orient = ' records' )
183+ ["df" , " im_path" , " precision" , " geom_col" , " output_path" ]
184+ ].to_dict (orient = " records" )
129185 func_to_call = georegister_px_df
130186
131187 if not args .batch :
@@ -134,10 +190,11 @@ def main():
134190 return result
135191 else :
136192 with Pool (processes = args .workers ) as pool :
137- result = tqdm (pool .starmap (_func_wrapper , zip (repeat (func_to_call ),
138- arg_dict_list )))
193+ result = tqdm (
194+ pool .starmap (_func_wrapper , zip (repeat (func_to_call ), arg_dict_list ))
195+ )
139196 pool .close ()
140197
141198
142- if __name__ == ' __main__' :
199+ if __name__ == " __main__" :
143200 main ()
0 commit comments