diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-01-18 18:41:39 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2016-01-19 00:18:02 +0100 |
commit | 494aada27e305658ce60b0815fd992857222682b (patch) | |
tree | 17e9fbf51fc054d18ddcd7fabc87e8684daf4eb4 /ishtar_common/scripts | |
parent | f384337ef0d9d2e40d09204f18c4a486e925132c (diff) | |
download | Ishtar-494aada27e305658ce60b0815fd992857222682b.tar.bz2 Ishtar-494aada27e305658ce60b0815fd992857222682b.zip |
Configure for publications on pypi, descriptions, etc.
Diffstat (limited to 'ishtar_common/scripts')
-rw-r--r-- | ishtar_common/scripts/__init__.py | 0 | ||||
-rwxr-xr-x | ishtar_common/scripts/import_from_csv.py | 68 | ||||
-rwxr-xr-x | ishtar_common/scripts/import_towns_from_osm.py | 110 |
3 files changed, 0 insertions, 178 deletions
diff --git a/ishtar_common/scripts/__init__.py b/ishtar_common/scripts/__init__.py deleted file mode 100644 index e69de29bb..000000000 --- a/ishtar_common/scripts/__init__.py +++ /dev/null diff --git a/ishtar_common/scripts/import_from_csv.py b/ishtar_common/scripts/import_from_csv.py deleted file mode 100755 index 9640f1851..000000000 --- a/ishtar_common/scripts/import_from_csv.py +++ /dev/null @@ -1,68 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -""" -Import departements and towns from csv file -""" - -DELIMITER = "," -QUOTECHAR = '"' - -import sys -import csv -sys.path.append('.') - -from django.core.management import setup_environ -import settings - -setup_environ(settings) - -from optparse import OptionParser - -from ishtar_common import models - -def insert_department(value): - idx, label = value - if models.Department.objects.filter(number=idx).count(): - return - models.Department(number=idx, label=label).save() - print idx, label, u" inserted" - -def insert_town(value): - idx, label = value - if models.Town.objects.filter(numero_insee=idx).count(): - return - try: - dpt = models.Department.objects.get(number=idx[:2]) - except: - return - models.Town(numero_insee=idx, name=label, departement=dpt).save() - print idx, label, u" inserted" - -tables = {u"department":insert_department, - u"town":insert_town} - -usage = u"usage: %%prog csv_file.csv table_name\n\n"\ - u"Table name must be in: %s." % u", ".join(tables.keys()) -parser = OptionParser(usage=usage) - -(options, args) = parser.parse_args() - -try: - assert len(args) == 2 -except AssertionError: - parser.error(u"You must provide one csv file and the table name.") - -try: - assert args[1] in tables.keys() -except AssertionError: - parser.error(u"Incorrect table name.") - -try: - values = csv.reader(open(args[0], 'rb'), delimiter=DELIMITER, - quotechar=QUOTECHAR) -except (IOError): - parser.error(u"Incorrect CSV file.") - -for value in values: - tables[args[1]](value) diff --git a/ishtar_common/scripts/import_towns_from_osm.py b/ishtar_common/scripts/import_towns_from_osm.py deleted file mode 100755 index fb301f09f..000000000 --- a/ishtar_common/scripts/import_towns_from_osm.py +++ /dev/null @@ -1,110 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -""" -Import towns from OpenStreetMap data. -Take an OSM xml file for argument. - -To get an OSM file (with a bounding box adapted to your needs): -curl --location --globoff "http://www.informationfreeway.org/api/0.6/node[place=village|town|city][bbox=-5.53711,41.90228,8.96484,51.50874]" -o city.osm -or from a whole xml/pbf export: -./osmosis --read-pbf ~/france-20110125.osm.pbf --node-key-value keyValueList="place.village,place.town,place.city" --write-xml city.osm -""" - -import sys -sys.path.append('.') - -from django.core.management import setup_environ -from django.core.exceptions import ObjectDoesNotExist -from django.contrib.gis.geos import Point -import settings - -setup_environ(settings) - -from optparse import OptionParser -from xml.parsers import expat - -from ishtar_base import models - -usage = "usage: %prog osm_file.xml" -parser = OptionParser(usage=usage) - -(options, args) = parser.parse_args() - -try: - assert len(args) == 1 -except AssertionError: - parser.error("You must provide one XML file") - - -ATTRS = [u"lat", u"lon"] - -# key : (mandatory, [restraint to keys]) -TAGS = {u"place":(True, [u"village", u"town", u"city"]), - u"ref:INSEE":(True, []), - u"population":(False, []) - } - -class TownParser: - - def __init__(self): - self._parser = expat.ParserCreate() - self._parser.returns_unicode = True - self._parser.StartElementHandler = self.start - self._parser.EndElementHandler = self.end - self._parser.CharacterDataHandler = self.data - self.town = {} - self.number = 0 - - def feed(self, data): - self._parser.ParseFile(data) - - def close(self): - self._parser.Parse("", 1) # end of data - del self._parser # get rid of circular references - - def start(self, tag, attrs): - if tag == u"node": - self.town = {} - for attr in ATTRS: - if attr in attrs: - self.town[attr] = attrs[attr] - if tag == u"tag": - if not u"k" in attrs or not u"v" in attrs: - return - if attrs[u"k"] in TAGS: - limit = TAGS[attrs[u"k"]][1] - if limit and \ - (attrs[u"v"] not in limit or \ - (type(limit) == unicode and limit not in attrs[u"v"])): - self.town["DEL"] = True - return - self.town[attrs[u"k"]] = attrs[u"v"] - - def end(self, tag): - if tag == u"node" and self.town and "DEL" not in self.town: - for k in TAGS: - if TAGS[k][0] and k not in self.town: - return - self.number += 1 - try: - town = models.Town.objects.get(numero_insee=self.town["ref:INSEE"]) - except ObjectDoesNotExist: - return - town.center = Point(float(self.town['lon']), float(self.town['lat']), - srid=4326) - town.save() - print town, "updated" - - def data(self, data): - pass - -p = TownParser() - -try: - p.feed(file(args[0])) - print u"%d towns updated" % p.number -except (IOError, expat.ExpatError): - parser.error("Incorrect XML file") - - |