Skip to content

Commit

Permalink
Merge merge_versions into master (#15)
Browse files Browse the repository at this point in the history
* Should work on all os and on python 2+3 now.

* Including latest changes

* Deleted unusual formatting issue

* Now everything should work on all os and python 2+3 and

even if code is run via IDLE…

* Clean up code before pull
  • Loading branch information
arjunane committed May 19, 2016
1 parent 764fdf2 commit f775989
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
18 changes: 11 additions & 7 deletions code/general_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,33 @@
###################################################################################

import codecs # Necessary in Python 3 (no standard String encoding)
import sys

# Function to execute entire SQL file
def execute_sql (conn, cur, filepath):
# Can't remember why one has to use utf-8-sig
fd = codecs.open(filepath, 'r', "utf-8-sig")

sqlfile = fd.read()

if (sys.version_info > (3, 0)):
fd = codecs.open(filepath, 'r', "utf-8-sig")
sqlfile = fd.read()
else:
fd = open(filepath, 'r')
sqlfile = fd.read().decode("utf-8-sig").encode("utf-8")

fd.close()

print ('Executing SQL-file %s...' %filepath)
cur.execute(sqlfile)
conn.commit()

print ('SQL-file %s executed!' %filepath)


# Yes-No function
def ask_yes_no(choice):

yes = set(['yes','y', 'ye', '']) # Enter means yes!
no = set(['no','n'])

while True:
if choice in yes:
return True
Expand Down
32 changes: 20 additions & 12 deletions code/osmTGmod.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@


# Imports Database Modules
from __future__ import (absolute_import, division, print_function) #, unicode_literals)
from builtins import * # This package seems to be unused

#import unicodecsv
#import io
from builtins import input

import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT as _il # Needed for creating Databases

import subprocess
import os
import io

import datetime
import sys
Expand All @@ -51,7 +49,6 @@
import build_up_db
import qgis_processing
import qgis_projects
#import csv

class grid_model:

Expand Down Expand Up @@ -233,7 +230,14 @@ def __init__(self,
# Writing conn-File
print ("Writing QGis-processing files...")
conn_str = """host=%s port=%s user=%s dbname=%s password=%s""" %(self.host,self.port,self.user,self.database,self.password)
fh = open(qgis_processing_path + "conn.txt","w")

if (sys.version_info > (3, 0)):
# Python 3 open
fh = open(qgis_processing_path + "conn.txt","w")
else:
# Python 2 io.open to process data as binary, not as str
fh = io.open(qgis_processing_path + "conn.txt","wb")

fh.write(conn_str)
fh.close()

Expand All @@ -249,14 +253,14 @@ def __init__(self,


# Function to download OSM-Data
# existing data is overwritten
def download_osm_data(self, filename):
if (sys.version_info > (3, 0)):
# Python 3 import
# Python 3 download
urllib.request.urlretrieve("http://ftp5.gwdg.de/pub/misc/openstreetmap/download.geofabrik.de/germany-latest.osm.pbf", self.raw_data_dir + "/" + filename)
else:
# Python 2 import
# Python 2 download
osm_data = urllib.URLopener()
# existing data is overwritten
osm_data.retrieve("http://ftp5.gwdg.de/pub/misc/openstreetmap/download.geofabrik.de/germany-latest.osm.pbf", self.raw_data_dir + "/" + filename)

# Function to filter OSM-Data
Expand Down Expand Up @@ -430,9 +434,13 @@ def write_to_csv (self, result_id, path):
query = 'SELECT * FROM results.%s WHERE result_id = %s' %(table, str(result_id))

outputquery = "COPY ({0}) TO STDOUT WITH DELIMITER ',' CSV HEADER".format(query)

fh = open(filename,"w")
# input("Press Enter to continue...")

if (sys.version_info > (3, 0)):
fh = open(filename, encoding='utf-8', mode = "w")
else:
fh = open(filename, "w")


self.cur.copy_expert(outputquery, fh)


Expand Down

0 comments on commit f775989

Please sign in to comment.