views:

351

answers:

7

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()
+2  A: 

There are many plotting packages for Python (and of course they support plotting polygons, it's one of the most fundamental features in that space;-), and the most popular one, I believe, is matplotlib.

Alex Martelli
can you write more about matplot lib? is it enough for visualizing polygons in python?
boofighter
+1  A: 

With matplotlib, you can draw polygons with the fill() command.

For example, to create a polygon with vertices (3,2),(4,2),(4,4),(3,4), you define x=[3,4,4,3], y=[2,2,4,4] (the respective x and y coordinates) and use fill(x,y). For example,

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

See http://matplotlib.sourceforge.net/api/pyplot%5Fapi.html#matplotlib.pyplot.fill for more about the fill() command, and the pylab API.

unutbu
thank you, but i already have those polygons in postgis database, in shapefile format (.shp). I just need to create application that will bring them from that database and visualize those polygons.
boofighter
+2  A: 

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

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])
Pridkett
thank you, but i already have those polygons in postgis database, in shapefile format (.shp). I just need to create application that will bring them from that database and visualize those polygons.
boofighter
A: 

thank you, but I already have those polygons in postgis database, in shapefile format (.shp). I just need to create application that will bring them from that database and visualize those polygons.

boofighter
A: 

What is the purpose you are trying to achieve here? Do you just want to look at the data in the .shp files? There is an open source application called Quantum GIS that allows you to open spatial data directly from PostGIS (and directly from a shapefile for that matter). If all you need to do is look at the polygons, this is probably the simplest way to do it. If this is not what you're trying to do, perhaps you can clarify your question.

Amanda Nyren
It is project for my university. I cant use any software like quantum gis. We just need to create an application to visualize those shapefiles in python.
boofighter
+1  A: 

maybe u can try the patch_collection example. I have copy it here with matplotlib.pyplot and only Polygon part:

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

The output pic is here: alt text
I think document matplotlib.patches.Polygon worth a look too. Hope this helps.

sunqiang
i understand, thank you. but i just know how to connect to database, but i dont know how to visualize polygons from that postgresql database. i dont know code which will apply that patch to .shp polygons in my database?
boofighter
oops, I just know a little of matplotlib, don't know the '.shp ' related things, hope someone here can handle this, though I think parsing the '.sha' format is not very Python specific.
sunqiang
A: 

Hi all,

I'm looking for a code to connect to postgis and export a table into shapefile in my local folder. How can i go about this? I already know how to do this manually but i need an automatic way.

You responce will be highly appreciated

Njoki