<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array">
    <added>
      <filename>database_pg_template.py</filename>
    </added>
    <added>
      <filename>read_data_pg.py</filename>
    </added>
    <added>
      <filename>test/database_pg_test_template.py</filename>
    </added>
    <added>
      <filename>test/test_read_data_pg.py</filename>
    </added>
  </added>
  <modified type="array">
    <modified>
      <diff>@@ -1,6 +1,6 @@
 commit.msg
 *.swp
 *.pyc
-database.py
+database_pg.py
 POSTGIS
 MYSQL</diff>
      <filename>.gitignore</filename>
    </modified>
    <modified>
      <diff>@@ -1,9 +1,5 @@
 # Read srtm data files and put them in the database.
-import pg, psycopg2
 from osgeo import gdal, gdal_array
-import database 
-import sys
-from math import sqrt
 import os
 
 import re
@@ -30,122 +26,13 @@ def loadTile(continent, filename):
   
   return gdal_array.DatasetReadAsArray(srtm)
 
-
-
-def createTableAltitude(db):
-  tables = db.get_tables()
-    
-  if not('public.altitude' in tables): 
-    db.query(&quot; \
-      CREATE TABLE altitude ( \
-        pos bigint NOT NULL, \
-        alt int NULL , \
-        PRIMARY KEY ( pos ) \
-      ); \
-    &quot;)
-  return True
-  
-def connectToDatabase(database):
-    return pg.DB(dbname=database.db,host='localhost', user=database.db_user, passwd=database.db_pass)
-      
 def connectToDatabasePsycopg2(database):
     conn = psycopg2.connect(&quot;dbname='&quot; + database.db + &quot;' host='localhost' user='&quot; + database.db_user + &quot;' password='&quot; + database.db_pass + &quot;'&quot;)
     return conn.cursor()
 
-def dropAllTables(db):
-    db.query(&quot;DROP TABLE IF EXISTS altitude;&quot;)
-
-def checkDatabaseEmpty(db):
-    # Test is the test database is as we expect it after setUp:
-    return db.get_tables() == ['information_schema.sql_features', 'information_schema.sql_implementation_info', 'information_schema.sql_languages', 'information_schema.sql_packages', 'information_schema.sql_parts', 'information_schema.sql_sizing', 'information_schema.sql_sizing_profiles', 'public.geometry_columns', 'public.spatial_ref_sys']
-
 def posFromLatLon(lat,lon):
   return (lat * 360 + lon) * 1200 * 1200
 
-def fetchTopLeftAltitude(db, lat, lon):
-  pos = posFromLatLon(lat,lon)
-  sql = db.query(&quot; \
-    SELECT \
-      alt \
-    FROM altitude \
-    WHERE \
-      pos = &quot; + str(pos) + &quot;\
-   &quot;)
-  return int(sql.getresult()[0][0])
-
-def insertTileIntoDatabase(cur, db_name, 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\
-      ) + &quot;\t&quot; + str(tile[row][col] ) + &quot;\n&quot;)
-
-  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 = &quot;/usr/bin/psql&quot;
-
-  p = subprocess.Popen('psql ' + db_name +  ' -c &quot;COPY altitude FROM STDIN;&quot;', stdin=f, shell=True);
-  p.wait()
-
-  f.close
-        
-def readTileFromDatabase(db, lat0, lon0):
-  # Calculate begin and end position
-  begin = posFromLatLon(lat0,lon0)
-  end = posFromLatLon(lat0 + 1, lon0 + 1)
-  sql = db.query(&quot; \
-    SELECT \
-      alt \
-    FROM altitude \
-    WHERE \
-      pos &gt;= &quot; + str(begin) + &quot;\
-      AND pos &lt; &quot; + str(end) + &quot;\
-    ORDER BY pos ASC \
-  &quot;)
-  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
-        
 def verify(db, number_of_tiles, files_hashes, continent, north, south, west, east):
     # For every tile, verify the bottom left coordinate.
     for file in files_hashes:
