Tôi đã thử sử dụng mã từ QGis Save Raster dưới dạng hình ảnh kết xuất . ilà một lớp raster:
pipelayer = i
pipeextent = pipelayer.extent()
pipewidth, pipeheight = (pipelayer.width(),
pipelayer.height())
piperenderer = pipelayer.renderer()
pipeprovider = pipelayer.dataProvider()
crs = pipelayer.crs().toWkt()
pipe = QgsRasterPipe()
pipe.set(pipeprovider.clone())
pipe.set(piperenderer.clone())
pipedFile = os.path.join(tempfile.gettempdir(),
safeLayerName + '_pipe.tif')
print pipedFile
file_writer = QgsRasterFileWriter(pipedFile)
file_writer.writeRaster(pipe,
pipewidth,
pipeheight,
pipeextent,
pipelayer.crs())
in_raster = pipedFile
prov_raster = os.path.join(tempfile.gettempdir(),
'json_' + safeLayerName +
'_prov.tif')
out_raster = dataPath + '.png'
crsSrc = i.crs()
crsDest = QgsCoordinateReferenceSystem(4326)
xform = QgsCoordinateTransform(crsSrc, crsDest)
extentRep = xform.transform(i.extent())
extentRepNew = ','.join([unicode(extentRep.xMinimum()),
unicode(extentRep.xMaximum()),
unicode(extentRep.yMinimum()),
unicode(extentRep.yMaximum())])
processing.runalg("gdalogr:warpreproject", in_raster,
i.crs().authid(), "EPSG:4326", "", 0, 1,
5, 2, 75, 6, 1, False, 0, False, "",
prov_raster)
processing.runalg("gdalogr:translate", prov_raster, 100,
True, "", 0, "", extentRepNew, False, 0,
0, 75, 6, 1, False, 0, False, "",
out_raster)
Nó không hoạt động, thay vào đó mang lại cho tôi raster chưa được xử lý như là đầu ra cuối cùng. Tôi thực sự không hiểu vấn đề, bởi vì đầu ra của writeRaster(tệp pipedFile) được tạo kiểu, vì vậy quá trình từ câu trả lời được liên kết đến ở trên đang hoạt động. Chỉ là khi tôi cố gắng sử dụng hình ảnh đầu ra để chạy qua GDAL warpreprojectvà translate, bằng cách nào đó nó trở lại với raster không được che chở.
Tôi đã làm gì sai?
CẬP NHẬT : Đầu ra của QgsRasterFileWriter được tạo kiểu. Kết quả của warpreprojectlà không theo kiểu, nhưng .tifđược đi kèm với một .aux.xmltập tin mà dường như có thông tin màu trên nó:
<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
<HistMin>-0.498046875</HistMin>
<HistMax>255.498046875</HistMax>
<BucketCount>256</BucketCount>
<IncludeOutOfRange>0</IncludeOutOfRange>
<Approximate>1</Approximate>
<HistCounts>314|4|3|2|3|5|4|4|5|8|10|9|16|16|23|26|37|46|58|62|69|77|77|94|127|94|131|136|133|162|169|172|171|184|200|186|207|196|186|177|179|194|182|182|178|177|181|195|196|196|195|195|179|226|206|215|193|197|202|212|207|206|232|204|229|253|233|240|248|234|234|268|240|238|259|286|268|287|259|264|255|267|268|271|256|321|277|284|317|286|290|277|312|334|319|325|323|310|349|357|347|326|337|338|336|383|374|349|411|382|382|417|406|405|414|427|404|434|447|430|468|424|446|442|450|461|448|458|457|459|412|414|469|466|443|475|464|472|481|519|504|458|473|481|514|523|522|494|542|580|604|623|686|648|707|763|808|799|819|853|932|1046|1062|1118|1145|1215|1293|1393|1335|1392|1390|1317|1232|1247|1215|1088|1032|896|847|731|708|666|608|634|556|500|484|456|433|408|406|401|386|388|414|414|387|382|340|346|382|385|368|348|334|376|359|318|339|326|357|315|319|357|333|328|329|310|344|295|300|351|347|324|358|349|363|361|347|396|386|433|395|434|479|473|523|550|644|655|672|778|882|1014|1183|1400|1681|2075|2531|3529|4851|7235|10793|60773|7297|1999</HistCounts>
</HistItem>
</Histograms>
<Metadata>
<MDI key="STATISTICS_MAXIMUM">255</MDI>
<MDI key="STATISTICS_MEAN">203.31482680479</MDI>
<MDI key="STATISTICS_MINIMUM">0</MDI>
<MDI key="STATISTICS_STDDEV">63.158702013579</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
Kết quả của translatelà không theo kiểu, và không có tập tin bổ sung. Tuy nhiên, vì đó là PNG không phải là GeoTIFF, có lẽ đó là lời giải thích.
Tôi đã thử xem xét expandtùy chọn translate, nhưng điều đó mang lại cho tôi các tệp hình ảnh không hiển thị trong trình duyệt web - Tôi chưa tìm hiểu lý do tại sao.
CẬP NHẬT 2 : Không, sử dụng expandkết quả trong không có tệp đầu ra nào cả. Tự hỏi về việc cố gắng pct2rgb.
CẬP NHẬT 3 : Tôi tự hỏi nếu khóa tập tin gây ra vấn đề. Nó có thể là?
gdalogr:translate, hãy thử thay thế prov_rasterbằng prov_raster['OUTPUT'].
prov_rasterchỉ là một chuỗi có đường dẫn đến tệp đó, vì vậy tôi nhận đượcTypeError: string indices must be integers