Changeset 34
- Timestamp:
- Oct 13, 2006, 11:21:29 AM (14 years ago)
- Location:
- trunk/npemap.org.uk/scripts/freethepostcode.org-importer
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/npemap.org.uk/scripts/freethepostcode.org-importer
-
Property
svn:ignore
set to
*.pyc
*.swp
currentlist
-
Property
svn:ignore
set to
-
trunk/npemap.org.uk/scripts/freethepostcode.org-importer/importer.py
r33 r34 10 10 11 11 from pyPgSQL import PgSQL 12 from geo_helper import turn_wgs84_into_osgb36, turn_osgb36_into_eastingnorthing 13 import os 12 14 13 15 # Database settings 16 dbtype = "postgres" 14 17 dbname = "npe_postcodes" 15 18 dbhost = "localhost" … … 20 23 # Connect to the database 21 24 dbh = None 22 if len(dbhost): 23 dbh = PgSQL.connect(database=dbname, host=dbhost, user=dbuser, password=dbpass) 25 if dbtype == "pgsql" or dbtype == "postgres" or dbtype == "postgresql": 26 if len(dbhost): 27 dbh = PgSQL.connect(database=dbname, host=dbhost, user=dbuser, password=dbpass) 28 else: 29 dbh = PgSQL.connect(database=dbname, user=dbuser, password=dbpass) 24 30 else: 25 dbh = PgSQL.connect(database=dbname, user=dbuser, password=dbpass)31 raise Exception("Unknown dbtype %s" % dbtype) 26 32 27 33 … … 41 47 42 48 43 # Download the latest list of postcodes 49 # Download the latest list of postcodes if needed 50 download = False 51 current_file = None 52 if os.path.isfile("currentlist"): 53 current_file = "currentlist" 54 if os.path.isfile("/tmp/currentlist"): 55 current_file = "/tmp/currentlist" 56 57 if not current_file == None: 58 print "Data found, do you wish to re-download?" 59 redownload = raw_input("") 60 if redownload == "y" or redownload == "yes": 61 download = True 62 else: 63 download = True 64 65 if download: 66 url = "http://www.freethepostcode.org/currentlist" 67 print "Downloading from %s" % url 68 os.system("wget -O currentlist '%s'" % url) 69 current_file = "currentlist" 70 print "" 71 72 73 # Read in the new list 74 postcodes = [] 75 ftpc = open(current_file, 'r') 76 for line in ftpc: 77 line = line[0:-1] 78 if line[0:1] == "#": 79 continue 80 81 parts = line.split(" ") 82 if not len(parts) == 4: 83 print "Invalid line '%s'" % line 84 85 # Turn lat+long into easting+northing 86 osll = turn_wgs84_into_osgb36(parts[0], parts[1], 0) 87 en = turn_osgb36_into_eastingnorthing(osll[0], osll[1]) 88 89 pc = {} 90 pc["easting"] = en[0] 91 pc["northing"] = en[1] 92 pc["outer"] = parts[2] 93 pc["inner"] = parts[3] 94 pc["raw"] = "%s %s" % (parts[2],parts[3]) 95 96 postcodes.append(pc) 97 44 98 45 99 # Prompt before removing all the current ones 100 sql = "SELECT COUNT(*) AS count FROM postcodes WHERE source = %s" 101 sth = dbh.cursor() 102 sth.execute(sql, ftp_source) 103 counts = sth.fetchall() 104 count = counts[0][0] 105 106 print "There are currently %d entries in the database from freethepostcode.org" % count 107 print "The new import contains %d entries" % len(postcodes) 108 print "Are you sure you wish to remove the old entries, to add new ones?" 109 confirm = raw_input("") 110 111 if confirm == "y" or confirm == "yes": 112 # Good, go ahead 113 pass 114 else: 115 print "" 116 raise Exception("Aborting import") 117 118 119 # Delete the old entries 120 sql = "DELETE FROM postcodes WHERE source = %s" 121 sth = dbh.cursor() 122 sth.execute(sql, ftp_source) 123 46 124 47 125 # Add the latest list to the database 126 127
Note: See TracChangeset
for help on using the changeset viewer.