forked from catmaid/CATMAID
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0042_volume_tin_representation.py
124 lines (101 loc) · 3.93 KB
/
0042_volume_tin_representation.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-07-17 15:45
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
# Construct a PostGIS TIN representation of a bounding box
bb_vertices = """
(({0}, {2}, {1}, {0})),
(({1}, {2}, {3}, {1})),
(({0}, {1}, {5}, {0})),
(({0}, {5}, {4}, {0})),
(({2}, {6}, {7}, {2})),
(({2}, {7}, {3}, {2})),
(({4}, {7}, {6}, {4})),
(({4}, {5}, {7}, {4})),
(({0}, {6}, {2}, {0})),
(({0}, {4}, {6}, {0})),
(({1}, {3}, {5}, {1})),
(({3}, {7}, {5}, {3}))
""".format(*[
'%{a}$s %{b}$s %{c}$s'.format(**{
'a': 1 if i & 0b001 else 2,
'b': 3 if i & 0b010 else 4,
'c': 5 if i & 0b100 else 6,
})
for i in range(8)
])
forward = """
DO $$
BEGIN
-- Make sure we only deal with polyhedeal surfaces and TINs
IF (SELECT COUNT(*) FROM catmaid_volume
WHERE ST_GeometryType(geometry) NOT IN ('ST_PolyhedralSurface', 'ST_Tin')
LIMIT 1) <> 0
THEN
RAISE EXCEPTION 'Only geometries of type ST_PolyhedralSurface and '
'ST_Tin are supported by CATMAID. Please fix volumes manually.';
END IF;
-- Make sure that all polyhedral surfaces have 30 vertices and six faces,
-- in which case we assume it is a simple box.
IF (SELECT COUNT(*) FROM catmaid_volume
WHERE ST_GeometryType(geometry) = 'ST_PolyhedralSurface'
AND (ST_NPoints(geometry) <> 30 OR ST_NumGeometries(geometry) <> 6)
LIMIT 1) <> 0
THEN
RAISE EXCEPTION 'All polyhedral surfaces need to be boxes, i.e. '
'have 30 vertices and 6 faces. Please fix volumes manually.';
END IF;
END
$$;
SELECT disable_history_tracking_for_table('catmaid_volume'::regclass,
get_history_table_name('catmaid_volume'::regclass));
SELECT drop_history_view_for_table('catmaid_volume'::regclass);
-- Convert polyhedral surfaces to TINs, assuming that we only deal with
-- boxes.
UPDATE catmaid_volume
SET geometry = ST_GeomFromEWKT(FORMAT('TINZ ({bb_vertices})',
ST_XMax(geometry),
ST_XMin(geometry),
ST_YMax(geometry),
ST_YMin(geometry),
ST_ZMax(geometry),
ST_ZMin(geometry)))
WHERE ST_GeometryType(geometry) = 'ST_PolyhedralSurface';
UPDATE catmaid_volume__history
SET geometry = ST_GeomFromEWKT(FORMAT('TINZ ({bb_vertices})',
ST_XMax(geometry),
ST_XMin(geometry),
ST_YMax(geometry),
ST_YMin(geometry),
ST_ZMax(geometry),
ST_ZMin(geometry)))
WHERE ST_GeometryType(geometry) = 'ST_PolyhedralSurface';
ALTER TABLE catmaid_volume
ALTER COLUMN geometry TYPE Geometry(TINZ);
ALTER TABLE catmaid_volume__history
ALTER COLUMN geometry TYPE Geometry(TINZ);
SELECT create_history_view_for_table('catmaid_volume'::regclass);
SELECT enable_history_tracking_for_table('catmaid_volume'::regclass,
get_history_table_name('catmaid_volume'::regclass), FALSE);
""".format(bb_vertices=bb_vertices)
backward = """
SELECT disable_history_tracking_for_table('catmaid_volume'::regclass,
get_history_table_name('catmaid_volume'::regclass));
SELECT drop_history_view_for_table('catmaid_volume'::regclass);
ALTER TABLE catmaid_volume
ALTER COLUMN geometry TYPE Geometry(GeometryZ);
ALTER TABLE catmaid_volume__history
ALTER COLUMN geometry TYPE Geometry(GeometryZ);
SELECT create_history_view_for_table('catmaid_volume'::regclass);
SELECT enable_history_tracking_for_table('catmaid_volume'::regclass,
get_history_table_name('catmaid_volume'::regclass), FALSE);
"""
class Migration(migrations.Migration):
dependencies = [
('catmaid', '0041_add_sampler_column_leaf_handling'),
]
operations = [
migrations.RunSQL(forward, backward)
]