<commit_before>import json
import math


def linestrings_intersect(line1, line2):
    """
    To valid whether linestrings from geojson are intersected with each other.

    Keyword arguments:
    line1 -- first line geojson object
    line2 -- second line geojson object

    if(line1 intersects with other) return intersect point array else empty array
    """
    intersects = []
    for i in range(0, len(line1['coordinates']) - 1):
        for j in range(0, len(line2['coordinates']) - 1):
            a1_x = line1['coordinates'][i][1]
            a1_y = line1['coordinates'][i][0]
            a2_x = line1['coordinates'][i + 1][1]
            a2_y = line1['coordinates'][i + 1][0]
            b1_x = line2['coordinates'][j][1]
            b1_y = line2['coordinates'][j][0]
            b2_x = line2['coordinates'][j + 1][1]
            b2_y = line2['coordinates'][j + 1][0]
            ua_t = (b2_x - b1_x) * (a1_y - b1_y) - (b2_y - b1_y) * (a1_x - b1_x)
            ub_t = (a2_x - a1_x) * (a1_y - b1_y) - (a2_y - a1_y) * (a1_x - b1_x)
            u_b = (b2_y - b1_y) * (a2_x - a1_x) - (b2_x - b1_x) * (a2_y - a1_y)
            if not u_b == 0:
                u_a = ua_t / u_b
                u_b = ub_t / u_b
                if 0 <= u_a and u_a <= 1 and 0 <= u_b and u_b <= 1:
                    intersects.append({'type': 'Point', 'coordinates': [a1_x + u_a * (a2_x - a1_x), a1_y + u_a * (a2_y - a1_y)]})
    # if len(intersects) == 0:
    #     intersects = False
    return intersects


def bbox_around_polycoords(coords):
    """
    bounding box
    """
    x_all = []
    y_all = []

    for first in coords[0]:
        x_all.append(first[1])
        y_all.append(first[0])

    return [min(x_all), min(y_all), max(x_all), max(y_all)]


def point_in_bbox(point, bounds):
    """
    valid whether the point is inside the bounding box
    """
    return not(point['coordinates'][1] < bounds[0] or point['coordinates'][1] > bounds[2] \
     or point['coordinates'][0] < bounds[1] or point['coordinates'][0] > bounds[3])


def pnpoly(x, y, coords):
    """
    the algorithm to judge whether the point is located in polygon
    reference: https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html#Explanation
    """
    vert = [[0, 0]]

    for coord in coords:
        for node in coord:
            vert.append(node)
        vert.append(coord[0])
        vert.append([0, 0])

    inside = False

    i = 0
    j = len(vert) - 1

    while i < len(vert):
        if ((vert[i][0] > y) != (vert[j][0] > y)) and (x < (vert[j][1] - vert[i][1]) \
         * (y - vert[i][0]) / (vert[j][0] - vert[i][0]) + vert[i][1]):
            inside = not inside
        j = i
        i += 1

    return inside


def point_in_polygon(point, poly):
    """
    valid whether the point is located in a polygon

    Keyword arguments:
    point -- point geojson object
    poly  -- polygon geojson object

    if(point inside poly) return true else false
    """
    coords = [poly['coordinates']] if poly['type'] == 'Polygon' else poly['coordinates']
    inside_box = False
    for coord in coords:
        if inside_box:
            break
        if point_in_bbox(point, bbox_around_polycoords(coord)):
            inside_box = True
    if not inside_box:
        return False

    inside_poly = False
    for coord in coords:
        if inside_poly:
            break
        if pnpoly(point['coordinates'][1], point['coordinates'][0], coord):
            inside_poly = True
    return inside_poly

def point_in_multipolygon(point, multipoly):
    """
    valid whether the point is located in a mulitpolygon (donut polygon is not supported)

    Keyword arguments:
    point      -- point geojson object
    multipoly  -- multipolygon geojson object

    if(point inside multipoly) return true else false
    """
    coords_array = [multipoly['coordinates']] if multipoly.type == "MultiPolygon" else multipoly.coordinates

    for coords in coords_array:
        if point_in_polygon(point, coords):
            return True

    return False

def number2radius(number):
    """
    convert degree into radius
    """
    return number*math.pi /180

def number2degree(number):
    """
    convert radius into degree
    """
    return number* 180/math.pi


