예제 #1
0
  def readTile(self, lat0, lon0):
    # We need the tile name to know which row key to get.
    tileName = tileFromLatLon(lat0,lon0)
    print tileName

    # Calculate begin and end position
    begin = posFromLatLon(lat0,lon0)
    end = posFromLatLon(lat0 + 1, lon0 + 1)

    try:
      data = self.cf.get(tileName, column_start=int(begin), column_finish=int(end))
      res = data.values()
    except Exception:
      res = []

    # Now turn the result into a 2D array

    tile = []

    # Calculate tile width (should be 1200, or 10 for test tiles)
    tile_width = int(sqrt(len(res)))
    i = 0
    for x in range(tile_width):
      row = []
      for y in range(tile_width):
        row.append(int(res[i]))
        i = i + 1

      tile.append(row)

    print tile
    return tile
예제 #2
0
  def readTile(self, lat0, lon0):
    # Calculate begin and end position
    begin = posFromLatLon(lat0,lon0)
    end = posFromLatLon(lat0 + 1, lon0 + 1)
    sql = self.db.query(" \
      SELECT \
        alt \
      FROM altitude \
      WHERE \
        pos >= " + str(begin) + "\
        AND pos < " + str(end) + "\
      ORDER BY pos ASC \
    ")
    res = sql.getresult()
    
    # Now turn the result into a 2D array

    tile = []
    
    # Calculate tile width (should be 1200, or 10 for test tiles)
    tile_width = int(sqrt(len(res)))
    i = 0
    for x in range(tile_width):
      row = []
      for y in range(tile_width):
        row.append(int(res[i][0]))
        i = i + 1

      tile.append(row)
    
    return tile
예제 #3
0
  def insertTile(self, tile, lat0, lon0):
    # We need the tile name to know which row key to get.
    tileName = tileFromLatLon(lat0, lon0)
    #print "tileName:", tileName

    # Calculate begin position
    begin = posFromLatLon(lat0,lon0)

    # We want to accumulate a bunch of columns before we insert into Cassandra.
    columns = {}
    i=0
    insert_max=25000

    # We drop the top row and right column.
    for row in range(1, len(tile)):
      for col in range(0, len(tile) - 1):
        alt = long(tile[row][col])
        key = long(begin + (row-1) * 1200 + col)
        #print key, alt
        columns[key] = int(alt)

        # We've reached our ceiling, insert. 
        # We could do a bulk insert but since we're hitting one key, a single mutation should be more efficient.
        if i == insert_max:
          self.cf.insert(tileName, columns)
          columns = {}
          i=0

        else:
          i+=1

    # Send insert any thing left after the loop exits.
    self.cf.insert(tileName, columns);
예제 #4
0
  def insertTile(self, tile, lat0, lon0):
    # I use the Psycopg2 connection, with its copy_to and 
    # copy_from commands, which use the more efficient COPY command. 
    # This method requires a temporary file.

    # Calculate begin position
    begin = posFromLatLon(lat0,lon0)

    # First we write the data into a temporary file.
    f = cStringIO.StringIO()

    # int16 to string conversion is really slow, so we speed things up with caching
    cache = {}

    # We drop the top row and right column.
    for row in range(1, len(tile)):
      for col in range(0, len(tile) - 1):
        alt = tile[row][col]
        if not cache.has_key(alt):
            cache[alt] = str(alt)
        alt = cache[alt]
        f.write(str(begin + (row-1) * 1200 + col) + "\t" + alt + "\n")

    # Now we read the data from the temporary file and put it in the
    # altitude table.
    f.seek(0) 
    self.cursor.copy_from(f, 'altitude') 
    f.close() 
예제 #5
0
  def fetchTopLeftAltitude(self, lat, lon):
    tileName = tileFromLatLon(lat,lon)
    pos = posFromLatLon(lat,lon)

    try:
      res = self.cf.get(tileName, columns=[pos])
    except Exception:
      res = []

    return int(res[pos])
예제 #6
0
 def fetchTopLeftAltitude(self, lat, lon):
   pos = posFromLatLon(lat,lon)
   sql = self.db.query(" \
     SELECT \
       alt \
     FROM altitude \
     WHERE \
       pos = " + str(pos) + "\
    ")
   return int(sql.getresult()[0][0])
