<commit_before>import requests
from polyline.codec import PolylineCodec
from osgeo.ogr import Geometry
from pandas import DataFrame
import numpy as np
from itertools import islice

__all__ = ['match', 'simple_viaroute', 'table', 'table_OD',
           'nearest', 'locate', 'decode_geom']


def match(points, host='http://localhost:5000', geometry=True,
          gps_precision=-1, matching_beta=-1, decode_polyline=True):
    url = ''.join([host, '/match?'])
    for loc, t in points:
        url += 'loc=' + str(loc[0]) + ',' + str(loc[1]) + '&t=' + str(t) + '&'
    url = url[:-1]
    url = ''.join([url, '&geometry=', str(geometry).lower(), '&gps_precision=',
                   str(gps_precision), '&matching_beta=', str(matching_beta)])
    r = requests.get(url)
    r_json = r.json()
    if decode_polyline:
        if 'matchings' in r_json.keys():
            for i, matching in enumerate(r_json['matchings']):
                geom_encoded = r_json["matchings"][i]["geometry"]
                geom_decoded = [[point[0] / 10.0,
                                 point[1] / 10.0] for point
                                in PolylineCodec().decode(geom_encoded)]
                r_json["matchings"][i]["geometry"] = geom_decoded
        else:
            print('No matching geometry to decode')
    return r_json


def decode_geom(encoded_polyline):
    """
    Function decoding an encoded polyline (with 'encoded polyline
    algorithme') and returning an osgeo.ogr.Geometry object
    Params:
    encoded_polyline: str
        The encoded string to decode
    """
    epa_dec = PolylineCodec().decode(encoded_polyline)
    fausse_liste = str(epa_dec)
    ma_ligne = Geometry(2)
    lineAddPts = ma_ligne.AddPoint_2D

    # List of points coordinates :
    lat, long = [], []
    latAppd, longAppd = lat.append, long.append
    valueliste = fausse_liste[1:len(fausse_liste) - 1].split(",")
    for i in valueliste:
        if '(' in i:
            latAppd(float(i[i.find('(') + 1:])/10)
        elif ')' in i:
            longAppd(float(i[i.find(' ') + 1:len(i) - 1])/10)

    for coord in zip(long, lat):
        lineAddPts(coord[0], coord[1])

    return ma_ligne


def simple_viaroute(coord_origin, coord_dest, alt=False,
                    output='raw', host='http://localhost:5000', z=None):
    """
    Function wrapping OSRM 'viaroute' function and returning the JSON reponse
    with the route_geometry decoded (in WKT or WKB) if needed.
    Params:
    coord_origine: list of two float
        [x ,y] where x is longitude and y is latitude
    coord_dest: list of two float
        [x ,y] where x is longitude and y is latitude
    alt: boolean, default False
        Query (and resolve geometry if asked) for alternatives routes
    output: str, default 'wkt'
        Define the type of output
    host: str, default 'http://localhost:5000'
        Url and port of the OSRM instance (no final bakslash)
    z: int, default None (None = parameter not passed to OSRM)
        The zoom level used for compressing the route

    Output:
    - if 'raw' : the original json returned by OSRM
    - if 'WKT' : the json returned by OSRM with the 'route_geometry' converted
                 in WKT format
    - if 'WKB' : the json returned by OSRM with the 'route_geometry' converted
                 in WKB format
    """
    if output.lower() in ('wkt', 'well-known-text', 'text'):
        output = 1
    elif output.lower() in ('wkb', 'well-known-binary'):
        output = 2
    elif 'raw' in output.lower():
        output = 3
    else:
        print('Unknow \'output\' parameter')
        return -1

    url = ('{}/viaroute?loc={}&loc={}&instructions=false'
           '&alt={}').format(host,
                             str(coord_origin[1])+','+str(coord_origin[0]),
                             str(coord_dest[1])+','+str(coord_dest[0]),
                             str(alt).lower())
    try:  # Dont use 'z' if not renseigned or if bad renseigned
        if 0 < int(z) < 19:
            url = ''.join([url, '&z={}'.format(z)])
    except:
        pass

    try:  # Querying the OSRM instance..
        rep = requests.get(url)
        parsed_json = rep.json()
    except Exception as err:
        print('Error while contacting OSRM instance : \n{}'.format(err))
        return -1
    if parsed_json['status'] is not 207:
        if output == 3:
            return parsed_json
        else:
            ogr_line = decode_geom(parsed_json['route_geometry'])
            if parsed_json['found_alternative']:
                list_alt = [decode_geom(altgeom).ExportToWkb() if output == 2
                            else decode_geom(altgeom).ExportToWkt() for altgeom
                            in parsed_json['alternative_geometries']]
                parsed_json['alternative_geometries'] = list_alt
            if output == 2:
                parsed_json['route_geometry'] = ogr_line.ExportToWkb()
                return parsed_json
            elif output == 1:
                parsed_json['route_geometry'] = ogr_line.ExportToWkt()
                return parsed_json
    else:
        print('Error - OSRM status : {} \n Full json reponse : {}'.format(
              parsed_json['status'], parsed_json))
        return -1