def test():
    """
    test for geojson-python-utils
    """
    #linestrings intersect
    diagonal_up_str = '{ "type": "LineString","coordinates": [[0, 0], [10, 10]]}'
    diagonal_down_str = '{ "type": "LineString","coordinates": [[10, 0], [0, 10]]}'
    far_away_str = '{ "type": "LineString","coordinates": [[100, 100], [110, 110]]}'
    diagonal_up = json.loads(diagonal_up_str)
    diagonal_down = json.loads(diagonal_down_str)
    far_away = json.loads(far_away_str)
    print "intersect:%s" % ('True' if linestrings_intersect(diagonal_up, diagonal_down) else 'False')
    print "not intersect:%s" % ('True' if linestrings_intersect(diagonal_up, far_away) else 'False')

    #point in polygon
    in_str = '{"type": "Point", "coordinates": [5, 5]}'
    out_str = '{"type": "Point", "coordinates": [15, 15]}'
    box_str = '{"type": "Polygon","coordinates": [[ [0, 0], [10, 0], [10, 10], [0, 10] ]]}'
    in_box = json.loads(in_str)
    out_box = json.loads(out_str)
    box = json.loads(box_str)
    print "inbox : %s " % ('True' if point_in_polygon(in_box, box) else 'False')
    print "outbox : %s" % ('True' if point_in_polygon(out_box, box) else 'False')

    #point in multipolygon
    point_str = '{"type": "Point", "coordinates": [0.5, 0.5]}'
    single_point_str = '{"type": "Point", "coordinates": [-1, -1]}'
    multipoly_str = '{"type":"MultiPolygon","coordinates":[[[[0,0],[0,10],[10,10],[10,0],[0,0]]],[[[10,10],[10,20],[20,20],[20,10],[10,10]]]]}'
    point = json.loads(point_str)
    single_point = json.loads(single_point_str)
    multipoly = json.loads(multipoly_str)
    print "inbox : %s " % ('True' if point_in_multipolygon(point, multipoly) else 'False')
    print "outbox : %s" % ('True' if point_in_multipolygon(single_point, multipoly) else 'False')


if __name__ == '__main__':
    test()<commit_msg>fix bug<commit_after>import json
import math


def linestrings_intersect(line1, line2):
    """
    To valid whether linestrings from geojson are intersected with each other.

    Keyword arguments:
    line1 -- first line geojson object
    line2 -- second line geojson object

    if(line1 intersects with other) return intersect point array else empty array
    """
    intersects = []
    for i in range(0, len(line1['coordinates']) - 1):
        for j in range(0, len(line2['coordinates']) - 1):
            a1_x = line1['coordinates'][i][1]
            a1_y = line1['coordinates'][i][0]
            a2_x = line1['coordinates'][i + 1][1]
            a2_y = line1['coordinates'][i + 1][0]
            b1_x = line2['coordinates'][j][1]
            b1_y = line2['coordinates'][j][0]
            b2_x = line2['coordinates'][j + 1][1]
            b2_y = line2['coordinates'][j + 1][0]
            ua_t = (b2_x - b1_x) * (a1_y - b1_y) - (b2_y - b1_y) * (a1_x - b1_x)
            ub_t = (a2_x - a1_x) * (a1_y - b1_y) - (a2_y - a1_y) * (a1_x - b1_x)
            u_b = (b2_y - b1_y) * (a2_x - a1_x) - (b2_x - b1_x) * (a2_y - a1_y)
            if not u_b == 0:
                u_a = ua_t / u_b
                u_b = ub_t / u_b
                if 0 <= u_a and u_a <= 1 and 0 <= u_b and u_b <= 1:
                    intersects.append({'type': 'Point', 'coordinates': [a1_x + u_a * (a2_x - a1_x), a1_y + u_a * (a2_y - a1_y)]})
    # if len(intersects) == 0:
    #     intersects = False
    return intersects


def bbox_around_polycoords(coords):
    """
    bounding box
    """
    x_all = []
    y_all = []

    for first in coords[0]:
        x_all.append(first[1])
        y_all.append(first[0])

    return [min(x_all), min(y_all), max(x_all), max(y_all)]


def point_in_bbox(point, bounds):
    """
    valid whether the point is inside the bounding box
    """
    return not(point['coordinates'][1] < bounds[0] or point['coordinates'][1] > bounds[2] \
     or point['coordinates'][0] < bounds[1] or point['coordinates'][0] > bounds[3])