예제 #7
0
 def testPosFromLatLon(self):
   self.assertEqual(posFromLatLon(0,0),0) 
   self.assertEqual(posFromLatLon(0,1),1200*1200) 
   self.assertEqual(posFromLatLon(0,2),1200*1200*2) 
   self.assertEqual(posFromLatLon(1,0),1200*1200*360) 
   self.assertEqual(posFromLatLon(0,-1),-1200*1200) 
   self.assertEqual(posFromLatLon(-37,145),(-37 * 360 + 145) * 1200 * 1200) 
예제 #8
0
 def testPosFromLatLon(self):
   self.assertEqual(posFromLatLon(0,0),0) 
   self.assertEqual(posFromLatLon(0,1),1200*1200) 
   self.assertEqual(posFromLatLon(0,2),1200*1200*2) 
   self.assertEqual(posFromLatLon(1,0),1200*1200*360) 
   self.assertEqual(posFromLatLon(0,-1),-1200*1200) 
   self.assertEqual(posFromLatLon(-37,145),(-37 * 360 + 145) * 1200 * 1200) 
def writeTileCsvFile(tile, lat0, lon0, top_row = 1, bottom_row = 1200, left_col = 0, right_col = 1199 ):
  # Calculate begin position
  begin = posFromLatLon(lat0,lon0)

  # First we write the data into a temporary file.
  f = open('data/tile.csv', 'w')
  # We drop the top row and right column.
  for row in range(top_row, bottom_row + 1 ):
    for col in range(left_col, right_col + 1):
      f.write(str(\
      begin + (row-1) * 1200 + col\
      ) + ", " + str(tile[row][col] ) + "\n")

  f.close() 
예제 #10
0
  def readTile(self, lat0, lon0):
    # Calculate begin and end position
    begin = posFromLatLon(lat0,lon0)
    end = posFromLatLon(lat0 + 1, lon0 + 1)
    sql = "SELECT alt FROM altitude WHERE pos >= %s AND pos < %s ORDER BY pos ASC"
    res = self.query(sql, (str(begin),str(end)))

    # Now turn the result into a 2D array

    tile = []

    # Calculate tile width (should be 1200, or 10 for test tiles)
    tile_width = int(sqrt(len(res)))
    i = 0
    for x in range(tile_width):
      row = []
      for y in range(tile_width):
        row.append(int(res[i][0]))
        i = i + 1

      tile.append(row)

    return tile
def writeTileCsvFile(tile,
                     lat0,
                     lon0,
                     top_row=1,
                     bottom_row=1200,
                     left_col=0,
                     right_col=1199):
    # Calculate begin position
    begin = posFromLatLon(lat0, lon0)

    # First we write the data into a temporary file.
    f = open('data/tile.csv', 'w')
    # We drop the top row and right column.
    for row in range(top_row, bottom_row + 1):
        for col in range(left_col, right_col + 1):
            f.write(str(\
            begin + (row-1) * 1200 + col\
            ) + ", " + str(tile[row][col] ) + "\n")

    f.close()
예제 #12
0
  def insertTile(self, tile, lat0, lon0):
    # I use the Psycopg2 connection, with its copy_to and 
    # copy_from commands, which use the more efficient COPY command. 
    # This method requires a temporary file.

    # Calculate begin position
    begin = posFromLatLon(lat0,lon0)

    # First we write the data into a temporary file.
    f = open('/tmp/tempcopy', 'w')
    # We drop the top row and right column.
    for row in range(1, len(tile)):
      for col in range(0, len(tile) - 1):
        f.write(str(\
        begin + (row-1) * 1200 + col\
        ) + "\t" + str(tile[row][col] ) + "\n")

    f.close() 

    # Now we read the data from the temporary file and put it in the
    # altitude table.

    f = open('/tmp/tempcopy', 'r')
    #cur.copy_from(f, 'altitude') 
    
    # Unfortunately the copy_from() command hangs on my computer for
    # some reason, so we try something else. See also:
    # http://lists.initd.org/pipermail/psycopg/2007-October/005684.html

    import subprocess

    #psqlcmd = os.path.join('c:\\', 'Program Files', 'PostgreSQL', '8.2',
    #'bin', 'psql')
    
    psqlcmd = "/usr/bin/psql"

    p = subprocess.Popen('psql ' + self.db_name +  ' -c "COPY altitude FROM STDIN;"', stdin=f, shell=True);
    p.wait()

    f.close
예제 #13
0
 def fetchTopLeftAltitude(self, lat, lon):
   pos = posFromLatLon(lat,lon)
   sql = " SELECT alt FROM altitude WHERE pos = %s "
   return int(self.query(sql, (pos,))[0][0])