2010-02-08 80 views
24

編輯光柵化GDAL層

這裏是做正確的方式,和documentation:我正在尋找的信息,如何

import random 
from osgeo import gdal, ogr  

RASTERIZE_COLOR_FIELD = "__color__" 

def rasterize(pixel_size=25) 
    # Open the data source 
    orig_data_source = ogr.Open("test.shp") 
    # Make a copy of the layer's data source because we'll need to 
    # modify its attributes table 
    source_ds = ogr.GetDriverByName("Memory").CopyDataSource(
      orig_data_source, "") 
    source_layer = source_ds.GetLayer(0) 
    source_srs = source_layer.GetSpatialRef() 
    x_min, x_max, y_min, y_max = source_layer.GetExtent() 
    # Create a field in the source layer to hold the features colors 
    field_def = ogr.FieldDefn(RASTERIZE_COLOR_FIELD, ogr.OFTReal) 
    source_layer.CreateField(field_def) 
    source_layer_def = source_layer.GetLayerDefn() 
    field_index = source_layer_def.GetFieldIndex(RASTERIZE_COLOR_FIELD) 
    # Generate random values for the color field (it's here that the value 
    # of the attribute should be used, but you get the idea) 
    for feature in source_layer: 
     feature.SetField(field_index, random.randint(0, 255)) 
     source_layer.SetFeature(feature) 
    # Create the destination data source 
    x_res = int((x_max - x_min)/pixel_size) 
    y_res = int((y_max - y_min)/pixel_size) 
    target_ds = gdal.GetDriverByName('GTiff').Create('test.tif', x_res, 
      y_res, 3, gdal.GDT_Byte) 
    target_ds.SetGeoTransform((
      x_min, pixel_size, 0, 
      y_max, 0, -pixel_size, 
     )) 
    if source_srs: 
     # Make the target raster have the same projection as the source 
     target_ds.SetProjection(source_srs.ExportToWkt()) 
    else: 
     # Source has no projection (needs GDAL >= 1.7.0 to work) 
     target_ds.SetProjection('LOCAL_CS["arbitrary"]') 
    # Rasterize 
    err = gdal.RasterizeLayer(target_ds, (3, 2, 1), source_layer, 
      burn_values=(0, 0, 0), 
      options=["ATTRIBUTE=%s" % RASTERIZE_COLOR_FIELD]) 
    if err != 0: 
     raise Exception("error rasterizing layer: %s" % err) 

原來的問題

使用osgeo.gdal.RasterizeLayer()(文檔字符串非常簡潔,我無法在C或C++ API文檔中找到它,我只找到java bindings的文檔)。

我適應一個unit test,並試圖在由多邊形的.SHP:

import os 
import sys 
from osgeo import gdal, gdalconst, ogr, osr 

def rasterize(): 
    # Create a raster to rasterize into. 
    target_ds = gdal.GetDriverByName('GTiff').Create('test.tif', 1280, 1024, 3, 
      gdal.GDT_Byte) 
    # Create a layer to rasterize from. 
    cutline_ds = ogr.Open("data.shp") 
    # Run the algorithm. 
    err = gdal.RasterizeLayer(target_ds, [3,2,1], cutline_ds.GetLayer(0), 
      burn_values=[200,220,240]) 
    if err != 0: 
     print("error:", err) 

if __name__ == '__main__': 
    rasterize() 

它運行良好,但我得到一個黑色的.tif。

什麼是burn_values參數?可以使用RasterizeLayer()來根據屬性值對具有不同顏色功能的圖層進行柵格化?

如果不行,我該用什麼?是否AGG適合渲染地理數據(我想要抗鋸齒和一個非常強大的渲染器,能夠正確繪製非常大和非常小的特徵,可能來自「髒數據」(退化多邊形等),有時指定在大座標中)?

比如我想從這個去:
http://i.imagehost.org/0232/from.png http://i.imagehost.org/0232/from.png

要這樣:
http://f.imagehost.org/0012/to_4.png http://f.imagehost.org/0012/to_4.png

這裏,多邊形由一個屬性值差異(顏色並不重要,我只想爲屬性的每個值有不同的值)。

+0

謝謝Luper,這對我今天非常有幫助! gdal的文檔很難找到正確的信息... – yosukesabai 2011-11-27 18:44:01

+0

嗨@Luper,太棒了!我正在尋找這個!考慮到我正確地指定了您的名字並鏈接到了這個問題,您是否允許將示例代碼中的(部分)代碼包含在GPLv3許可的開源項目中? – 2013-06-04 07:48:10

+0

@ andreas-h確定沒有問題。 – 2013-06-04 09:39:13

回答

8

編輯:我想我會用QGIS python綁定:http://www.qgis.org/wiki/Python_Bindings

這是我能想到的最簡單的方法。我記得以前用手滾動過東西,但它很醜。即使你必須做一個單獨的Windows安裝(爲了使Python能夠使用它),然後設置一個XML-RPC服務器以在一個單獨的python進程中運行它,qGIS會更容易。

我可以讓GDAL正常光柵化,這也很棒。

我沒有用GDAL了一段時間,但這裏是我的猜測:

burn_values是假的顏色,如果你不使用Z值。如果您使用burn=[1,2,3],burn_values=[255,0,0],則多邊形內的所有東西都是[255,0,0](紅色)。我不確定發生什麼事情 - 他們可能不會計劃。

如果您想使用Z值,請使用gdal.RasterizeLayer(ds,bands,layer,burn_values, options = ["BURN_VALUE_FROM=Z"])

我只是從你在看測試拉這樣的:http://svn.osgeo.org/gdal/trunk/autotest/alg/rasterize.py

另一種方法 - 拉多邊形對象出來,並利用身材勻稱,這可能不是吸引力吸引他們。或者查看geodjango(我認爲它使用openlayers來使用JavaScript繪製到瀏覽器中)。

另外,你是否需要光柵化?如果你真的想要精確的話,PDF導出可能會更好。

實際上,我想我發現使用Matplotlib(在提取和投影特徵之後)比光柵化更容易,並且我可以獲得更多的控制權。

編輯:

較低水平的做法是在這裏:

http://svn.osgeo.org/gdal/trunk/gdal/swig/python/samples/gdal2grd.py \

最後,你可以在多邊形迭代(將它們變成一個局部投影后),並直接繪製出來。但你最好不要有複雜的多邊形,否則你會有一點悲傷。如果你有複雜的多邊形......你可能最好使用http://trac.gispython.org/lab的勻稱和r-tree,如果你想要推出你自己的繪圖儀。

Geodjango可能是一個很好的地方問..他們會比我知道更多。他們有郵寄名單嗎?還有很多Python映射專家,但他們都沒有擔心這一點。我想他們只是用qGIS或GRASS或其他東西來繪製它。

說真的,我希望有人知道他們在做什麼可以回覆。

+0

是的,我需要柵格化(我編輯了問題以顯示 我想做的事)。也許有一個像「BURN_VALUE_FROM_Z」這樣的選項可以使用一個屬性? – 2010-02-08 11:43:27

+0

另外,我不明白爲什麼我在測試中會以黑色圖像結束。 – 2010-02-08 13:11:21

+1

我已經能夠在#gdal的人員的幫助下使用RasterizeLayer。問題在於轉換/範圍轉換,必須使源和目標擴展區匹配或刪除所有內容。這實際上是在文檔中解釋的,我不知道我是如何錯過它的:D 無論如何你的研究,我會接受你的答案,並用固定代碼編輯我的問題。 – 2010-02-09 03:15:50