def table(list_coords, list_ids, output='df',
          host='http://localhost:5000'):
    """
    Function wrapping OSRM 'table' function in order to get a matrix of
    time distance as a numpy array or as a DataFrame
    Params :
        list_coords: list
            A list of coord as [x, y] , like :
                 list_coords = [[21.3224, 45.2358],
                                [21.3856, 42.0094],
                                [20.9574, 41.5286]] (coords have to be float)
        list_ids: list
            A list of the corresponding unique id, like :
                     list_ids = ['name1',
                                 'name2',
                                 'name3'] (id can be str, int or float)
        host: str, default 'http://localhost:5000'
            Url and port of the OSRM instance (no final bakslash)
        output: str, default 'pandas'
            The type of matrice to return (DataFrame or numpy array)
                'pandas', 'df' or 'DataFrame' for a DataFrame
                'numpy', 'array' or 'np' for a numpy array
    Output:
        - 'numpy' : a numpy array containing the time in minutes
                (or NaN when OSRM encounter an error to compute a route)
        or
        - 'pandas' : a labeled DataFrame containing the time matrix in minutes
                (or NaN when OSRM encounter an error to compute a route)

        -1 is return in case of any other error (bad 'output' parameter,
            wrong list of coords/ids, unknow host,
            wrong response from the host, etc.)
    """
    if output.lower() in ('numpy', 'array', 'np'):
        output = 1
    elif output.lower() in ('pandas', 'dataframe', 'df'):
        output = 2
    else:
        print('Unknow output parameter')
        return -1

    query = [host, '/table?loc=']
    for coord, uid in zip(list_coords, list_ids):  # Preparing the query
        tmp = ''.join([str(coord[1]), ',', str(coord[0]), '&loc='])
        query.append(tmp)
    query = (''.join(query))[:-5]
    try:  # Querying the OSRM local instance
        rep = requests.get(query)
        parsed_json = rep.json()
    except Exception as err:
        print('Error while contacting OSRM instance : \n{}'.format(err))
        return -1

    if 'distance_table' in parsed_json.keys():  # Preparing the result matrix
        mat = np.array(parsed_json['distance_table'])
        if len(mat) < len(list_coords):
            print(('The array returned by OSRM is smaller to the size of the '
                   'array requested\nOSRM parameter --max-table-size should be'
                   ' increased or function osrm.table_OD(...) should be used'))
            return -1
        mat = mat/(10*60)  # Conversion in minutes
        mat = mat.round(1)
        mat[mat == 3579139.4] = np.NaN  # Flag the errors with NaN
        if output == 1:
            return mat
        elif output == 2:
            df = DataFrame(mat, index=list_ids, columns=list_ids, dtype=float)
            return df
    else:
        print('No distance table return by OSRM local instance')
        return -1


def chunk(it, size):<commit_msg>Fix array creation type + ogr import<commit_after>import requests
from polyline.codec import PolylineCodec
try:
    from osgeo.ogr import Geometry
except:
    from ogr import Geometry
