Reputation: 171
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()
Upvotes: 1
Views: 3262
Reputation: 6492
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:
I think document matplotlib.patches.Polygon worth a look too. Hope this helps.
Upvotes: 1
Reputation: 495
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.
Upvotes: 0
Reputation: 5543
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])
Upvotes: 3
Reputation: 880937
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_api.html#matplotlib.pyplot.fill for more about the fill() command, and the pylab API.
Upvotes: 1
Reputation: 882761
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.
Upvotes: 2