openerp-community-reviewer team mailing list archive
-
openerp-community-reviewer team
-
Mailing list archive
-
Message #01382
lp:~savoirfairelinux-openerp/geospatial-addons/geoengine_geocoder_ca_geocoder into lp:geospatial-addons
Maxime Chambreuil (http://www.savoirfairelinux.com) has proposed merging lp:~savoirfairelinux-openerp/geospatial-addons/geoengine_geocoder_ca_geocoder into lp:geospatial-addons.
Requested reviews:
Geospatial Addons Core Editors (geospatial-addons-core-editors)
For more details, see:
https://code.launchpad.net/~savoirfairelinux-openerp/geospatial-addons/geoengine_geocoder_ca_geocoder/+merge/195296
--
https://code.launchpad.net/~savoirfairelinux-openerp/geospatial-addons/geoengine_geocoder_ca_geocoder/+merge/195296
Your team Geospatial Addons Core Editors is requested to review the proposed merge of lp:~savoirfairelinux-openerp/geospatial-addons/geoengine_geocoder_ca_geocoder into lp:geospatial-addons.
=== added directory 'geoengine_geocoder_ca_geocoder'
=== added file 'geoengine_geocoder_ca_geocoder/__init__.py'
--- geoengine_geocoder_ca_geocoder/__init__.py 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/__init__.py 2013-11-14 19:37:29 +0000
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# Copyright (C) 2012 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+import geocoder_ca_coder
+import wizard
=== added file 'geoengine_geocoder_ca_geocoder/__openerp__.py'
--- geoengine_geocoder_ca_geocoder/__openerp__.py 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/__openerp__.py 2013-11-14 19:37:29 +0000
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+{'name': 'Auto Geocoding of partners using geocoder.ca',
+ 'version': '0.1',
+ 'category': 'GeoBI',
+ 'description': """ Automatically geocode addresses using http://www.geocoder.ca/ api. """,
+ 'data': ['wizard/bulk_encode_view.xml'],
+ 'author': 'Savoir-faire Linux',
+ 'license': 'AGPL-3',
+ 'website': 'http://www.savoirfairelinux.com',
+ 'depends': ['geoengine_geoname_geocoder'],
+ 'installable': True,
+ 'active': False,
+ 'icon': '/base_geoengine/static/src/images/map_icon.png'}
+# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
=== added file 'geoengine_geocoder_ca_geocoder/geocoder_ca_coder.py'
--- geoengine_geocoder_ca_geocoder/geocoder_ca_coder.py 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/geocoder_ca_coder.py 2013-11-14 19:37:29 +0000
@@ -0,0 +1,118 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+#TODO create a base Geocoder module
+from urllib import urlencode
+from urllib2 import urlopen
+import xml.dom.minidom
+import logging
+
+from shapely.geometry import Point
+
+from osv import fields, osv
+from openerp.osv.osv import except_osv
+from tools.translate import _
+
+
+logger = logging.getLogger('GeoCoder.ca address encoding')
+
+
+class res_partner_address(osv.osv):
+ """Auto geo coding of addresses"""
+ _name = "res.partner.address"
+ _inherit = "res.partner.address"
+
+ def _get_point_from_reply(self, answer):
+ """Parse geoname answer code inspired by geopy library"""
+
+ def get_first_text(node, tag_names, strip=None):
+ """Get the text value of the first child of ``node`` with tag
+ ``tag_name``. The text is stripped using the value of ``strip``."""
+ if isinstance(tag_names, basestring):
+ tag_names = [tag_names]
+ if node:
+ while tag_names:
+ nodes = node.getElementsByTagName(tag_names.pop(0))
+ if nodes:
+ child = nodes[0].firstChild
+ return child and child.nodeValue.strip(strip)
+
+ def parse_code(code):
+ latitude = get_first_text(code, 'latt') or None
+ longitude = get_first_text(code, 'longt') or None
+ latitude = latitude and float(latitude)
+ longitude = longitude and float(longitude)
+ return Point(longitude, latitude)
+
+ res = answer.read()
+ print res
+ if not isinstance(res, basestring):
+ return False
+ doc = xml.dom.minidom.parseString(res)
+ codes = doc.getElementsByTagName('geodata')
+ if len(codes) < 1:
+ return False
+ return parse_code(codes[0])
+
+ def geocode_from_geocoder_ca(self, cursor, uid, ids, srid='900913',
+ strict=True, context=None):
+ context = context or {}
+ base_url = u'http://geocoder.ca?'
+ filters = {}
+ if not isinstance(ids, list):
+ ids = [ids]
+ for add in self.browse(cursor, uid, ids, context):
+ logger.info('geolocalize %s', add.name)
+ if add.country_id.code and (add.city or add.zip):
+ if add.zip:
+ filters[u'postal'] = add.zip.encode('utf-8')
+ filters[u'geoit'] = u'XML'
+ try:
+ url = base_url + urlencode(filters)
+ answer = urlopen(url)
+ print url
+ data = {'geo_point': self._get_point_from_reply(answer)}
+ add.write(data)
+ # We use postgres to do projection in order not to install GDAL dependences
+ sql = ("UPDATE res_partner_address"
+ " SET geo_point = ST_Transform(st_SetSRID(geo_point, 4326), %s)"
+ " WHERE id = %s")
+ cursor.execute(sql, (srid, add.id))
+ except Exception, exc:
+ logger.exception('error while updating geocodes')
+ if strict:
+ raise except_osv(_('Geoencoding fails'), str(exc))
+ return ids
+
+ def write(self, cursor, uid, ids, vals, context=None):
+ res = super(res_partner_address, self).write(cursor, uid, ids, vals, context=None)
+ do_geocode = self._can_geocode(cursor, uid, context)
+ if do_geocode and "country_id" in vals or 'city' in vals or 'zip' in vals:
+ self.geocode_from_geocoder_ca(cursor, uid, ids, context=context)
+ return res
+
+ def create(self, cursor, uid, vals, context=None):
+ res = super(res_partner_address, self).create(cursor, uid, vals, context=None)
+ do_geocode = self._can_geocode(cursor, uid, context=context)
+ if do_geocode:
+ self.geocode_from_geocoder_ca(cursor, uid, res, context=context)
+ return res
+
+res_partner_address()
=== added directory 'geoengine_geocoder_ca_geocoder/i18n'
=== added directory 'geoengine_geocoder_ca_geocoder/wizard'
=== added file 'geoengine_geocoder_ca_geocoder/wizard/__init__.py'
--- geoengine_geocoder_ca_geocoder/wizard/__init__.py 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/wizard/__init__.py 2013-11-14 19:37:29 +0000
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# Author: Nicolas Bessi
+# Copyright 2011-2012 Camptocamp SA
+#
+# This program 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.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# 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 <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+import bulk_encode
=== added file 'geoengine_geocoder_ca_geocoder/wizard/bulk_encode.py'
--- geoengine_geocoder_ca_geocoder/wizard/bulk_encode.py 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/wizard/bulk_encode.py 2013-11-14 19:37:29 +0000
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+# OpenERP, Open Source Management Solution
+# Copyright (C) 2013 Savoir-faire Linux (<http://www.savoirfairelinux.com>).
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from osv import fields, osv
+
+class GeoNameEncoder(osv.osv_memory):
+ _name = "geoengine.geocoder_ca.encoder"
+ _inherit = 'geoengine.geoname.encoder'
+
+ def encode(self, cursor, uid, wiz_id, context=None):
+ add_obj = self.pool.get('res.partner.address')
+ context = context or {}
+ if isinstance(wiz_id, list):
+ wiz_id = wiz_id[0]
+ current = self.browse(cursor, uid, wiz_id, context)
+ if current.encode_all :
+ add_ids = add_obj.search(cursor, uid, [], context=context)
+ add_list = add_obj.browse(cursor, uid, add_ids, context)
+ else:
+ add_list = current.add_to_encode or []
+ for add in add_list:
+ add.geocode_from_geocoder_ca(strict=False)
+ return {}
+GeoNameEncoder()
=== added file 'geoengine_geocoder_ca_geocoder/wizard/bulk_encode_view.xml'
--- geoengine_geocoder_ca_geocoder/wizard/bulk_encode_view.xml 1970-01-01 00:00:00 +0000
+++ geoengine_geocoder_ca_geocoder/wizard/bulk_encode_view.xml 2013-11-14 19:37:29 +0000
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+ <data>
+
+ <record id="geocoder_ca_geocoder_wizard" model="ir.ui.view">
+ <field name="name">geoengine.geocoder_ca.encoder</field>
+ <field name="model">geoengine.geocoder_ca.encoder</field>
+ <field name="inherit_id" ref="geoengine_geoname_geocoder.geonames_geocoder_wizard"/>
+ <field name="type">form</field>
+ <field name="arch" type="xml">
+ <field name="add_to_encode" position="after">
+ <newline />
+ </field>
+ </field>
+ </record>
+
+ <record id="action_geocoder_ca_geocoder_wizard" model="ir.actions.act_window">
+ <field name="name">Geolocalize addresses</field>
+ <field name="type">ir.actions.act_window</field>
+ <field name="res_model">geoengine.geocoder_ca.encoder</field>
+ <field name="view_mode">form</field>
+ <field name="view_id" ref="geocoder_ca_geocoder_wizard"/>
+ </record>
+
+ <menuitem
+ name="Localize addresses from Geocoder.ca"
+ id="actiongeocoder_ca_geocoder_wizard_menu"
+ parent="base_geoengine.geoengine_base_menu"
+ action="action_geocoder_ca_geocoder_wizard"
+ groups="base_geoengine.group_geoengine_admin"
+ />
+
+ </data>
+</openerp>
=== modified file 'geoengine_geoname_geocoder/geonames_coder.py'
--- geoengine_geoname_geocoder/geonames_coder.py 2012-10-01 10:26:49 +0000
+++ geoengine_geoname_geocoder/geonames_coder.py 2013-11-14 19:37:29 +0000
@@ -94,6 +94,8 @@
try:
url = base_url + urlencode(filters)
answer = urlopen(url)
+ print url
+ print answer.readlines()
data = {'geo_point': self._get_point_from_reply(answer)}
add.write(data)
# We use postgres to do projection in order not to install GDAL dependences
Follow ups