source: subversion/applications/rendering/genericHeatmapRender/binnedNodeHeatmap_Demo.py @ 29094

Last change on this file since 29094 was 18560, checked in by stevefaeembra, 10 years ago

Added binned node density plot script

  • Property svn:mime-type set to text/plain
File size: 2.8 KB
Line 
1'''
2    This program is free software; you can redistribute it and/or modify
3    it under the terms of the GNU General Public License as published by
4    the Free Software Foundation; either version 2 of the License, or
5    (at your option) any later version.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License along
13    with this program; if not, write to the Free Software Foundation, Inc.,
14    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15   
16    @Author : Steven Kay
17   
18    quick-and-dirty example of a node density heatmap
19    takes a partial OSM XML file (such as one downloaded from Geofabrik.de) and
20    bins nodes into an array, then plots as a heatmap in matplotlib
21   
22'''
23
24
25from pylab import *
26import string
27import math
28import matplotlib.cm as cm
29import matplotlib.pyplot as plt
30import numpy as np
31import math
32import random
33from matplotlib.mlab import griddata
34import matplotlib.image as im
35import matplotlib.pylab as pylab
36import numpy.ma as ma
37
38from xml.sax import make_parser
39from xml.sax.handler import ContentHandler
40import xml.etree.ElementTree as ET
41
42
43# bottom left corner 8W, 54N
44bounds=(-8.0,54.0) # bottom left-corner
45cdata = np.zeros((800, 1100))
46
47class Way(object):
48   
49    def __init__(self):
50        pass
51
52class Tag_Handler(ContentHandler):
53
54    # search using sax parser for elements   
55    buckets={}
56    ways=0
57    maxx=0
58    maxy=0
59   
60    def __init__(self):
61        pass
62   
63    def register(self, lat, lon):
64       
65        xpos=int((lon-bounds[0])*100.0) # 100 cells per degree
66        ypos=int((lat-bounds[1])*100.0)
67        try:
68            cdata[800-ypos,xpos]+=1
69        except:
70            # ignore points outside grid
71            self.maxx=xpos
72            self.maxy=ypos
73           
74        if self.ways % 100000==0:
75            print self.ways
76       
77    def startElement(self, name, attrs):
78       
79        if name=='node':           
80            self.elementID=attrs.get('id')
81            self.register(float(attrs.get('lat')),float(attrs.get('lon')))
82            self.ways += 1         
83        return
84   
85    def characters (self, ch):
86       
87        pass
88   
89    def endElement(self, name):
90       
91        pass
92
93# parse an osm xml file
94print "Parsing"
95fi=open(r"d:\xfer\osm data\scotland.xml","r")
96parser = make_parser()   
97curHandler = Tag_Handler()
98parser.setContentHandler(curHandler)
99parser.parse(fi)
100fi.close()
101
102cdata=np.log1p(cdata) # log coloring
103
104imshow(cdata, interpolation='mitchell',cmap=cm.jet,alpha=1.0)
105grid(False)
106show()
Note: See TracBrowser for help on using the repository browser.