Đây là cách thích hợp để làm điều đó, và 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)
gốc câu hỏi
Tôi đang tìm thông tin về cách để sử dụng osgeo.gdal.RasterizeLayer()
(chuỗi tài liệu rất ngắn gọn và tôi không thể tìm thấy nó trong tài liệu API C hoặc C++. Tôi chỉ tìm thấy tài liệu cho số java bindings).
tôi chuyển thể một unit test và thử nó trên một .shp làm bằng đa giác:
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()
Nó chạy tốt, nhưng tất cả tôi có được là một .tif đen.
Thông số burn_values
là gì? Có thể sử dụng RasterizeLayer()
để rasterize một lớp với các đối tượng có màu khác nhau dựa trên giá trị của thuộc tính không?
Nếu không, tôi nên sử dụng thông tin gì? Là AGG thích hợp để hiển thị dữ liệu địa lý (tôi muốn không khử răng cưa và trình kết xuất đồ họa mạnh mẽ, có thể vẽ chính xác các tính năng rất lớn và nhỏ, có thể từ "dữ liệu bẩn" (đa giác thoái hóa, v.v ...) và đôi khi được chỉ định ở tọa độ lớn)?
Ví dụ tôi muốn đi từ này:
http://i.imagehost.org/0232/from.png http://i.imagehost.org/0232/from.png
Để này:
http://f.imagehost.org/0012/to_4.png http://f.imagehost.org/0012/to_4.png
Ở đây, đa giác được phân biệt bởi giá trị của một thuộc tính (màu sắc không quan trọng, Tôi chỉ muốn có một cái khác nhau cho mỗi giá trị của thuộc tính).
Cảm ơn Luper, điều này rất hữu ích cho tôi hôm nay! tài liệu của gdal rất khó tìm được thông tin phù hợp ... – yosukesabai
Xin chào @Luper, tuyệt vời! Tôi đã tìm kiếm chính xác điều này! Bạn có cho phép bao gồm (các phần) mã ví dụ của bạn trong dự án nguồn mở được cấp phép GPLv3 không, vì tôi đã gán đúng tên của bạn và liên kết đến câu hỏi này? –
@ andreas-h chắc chắn không có vấn đề gì. –