6
6
import click
7
7
import pandas as pd
8
8
9
+ from .const import COMPRESSION_METHODS , CORE_COLUMNS
9
10
from .convert import convert as convert_
10
11
from .convert import list_all_converter_ids , list_all_converters
11
12
from .create_geojson import create_geojson as create_geojson_
12
13
from .create_geoparquet import create_geoparquet as create_geoparquet_
13
14
from .describe import describe as describe_
14
- from .merge import merge as merge_ , DEFAULT_COLUMNS , DEFAULT_CRS
15
+ from .improve import improve as improve_
16
+ from .merge import merge as merge_ , DEFAULT_CRS
15
17
from .jsonschema import jsonschema as jsonschema_
16
18
from .rename_extension import rename_extension as rename_extension_
17
19
from .util import (check_ext_schema_for_cli , log , parse_converter_input_files ,
18
- valid_file_for_cli , valid_file_for_cli_with_ext ,
20
+ parse_map , valid_file_for_cli , valid_file_for_cli_with_ext ,
19
21
valid_files_folders_for_cli , valid_folder_for_cli )
20
22
from .validate import validate as validate_
21
23
from .validate_schema import validate_schema as validate_schema_
@@ -376,7 +378,7 @@ def jsonschema(schema, out, fiboa_version, id_):
376
378
)
377
379
@click .option (
378
380
'--compression' , '-pc' ,
379
- type = click .Choice ([ "brotli" , "gzip" , "lz4" , "snappy" , "zstd" , "none" ] ),
381
+ type = click .Choice (COMPRESSION_METHODS ),
380
382
help = 'Compression method for the Parquet file.' ,
381
383
show_default = True ,
382
384
default = "brotli"
@@ -385,7 +387,7 @@ def jsonschema(schema, out, fiboa_version, id_):
385
387
'--geoparquet1' , '-gp1' ,
386
388
is_flag = True ,
387
389
type = click .BOOL ,
388
- help = 'Enforces generating a GeoParquet 1.0 file bounding box . Defaults to GeoParquet 1.1 with bounding box.' ,
390
+ help = 'Enforces generating a GeoParquet 1.0 file. Defaults to GeoParquet 1.1 with bounding box.' ,
389
391
default = False
390
392
)
391
393
@click .option (
@@ -394,13 +396,20 @@ def jsonschema(schema, out, fiboa_version, id_):
394
396
help = 'Url of mapping file. Some converters use additional sources with mapping data.' ,
395
397
default = None
396
398
)
397
- def convert (dataset , out , input , cache , source_coop , collection , compression , geoparquet1 , mapping_file ):
399
+ @click .option (
400
+ '--original-geometries' , '-og' ,
401
+ is_flag = True ,
402
+ type = click .BOOL ,
403
+ help = 'Keep the source geometries as provided, i.e. this option disables that geomtries are made valid and converted to Polygons.' ,
404
+ default = False
405
+ )
406
+ def convert (dataset , out , input , cache , source_coop , collection , compression , geoparquet1 , mapping_file , original_geometries ):
398
407
"""
399
408
Converts existing field boundary datasets to fiboa.
400
409
"""
401
410
log (f"fiboa CLI { __version__ } - Convert '{ dataset } '\n " , "success" )
402
411
try :
403
- convert_ (dataset , out , input , cache , source_coop , collection , compression , geoparquet1 , mapping_file )
412
+ convert_ (dataset , out , input , cache , source_coop , collection , compression , geoparquet1 , mapping_file , original_geometries )
404
413
except Exception as e :
405
414
log (e , "error" )
406
415
sys .exit (1 )
@@ -518,7 +527,7 @@ def rename_extension(folder, title, slug, org = "fiboa", prefix = None):
518
527
multiple = True ,
519
528
help = 'Additional column names to include.' ,
520
529
show_default = True ,
521
- default = DEFAULT_COLUMNS ,
530
+ default = CORE_COLUMNS ,
522
531
)
523
532
@click .option (
524
533
'--exclude' , '-e' ,
@@ -536,7 +545,7 @@ def rename_extension(folder, title, slug, org = "fiboa", prefix = None):
536
545
)
537
546
@click .option (
538
547
'--compression' , '-pc' ,
539
- type = click .Choice ([ "brotli" , "gzip" , "lz4" , "snappy" , "zstd" , "none" ] ),
548
+ type = click .Choice (COMPRESSION_METHODS ),
540
549
help = 'Compression method for the Parquet file.' ,
541
550
show_default = True ,
542
551
default = "brotli"
@@ -545,7 +554,7 @@ def rename_extension(folder, title, slug, org = "fiboa", prefix = None):
545
554
'--geoparquet1' , '-gp1' ,
546
555
is_flag = True ,
547
556
type = click .BOOL ,
548
- help = 'Enforces generating a GeoParquet 1.0 file bounding box . Defaults to GeoParquet 1.1 with bounding box.' ,
557
+ help = 'Enforces generating a GeoParquet 1.0 file. Defaults to GeoParquet 1.1 with bounding box.' ,
549
558
default = False
550
559
)
551
560
def merge (datasets , out , crs , include , exclude , extension , compression , geoparquet1 ):
@@ -564,6 +573,76 @@ def merge(datasets, out, crs, include, exclude, extension, compression, geoparqu
564
573
sys .exit (1 )
565
574
566
575
576
+ ## IMPROVE (add area, perimeter, and fix geometries)
577
+ @click .command ()
578
+ @click .argument ('input' , nargs = 1 , type = click .Path (exists = True ))
579
+ @click .option (
580
+ '--out' , '-o' ,
581
+ type = click .Path (exists = False ),
582
+ help = 'Path to write the GeoParquet file to. If not given, overwrites the input file.' ,
583
+ default = None
584
+ )
585
+ @click .option (
586
+ '--rename-column' , '-r' ,
587
+ type = click .STRING ,
588
+ callback = lambda ctx , param , value : parse_map (value ),
589
+ multiple = True ,
590
+ help = 'Renaming of columns. Provide the old name and the new name separated by an equal sign. Can be used multiple times.'
591
+ )
592
+ @click .option (
593
+ '--add-sizes' , '-sz' ,
594
+ is_flag = True ,
595
+ type = click .BOOL ,
596
+ help = 'Computes missing sizes (area, perimeter)' ,
597
+ default = False
598
+ )
599
+ @click .option (
600
+ '--fix-geometries' , '-g' ,
601
+ is_flag = True ,
602
+ type = click .BOOL ,
603
+ help = 'Tries to fix invalid geometries that are repored by the validator (uses GeoPanda\' s make_valid method internally)' ,
604
+ default = False
605
+ )
606
+ @click .option (
607
+ '--explode-geometries' , '-e' ,
608
+ is_flag = True ,
609
+ type = click .BOOL ,
610
+ help = 'Converts MultiPolygons to Polygons' ,
611
+ default = False
612
+ )
613
+ @click .option (
614
+ '--crs' ,
615
+ type = click .STRING ,
616
+ help = 'Coordinate Reference System (CRS) to use for the GeoParquet file.' ,
617
+ show_default = True ,
618
+ default = None ,
619
+ )
620
+ @click .option (
621
+ '--compression' , '-pc' ,
622
+ type = click .Choice (COMPRESSION_METHODS ),
623
+ help = 'Compression method for the Parquet file.' ,
624
+ show_default = True ,
625
+ default = "brotli"
626
+ )
627
+ @click .option (
628
+ '--geoparquet1' , '-gp1' ,
629
+ is_flag = True ,
630
+ type = click .BOOL ,
631
+ help = 'Enforces generating a GeoParquet 1.0 file. Defaults to GeoParquet 1.1 with bounding box.' ,
632
+ default = False
633
+ )
634
+ def improve (input , out , rename_column , add_sizes , fix_geometries , explode_geometries , crs , compression , geoparquet1 ):
635
+ """
636
+ "Improves" a fiboa GeoParquet file according to the given parameters.
637
+ """
638
+ log (f"fiboa CLI { __version__ } - Improve datasets\n " , "success" )
639
+ try :
640
+ improve_ (input , out , rename_column , add_sizes , fix_geometries , explode_geometries , crs , compression , geoparquet1 )
641
+ except Exception as e :
642
+ log (e , "error" )
643
+ sys .exit (1 )
644
+
645
+
567
646
cli .add_command (describe )
568
647
cli .add_command (validate )
569
648
cli .add_command (validate_schema )
@@ -574,6 +653,7 @@ def merge(datasets, out, crs, include, exclude, extension, compression, geoparqu
574
653
cli .add_command (converters )
575
654
cli .add_command (rename_extension )
576
655
cli .add_command (merge )
656
+ cli .add_command (improve )
577
657
578
658
if __name__ == '__main__' :
579
659
cli ()
0 commit comments