make_wkt.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:kaggle-satellite-imagery-feature-detection 作者: toshi-k 项目源码 文件源码
def img_to_wkt(img, scale_rate, target_name, W, H, Xmax, Ymin, t_value, cla):

    W_dash = W * W / (W + 1)
    H_dash = H * H / (H + 1)

    if scale_rate < 0.99:
        img_tiny = rescale(img, scale_rate)
    else:
        img_tiny = img

    bmp_image_path = '_temp/' + target_name + '.bmp'
    target_json_path = '_temp/' + target_name + '.json'

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        imsave(bmp_image_path, img_tiny)

    os.system('potrace -a 2 -t ' + str(t_value) + ' -b geojson -i ' + bmp_image_path + ' -o ' + target_json_path)

    f = open(target_json_path)
    data = json.load(f)
    f.close()

    os.remove(target_json_path)
    os.remove(bmp_image_path)

    # type of 'data' is feature collection 
    # we only need focus on features
    features = data['features']

    list_polygons = list()

    for i in range(len(features)):

        shapely_polygon = shape(geojson.loads(json.dumps(features[i]['geometry'])))

        if scale_rate < 0.99:
            shapely_polygon = scale(shapely_polygon, 1/scale_rate, 1/scale_rate, origin=(0, 0))

        list_polygons.append(shapely_polygon.buffer(0.0))

    multi = MultiPolygon(list_polygons)

    multi = scale(multi, 1, -1, 1, origin=(float(W)/2, float(H)/2))
    multi = scale(multi, Xmax / W_dash, Ymin / H_dash, origin=(0, 0))

    if cla != 6:
        multi = multi.simplify(1e-6, preserve_topology=True)
    else:
        multi = multi.simplify(1e-5, preserve_topology=True)

    multi = multi.buffer(0)

    if multi.type == 'Polygon':
        multi = MultiPolygon([multi])

    return multi

# tpex's evaluation code can validate topology more strictly
# https://github.com/cxz/tpex
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号