Commit 8acc9832 authored by Pierre Arnaud's avatar Pierre Arnaud

Add script to convert UrbanTransportsPerimeterOfFrance to TransportsTerritorialRessortOfFrance

parent a464fcab
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Territoria -- Hierarchical database of territories
# By: Emmanuel Raviart <>
# Copyright (C) 2008, 2009, 2010, 2011, 2012 Easter-eggs & Emmanuel Raviart
# This file is part of Territoria.
# Territoria is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# Territoria is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <>.
import csv
import logging
import os
import sys
from optparse import OptionParser
from pprint import pprint
import pymongo
from bson import ObjectId
from suq import monpyjama
from territoria2 import conv, territories
app_name = os.path.splitext(os.path.basename(__file__))[0]
log = logging.getLogger(app_name)
def main(argv = None):
if argv is None:
argv = sys.argv
parser = OptionParser("usage: %prog [options] source_file")
parser.add_option('-c', '--collection', default = 'territories', help = "Name of MongoDB collection")
parser.add_option('-d', '--database', default = 'souk', help = "Name of MongoDB database")
parser.add_option('-e', '--encoding', default = 'utf-8', help = 'Encoding of CSV file (default: "utf-8")')
parser.add_option('-s', '--separator', default = ',', help = 'CSV delimiter (default: ",")')
parser.add_option('-v', '--verbose', action = 'store_true', default = False, help = "Display more messages")
options, arguments = parser.parse_args(argv[1:])
logging.basicConfig(level = logging.DEBUG if options.verbose else logging.WARNING, stream = sys.stdout)
monpyjama.Wrapper.db = pymongo.Connection()[options.database]
territories.Territory.collection_name = options.collection
pois = monpyjama.Wrapper.db.pois.find(
# u'_id': ObjectId('4f8c3255de77fe337c000e9a'),
for poi in pois:
if poi.get(u'territories'):
for i, el in enumerate(poi[u'territories']):
for j, elem in enumerate(el):
if poi[u'territories'][i][j][u'kind'] == u'UrbanTransportsPerimeterOfFrance':
poi[u'territories'][i][j][u'kind'] = u'TransportsTerritorialRessortOfFrance'
poi[u'territories'][i][j][u'code'] = poi[u'territories'][i][j][u'code'].replace(u'PTU', u'RT')
if poi[u'metadata'].get(u'territories-index'):
for i, el in enumerate(poi[u'territories']):
if poi[u'metadata'][u'territories-index'][i][u'kind'] == u'UrbanTransportsPerimeterOfFrance':
poi[u'metadata'][u'territories-index'][i][u'kind'] = u'TransportsTerritorialRessortOfFrance'
poi[u'metadata'][u'territories-index'][i][u'code'] = poi[u'territories'][i][j][u'code'].replace(u'PTU', u'RT')
return 0
if __name__ == "__main__":
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment