<<TableOfContents: execution failed [Argument "maxdepth" must be an integer value, not "[maxdepth]"] (see also the log)>>

## 用ogr和PIL把矢量数据转化成栅格图像

### 求索

PIL和GDAL可以互相共享数据，互相协调互相补充，这很好。现在再加上PIL对ogr的矢量绘图支持，真的是－－

－－“Perfect！”

(似乎这一篇的名称可以改成“江湖”，哈哈，说笑，小说看多了)

### 身手

```   1 Example: Draw a Grey Cross Over an Image
2 import Image, ImageDraw
3 im = Image.open("lena.pgm")
4 draw = ImageDraw.Draw(im)
5 draw.line((0, 0) + im.size, fill=128)
6 draw.line((0, im.size[1], im.size[0], 0), fill=128)
7 del draw
8 # write to stdout
9 im.save(sys.stdout, "PNG")
```

```   1 im.save("lena.png","PNG")
```

```   1 import Image, ImageDraw
2 import sys
3 import ogr
4 from Numeric import *
```

```   1 ds = ogr.Open("e:/gisdata/data/streets.shp")
2 layer = ds.GetLayer()
3 extent = layer.GetExtent()
4 geowidth = extent[1]-extent[0]
5 geoheight = extent[3]-extent[2]
6 width = 1000
7 height = 1000/geowidth*geoheight
8 scale = geowidth/width
```

```   1 # create a new png file
2 imn = Image.new("RGB",(width,height),(255,255,255))
3 imn.save("test.pgm","PNG")
4 del imn
```

Image.new并不能写入数据到硬盘上，所以要用imn.save写入，这里保存的是PNG的格式。

```   1 import Image, ImageDraw
2 import sys,math
3 import ogr
4 from Numeric import *
5 class PILImgDC:
6     def __init__(self,layer):
7         self.layer = layer
8         self.features = [layer.GetFeature(i) for i in range(layer.GetFeatureCount())]
9         FeatureDrawMap = {
10             #ogr.wkbPoint:self.__DrawPoint,
11             #ogr.wkbMultiPoint:self.__DrawPoints,
12             ogr.wkbLineString:self.__DrawLine,
13             ogr.wkbMultiLineString:self.__DrawLines,
14             #ogr.wkbPolygon:self.__DrawPolygon,
15             #ogr.wkbMultiPolygon:self.__DrawPolygons
16             }
17         self.featureDrawMap = FeatureDrawMap
18         self.fill = (128,128,128)
19     def SetFillColour(self,color):
20         self.fill = color
21     def SetInputImg(self,imgname):
22         self.iimgname = imgname
23     def DrawAll(self,width,ofname):
24         extent = layer.GetExtent()
25         geowidth = math.fabs(extent[1]-extent[0])
26         geoheight = math.fabs(extent[3]-extent[2])
27         self.minx = min(extent[0],extent[1])
28         self.maxy = max(extent[3],extent[2])
29         height = width/geowidth*geoheight
30         self.scale = width/geowidth
31         im = Image.new("RGB",(width,height),(255,255,255))
32         self.draw = ImageDraw.Draw(im)
33         for feature in self.features:
34             geomref = feature.GetGeometryRef()
35             geomreftype = geomref.GetGeometryType()
36             drawFun = self.featureDrawMap[geomreftype]
37             drawFun(geomref)
38         del self.draw
39         im.save(ofname, "PNG")
40     def __GetPoints(self,georef):
41         pointcount = georef.GetPointCount()
42         xs,ys = [],[]
43         for i in range(pointcount):
44             xs.append(georef.GetX(i))
45             ys.append(georef.GetY(i))
46         xs = array(xs,Float); ys = array(ys,Float)
47         xs = (xs-self.minx)*self.scale
48         ys = (self.maxy-ys)*self.scale
49         xs.shape=(-1,1);ys.shape=(-1,1)
50         points = concatenate((xs,ys),1)
51         return points
52     def __DrawLine(self,georef):
53         points = self.__GetPoints(georef)
54         xys = reshape(points, (1,-1))[0]
55         self.draw.line(xys,fill=self.fill)
56     def __DrawLines(self,georef):
57         sublinecount = georef.GetGeometryCount()
58         for i in range(sublinecount):
59             self.__DrawLine(georef.GetGeometryRef(i))
60 if __name__=="__main__":
61     ds = ogr.Open("e:/gisdata/shp/lines.shp")
62     layer = ds.GetLayer()
63     dc = PILImgDC(layer)
64     dc.SetFillColour((100,100,0))
65     dc.DrawAll(1000,"output.png")
```

### wx中的代码

```import wx
import random

def makeRandomLines(num, w, h):
pnts = []
for i in range(num):
x1 = random.randint(0, w)
y1 = random.randint(0, h)
x2 = random.randint(0, w)
y2 = random.randint(0, h)
pnts.append( (x1,y1, x2,y2) )
return pnts

app = wx.PySimpleApp()
bitmap = wx.EmptyBitmap(100,100)

dc = wx.MemoryDC();
dc.SelectObject(bitmap)

lines = makeRandomLines(12,100,100)

dc.BeginDrawing()

dc.DrawLineList(lines, wx.Pen("GREEN", 5))

dc.EndDrawing()

bitmap.SaveFile("f:/proj/sav.bmp",wx.BITMAP_TYPE_BMP )```

## 反馈

lilin/ogr-pil (last edited 2009-12-25 07:08:56 by localhost)