0
# Read srtm data files and put them in the database.
0
from osgeo import gdal, gdal_array
0
@@ -19,10 +21,15 @@ def createTableAltitude(db):
0
PRIMARY KEY ( lat, lon ) \
0
def connectToDatabase(database):
0
return pg.DB(dbname=database.db,host='localhost', user=database.db_user, passwd=database.db_pass)
0
+def connectToDatabasePsycopg2(database):
0
+ conn = psycopg2.connect("dbname='" + database.db + "' host='localhost' user='" + database.db_user + "' password='" + database.db_pass + "'")
0
db.query("DROP TABLE IF EXISTS altitude;")
0
@@ -31,17 +38,117 @@ def checkDatabaseEmpty(db):
0
# Test is the test database is as we expect it after setUp:
0
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']
0
-db = connectToDatabase(database)
0
+def insertTileIntoDatabase(cur, db_name, tile, lat0, lon0):
0
+ # I use the Psycopg2 connection, with its copy_to and
0
+ # copy_from commands, which use the more efficient COPY command.
0
+ # This method requires a temporary file.
0
+ # First we write the data into a temporary file.
0
+ f = open('/tmp/tempcopy', 'w')
0
+ for row in range(len(tile) - 1):
0
+ for col in range(len(tile) - 1):
0
+ f.write(str(lat0 - float(row)/1200.) + "\t" + str(lon0 + float(col)/1200.) + "\t" + str(tile[row][col] ) + "\n")
0
+ # Now we read the data from the temporary file and put it in the
0
+ f = open('/tmp/tempcopy', 'r')
0
+ #cur.copy_from(f, 'altitude')
0
+ # Unfortunately the copy_from() command hangs on my computer for
0
+ # some reason, so we try something else. See also:
0
+ # http://lists.initd.org/pipermail/psycopg/2007-October/005684.html
0
+ #psqlcmd = os.path.join('c:\\', 'Program Files', 'PostgreSQL', '8.2',
0
+ psqlcmd = "/usr/bin/psql"
0
+ p = subprocess.Popen('psql ' + db_name + ' -c "COPY altitude FROM STDIN;"', stdin=f, shell=True);
0
+def readTileFromDatabase(db, lat0, lon0):
0
+ lat <= " + str(lat0) + "\
0
+ AND lat > " + str(lat0 -1) + "\
0
+ AND lon >= " + str(lon0) + "\
0
+ AND lon < " + str(lon0 + 1) + "\
0
+ ORDER BY lat DESC, lon ASC \
0
+ # Now turn the result into a 2D array
0
+ # Calculate tile width (should be 1200, or 10 for test tiles)
0
+ tile_width = int(sqrt(len(res)))
0
+ for x in range(tile_width):
0
+ for y in range(tile_width):
0
+ row.append(int(res[i][0]))
0
+def getLatLonFromFileName(name):
0
+ # Split up in lat and lon:
0
+ p = re.compile('[NSEW]\d*')
0
+ [lat_str, lon_str] = p.findall(name)
0
+ lat = int(lat_str[1:])
0
+ lat = -int(lat_str[1:])
0
+ lon = int(lon_str[1:])
0
+ lon = -int(lon_str[1:])
0
+if __name__ == '__main__':
0
+ db = connectToDatabase(database)
0
+ # Does the user want to empty the database first?
0
+ if 'empty' in sys.argv:
0
+ print "Deleting tables from databse..."
0
+ # Make sure the database is empty before we start:
0
+ if not checkDatabaseEmpty(db):
0
+ print "Database is not empty. Run 'read_data empty' to empty it first."
0
+ # Second database connection with psychopg2
0
+ db_psycopg2 = connectToDatabasePsycopg2(database)
0
+ createTableAltitude(db)
0
+ tile = loadTile('S11E119')
0
-# Does the user want to empty the database first?
0
-if 'empty' in sys.argv:
0
- print "Deleting tables from databse..."
0
+ print( "Get lat and lon from filename...")
0
+ [lat,lon] = getLatLonFromFileName("S11E119")
0
-# Make sure the database is empty before we start:
0
-if not checkDatabaseEmpty(db):
0
- print "Database is not empty. Run 'read_data empty' to empty it first."
0
+ print("Insert data...")
0
+ insertTileIntoDatabase(db_psycopg2, "srtm" , tile, lat, lon)
0
-createTableAltitude(db)