Sjors / srtm2postgis

Imports data from the NASA Shuttle Radar Topography Mission into a PostGIS database (and other databases in the futue).

srtm2postgis / read_data.py
65e3aac1 » sjors 2008-05-30 Created test database & mor... 1 # Read srtm data files and put them in the database.
2 from osgeo import gdal, gdal_array
3cb585dc » Sjors 2008-07-16 Unzips the .hgt.zip files d... 3 import os
65e3aac1 » sjors 2008-05-30 Created test database & mor... 4
05874578 » Sjors 2008-07-16 Insert data into database f... 5 import re
3cb585dc » Sjors 2008-07-16 Unzips the .hgt.zip files d... 6 import zipfile
05874578 » Sjors 2008-07-16 Insert data into database f... 7
8 from data import util
9
65e3aac1 » sjors 2008-05-30 Created test database & mor... 10 # Main functions
11
23bb3700 » Sjors 2008-07-12 Made some changes. 12 def loadTile(continent, filename):
3cb585dc » Sjors 2008-07-16 Unzips the .hgt.zip files d... 13 # Unzip it
14 zf = zipfile.ZipFile('data/' + continent + '/' + filename + ".hgt.zip")
15 for name in zf.namelist():
16 outfile = open('data/' + continent + '/' + name, 'wb')
17 outfile.write(zf.read(name))
18 outfile.flush()
19 outfile.close()
20
21 # Read it
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 22 srtm = gdal.Open('data/' + continent + '/' + filename + '.hgt')
3cb585dc » Sjors 2008-07-16 Unzips the .hgt.zip files d... 23
24 # Clean up
25 os.remove('data/' + continent + '/' + filename + '.hgt')
26
65e3aac1 » sjors 2008-05-30 Created test database & mor... 27 return gdal_array.DatasetReadAsArray(srtm)
28
29c9a98a » sjors 2008-05-30 Insert one tile in database 29 def connectToDatabasePsycopg2(database):
30 conn = psycopg2.connect("dbname='" + database.db + "' host='localhost' user='" + database.db_user + "' password='" + database.db_pass + "'")
31 return conn.cursor()
65e3aac1 » sjors 2008-05-30 Created test database & mor... 32
89664c96 » sjors 2008-06-04 Space saving measures 33 def posFromLatLon(lat,lon):
6ef5d6ec » sjors 2008-06-16 Added some tests to be on t... 34 return (lat * 360 + lon) * 1200 * 1200
89664c96 » sjors 2008-06-04 Space saving measures 35
05874578 » Sjors 2008-07-16 Insert data into database f... 36 def verify(db, number_of_tiles, files_hashes, continent, north, south, west, east):
6ef5d6ec » sjors 2008-06-16 Added some tests to be on t... 37 # For every tile, verify the bottom left coordinate.
05874578 » Sjors 2008-07-16 Insert data into database f... 38 for file in files_hashes:
4991478c » sjors 2008-05-30 Load all tiles in the datab... 39 # Strip .hgt.zip extension:
40 file = file[1][0:-8]
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 41
05874578 » Sjors 2008-07-16 Insert data into database f... 42 [lat,lon] = util.getLatLonFromFileName(file)
4991478c » sjors 2008-05-30 Load all tiles in the datab... 43
89664c96 » sjors 2008-06-04 Space saving measures 44 # Only a smaller part of Australia (see below):
05874578 » Sjors 2008-07-16 Insert data into database f... 45 if util.inBoundingBox(lat, lon, north, south, west, east):
e530ad77 » sjors 2008-06-16 Supports whole continent or... 46
47 print "Verify " + file + "..."
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 48
89664c96 » sjors 2008-06-04 Space saving measures 49 # Get top left altitude from file:
05874578 » Sjors 2008-07-16 Insert data into database f... 50 coordinate_file = loadTile(continent, file)[1][0]
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 51
89664c96 » sjors 2008-06-04 Space saving measures 52 # Get top left altitude from database:
d9303287 » sjors 2008-07-30 Move database activity into... 53 coordinate_db = db.fetchTopLeftAltitude(lat,lon)
89664c96 » sjors 2008-06-04 Space saving measures 54
55 if coordinate_db != coordinate_file:
56 print "Mismatch tile " + file[1]
57 exit()
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 58
4991478c » sjors 2008-05-30 Load all tiles in the datab... 59 # Check the total number of points in the database:
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 60
88bb3c91 » sjors 2008-06-16 Fixed bug in verification. 61 print "Check the total number of points in the database..."
89664c96 » sjors 2008-06-04 Space saving measures 62
63 sql = db.query("SELECT count(pos) FROM altitude")
4991478c » sjors 2008-05-30 Load all tiles in the datab... 64 total = int(sql.getresult()[0][0])
89664c96 » sjors 2008-06-04 Space saving measures 65 if not total == number_of_tiles * 1200 * 1200:
4991478c » sjors 2008-05-30 Load all tiles in the datab... 66 print "Not all tiles have been (completely) inserted!"
67 exit()
68
69 print "All tiles seem to have made it into the database! Enjoy."
79fe5a1e » Sjors 2008-07-16 Insert data into database f... 70
4991478c » sjors 2008-05-30 Load all tiles in the datab... 71 exit()
29c9a98a » sjors 2008-05-30 Insert one tile in database 72