Apr 17 2008

django epydocs

Tag: Uncategorizeddenis @ 7:04 pm

I couldn’t find it, so here it is: Django epydocs


Apr 11 2008

top commands

Tag: Uncategorizeddenis @ 12:40 am

2ni:~ denislaprise$ history|awk ‘{a[$2]++} END{for(i in a){printf “%5d\t%s\n”,a[i],i}}’|sort -rn|head
120 cd
97 ls
45 ssh
41 vi
19 svn
13 scp
13 python
12 make
11 find
10 wget

via Tom


Apr 02 2008

downloading google maps tiles using modestmaps

Tag: codedenis @ 1:14 pm

ModestMaps is one the nicest mapping API out there: it just feels right. I especially love the python version, which I used to download some Google Maps tiles (anyone remember gMerge?). The following script gets a Google Maps permalink and downloads the tiles all the way down to the maximum zoom level. You may need to alter the file retrieval and naming logic. The beauty here lies in the fact that ModestMaps allows you to download any layer from many APIs (Google, Virtual Earth, Yahoo Maps and OpenStreetMaps) by changing the Google.AerialProvider() call.

import sys, urllib2, re,os, Image, httplib
 
def parselink(link):
 qs = link[link.index('?')+1:]
 vals = [v.split('=') for v in qs.split('&')]
 params = {}
 for v in vals:
  params[v[0]] = v[1]
 zoom = int(params['z'] or '0')
 center = map(float, params['ll'].split(','))
 span = map(float, params['spn'].split(','))
 ne, sw = list(center), list(center)
 ne[0] += span[0]
 ne[1] += span[1]
 sw[0] -= span[0]
 sw[1] -= span[1]
 return ne, sw, zoom
 
def fetch(pt):
 if pt.zoom > Core.Coordinate.MAX_ZOOM:
  return
 
 if download(pt):
  z = pt.zoomBy(1)
  fetch(z)
  fetch(z.right())
  fetch(z.down())
  fetch(z.down().right())
 
def download(pt):
  url = layer.getTileUrls(pt)[0]
  try:
   os.makedirs("tiles/%d/%d" % (pt.zoom, pt.row))
  except: pass
  fn = "tiles/%d/%d/%d_%d.jpg" % (pt.zoom, pt.row, pt.row, pt.column)
  if os.path.exists(fn):
   try:
    m = Image.open(fn)
    a,b,c,d = m.getbbox()
    return d > 1
   except:
    return False
  print url
  try:
   # should use urllib2 to fake user agent here
   data = urllib.urlopen(url).read()
   out = open(fn, 'w')
   out.write(data)
   out.close()
   return True
  except urllib2.HTTPError, e:
   if e.code == 404:
    #empty tile
    out = Image.new('RGB', (1,1))
    out.save(fn)
    return False
 
  #should not reach this point. add better error handling
  sys.exit(-1)
 
if __name__ == "__main__":
 sys.path.append('trunk/py/')
 from ModestMaps import *
 
 layer = Google.AerialProvider()
 bbox = parselink('http://maps.google.com/maps?f=q&hl=en&geocode=&q=new++york,+ny&ie=UTF8&ll=40.75506,-73.969917&spn=0.100907,0.197926&t=h&z=13&iwloc=addr')
 ne = layer.locationCoordinate(Geo.Location(bbox[0][0], bbox[0][1])).zoomTo(bbox[-1]).container()
 sw = layer.locationCoordinate(Geo.Location(bbox[1][0], bbox[1][1])).zoomTo(bbox[-1]).container()
 cur = ne.copy()
 while cur.row <= sw.row:
   while cur.column >= sw.column:
    fetch(cur.copy())
    cur = cur.left()
   cur.column = ne.column
   cur = cur.down()

Or get the file here