如何使用 python 抓取地理 TIFF 图像 [英] How to grab a geo TIFF image with python
问题描述
今天我遇到了麻烦,因为我第一次必须处理 TIFF 文件,并且出现错误.我正在尝试获取具有欧洲污染因子值的栅格,因此我对保持图像的高分辨率水平并不感兴趣,但只是为了保留数据,我可以操纵图像.我的代码很简单:
Today I'm in trouble because for the first time I have to work with TIFF files, and I have an error. I'm trying to grab a raster with the values of pollution agents in Europe, so I'm not interesting to keep an high level of resolution of the image, but only to keep data, I can manipulate the image. My code is very simple:
from __future__ import print_function
import numpy
import urllib2
from PIL import Image
f = open('./maccrasters/prova.tif','w')
fullMap = urllib2.urlopen("http://wdc.dlr.de/wdcservices/wcs.php?COVERAGE=17e72d93-76d9-4af6-9899-b7b04e2763c8&service=wcs&version=1.0.0&crs=epsg:4326&bbox=-25,30,45,70&RESX=0.1&RESY=0.1&request=getcoverage&format=application/x-tiff-32f&TIME=2015-12-13T00&elevation=0&OUTPUTFILENAME=17e72d93-76d9-4af6-9899-b7b04e2763c8_2015-12-13T00_0")
im = Image.open(fullMap)
print(im.format, im.size, im.mode) # output is TIFF (700,400) F
im.show()
im.save(f, "TIFF")
系统向我返回一个输出,但我找不到该错误的解决方案:
The system returns me an output and I can't find a solution for that error:
_TIFFVSetField: ./maccrasters/prova.tif: Invalid tag "TileOffsets" (not supported by codec).
Traceback (most recent call last):
File "getrasters.py", line 20 in <module>
im.save(f, "TIFF")
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1665, in save
save_handler(self, fp, filename)
File "C:\Python27\lib\site-packages\PIL\TiffImagePlugin.py", line 1307, in _save
e = Image._getencoder(im.mode, 'libtiff', a, im.encoderconfig)
File "C:\Python27\lib\site-packages\PIL\Image.py",line 430, in _getencoder
return encoder(mode, *args + extra)
RuntimeError: Error setting from dictionary
有人可以帮助我吗?
推荐答案
geoTiff
您的文件不是常规的 tiff 文件,而是需要特殊库的 geoTiff 文件.
geoTiff
Your file is not a regular tiff-file, it's a geoTiff file which needs a special library.
对于python,有georasters
库来读取这些文件.然后您可以使用 matplotlib
显示它们.
For python there is the georasters
library to read those files. You can then show them with matplotlib
.
在我看来,使用 requests
有一个比 urllib 更好的界面:
Using requests
has a way nicer interface than urllib in my opinion:
import requests
from PIL import Image
import georasters as gr
import matplotlib.pyplot as plt
url = 'http://wdc.dlr.de/wdcservices/wcs.php'
query = {
'COVERAGE': '17e72d93-76d9-4af6-9899-b7b04e2763c8',
'service': 'wcs',
'version': '1.0.0',
'crs': 'epsg:4326',
'bbox': '-25,30,45,70',
'RESX': '0.1',
'RESY': '0.1',
'request': 'getcoverage',
'format': 'application/x-tiff-32f',
'TIME': '2015-12-13T00',
'elevation': '0',
'OUTPUTFILENAME': '17e72d93-76d9-4af6-9899-b7b04e2763c8_2015-12-13T00_0'
}
with open('test.tiff', 'wb') as f:
ret = requests.get(url, stream=True, params=query)
for data in ret.iter_content(1024):
f.write(data)
data = gr.from_file('test.tiff')
plt.imshow(data.raster, cmap='gray')
plt.show()
结果:
这篇关于如何使用 python 抓取地理 TIFF 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!