def pnpoly(x, y, coords):
    """
    the algorithm to judge whether the point is located in polygon
    reference: https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html#Explanation
    """
    vert = [[0, 0]]

    for coord in coords:
        for node in coord:
            vert.append(node)
        vert.append(coord[0])
        vert.append([0, 0])

    inside = False

    i = 0
    j = len(vert) - 1

    while i < len(vert):
        if ((vert[i][0] > y) != (vert[j][0] > y)) and (x < (vert[j][1] - vert[i][1]) \
         * (y - vert[i][0]) / (vert[j][0] - vert[i][0]) + vert[i][1]):
            inside = not inside
        j = i
        i += 1

    return inside


def _point_in_polygon(point, coords):
    inside_box = False
    for coord in coords:
        if inside_box:
            break
        if point_in_bbox(point, bbox_around_polycoords(coord)):
            inside_box = True
    if not inside_box:
        return False

    inside_poly = False
    for coord in coords:
        if inside_poly:
            break
        if pnpoly(point['coordinates'][1], point['coordinates'][0], coord):
            inside_poly = True
    return inside_poly

def point_in_polygon(point, poly):
    """
    valid whether the point is located in a polygon

    Keyword arguments:
    point -- point geojson object
    poly  -- polygon geojson object

    if(point inside poly) return true else false
    """
    coords = [poly['coordinates']] if poly['type'] == 'Polygon' else poly['coordinates']
    return _point_in_polygon(point, coords)

def point_in_multipolygon(point, multipoly):
    """
    valid whether the point is located in a mulitpolygon (donut polygon is not supported)

    Keyword arguments:
    point      -- point geojson object
    multipoly  -- multipolygon geojson object

    if(point inside multipoly) return true else false
    """
    coords_array = [multipoly['coordinates']] if multipoly['type'] == "MultiPolygon" else multipoly['coordinates']

    for coords in coords_array:
        if _point_in_polygon(point, coords):
            return True

    return False

def number2radius(number):
    """
    convert degree into radius
    """
    return number*math.pi /180

def number2degree(number):
    """
    convert radius into degree
    """
    return number* 180/math.pi


def test():
    """
    test for geojson-python-utils
    """
    #linestrings intersect
    diagonal_up_str = '{ "type": "LineString","coordinates": [[0, 0], [10, 10]]}'
    diagonal_down_str = '{ "type": "LineString","coordinates": [[10, 0], [0, 10]]}'
    far_away_str = '{ "type": "LineString","coordinates": [[100, 100], [110, 110]]}'
    diagonal_up = json.loads(diagonal_up_str)
    diagonal_down = json.loads(diagonal_down_str)
    far_away = json.loads(far_away_str)
    print "intersect:%s" % ('True' if linestrings_intersect(diagonal_up, diagonal_down) else 'False')
    print "not intersect:%s" % ('True' if linestrings_intersect(diagonal_up, far_away) else 'False')

    #point in polygon
    in_str = '{"type": "Point", "coordinates": [5, 5]}'
    out_str = '{"type": "Point", "coordinates": [15, 15]}'
    box_str = '{"type": "Polygon","coordinates": [[ [0, 0], [10, 0], [10, 10], [0, 10] ]]}'
    in_box = json.loads(in_str)
    out_box = json.loads(out_str)
    box = json.loads(box_str)
    print "inbox : %s " % ('True' if point_in_polygon(in_box, box) else 'False')
    print "outbox : %s" % ('True' if point_in_polygon(out_box, box) else 'False')

    #point in multipolygon
    point_str = '{"type": "Point", "coordinates": [0.5, 0.5]}'
    single_point_str = '{"type": "Point", "coordinates": [-1, -1]}'
    multipoly_str = '{"type":"MultiPolygon","coordinates":[[[[0,0],[0,10],[10,10],[10,0],[0,0]]],[[[10,10],[10,20],[20,20],[20,10],[10,10]]]]}'
    point = json.loads(point_str)
    single_point = json.loads(single_point_str)
    multipoly = json.loads(multipoly_str)
    print "multi_in : %s " % ('True' if point_in_multipolygon(point, multipoly) else 'False')
    print "multi_out : %s" % ('True' if point_in_multipolygon(single_point, multipoly) else 'False')


if __name__ == '__main__':
    test()
{'fraction_non_alphanumeric': 0.15392764015645372, 'fraction_numerical': 0.031616688396349416, 'mean_word_length': 2.7474809160305345, 'pattern_counts': {'":': 36, '<': 19, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '2683174', 'n_tokens_mistral': 4599, 'n_tokens_neox': 4386, 'n_words': 1076}