<commit_before># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import fiona
from shapely.geometry import mapping, shape

from .main import Centerline
from .utils import get_ogr_driver, is_polygon


def create_centerlines(src, dst, density=0.5):
    """Create centerlines and save the to an ESRI Shapefile.

    Reads polygons from the `src` ESRI Shapefile, creates Centerline
    objects with the specified `density` parameter and writes them to
    the `dst` ESRI Shapefile.

    Only Polygon features are converted to centerlines. Features of
    different types are skipped.

    Args:
        src (str): source ESRI Shapefile
        dst (str): destination ESRI Shapefile
        density (:obj:`float`, optional): the Centerline's density.
            Defaults to 0.5 (meters)

    Returns:
        None

    """
    try:
        DST_DRIVER = get_ogr_driver(filepath=dst)
    except ValueError:
        raise

    with fiona.drivers():
        with fiona.open(path=src, mode='r') as source:
            SCHEMA = source.schema.copy()
            SCHEMA.update({'geometry': 'MultiLineString'})
            with fiona.open(
                    path=dst,
                    mode='w',
                    driver=DST_DRIVER.GetName(),
                    schema=SCHEMA,
                    crs=source.crs,
                    encoding=source.encoding) as destination:
                for record in source:
                    geom = record.get('geometry')

                    # Skip geometries that are not of type Polygon
                    if is_polygon(geometry_type=geom.get('type')):
                        continue

                    input_geom = shape(geom)
                    attributes = record.get('properties')
                    centerline_obj = Centerline(
                        input_geom=input_geom,
                        interpolation_dist=density,
                        **attributes
                    )

                    centerline = {}
                    centerline['geometry'] = mapping(centerline_obj)
                    centerline['properties'] = {
                        k: v
                        for k, v in centerline_obj.__dict__.items()
                        if k in attributes.keys()
                    }
                    destination.write(centerline)

    return None<commit_msg>Create the centerline dictionary at once<commit_after># -*- coding: utf-8 -*-

from __future__ import unicode_literals

import fiona
from shapely.geometry import mapping, shape

from .main import Centerline
from .utils import get_ogr_driver, is_polygon


def create_centerlines(src, dst, density=0.5):
    """Create centerlines and save the to an ESRI Shapefile.

    Reads polygons from the `src` ESRI Shapefile, creates Centerline
    objects with the specified `density` parameter and writes them to
    the `dst` ESRI Shapefile.

    Only Polygon features are converted to centerlines. Features of
    different types are skipped.

    Args:
        src (str): source ESRI Shapefile
        dst (str): destination ESRI Shapefile
        density (:obj:`float`, optional): the Centerline's density.
            Defaults to 0.5 (meters)

    Returns:
        None

    """
    try:
        DST_DRIVER = get_ogr_driver(filepath=dst)
    except ValueError:
        raise

    with fiona.drivers():
        with fiona.open(path=src, mode='r') as source:
            SCHEMA = source.schema.copy()
            SCHEMA.update({'geometry': 'MultiLineString'})
            with fiona.open(
                    path=dst,
                    mode='w',
                    driver=DST_DRIVER.GetName(),
                    schema=SCHEMA,
                    crs=source.crs,
                    encoding=source.encoding) as destination:
                for record in source:
                    geom = record.get('geometry')

                    # Skip geometries that are not of type Polygon
                    if is_polygon(geometry_type=geom.get('type')):
                        continue

                    input_geom = shape(geom)
                    attributes = record.get('properties')
                    centerline_obj = Centerline(
                        input_geom=input_geom,
                        interpolation_dist=density,
                        **attributes
                    )

                    centerline_dict = {
                        'geometry': mapping(centerline_obj),
                        'properties': {
                            k: v
                            for k, v in centerline_obj.__dict__.items()
                            if k in attributes.keys()
                        }
                    }

                    destination.write(centerline_dict)

    return None
{'fraction_non_alphanumeric': 0.07574485942089802, 'fraction_numerical': 0.00209819555182543, 'mean_word_length': 1.5010493179433368, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '299270', 'n_tokens_mistral': 1262, 'n_tokens_neox': 1148, 'n_words': 336}