@@ -163,7 +50,7 @@ def verify(db, number_of_tiles, files_hashes, continent, north, south, west, eas
         coordinate_file = loadTile(continent, file)[1][0]
     
         # Get top left altitude from database:
-        coordinate_db = fetchTopLeftAltitude(db,lat,lon)
+        coordinate_db = db.fetchTopLeftAltitude(lat,lon)
         
         if coordinate_db != coordinate_file:
           print &quot;Mismatch tile &quot; + file[1]
@@ -183,82 +70,3 @@ def verify(db, number_of_tiles, files_hashes, continent, north, south, west, eas
     
     exit()
 
-def main():
-  db = connectToDatabase(database) 
-  from data import files
-  
-  try:
-      continent = sys.argv[1]
-      
-  except: 
-      print &quot;Please specify the continent. Africa, Australia, Eurasia, Islands, North_America or South_America.&quot;
-
-  # Does the user want to empty the database?
-  if 'empty' in sys.argv:
-    print &quot;Deleting tables from databse...&quot; 
-    dropAllTables(db)
-    print &quot;Done...&quot;
-    exit()
-
-  [north, south, west, east] = util.getBoundingBox(sys.argv, 3)
-      
-  files_hashes = util.getFilesHashes(continent)
-  
-  number_of_tiles = util.numberOfFiles(files_hashes, north, south, west, east)
-  
-  # Verify result?
-  if 'verify' in sys.argv:
-    verify(db, number_of_tiles, files_hashes, continent,  north, south, west, east)
-
-  # If a tile name is given as the sixth argument it will resume from there.
-  p = re.compile('[NSEW]\d*')
-  resume_from = &quot;&quot;
-  try:
-    if(p.find(sys.argv[6])):
-      resume_from = sys.argv[6]
-      
-  except: 
-    None
-    
-  # Second database connection with psychopg2 
-  db_psycopg2 = connectToDatabasePsycopg2(database)
-
-  createTableAltitude(db)
-
-  i = 0
-
-  for file in files_hashes:
-    # Strip .hgt.zip extension:
-    file = file[1][0:-8] 
-    # Get latitude and longitude from file name 
-    [lat,lon] = util.getLatLonFromFileName(file)
-    
-    if util.inBoundingBox(lat, lon, north, south, west, east):
-      i = i + 1
-  
-      # Are we resuming?
-      if resume_from == file:
-        resume_from = &quot;&quot; 
-      
-      if resume_from == &quot;&quot;:
-
-        # Load tile from file
-        tile = loadTile(continent, file)
-
-        # First check if the tile is not already in the database:
-
-        try:
-          fetchTopLeftAltitude(db, lat, lon)
-          print(&quot;Skipping tile &quot; + file + &quot; (&quot; + str(i) + &quot; / &quot; + str(number_of_tiles) + &quot;) ...&quot;)
-
-        except:
-          print(&quot;Insert data for tile &quot; + file + &quot; (&quot; + str(i) + &quot; / &quot; + str(number_of_tiles) + &quot;) ...&quot;)
-
-          insertTileIntoDatabase(db_psycopg2, &quot;srtm&quot; , tile, lat, lon)
-
-  print(&quot;All tiles inserted. Pleasy verify the result with python \
-  read_data.py verify&quot;)
-
-
-if __name__ == '__main__':
-  main()</diff>
      <filename>read_data.py</filename>
    </modified>
    <modified>
      <diff>@@ -1,2 +1,2 @@
 *.swp
-database_test.py
+database_pg_test.py</diff>
      <filename>test/.gitignore</filename>
    </modified>
    <modified>
      <diff>@@ -8,8 +8,6 @@ python download.py Australia 0 -11 -11 119 119
 echo &quot;Verify this download...&quot;
 python test/verify_download.py Australia -11 -11 119 119
 
-cd data/Australia &amp;&amp; unzip S11E119.hgt.zip &amp;&amp; cd ../..
-
 echo &quot;Test database insert...&quot;
-python test/test_read_data.py
+python test/test_read_data_pg.py
 </diff>
      <filename>test/test_everything.sh</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>database_template.py</filename>
    </removed>
    <removed>
      <filename>test/database_test_template.py</filename>
    </removed>
    <removed>
      <filename>test/test_read_data.py</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>3b5608193e6bed6f39721ee0467afa35022308e1</id>
    </parent>
  </parents>
  <author>
    <name>sjors</name>
    <email>sjors@b9d5c4c9-76e1-0310-9c85-f3177eceb1e4</email>
  </author>
  <url>http://github.com/Sjors/srtm2postgis/commit/d93032878a6ff948b077fde4d19b25837981f2d6</url>
  <id>d93032878a6ff948b077fde4d19b25837981f2d6</id>
  <committed-date>2008-07-30T23:22:31-07:00</committed-date>
  <authored-date>2008-07-30T23:22:31-07:00</authored-date>
  <message>Move database activity into a seperate class.


git-svn-id: http://svn.openstreetmap.org/applications/utils/srtm2postgis/trunk@9393 b9d5c4c9-76e1-0310-9c85-f3177eceb1e4</message>
  <tree>447c522c0014ea4bd39229b9f673e8c5c9f11ba4</tree>
  <committer>
    <name>sjors</name>
    <email>sjors@b9d5c4c9-76e1-0310-9c85-f3177eceb1e4</email>
  </committer>
</commit>
