forked from OCA/geospatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_db.py
92 lines (81 loc) · 2.81 KB
/
geo_db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Copyright 2011-2012 Nicolas Bessi (Camptocamp SA)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
"""Helper to setup Postgis"""
import logging
from odoo import _
from odoo.exceptions import MissingError
from odoo.tools import sql
logger = logging.getLogger("geoengine.sql")
_schema = logging.getLogger("odoo.schema")
def init_postgis(cr):
"""Initialize postgis
Add PostGIS support to the database. PostGIS is a spatial database
extender for PostgreSQL object-relational database. It adds support for
geographic objects allowing location queries to be run in SQL.
"""
cr.execute(
"""
SELECT
tablename
FROM
pg_tables
WHERE
tablename='spatial_ref_sys';
"""
)
check = cr.fetchone()
if check:
return {}
try:
cr.execute(
"""
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
"""
)
except Exception as exc:
raise MissingError(
_(
"Error, can not automatically initialize spatial postgis"
" support. Database user may have to be superuser and"
" postgres/postgis extensions with their devel header have"
" to be installed. If you do not want Odoo to connect with a"
" super user you can manually prepare your database. To do"
" this, open a client to your database using a super user and"
" run:\n"
"CREATE EXTENSION postgis;\n"
"CREATE EXTENSION postgis_topology;\n"
)
) from exc
def create_geo_column(cr, tablename, columnname, geotype, srid, dim, comment=None):
"""Create a geometry column with the given type.
:params: srid: geometry's projection srid
:params: dim: geometry's dimension (2D or 3D)
"""
cr.execute(
"SELECT AddGeometryColumn( %s, %s, %s, %s, %s)",
(tablename, columnname, srid, geotype, dim),
)
if comment:
# pylint: disable=E8103
cr.execute(
'COMMENT ON COLUMN "{}"."{}" IS %s'.format(tablename, columnname),
(comment,),
)
_schema.debug(
"Table %r: added geometry column %r of type %s", tablename, columnname, geotype
)
def _postgis_index_name(table, col_name):
return "{}_{}_gist_index".format(table, col_name)
def create_geo_index(cr, columnname, tablename):
"""Create the given index unless it exists."""
indexname = _postgis_index_name(tablename, columnname)
if sql.index_exists(cr, indexname):
return
# pylint: disable=E8103
cr.execute(
"CREATE INDEX {} ON {} USING GIST ( {} )".format(
indexname, tablename, columnname
)
)
_schema.debug("Table %r: created index %r", tablename, indexname)