@@ -31,7 +31,7 @@ def match_min_max_with_pixel_size(min_d: float, max_d: float, pixel_per_meter: f
3131 return min_d , max_d
3232
3333
34- def color (
34+ def color_from_stream (
3535 input_file : str ,
3636 output_file : str ,
3737 proj = "" ,
@@ -156,28 +156,58 @@ def color(
156156 return tmp_ortho , tmp_ortho_irc
157157
158158
159- def parse_args ():
160- parser = argparse .ArgumentParser ("Colorize tool" , formatter_class = argparse .RawTextHelpFormatter )
161- parser .add_argument ("--input" , "-i" , type = str , required = True , help = "Input file" )
162- parser .add_argument ("--output" , "-o" , type = str , default = "" , help = "Output file" )
163- parser .add_argument (
164- "--proj" , "-p" , type = str , default = "" , help = "Projection, default will use projection from metadata input"
159+ def color_from_files (
160+ input_file : str ,
161+ output_file : str ,
162+ rgb_image : str ,
163+ irc_image : str ,
164+ color_rvb_enabled = True ,
165+ color_ir_enabled = True ,
166+ veget_index_file = "" ,
167+ vegetation_dim = "Deviation" ,
168+ ):
169+ pipeline = pdal .Reader .las (filename = input_file )
170+
171+ writer_extra_dims = "all"
172+
173+ if veget_index_file and veget_index_file != "" :
174+ print (f"Remplissage du champ { vegetation_dim } à partir du fichier { veget_index_file } " )
175+ pipeline |= pdal .Filter .colorization (raster = veget_index_file , dimensions = f"{ vegetation_dim } :1:256.0" )
176+ writer_extra_dims = [f"{ vegetation_dim } =ushort" ]
177+
178+ # Warning: the initial color is multiplied by 256 despite its initial 8-bits encoding
179+ # which turns it to a 0 to 255*256 range.
180+ # It is kept this way because of other dependencies that have been tuned to fit this range
181+ if color_rvb_enabled :
182+ pipeline |= pdal .Filter .colorization (raster = rgb_image , dimensions = "Red:1:256.0, Green:2:256.0, Blue:3:256.0" )
183+ if color_ir_enabled :
184+ pipeline |= pdal .Filter .colorization (raster = irc_image , dimensions = "Infrared:1:256.0" )
185+
186+ pipeline |= pdal .Writer .las (
187+ filename = output_file , extra_dims = writer_extra_dims , minor_version = "4" , dataformat_id = "8" , forward = "all"
165188 )
166- parser .add_argument ("--resolution" , "-r" , type = float , default = 5 , help = "Resolution, in pixel per meter" )
167- parser .add_argument ("--timeout" , "-t" , type = int , default = 300 , help = "Timeout, in seconds" )
168- parser .add_argument ("--rvb" , action = "store_true" , help = "Colorize RVB" )
169- parser .add_argument ("--ir" , action = "store_true" , help = "Colorize IR" )
170- parser .add_argument (
171- "--vegetation" ,
172- type = str ,
173- default = "" ,
174- help = "Vegetation file (raster), value will be stored in 'vegetation_dim' field" ,
189+
190+ print ("Traitement du nuage de point" )
191+ pipeline .execute ()
192+
193+
194+ def argument_parser ():
195+ parser = argparse .ArgumentParser ("Colorize tool" )
196+ subparsers = parser .add_subparsers (required = True )
197+
198+ # first command is 'from_stream'
199+ from_stream = subparsers .add_parser ("from_stream" , help = "Images are downloaded from streams" )
200+ from_stream .add_argument (
201+ "--proj" , "-p" , type = str , default = "" , help = "Projection, default will use projection from metadata input"
175202 )
176- parser .add_argument (
177- "--vegetation_dim" , type = str , default = "Deviation" , help = "name of the extra_dim uses for the vegetation value"
203+ from_stream .add_argument ("--timeout" , "-t" , type = int , default = 300 , help = "Timeout, in seconds" )
204+ from_stream .add_argument ("--rvb" , action = "store_true" , help = "Colorize RVB" )
205+ from_stream .add_argument ("--ir" , action = "store_true" , help = "Colorize IR" )
206+ from_stream .add_argument ("--resolution" , "-r" , type = float , default = 5 , help = "Resolution, in pixel per meter" )
207+ from_stream .add_argument (
208+ "--check-images" , "-c" , action = "store_true" , help = "Check that downloaded image is not white"
178209 )
179- parser .add_argument ("--check-images" , "-c" , action = "store_true" , help = "Check that downloaded image is not white" )
180- parser .add_argument (
210+ from_stream .add_argument (
181211 "--stream-RGB" ,
182212 type = str ,
183213 default = "ORTHOIMAGERY.ORTHOPHOTOS" ,
@@ -186,27 +216,49 @@ def parse_args():
186216for 20cm resolution rasters, use HR.ORTHOIMAGERY.ORTHOPHOTOS
187217for 50 cm resolution rasters, use ORTHOIMAGERY.ORTHOPHOTOS.BDORTHO""" ,
188218 )
189- parser .add_argument (
219+ from_stream .add_argument (
190220 "--stream-IRC" ,
191221 type = str ,
192222 default = "ORTHOIMAGERY.ORTHOPHOTOS.IRC" ,
193223 help = """WMS raster stream for IRC colorization. Default to ORTHOIMAGERY.ORTHOPHOTOS.IRC
194224Documentation about possible stream : https://geoservices.ign.fr/services-web-experts-ortho""" ,
195225 )
196- parser .add_argument (
226+ from_stream .add_argument (
197227 "--size-max-GPF" ,
198228 type = int ,
199229 default = 5000 ,
200230 help = "Maximum edge size (in pixels) of downloaded images."
201231 " If input file needs more, several images are downloaded and merged." ,
202232 )
233+ add_common_options (from_stream )
234+ from_stream .set_defaults (func = from_stream_func )
203235
204- return parser .parse_args ()
236+ # second command is 'from_files'
237+ from_files = subparsers .add_parser ("from_files" , help = "Images are in directories from RGB/IRC" )
238+ from_files .add_argument ("--image_RGB" , type = str , required = True , help = "RGB image filepath" )
239+ from_files .add_argument ("--image_IRC" , type = str , required = True , help = "IRC image filepath" )
240+ add_common_options (from_files )
241+ from_files .set_defaults (func = from_files_func )
205242
243+ return parser
206244
207- if __name__ == "__main__" :
208- args = parse_args ()
209- color (
245+
246+ def add_common_options (parser ):
247+ parser .add_argument ("--input" , "-i" , type = str , required = True , help = "Input file" )
248+ parser .add_argument ("--output" , "-o" , type = str , default = "" , help = "Output file" )
249+ parser .add_argument (
250+ "--vegetation" ,
251+ type = str ,
252+ default = "" ,
253+ help = "Vegetation file (raster), value will be stored in 'vegetation_dim' field" ,
254+ )
255+ parser .add_argument (
256+ "--vegetation_dim" , type = str , default = "Deviation" , help = "name of the extra_dim uses for the vegetation value"
257+ )
258+
259+
260+ def from_stream_func (args ):
261+ color_from_stream (
210262 input_file = args .input ,
211263 output_file = args .output ,
212264 proj = args .proj ,
@@ -221,3 +273,33 @@ def parse_args():
221273 stream_IRC = args .stream_IRC ,
222274 size_max_gpf = args .size_max_GPF ,
223275 )
276+
277+
278+ def from_files_func (args ):
279+ if args .image_RGB and args .image_RGB != "" :
280+ color_rvb_enabled = True
281+ else :
282+ color_rvb_enabled = False
283+ if args .image_IRC and args .image_IRC != "" :
284+ color_irc_enabled = True
285+ else :
286+ color_irc_enabled = False
287+
288+ if not color_rvb_enabled and not color_irc_enabled :
289+ raise ValueError ("At least one of --rvb or --ir must be provided" )
290+
291+ color_from_files (
292+ input_file = args .input ,
293+ output_file = args .output ,
294+ rgb_image = args .image_RGB ,
295+ irc_image = args .image_IRC ,
296+ color_rvb_enabled = color_rvb_enabled ,
297+ color_ir_enabled = color_irc_enabled ,
298+ veget_index_file = args .vegetation ,
299+ vegetation_dim = args .vegetation_dim ,
300+ )
301+
302+
303+ if __name__ == "__main__" :
304+ args = argument_parser .parse_args ()
305+ args .func (args )
0 commit comments