查找 GeoTiff 图像中每个像素的纬度/经度坐标

2024-01-10

我目前有一个来自 GeoTiff 文件的 171 x 171 图像(尽管在其他情况下,我可能有更大的图像)。我的目标是获取图像中的每个像素并将其转换为纬度/经度对。

我已经能够根据此 StackOverflow 帖子将图像的角点转换为纬度/经度对:从 GeoTIFF 文件获取纬度和经度 https://stackoverflow.com/questions/2922532/obtain-latitude-and-longitude-from-a-geotiff-file。这篇文章很有帮助,因为我的原始坐标位于 UTM Zone 15。

但是,我现在想将图像的所有像素转换为纬度、经度对,并将结果存储在相同维度的 numpy 数组中。因此输出将是一个 171 x 171 x 2 的 numpy 数组,其中 numpy 数组的每个元素都是(经度,纬度)对的元组。

我见过的最相关的帖子是https://scriptndebug.wordpress.com/2014/11/24/latitudelongitude-of-each-pixel-using-python-and-gdal/ https://scriptndebug.wordpress.com/2014/11/24/latitudelongitude-of-each-pixel-using-python-and-gdal/。然而,该帖子建议本质上在每个像素上创建一个 for 循环并转换为纬度、经度。有没有更高效的方法呢?

