Sjors / srtm2postgis
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
cd0fcd7
srtm2postgis / read_data.py
| 65e3aac1 » | sjors | 2008-05-30 | 1 | # Read srtm data files and put them in the database. | |
| 2 | from osgeo import gdal, gdal_array | ||||
| 3cb585dc » | Sjors | 2008-07-16 | 3 | import os | |
| 65e3aac1 » | sjors | 2008-05-30 | 4 | ||
| 05874578 » | Sjors | 2008-07-16 | 5 | import re | |
| 3cb585dc » | Sjors | 2008-07-16 | 6 | import zipfile | |
| 05874578 » | Sjors | 2008-07-16 | 7 | ||
| 8 | from data import util | ||||
| 9 | |||||
| 65e3aac1 » | sjors | 2008-05-30 | 10 | # Main functions | |
| 11 | |||||
| 23bb3700 » | Sjors | 2008-07-12 | 12 | def loadTile(continent, filename): | |
| 3cb585dc » | Sjors | 2008-07-16 | 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 | 22 | srtm = gdal.Open('data/' + continent + '/' + filename + '.hgt') | |
| 3cb585dc » | Sjors | 2008-07-16 | 23 | ||
| 24 | # Clean up | ||||
| 25 | os.remove('data/' + continent + '/' + filename + '.hgt') | ||||
| 26 | |||||
| 65e3aac1 » | sjors | 2008-05-30 | 27 | return gdal_array.DatasetReadAsArray(srtm) | |
| 28 | |||||
| 29c9a98a » | sjors | 2008-05-30 | 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 | 32 | ||
| 89664c96 » | sjors | 2008-06-04 | 33 | def posFromLatLon(lat,lon): | |
| 6ef5d6ec » | sjors | 2008-06-16 | 34 | return (lat * 360 + lon) * 1200 * 1200 | |
| 89664c96 » | sjors | 2008-06-04 | 35 | ||
| 05874578 » | Sjors | 2008-07-16 | 36 | def verify(db, number_of_tiles, files_hashes, continent, north, south, west, east): | |
| 6ef5d6ec » | sjors | 2008-06-16 | 37 | # For every tile, verify the bottom left coordinate. | |
| 05874578 » | Sjors | 2008-07-16 | 38 | for file in files_hashes: | |
| 4991478c » | sjors | 2008-05-30 | 39 | # Strip .hgt.zip extension: | |
| 40 | file = file[1][0:-8] | ||||
| 79fe5a1e » | Sjors | 2008-07-16 | 41 | ||
| 05874578 » | Sjors | 2008-07-16 | 42 | [lat,lon] = util.getLatLonFromFileName(file) | |
| 4991478c » | sjors | 2008-05-30 | 43 | ||
| 89664c96 » | sjors | 2008-06-04 | 44 | # Only a smaller part of Australia (see below): | |
| 05874578 » | Sjors | 2008-07-16 | 45 | if util.inBoundingBox(lat, lon, north, south, west, east): | |
| e530ad77 » | sjors | 2008-06-16 | 46 | ||
| 47 | print "Verify " + file + "..." | ||||
| 79fe5a1e » | Sjors | 2008-07-16 | 48 | ||
| 89664c96 » | sjors | 2008-06-04 | 49 | # Get top left altitude from file: | |
| 05874578 » | Sjors | 2008-07-16 | 50 | coordinate_file = loadTile(continent, file)[1][0] | |
| 79fe5a1e » | Sjors | 2008-07-16 | 51 | ||
| 89664c96 » | sjors | 2008-06-04 | 52 | # Get top left altitude from database: | |
| d9303287 » | sjors | 2008-07-30 | 53 | coordinate_db = db.fetchTopLeftAltitude(lat,lon) | |
| 89664c96 » | sjors | 2008-06-04 | 54 | ||
| 55 | if coordinate_db != coordinate_file: | ||||
| 56 | print "Mismatch tile " + file[1] | ||||
| 57 | exit() | ||||
| 79fe5a1e » | Sjors | 2008-07-16 | 58 | ||
| 4991478c » | sjors | 2008-05-30 | 59 | # Check the total number of points in the database: | |
| 79fe5a1e » | Sjors | 2008-07-16 | 60 | ||
| 88bb3c91 » | sjors | 2008-06-16 | 61 | print "Check the total number of points in the database..." | |
| 89664c96 » | sjors | 2008-06-04 | 62 | ||
| 63 | sql = db.query("SELECT count(pos) FROM altitude") | ||||
| 4991478c » | sjors | 2008-05-30 | 64 | total = int(sql.getresult()[0][0]) | |
| 89664c96 » | sjors | 2008-06-04 | 65 | if not total == number_of_tiles * 1200 * 1200: | |
| 4991478c » | sjors | 2008-05-30 | 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 | 70 | ||
| 4991478c » | sjors | 2008-05-30 | 71 | exit() | |
| 29c9a98a » | sjors | 2008-05-30 | 72 | ||
