@click.command()
@click.option('-f', '--separate-folders', is_flag=True,
  default=False)
@click.option('-st', '--style-type', type=click.Choice(STYLE_TYPES),
  default=None)
@click.option('-sf', '--style-filename',
  default='style.json')
@click.argument('kml_path')
@click.argument('output_dir')
def main(kml_path, output_dir, separate_folders=False,
  style_type=None, style_filename='style.json'):
    """
    Given a path to a KML file, convert it to one or several GeoJSON
    FeatureCollection files and save the result(s) to the given
    output directory.

    If ``separate_folders == False`` (the default), then create one
    GeoJSON file.
    If ``separate_folders == True``, then create several GeoJSON
    files, one for each folder in the KML file
    that contains geodata or that has a descendant node that contains geodata.
    Warning: this can produce GeoJSON files with the same geodata in case
    the KML file has nested folders with geodata.

    If a ``style_type`` is given (default is ``None``),
    then also build a JSON style
    file of the given style type and save it to the output directory
    under the name given by ``style_filename``
    (which defaults to 'style.json')
    """
    # Create absolute paths
    kml_path = pathlib.Path(kml_path).resolve()
    if output_dir is None:
        output_dir = kml_path.parent
    else:
        output_dir = pathlib.Path(output_dir)
    if not output_dir.exists():
        output_dir.mkdir()
    output_dir = output_dir.resolve()

    # Parse KML
    with kml_path.open('r') as src:
        kml_str = src.read()
    root = md.parseString(kml_str)

    # Build GeoJSON layers
    if separate_folders:
        layers = build_layers(root)
    else:
        name = kml_path.name.replace('.kml', '')
        layers = [build_feature_collection(root, name=name)]

    # Create filenames for layers
    filenames = disambiguate(
      [to_filename(layer['properties']['name'])
      for layer in layers])
    filenames = [name + '.geojson' for name in filenames]

    # Write layers to files
    for i in range(len(layers)):
        path = pathlib.Path(output_dir, filenames[i])
        with path.open('w') as tgt:
            json.dump(layers[i], tgt)

    # Build and export style file if desired
    if style_type in STYLE_TYPES:
        builder_name = 'build_{!s}_style'.format(style_type)
        style_dict = globals()[builder_name](root)
        path = pathlib.Path(output_dir, style_filename)
        with path.open('w') as tgt:
            json.dump(style_dict, tgt)
dataset/ETHPy150Open JamesMilnerUK/Loxo/conversiontools/kml/kml.py/main/original