-
Notifications
You must be signed in to change notification settings - Fork 1
/
mixins.py
75 lines (66 loc) · 2.49 KB
/
mixins.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
from contextlib import contextmanager
try:
from osgeo import ogr
except ImportError:
import ogr
class DataManagerMixin(object):
@staticmethod
def build_connection_string(DB_server,
DB_Name,
DB_user,
DB_Pass,
DB_Port=5432):
connectionString = "host=%s port=%d dbname=%s user=%s password=%s" % (
DB_server, DB_Port, DB_Name, DB_user, DB_Pass)
return connectionString
@staticmethod
@contextmanager
def open_source(source_path, is_postgres=False):
full_path = "PG: " + source_path if is_postgres else source_path
source = ogr.Open(full_path)
yield source
source.FlushCache()
source = None
@staticmethod
def source_layer_exists(source, layername):
layer = source.GetLayerByName(layername)
if layer:
return True
return False
@classmethod
def read_source_schema(cls, source):
layers = cls.get_source_layers(source)
return tuple((layer.name,
layer.get_schema() + layer.geometry_fields_schema())
for layer in layers)
@staticmethod
def get_source_layers(source):
from .layer_manager import GpkgLayer
return [
GpkgLayer(layer, source) for layer in source
if layer.GetName() != "layer_styles"
]
@staticmethod
def compare_schema(layer1, layer2, ignore_case=False):
schema1 = layer1.get_full_schema()
schema2 = layer2.get_full_schema()
if ignore_case:
schema1 = [(field[0].lower(), field[1], field[2])
for field in layer1.get_full_schema()]
schema2 = [(field[0].lower(), field[1], field[2])
for field in layer2.get_full_schema()]
schema1.sort(key=lambda field: field[0])
schema2.sort(key=lambda field: field[0])
new_fields = [field for field in schema1 if field not in schema2]
deleted_fields = [field for field in schema2 if field not in schema1]
deleted_fields.sort(key=lambda field: field[0])
new_fields.sort(key=lambda field: field[0])
return {
"compatible": schema1 == schema2,
"deleted_fields": deleted_fields,
"new_fields": new_fields,
}
@staticmethod
def get_layers_features(layers):
for lyr in layers:
yield lyr.get_features()