from pandas import DataFrame
import numpy as np
from itertools import islice

__all__ = ['match', 'simple_viaroute', 'table', 'table_OD',
           'nearest', 'locate', 'decode_geom']


def match(points, host='http://localhost:5000', geometry=True,
          gps_precision=-1, matching_beta=-1, decode_polyline=True):
    url = ''.join([host, '/match?'])
    for loc, t in points:
        url += 'loc=' + str(loc[0]) + ',' + str(loc[1]) + '&t=' + str(t) + '&'
    url = url[:-1]
    url = ''.join([url, '&geometry=', str(geometry).lower(), '&gps_precision=',
                   str(gps_precision), '&matching_beta=', str(matching_beta)])
    r = requests.get(url)
    r_json = r.json()
    if decode_polyline:
        if 'matchings' in r_json.keys():
            for i, matching in enumerate(r_json['matchings']):
                geom_encoded = r_json["matchings"][i]["geometry"]
                geom_decoded = [[point[0] / 10.0,
                                 point[1] / 10.0] for point
                                in PolylineCodec().decode(geom_encoded)]
                r_json["matchings"][i]["geometry"] = geom_decoded
        else:
            print('No matching geometry to decode')
    return r_json


def decode_geom(encoded_polyline):
    """
    Function decoding an encoded polyline (with 'encoded polyline
    algorithme') and returning an osgeo.ogr.Geometry object
    Params:
    encoded_polyline: str
        The encoded string to decode
    """
    epa_dec = PolylineCodec().decode(encoded_polyline)
    fausse_liste = str(epa_dec)
    ma_ligne = Geometry(2)
    lineAddPts = ma_ligne.AddPoint_2D

    # List of points coordinates :
    lat, long = [], []
    latAppd, longAppd = lat.append, long.append
    valueliste = fausse_liste[1:len(fausse_liste) - 1].split(",")
    for i in valueliste:
        if '(' in i:
            latAppd(float(i[i.find('(') + 1:])/10)
        elif ')' in i:
            longAppd(float(i[i.find(' ') + 1:len(i) - 1])/10)

    for coord in zip(long, lat):
        lineAddPts(coord[0], coord[1])

    return ma_ligne


def simple_viaroute(coord_origin, coord_dest, alt=False,
                    output='raw', host='http://localhost:5000', z=None):
    """
    Function wrapping OSRM 'viaroute' function and returning the JSON reponse
    with the route_geometry decoded (in WKT or WKB) if needed.
    Params:
    coord_origine: list of two float
        [x ,y] where x is longitude and y is latitude
    coord_dest: list of two float
        [x ,y] where x is longitude and y is latitude
    alt: boolean, default False
        Query (and resolve geometry if asked) for alternatives routes
    output: str, default 'wkt'
        Define the type of output
    host: str, default 'http://localhost:5000'
        Url and port of the OSRM instance (no final bakslash)
    z: int, default None (None = parameter not passed to OSRM)
        The zoom level used for compressing the route

    Output:
    - if 'raw' : the original json returned by OSRM
    - if 'WKT' : the json returned by OSRM with the 'route_geometry' converted
                 in WKT format
    - if 'WKB' : the json returned by OSRM with the 'route_geometry' converted
                 in WKB format
    """
    if output.lower() in ('wkt', 'well-known-text', 'text'):
        output = 1
    elif output.lower() in ('wkb', 'well-known-binary'):
        output = 2
    elif 'raw' in output.lower():
        output = 3
    else:
        print('Unknow \'output\' parameter')
        return -1

    url = ('{}/viaroute?loc={}&loc={}&instructions=false'
           '&alt={}').format(host,
                             str(coord_origin[1])+','+str(coord_origin[0]),
                             str(coord_dest[1])+','+str(coord_dest[0]),
                             str(alt).lower())
    try:  # Dont use 'z' if not renseigned or if bad renseigned
        if 0 < int(z) < 19:
            url = ''.join([url, '&z={}'.format(z)])
    except:
        pass

    try:  # Querying the OSRM instance..
        rep = requests.get(url)
        parsed_json = rep.json()
    except Exception as err:
        print('Error while contacting OSRM instance : \n{}'.format(err))
        return -1
    if parsed_json['status'] is not 207:
        if output == 3:
            return parsed_json
        else:
            ogr_line = decode_geom(parsed_json['route_geometry'])
            if parsed_json['found_alternative']:
                list_alt = [decode_geom(altgeom).ExportToWkb() if output == 2
                            else decode_geom(altgeom).ExportToWkt() for altgeom
                            in parsed_json['alternative_geometries']]
                parsed_json['alternative_geometries'] = list_alt
            if output == 2:
                parsed_json['route_geometry'] = ogr_line.ExportToWkb()
                return parsed_json
            elif output == 1:
                parsed_json['route_geometry'] = ogr_line.ExportToWkt()
                return parsed_json
    else:
        print('Error - OSRM status : {} \n Full json reponse : {}'.format(
              parsed_json['status'], parsed_json))
        return -1


