已知乙個GeoTiff,得到其邊界向量的方法

2021-08-11 09:56:34 字數 2019 閱讀 7343

在製作geotiff的樣本時,經常需要知道它的邊界向量,原因如下:

沒有原始的裁切向量;

裁切它的向量柵格化後與影像相差乙個畫素;

**摘要:

製作乙個與原影像同座標系但是值全部為0的中間影像a;

將a向量化

不足:沒有座標系,需要手動新增

附**

import gdal

import ogr

import sys

import cv2

import numpy as np

import os

import osr

def main(argv):

if len(argv) < 2:

print('[target image] [shpfile name]')

sys.exit(0)

# this allows gdal to throw python exceptions

gdal.useexceptions()

# get raster datasource

src_ds = gdal.open(argv[1])

if src_ds is none:

print('unable to open %s' % src_filename)

sys.exit(1)

# create a temp tif file to simplify polygonize

xsize = src_ds.rasterxsize

ysize = src_ds.rasterysize

arr = np.zeros([ysize, xsize])

# in case of 'tif' or 'tiff'

if argv[1][-5] == '.':

temp_tif_fn = argv[1][0:-5] + '_temp.tif'

else:

temp_tif_fn = argv[1][0:-4] + '_temp.tif'

cv2.imwrite(temp_tif_fn, arr)

# add geo info

geotrans = src_ds.getgeotransform()

srcpro = src_ds.getprojection()

target_ds = gdal.open(temp_tif_fn,gdal.ga_update)

target_ds.setgeotransform(geotrans)

target_ds.setprojection(srcpro)

target_ds.flushcache()

target_ds = none

src_ds = gdal.open(temp_tif_fn)

srcband = src_ds.getrasterband(1)

# create output datasource

# output datasource file name

if argv[1][-5] == '.':

dst_layername = argv[1][0:-5]

else:

dst_layername = argv[1][0:-4]

drv = ogr.getdriverbyname("esri shapefile")

dst_ds = drv.createdatasource(dst_layername + ".shp")

dst_layer = dst_ds.createlayer(dst_layername, srs = none )

gdal.polygonize( srcband, none, dst_layer, -1, , callback=none )

# todo: add geo info to shpfile

del src_ds

os.remove(temp_tif_fn)

if __name__ == "__main__":

main(sys.argv)

刪除乙個已知節點

delete a node in a list input plisthead the head of list ptobedeleted the node to be deleted void deletenode listnode plisthead,listnode ptobedeleted ...

已知乙個datetime,篩選其前七天的資料

一開始想的很簡單,直接用timedelta求7天的時間差,然後原始資料減去時間差,然後只需大於data pre seven,小於a即可,如下 a pd.to datetime 20120701 11 22 33 sevenday datetime.timedelta days 7 date pre ...

已知n是乙個整數,

已知n是乙個整數,1 n 100 請完成函式judge,要求當n是奇數時列印odd,當n為偶數且在閉區間 2,5 之間時列印 the number is between 2 and 5 當n為偶數且在比區間 6,20 之間時列印 the number is between 6 and 20 當n為偶...