Skip to content

Commit

Permalink
Update configuration moved to django.settings. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
mattesCZ committed Jan 25, 2014
1 parent 4e86c93 commit aa15e60
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 126 deletions.
10 changes: 10 additions & 0 deletions mtbmap/settings_local.py.example
Expand Up @@ -44,3 +44,13 @@ SECRET_KEY = '----John-Does-Secret-Key----'

MAPNIK_STYLES = os.path.join(ROOT_PATH, 'styles/mapnik/my_styles/')
SRTM_DATA = os.path.join(ROOT_PATH, 'Data/shadingdata/SRTMv2/')

# update OSM data settings
OSM_DATADIR = os.path.join(ROOT_PATH, 'Data/')
OSM_DOWNLOAD = True
OSM_FORMAT = 'pbf'
OSM_SOURCE_URI = 'http://download.geofabrik.de/europe-latest.osm.pbf'

OSM2PGSQL = os.path.join(ROOT_PATH, 'sw/osm2pgsql/osm2pgsql')
OSM2PGSQL_STYLE = os.path.join(ROOT_PATH, 'osm_data_processing/config/mtbmap.style')
OSM2PGSQL_CACHE = 2048
33 changes: 0 additions & 33 deletions osm_data_processing/default.conf

This file was deleted.

Expand Up @@ -13,17 +13,12 @@ class Command(BaseCommand):
help = 'Update data used for map rendering and timestamp.'

def handle(self, *args, **options):
if (len(args)==1):
config_file = args[0]
self.stdout.write("Reading configuration file: %s" % config_file)
else:
config_file = '../../default.conf'
date = updatemap(config_file)
date = updatemap()
if date:
copy_osmpoints()
copy_osmlines()
tile_layer = TileLayer.objects.get(slug='mtb-map')
tile_layer.last_update = date
tile_layer.save()
else:
self.stderr.write('An error occured')
self.stderr.write('An error occurred')
141 changes: 55 additions & 86 deletions osm_data_processing/updatemap.py
Expand Up @@ -4,105 +4,84 @@
from osm_data_processing.relations2lines.relations2lines import run

# Global imports
import sys, ConfigParser
import string, os, re, shutil
import string, os
import datetime

# Django imports
from django.conf import settings

def exists(name, path):
if os.path.exists(path):
print name + ' succesfully set to: ' + path
print '%s successfully set to: %s' % (name, path)
else:
print 'non-existing file or folder: ' + path
print 'correct variable ' + name + ' in configuration file'
raise UpdateError('Please, correct variable ' + name + ' in configuration file')
print 'non-existing file or folder: %s' % (path)
print 'correct variable %s in configuration file' % (name)
raise UpdateError('Please, correct variable %s in configuration file' % (name))

def downloadFile(source, datadir):
def download_file(source, datadir):
os.chdir(datadir)
return os.system('wget -nv -t 3 -N ' + source)
return os.system('wget -nv -t 3 -N %s' % (source))

def loadDB(osm2pgsql, database, file, style, cache, port):
loadCommand = osm2pgsql + ' -s -d ' + database + ' ' + file + ' -S ' + style + ' -C ' + str(cache) + ' -P ' + str(port) + ' --number-processes 8 '
return os.system(loadCommand)
def load_db(osm2pgsql, database, file, style, cache, port):
load_command = '%s -s -d %s %s -S %s -C %s -P %s --number-processes 8 ' % (osm2pgsql, database, file, style, cache, port)
return os.system(load_command)

class UpdateError(Exception):
def __init__(self, msg):
self.msg = msg

def updatemap(config_file):
# set variables from configuration file passed as the command line parameter
# default is default.conf
configFile = config_file

if (os.path.exists(configFile)):
try:
config = ConfigParser.ConfigParser()
config.read(configFile)
except ConfigParser.Error:
print 'Configuration file is not well formed, nothing was done'
sys.exit(1)

