2009-11-21 20 views
1

我需要使用python從「postgis」數據庫(postgresql)可視化空間數據(20多邊形)。我知道如何連接postgresql數據庫與Python,但我不知道如何可視化這些多邊形。這是我的大學的項目。我們需要使用例如matplotlib並在python中創建應用程序,它將從postgresql數據庫中可視化shapefile(.shp)。postgresql和python

我開始使用此代碼,但我不知道如何繼續:

import psycopg2 
conn = psycopg2.connect("dbname='***' user='***' host='***' password='***'") 
print 'Succesfully connected' 

cur = conn.cursor() 
cur.execute("""SELECT astext(the_geom) from buildings;""") 
listpoly = cur.fetchall() 
conn.close() 

回答

2

有許多繪圖包的Python(當然他們支持繪製多邊形,它是在這個空間最根本的特徵之一;-),而我認爲最受歡迎的是matplotlib

+0

你可以寫更多關於matplot lib嗎?是否足以在python中可視化多邊形? – boofighter 2009-11-21 19:47:58

1

使用matplotlib,可以使用fill() command繪製多邊形。例如,要創建一個頂點爲(3,2),(4,2),(4,4),(3,4), 的多邊形,可以定義x = [3,4,4,3 ],y = [2,2,4,4](相應的x和y座標) 並使用fill(x,y)。例如,

import pylab 
z=[(3,2),(4,2),(4,4),(3,4)] 
x,y=zip(*z) 
pylab.fill(x,y, 'b', alpha=0.2, edgecolor='r') 
pylab.show() 

有關fill()命令和pylab API的更多信息,請參閱http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.fill

+0

謝謝,但我已經在postgis數據庫中使用了shapefile格式(.shp)中的那些多邊形。我只需要創建應用程序,它將從數據庫中提取它們並可視化這些多邊形。 – boofighter 2009-11-21 20:45:40

3

當我需要創建柵格(位圖)圖形時,我使用Python Imaging Library祝你好運。它有一個非常簡單的界面,並且可以很容易地將圖形疊加在另一個圖像上。不幸的是,PIL不會經常更新。可能因爲它正常工作。以下是如何做一個簡單的多邊形(從Nadia Alrami's excellent little intro to PIL解除):

im = Image.new('RGBA', (100, 100), (0, 0, 0, 0)) # Create a blank image 
draw = ImageDraw.Draw(im) 
lines = [(50, 0), (0, 40), (20, 100), (80, 100), (100, 40)] 
draw.polygon(lines, fill="black") 

對於更復雜的圖像,我傾向於使用svgfig包蟒蛇,使它們在SVG。因爲它是SVG,所以它們非常適合在Web瀏覽器上擴展和共享。不幸的是,我不相信svgfig有一個很好的多邊形功能,所以你需要做這樣的事情砍你自己:

def polygon(points): 
    for x in xrange(len(points)-1): 
     line(points[x][0], points[x][1], points[x+1][0], points[x+1][1]) 
    line(points[-1][0], points[-1][1], points[0][0], points[0][1]) 
+0

謝謝,但我已經在postgis數據庫中使用了shapefile格式(.shp)中的那些多邊形。我只需要創建應用程序,它將從數據庫中提取它們並可視化這些多邊形。 – boofighter 2009-11-21 20:46:32

0

什麼是你正試圖在這裏達到目的?你只想看看.shp文件中的數據嗎?有一個名爲Quantum GIS的開源應用程序,允許您直接從PostGIS打開空間數據(並直接從shapefile中打開)。如果你只需要看多邊形,這可能是最簡單的方法。如果這不是你想要做的,也許你可以澄清你的問題。

+0

這是我的大學的項目。我無法使用任何類似量子gis的軟件。我們只需要創建一個應用程序來在python中可視化這些shapefile文件。 – boofighter 2009-11-22 14:08:18

1

也許你可以試試patch_collection example。我有matplotlib.pyplot只有多邊形部分它拷貝到這裏:

import matplotlib 
import matplotlib.pyplot as plt 
from matplotlib.patches import Polygon 
from matplotlib.collections import PatchCollection 
import numpy as np 
fig = plt.figure() 
ax = fig.add_subplot(111) 
patches = [] 
for x in xrange(2): 
    polygon = Polygon(np.random.rand(3, 2), True) 
    patches.append(polygon) 
p = PatchCollection(patches, cmap=matplotlib.cm.jet, alpha=0.4) 
colors = 100*np.random.rand(len(patches)) 
p.set_array(np.array(colors)) 
ax.add_collection(p) 
plt.colorbar(p) 

plt.show() 

輸出PIC是herealt text http://i46.tinypic.com/v762a9.jpg
我覺得文檔matplotlib.patches.Polygon值得一看了。希望這可以幫助。

+0

我明白了,謝謝。但我只知道如何連接到數據庫,但我不知道如何從該postgresql數據庫可視化多邊形。我不知道將在我的數據庫中應用該修補程序到.shp多邊形的代碼? – boofighter 2009-11-22 13:09:18

+0

oops,我只知道一點matplotlib,不知道'.shp'相關的東西,希望這裏有人可以處理這個,儘管我認爲解析'.sha'格式並不是Python特有的。 – sunqiang 2009-11-22 13:54:50