Left: | ||
Right: |
LEFT | RIGHT |
---|---|
(no file at all) | |
1 # This file is part of trytond_gis. The COPYRIGHT file at the top level of | |
ced
2017/09/20 17:28:20
I find that the file should be:
trytond_gis/model
| |
2 # this repository contains the full copyright notices and license terms. | |
3 from __future__ import absolute_import | |
4 | |
5 from sql import Null | |
6 from sql.operators import Not | |
7 from trytond import backend | |
8 from trytond.model import fields | |
9 from trytond.model.fields.field import SQL_OPERATORS | |
10 | |
11 from trytond_gis import _GeoJSON, sql as geo_sql | |
12 | |
13 GEOGRAPHIC_OPERATORS = { | |
14 '=': geo_sql.ST_Equals, | |
15 '!=': lambda l, r: Not(geo_sql.ST_Equals(l, r)), | |
16 } | |
17 | |
18 | |
19 class Geometry(fields.Field): | |
20 _type = 'geometry' | |
21 _geometry_type = 'GEOMETRY' | |
22 | |
23 def __init__(self, string="", dimension=2, loading='lazy', **kwargs): | |
24 super(Geometry, self).__init__( | |
25 string=string, loading=loading, **kwargs) | |
26 self.dimension = dimension | |
27 | |
28 @property | |
29 def _sql_type(self): | |
30 assert backend.name() in {'postgis'} | |
31 return 'GIS_%s(%s)' % ( | |
32 self._geometry_type, self.dimension) | |
33 | |
34 def sql_format(self, value): | |
35 if isinstance(value, dict): | |
36 return _GeoJSON(value) | |
ced
2017/09/20 17:28:20
I think it should go thought the database sql_form
| |
37 return super(Geometry, self).sql_format(value) | |
38 | |
39 def convert_domain(self, domain, tables, Model): | |
40 table, _ = tables[None] | |
41 name, operator, value = domain | |
42 | |
43 assert operator in GEOGRAPHIC_OPERATORS | |
44 | |
45 column = self.sql_column(table) | |
46 | |
47 if operator in {'=', '!='} and value is Null: | |
48 Operator = SQL_OPERATORS[operator] | |
49 return Operator(column, Null) | |
50 Operator = GEOGRAPHIC_OPERATORS[operator] | |
51 expression = Operator(column, self._domain_value(operator, value)) | |
ced
2017/09/20 17:28:20
I think it will be better to have only one return:
| |
52 | |
53 return expression | |
ced
2017/09/20 17:28:20
Why not return directly the Operator?
| |
54 | |
55 | |
56 class Point(Geometry): | |
57 _geometry_type = 'POINT' | |
58 | |
59 | |
60 class LineString(Geometry): | |
61 _geometry_type = 'LINESTRING' | |
62 | |
63 | |
64 class Polygon(Geometry): | |
65 _geometry_type = 'POLYGON' | |
66 | |
67 | |
68 class MultiPoint(Geometry): | |
69 _geometry_type = 'MULTIPOINT' | |
70 | |
71 | |
72 class MultiLineString(Geometry): | |
73 _geometry_type = 'MULTILINESTRING' | |
74 | |
75 | |
76 class MultiPolygon(Geometry): | |
77 _geometry_type = 'MULTIPOLYGON' | |
78 | |
79 | |
80 class GeometryCollection(Geometry): | |
81 _geometry_type = 'GEOMETRYCOLLECTION' | |
LEFT | RIGHT |