forked from OCA/geospatial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeo_convertion_helper.py
42 lines (38 loc) · 1.27 KB
/
geo_convertion_helper.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
# Copyright 2011-2012 Nicolas Bessi (Camptocamp SA)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
from odoo import _
try:
import geojson
from shapely import wkb, wkt
from shapely.geometry import shape
from shapely.geometry.base import BaseGeometry
except ImportError:
logger = logging.getLogger(__name__)
logger.warning(_("Shapely or geojson are not available in the sys path"))
def value_to_shape(value, use_wkb=False):
"""Transforms input into a Shapely object"""
if not value:
return wkt.loads("GEOMETRYCOLLECTION EMPTY")
if isinstance(value, str):
# We try to do this before parsing json exception
# exception are ressource costly
if "{" in value:
geo_dict = geojson.loads(value)
return shape(geo_dict)
elif use_wkb:
return wkb.loads(value, hex=True)
else:
return wkt.loads(value)
elif hasattr(value, "wkt"):
if isinstance(value, BaseGeometry):
return value
else:
return wkt.loads(value.wkt)
else:
raise TypeError(
_(
"Write/create/search geo type must be wkt/geojson "
"string or must respond to wkt"
)
)