def table(list_coords, list_ids, output='df',
          host='http://localhost:5000'):
    """
    Function wrapping OSRM 'table' function in order to get a matrix of
    time distance as a numpy array or as a DataFrame
    Params :
        list_coords: list
            A list of coord as [x, y] , like :
                 list_coords = [[21.3224, 45.2358],
                                [21.3856, 42.0094],
                                [20.9574, 41.5286]] (coords have to be float)
        list_ids: list
            A list of the corresponding unique id, like :
                     list_ids = ['name1',
                                 'name2',
                                 'name3'] (id can be str, int or float)
        host: str, default 'http://localhost:5000'
            Url and port of the OSRM instance (no final bakslash)
        output: str, default 'pandas'
            The type of matrice to return (DataFrame or numpy array)
                'pandas', 'df' or 'DataFrame' for a DataFrame
                'numpy', 'array' or 'np' for a numpy array
    Output:
        - 'numpy' : a numpy array containing the time in minutes
                (or NaN when OSRM encounter an error to compute a route)
        or
        - 'pandas' : a labeled DataFrame containing the time matrix in minutes
                (or NaN when OSRM encounter an error to compute a route)

        -1 is return in case of any other error (bad 'output' parameter,
            wrong list of coords/ids, unknow host,
            wrong response from the host, etc.)
    """
    if output.lower() in ('numpy', 'array', 'np'):
        output = 1
    elif output.lower() in ('pandas', 'dataframe', 'df'):
        output = 2
    else:
        print('Unknow output parameter')
        return -1

    query = [host, '/table?loc=']
    for coord, uid in zip(list_coords, list_ids):  # Preparing the query
        tmp = ''.join([str(coord[1]), ',', str(coord[0]), '&loc='])
        query.append(tmp)
    query = (''.join(query))[:-5]
    try:  # Querying the OSRM local instance
        rep = requests.get(query)
        parsed_json = rep.json()
    except Exception as err:
        print('Error while contacting OSRM instance : \n{}'.format(err))
        return -1

    if 'distance_table' in parsed_json.keys():  # Preparing the result matrix
        mat = np.array(parsed_json['distance_table'], dtype='float64')
        if len(mat) < len(list_coords):
            print(('The array returned by OSRM is smaller to the size of the '
                   'array requested\nOSRM parameter --max-table-size should be'
                   ' increased or function osrm.table_OD(...) should be used'))
            return -1
        mat = mat/(10*60)  # Conversion in minutes
        mat = mat.round(1)
        mat[mat == 3579139.4] = np.NaN  # Flag the errors with NaN
        if output == 1:
            return mat
        elif output == 2:
            df = DataFrame(mat, index=list_ids, columns=list_ids, dtype=float)
            return df
    else:
        print('No distance table return by OSRM local instance')
        return -1


def chunk(it, size):
{'fraction_non_alphanumeric': 0.11572753209700427, 'fraction_numerical': 0.015572990965287685, 'mean_word_length': 2.063546977421704, 'pattern_counts': {'":': 0, '<': 9, '<?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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3975898', 'n_tokens_mistral': 5257, 'n_tokens_neox': 4986, 'n_words': 1555}