示例#1
0
def get_buffer(filename, lat, lon, radius):
    """
    Given a geojson file, latitude and longitude in 4326 projection,
    and a radius, write to file (as geojson) all the LineStrings and
    Points that overlap a circular buffer around the coordinates.
    Args:
        filename
        lat
        lon
        radius
    Returns:
        A list of overlapping geojson features 4326 projection
    """

    segments = read_geojson(filename)

    # Calculate the bounding circle
    overlapping = []
    point = get_reproject_point(lat, lon, transformer_4326_to_3857)
    buffered_poly = point.buffer(radius)
    for segment in segments:
        if segment.geometry.intersects(buffered_poly):

            if type(segment.geometry) == LineString:
                coords = [x for x in segment.geometry.coords]
                overlapping.append({
                    'geometry': {
                        'coordinates': coords,
                        'type': 'LineString'
                    },
                    'type': 'Feature',
                    'properties': segment.properties
                })
            elif type(segment.geometry) == Point:
                overlapping.append({
                    'geometry': {
                        'coordinates':
                        [segment.geometry.x, segment.geometry.y],
                        'type': 'Point'
                    },
                    'properties': segment.properties
                })
            elif type(segment.geometry) == MultiLineString:
                overlapping.append({
                    'geometry': {
                        'coordinates':
                        [[y for y in x.coords] for x in segment.geometry],
                        'type':
                        'MultiLineString'
                    },
                    'properties': segment.properties
                })
            else:
                print("{} not implemented yet, skipping...".format(
                    type(segment.geometry)))
    if overlapping:
        overlapping = prepare_geojson(overlapping)
    return overlapping
def test_get_buffer():
    results = make_map_subset.get_buffer(
        os.path.join(TEST_FP, 'data', 'test_make_map.geojson'), 42.3693239,
        -71.10103649999999, 20)
    assert len(results['features']) == 5
    lines = [
        x for x in results['features'] if x['geometry']['type'] == 'LineString'
    ]
    assert len(lines) == 4
    point = [
        x for x in results['features'] if x['geometry']['type'] == 'Point'
    ]
    assert len(point) == 1

    # Make sure that all the resulting features are at least partially
    # within the buffer
    center_point = get_reproject_point(42.3693239, -71.10103649999999,
                                       transformer_4326_to_3857)
    buff_poly = center_point.buffer(20)

    # To do this, have to convert the points and linestrings back to 3857
    reprojected_lines = reproject_records(lines)
    for r in reprojected_lines:
        assert r['geometry'].intersects(buff_poly)

    point_3857 = get_reproject_point(point[0]['geometry']['coordinates'][1],
                                     point[0]['geometry']['coordinates'][0],
                                     transformer_4326_to_3857)
    assert point_3857.within(buff_poly)

    results = make_map_subset.get_buffer(
        os.path.join(TEST_FP, 'data', 'test_make_map.geojson'), 42.3601,
        71.0589, 20)
    assert results == []

    # Test multilinestring
    results = make_map_subset.get_buffer(
        os.path.join(TEST_FP, 'data', 'make_map_multilinestring.geojson'),
        42.3693167036633, -71.1010048225989, 20)
    assert len(results['features']) == 1
示例#3
0
def get_waze_buffer(filename, outfile, lat, lon, radius):
    """
    Get waze elements that fall within a certain area
    Write them back out to a json file
    """
    items = json.load(open(filename))

    items = [get_linestring(x) for x in items]
    items = reproject_records(items)

    point = get_reproject_point(lat, lon)
    buffered_poly = point.buffer(radius)
    count = 0
    results = []
    for item in items:
        if item['geometry'].intersects(buffered_poly):
            count += 1
            results.append(item['properties'])
    print("{} results found".format(count))

    with open(outfile, 'w') as f:
        json.dump(results, f)