postgresql 和 python [英] postgresql and python

查看:71
本文介绍了postgresql 和 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 python 从postgis"数据库 (postgresql) 中可视化空间数据(20 个多边形).我只知道如何使用 python 连接 postgresql 数据库,但我不知道如何可视化这些多边形.这是我大学的项目.我们需要使用例如 matplotlib 并在 python 中创建应用程序,它将可视化来自 postgresql 数据库的 shapefiles (.shp).

I need to visualize spatial data (20 polygons) from "postgis" database (postgresql), with python. I know how to connect postgresql database with python only, but I don`t how to visualize those polygons. It is project for my university. We need to use for example matplotlib and create application in python which will visualize shapefiles (.shp) from postgresql database.

我从这段代码开始,但我不知道如何继续:

I started with this code but I dont know how to continue:

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()

推荐答案

我很幸运使用了 Python 图像库 当我需要创建光栅(位图)图形时.它有一个非常简单的界面,可以很容易地将图形叠加到另一个图像之上.不幸的是,PIL 不经常更新.可能是因为它只是有效.以下是如何制作一个简单的多边形(摘自 Nadia Alrami 对 PIL 的出色介绍):

I've had good luck using the Python Imaging Library when I've needed to create raster (bitmap) graphics. It's got a pretty simple interface and makes it pretty easy to overlay graphics on top of another image. Unfortunately, PIL isn't updated that often. Probably because it just works. Here's how to do a simple polygon (lifted from 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 有一个很好的多边形函数,所以你需要通过做这样的事情来破解你自己的:

For more complex images, I tend to render them in SVG using the svgfig package for python. Because it's SVG they're great for scaling and sharing on a web browser. Unfortunately, I don't believe that svgfig has a nice polygon function, so you'll need to hack your own by doing something like this:

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])

这篇关于postgresql 和 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