Tim Condit
unread,Oct 6, 2006, 1:34:13 AM10/6/06Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to networkx...@lists.sourceforge.net
Greetings,
I'm brand new to the list, but may be making regular appearances over the next few months. :)
I am using NetworkX and TIGER/Line data from the U.S. Census as the
basis of a route finding project. I'm querying for
longitude/latitude data, and want to plot a series of points
(nodes). The result set from SQLAlchemy is a dict, keyed off the
field names. It looks like this:
results=getrp(zipcode)
for result in results:
print "result.items:",result.items()
=> result.items: [('tlid', 186700174L), ('frlong', -122299134L),
('frlat', 47464521L), ('tolong', -122298734L), ('tolat', 47459721L)]
That's a TIGER/Line ID, from longitude, from latitude, to longitude, to latitude.
I just want to plot each pair of points, and the edges (representing
street segments), so I can visualize my work. Am I going about it
the right way? Or is there an easier way? I've tried a
number of things, and feel like I'm missing something.
Suggestions are gratefully accepted.
The code below results in this error:
timc@kitsap:~/agents/sandbox/learning$ python m.py
Traceback (most recent call last):
File "m.py", line 45, in ?
NX.draw_networkx_nodes(G,G.pos[to_tlid],node_size=12,node_color='g')
File "build/bdist.linux-i686/egg/networkx/drawing/nx_pylab.py", line 201, in draw_networkx_nodes
TypeError: tuple indices must be integers
####
import networkx as NX
import sqlalchemy as sql
# Set up database connection.
db=sql.create_engine("mysql://readonly:readonly@kitsap/agents")
metadata=sql.BoundMetaData(db)
# Tables are already created. Just load the existing schema.
rt1=sql.Table('tiger_01', metadata, autoload=True)
zipcode=98158
def getrp(zipcode):
'''Fetch and return a SQLAlchemy ResultProxy.'''
# Do the SELECT based on ZIPL for now...
results=sql.select([rt1.c.TLID,
rt1.c.FRLONG,
rt1.c.FRLAT,
rt1.c.TOLONG,
rt1.c.TOLAT,
],rt1.c.ZIPL==zipcode).execute()
return results
G=NX.XGraph(name='The %s zipcode area' % zipcode)
G.pos={}
results=getrp(zipcode)
#for result in results:
# print "result.items:",result.items()
for result in results:
fr_tlid='fr_'+str(result['tlid'])
frlong=result['frlong']
frlat=result['frlat']
to_tlid='to_'+str(result['tlid'])
tolong=result['tolong']
tolat=result['tolat']
G.add_node(fr_tlid)
G.add_node(to_tlid)
G.pos[fr_tlid]=(frlong,frlat)
G.pos[to_tlid]=(tolong,tolat)
G.add_edge(fr_tlid,to_tlid)
NX.draw_networkx_nodes(G,G.pos[to_tlid],node_size=12,node_color='g')
NX.draw_networkx_edges(G,G.pos,width=0.3,edge_color='r')
####
And thanks for this terrific software!
Cheers,
Tim Condit