确定地理位置是否在Python中的给定多边形内,而不使用数据文件?

问题描述

我目前正以以下方式使用Rasterio来确定my_point所定义的多边形中是否包含geometries(显然,在此示例中给出了特定的数字(epsg:27700坐标)):

geometries = [{'type': 'polygon','coordinates': [[[290000,92000],[291000,93000],[290000,93000]]]}]
my_point = (290500,92500)

with Rasterio.open("myfile.tif") as src:
    out_image,out_transform = mask(src,geometries,crop=True,all_touched=False,filled=False)
    out_Meta = src.Meta.copy()

    out_Meta.update({"driver": "GTiff","height": out_image.shape[1],"width": out_image.shape[2],"transform": out_transform})
    with MemoryFile() as memfile:
        with memfile.open(**out_Meta) as dataset:
            dataset.write(out_image)
        memfile.seek(0)
        buffer = memfile.read()
    with Rasterio.open(BytesIO(buffer)) as subset:
        row,col = subset.index(my_point[0],my_point[1])
    if not out_image.mask[0,row,col]:
        print("inside polygon")
    else:
        print("outside polygon")

但是,由于我只在乎该点是在内部还是外部,而不是源数据中的实际像素值,因此似乎不必使用数据文件来实现此目的。换句话说,我关心的是out_image.mask,而不是out_image数据。是否有更好的方法可以在Python中实现此目标,而不必使用源数据文件,并且最好使用相同或更少的代码行?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)