为了提供有关我的实际用例的更多背景信息,我的最终目标是拥有一堆卫星图像(例如在本例中,每个图像都是 171 x 171)。我正在尝试创建一个建筑分割模型。现在,我尝试通过在每个图像上创建一个掩码来生成标记数据点,该掩码将像素标记为 1(如果对应于建筑物),否则标记为 0。 首先,我使用 Microsoft 美国建筑足迹数据:https://github.com/microsoft/USBuildingFootprints https://github.com/microsoft/USBuildingFootprints他们发布了检测到的建筑物的多边形(由纬度、经度定义)的 GeoJSON 文件。我考虑这样做的方式是:

  1. 查找图像中每个像素的纬度和经度。因此,我将拥有 171 x 171 积分。将其放入 GeoSeries 中
  2. 将点(在 GeoSeries 中)与 Microsoft 美国建筑足迹数据相交(使用 GeoPandas 相交:https://geopandas.org/reference.html#geopandas.GeoSeries.intersects https://geopandas.org/reference.html#geopandas.GeoSeries.intersects)
  3. 如果该点与 Microsoft 美国建筑足迹数据中的任何多边形相交,则标记为 1,否则标记为 0。

现在我正在进行步骤(1),即有效地找到图像中每个像素的纬度/经度坐标。


不幸的是,我还找不到比循环所有像素更好的解决方案。到目前为止,这是我的解决方案:

import glob
import os
import pickle
import sys

import gdal
import geopandas as gpd
import matplotlib
import matplotlib.pyplot as plt
from numba import jit
import numpy as np
from osgeo import osr
import PIL
from PIL import Image, TiffImagePlugin
from shapely.geometry import Point, Polygon, box
import torch


def pixel2coord(img_path, x, y):
    """
    Returns latitude/longitude coordinates from pixel x, y coords

    Keyword Args:
      img_path: Text, path to tif image
      x: Pixel x coordinates. For example, if numpy array, this is the column index
      y: Pixel y coordinates. For example, if numpy array, this is the row index
    """
    # Open tif file
    ds = gdal.Open(img_path)

    old_cs = osr.SpatialReference()
    old_cs.ImportFromWkt(ds.GetProjectionRef())

    # create the new coordinate system
    # In this case, we'll use WGS 84
    # This is necessary becuase Planet Imagery is default in UTM (Zone 15). So we want to convert to latitude/longitude
    wgs84_wkt = """
    GEOGCS["WGS 84",
        DATUM["WGS_1984",
            SPHEROID["WGS 84",6378137,298.257223563,
                AUTHORITY["EPSG","7030"]],
            AUTHORITY["EPSG","6326"]],
        PRIMEM["Greenwich",0,
            AUTHORITY["EPSG","8901"]],
        UNIT["degree",0.01745329251994328,
            AUTHORITY["EPSG","9122"]],
        AUTHORITY["EPSG","4326"]]"""
    new_cs = osr.SpatialReference()
    new_cs.ImportFromWkt(wgs84_wkt)

    # create a transform object to convert between coordinate systems
    transform = osr.CoordinateTransformation(old_cs,new_cs) 
    
    gt = ds.GetGeoTransform()

    # GDAL affine transform parameters, According to gdal documentation xoff/yoff are image left corner, a/e are pixel wight/height and b/d is rotation and is zero if image is north up. 
    xoff, a, b, yoff, d, e = gt

    xp = a * x + b * y + xoff
    yp = d * x + e * y + yoff

    lat_lon = transform.TransformPoint(xp, yp) 

    xp = lat_lon[0]
    yp = lat_lon[1]
    
    return (xp, yp)


def find_img_coordinates(img_array, image_filename):
    img_coordinates = np.zeros((img_array.shape[0], img_array.shape[1], 2)).tolist()
    for row in range(0, img_array.shape[0]):
        for col in range(0, img_array.shape[1]): 
            img_coordinates[row][col] = Point(pixel2coord(img_path=image_filename, x=col, y=row))
    return img_coordinates


def find_image_pixel_lat_lon_coord(image_filenames, output_filename):
    """
    Find latitude, longitude coordinates for each pixel in the image

    Keyword Args:
      image_filenames: A list of paths to tif images
      output_filename: A string specifying the output filename of a pickle file to store results

    Returns image_coordinates_dict whose keys are filenames and values are an array of the same shape as the image with each element being the latitude/longitude coordinates.
    """
    image_coordinates_dict = {}
    for image_filename in image_filenames:
        print('Processing {}'.format(image_filename))
        img = Image.open(image_filename)
        img_array = np.array(img)
        img_coordinates = find_img_coordinates(img_array=img_array, image_filename=image_filename)
        image_coordinates_dict[image_filename] = img_coordinates
        with open(os.path.join(DATA_DIR, 'interim', output_filename + '.pkl'), 'wb') as f:
            pickle.dump(image_coordinates_dict, f)
    return image_coordinates_dict

这些是我的辅助功能。因为这需要很长时间find_image_pixel_lat_lon_coord我将结果保存到字典中image_coordinates_dict我将其写入 pickle 文件以保存结果。

那么我使用它的方式是:

# Create a list with all tif imagery
image_filenames = glob.glob(os.path.join(image_path_dir, '*.tif'))

image_coordinates_dict = find_image_pixel_lat_lon_coord(image_filenames, output_filename='image_coordinates')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查找 GeoTiff 图像中每个像素的纬度/经度坐标 的相关文章

随机推荐

  • 任务管理器、ProcessExplorer 或类似工具:监视和管理 CLR 线程

    有没有一种工具可以查看托管线程在 CLR 中运行的情况 理想情况下 我希望看到 CPU 负载 状态 托管名称和托管 id 即使该线程属于线程池 或者是后台线程 它将能够对线程池 前台线程和后台线程进行分组 折叠 动机 我正在使用 CLR P
  • 未生成 iOS 的 Xcode 6.1 静态库 .a

    我尝试使用 Xcode 6 1 为我的 iOS 设备制作静态库 我在 Xcode 上选择一个带有模板 Cocoa Touch Static Library 的新项目并将其命名为 MyLib 对于 MyLib 目标 我在模拟器中选择 iPho
  • 在 kotlin js 中嵌入资源

    在 kotlin jvm 中 或者在 java 中 不管怎样 我们可以通过资源输入流读取资源内容 有没有办法在 kotlin js 中做到这一点 现在我正在通过 ajax 调用请求资源 但最好将资源自动嵌入到已编译的 javascript
  • 从 JSON 文件导入 Google 应用脚本项目

    在 Google Drive 中 可以将应用程序脚本项目下载为 json file 当此类文件导入回 Google 云端硬盘时 它与 Google 脚本编辑器应用程序没有正确关联 有什么办法可以正确地做到这一点吗 导入和导出 Apps 脚本
  • iphone如何指定Class数据类型必须采用某种协议

    在我的应用程序中 我需要返回 Class 作为返回类型 例如 应用 m Class getParserClass return NCCurrencyParser class NCCurrencyParser m interface NCCu
  • 查找文本中出现的大量短语

    我正在构建一个后端并尝试解决以下问题 客户端向后端提交文本 大约2000平均字符数 接收请求的后端端点必须对提交的文本应用短语突出显示 周围有80k要匹配的短语 短语是一个简单的对象 phrase phrase to match link
  • 如何确定 Colliderect 中对象相互穿过的原因

    由于某种原因 Colliderect 无法工作 雨水会穿过人行道 这真的很烦人 因为所有这些未使用的精灵都会产生大量的延迟 import pygame import random class Square pygame sprite Spr
  • 使用 jQuery 从一组选择菜单中删除和添加选项

    这比标题所描述的要复杂一些 但以下是基本的业务规则 上面有三个选择菜单 页面 每个页面都填充相同的内容 选项和值 总会有三个选择 菜单 总会有相同的数字 每个选择中的选项 值 菜单 在任一问题中选择一个问题 菜单将删除该问题作为选项 另外两
  • 使用“car”跨列范围重新编码

    我在网上查了一下 不知道如何申请car重新编码一系列列的值 要重新编码单个列的值 我将运行以下命令 df dv r lt recode df dv 2 1 1 0 0 NA 然后 如果我想对整个 data frame 执行此操作 我可以运行
  • 选项[selected=true] 不起作用

    我有这个命令 visibleSelect 是保存多个选择列表的 jquery 变量 var selectedOption visibleSelect find option selected true 从观察窗我可以看到selectedOp
  • 如何在 Xcode 7.0 beta 2 中运行 iOS 7.1 模拟器?

    我已经安装了最新的 Xcode 7 beta 2 版本 当我尝试在 iOS 7 1 模拟器中运行该应用程序时 它给出了以下错误消息 iOS 7 1 模拟器运行时不可用 无法打开 liblaunch sim dylib 尝试重新安装 Xcod
  • 如何从 gi.repository 导入 gtk.gdk

    我有这个 python 代码 可以截取 x 屏幕的屏幕截图 usr bin python import gtk gdk w gtk gdk get default root window sz w get size print The si
  • 在 Ruby on Rails 中处理国际货币输入

    I have 一个应用程序 http yourdough com处理货币输入 但是 如果您在美国 则可以输入一个数字 12 345 67 在法国 可能是12 345 67 在 Rails 中 是否有一种简单的方法可以使货币输入适应区域设置
  • 将 PictureBox 内容发送到 MsPaint

    如何发送要在 Paint 中编辑的图片框的内容 我想过快速暂时保存它 然后发送要加载的临时地址 但我认为这会导致一些小的保存问题 不幸的是我现在用 C 提供答案 幸运的是 只是语法而不是内容需要改变 假设这是您的图片框控件 获取内容 作为位
  • 为什么 Firefox 不显示我的 SVG 图标,该怎么办?

    Context我正在创建一个仅使用 HTML CSS 和 JS 的静态网站 用于学习目的 我成功地实现了两个主题 为了改变它 我添加了一个SVG图标在一个button元素 然后 svg 根据主题 月亮或太阳 而变化 Problem虽然一切在
  • 在 case 内使用 if、else if、else 和循环进行切换

    出于我的问题的目的 我只包括案例 1 但其他情况是相同的 假设 value 当前为 1 我们转到情况 1 for 循环遍历数组以查看每个元素是否与whatever value 变量匹配 在这种情况下 如果确实如此 我们将 value 变量声
  • plupload 在 IE 9 中似乎无法上传文件。在其他浏览器中可以使用

    在我们的项目中 我们使用 plupload 上传单个 Excel 文件 这适用于除 IE9 之外的所有浏览器 单击上传链接时 会显示文件对话框 但尝试打开 Excel 时没有任何反应 以下是供参考的代码 任何解决此问题的帮助将不胜感激 提前
  • 人们如何使用 Entity Framework 6 进行单元测试,您应该担心吗?

    我刚刚开始进行单元测试和 TDD 我以前涉足过 但现在我决心将其添加到我的工作流程中并编写更好的软件 我昨天问了一个问题 其中包括这一点 但这似乎是一个独立的问题 我坐下来开始实现一个服务类 我将使用该服务类从控制器中抽象出业务逻辑 并使用
  • 获取当月日历中的所有日期

    如何获取当前 某个月份日历中的所有日期 例如本月 如图所示 所以结果是 07 31 2016 08 01 2016 08 02 2016 08 31 2016 09 01 2016 09 02 2016 09 03 2016 有什么想法吗
  • 查找 GeoTiff 图像中每个像素的纬度/经度坐标

    我目前有一个来自 GeoTiff 文件的 171 x 171 图像 尽管在其他情况下 我可能有更大的图像 我的目标是获取图像中的每个像素并将其转换为纬度 经度对 我已经能够根据此 StackOverflow 帖子将图像的角点转换为纬度 经度