def updatemap():
try:
datadir = settings.OSM_DATADIR
exists('datadir', datadir)
database = settings.DATABASES['osm_data']['NAME']
print 'database name set to : %s' % (database)
port = settings.DATABASES['osm_data']['PORT']
style = settings.OSM2PGSQL_STYLE
exists('style', style)
cache = settings.OSM2PGSQL_CACHE
try:
homepath = config.get('update', 'homepath')
exists('homepath', homepath)
datadir = config.get('update', 'datadir')
exists('datadir', datadir)
database = config.get('update', 'database')
print 'database name set to : ' + database
port = config.get('update', 'port')
style = config.get('update', 'style')
exists('style', style)
cache = config.get('update', 'cache')
try:
float(cache)
print 'cache succesfully set to: ' + cache + 'MB'
except ValueError:
print 'variable cache must be a number, you have passed : ' + cache
cache = '2048'
print 'cache set to default: 2048MB'
osm2pgsql = config.get('update', 'osm2pgsql')
exists('osm2pgsql', osm2pgsql)

download = config.get('update', 'download')
format = config.get('update', 'format')
if (format=='pbf' or format=='xml'):
print 'Using ' + format + ' format.'
else:
raise UpdateError('Incorrect format, use xml or pbf.')
source_uri = config.get('update', 'source')
if (download=='yes'):
source = (source_uri, string.split(source_uri, '/')[-1])
else:
source = source_uri
except ConfigParser.Error:
print 'Some variables are missing in configuration file. Nothing was done.'
sys.exit(0)
except UpdateError, ue:
print ue.msg
print 'Nothing was done.'

else:
print "Missing configuration file, nothing was done"
sys.exit(1)

float(cache)
print 'cache succesfully set to: %s MB' % (cache)
except (ValueError, TypeError):
print 'variable cache must be a number, you have passed : %s' % (cache)
cache = 2048
print 'cache set to default: 2048MB'
osm2pgsql = settings.OSM2PGSQL
exists('osm2pgsql', osm2pgsql)
format = settings.OSM_FORMAT
if (format=='pbf' or format=='xml'):
print 'Using %s format.' % (format)
else:
raise UpdateError('Incorrect format, use xml or pbf.')
source_uri = settings.OSM_SOURCE_URI
if settings.OSM_DOWNLOAD:
source = (source_uri, string.split(source_uri, '/')[-1])
else:
source = source_uri
except UpdateError, ue:
print ue.msg
print 'Nothing was done.'
return None
try:
#download files
if (download == 'yes'):
print 'Downloading file ' + source[1] + ' from ' + source[0] + ' ...'
result = downloadFile(source[0], datadir)
if (settings.OSM_DOWNLOAD):
print 'Downloading file %s from %s ...' % (source[1], source[0])
result = download_file(source[0], datadir)
if (result!=0):
raise UpdateError('An error occured while downloading file ' + source[1])
raise UpdateError('An error occurred while downloading file %s' % (source[1]))
else:
sourceFile = source[1]
print 'File ' + source[1] + ' successfully downloaded.'
source_file = source[1]
print 'File %s successfully downloaded.' % (source_file)
else:
sourceFile = source
source_file = source

datetime_in_sec = os.path.getmtime(datadir + sourceFile)
datetime_in_sec = os.path.getmtime(datadir + source_file)
date = datetime.date.fromtimestamp(datetime_in_sec)

#osm2pgsql
if (loadDB(osm2pgsql, database, datadir + sourceFile, style, cache, port) != 0):
raise UpdateError('An osm2pgsql error occured. Database was probably cleaned.')
if (load_db(osm2pgsql, database, datadir + source_file, style, cache, port) != 0):
raise UpdateError('An osm2pgsql error occurred. Database was probably cleaned.')
else:
print 'OSM data successfully loaded to database, running relations2lines.py ...'
#relations2lines
Expand All @@ -114,13 +93,3 @@ def updatemap(config_file):
print ue.msg
print 'Map data was not uploaded.'
return None

# finally:
# if (os.path.exists(datadir + merged)):
# os.remove(datadir + merged)
# for file in boundedFiles:
# if (os.path.exists(datadir + file)):
# os.remove(datadir + file)
# for file in sourceFiles:
# if (os.path.exists(datadir + file)):
# os.remove(datadir + file)

0 comments on commit aa15e60

Please sign in to comment.