cetk 0.0.4.dev0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cetk/__init__.py +87 -0
- cetk/db.py +41 -0
- cetk/edb/__init__.py +0 -0
- cetk/edb/apps.py +9 -0
- cetk/edb/cache.py +249 -0
- cetk/edb/const.py +26 -0
- cetk/edb/copy.py +18 -0
- cetk/edb/exporters.py +551 -0
- cetk/edb/importers/__init__.py +36 -0
- cetk/edb/importers/activity_import.py +279 -0
- cetk/edb/importers/codeset_import.py +124 -0
- cetk/edb/importers/gridsource_import.py +256 -0
- cetk/edb/importers/roadsource_import.py +1602 -0
- cetk/edb/importers/source_import.py +958 -0
- cetk/edb/importers/timevar_import.py +115 -0
- cetk/edb/importers/utils.py +92 -0
- cetk/edb/importers/validation.py +207 -0
- cetk/edb/ltreefield.py +76 -0
- cetk/edb/migrations/0001_initial.py +1731 -0
- cetk/edb/migrations/0002_data.py +156 -0
- cetk/edb/migrations/0003_create_gpkg_tables.py +14 -0
- cetk/edb/migrations/__init__.py +0 -0
- cetk/edb/models/__init__.py +49 -0
- cetk/edb/models/base.py +52 -0
- cetk/edb/models/common_models.py +144 -0
- cetk/edb/models/fleets.py +160 -0
- cetk/edb/models/gridsource_models.py +163 -0
- cetk/edb/models/road_classes.py +197 -0
- cetk/edb/models/road_models.py +413 -0
- cetk/edb/models/source_models.py +399 -0
- cetk/edb/models/timevar_models.py +111 -0
- cetk/edb/rasterize/__init__.py +1 -0
- cetk/edb/rasterize/rasterizer.py +1544 -0
- cetk/edb/signals.py +43 -0
- cetk/edb/traffic.py +75 -0
- cetk/edb/units.py +160 -0
- cetk/emissions/__init__.py +0 -0
- cetk/emissions/aggregate_emissions.sql +303 -0
- cetk/emissions/areasource_emissions.sql +53 -0
- cetk/emissions/calc.py +148 -0
- cetk/emissions/filters.py +179 -0
- cetk/emissions/gridsource_emissions.sql +52 -0
- cetk/emissions/pointsource_emissions.sql +53 -0
- cetk/emissions/queries.py +221 -0
- cetk/emissions/roadsource_emissions.sql +211 -0
- cetk/emissions/used_substances.sql +56 -0
- cetk/emissions/views.py +89 -0
- cetk/logging.py +68 -0
- cetk/settings.py +85 -0
- cetk/tools/__init__.py +1 -0
- cetk/tools/cetk_command.py +599 -0
- cetk/tools/manage.py +24 -0
- cetk/tools/utils.py +334 -0
- cetk/utils.py +62 -0
- cetk-0.0.4.dev0.dist-info/LICENSE +201 -0
- cetk-0.0.4.dev0.dist-info/METADATA +297 -0
- cetk-0.0.4.dev0.dist-info/RECORD +60 -0
- cetk-0.0.4.dev0.dist-info/WHEEL +5 -0
- cetk-0.0.4.dev0.dist-info/entry_points.txt +3 -0
- cetk-0.0.4.dev0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/*Retrieve roads using optional filters */
|
|
2
|
+
WITH sources as (
|
|
3
|
+
SELECT
|
|
4
|
+
id as source_id,
|
|
5
|
+
name as source_name,
|
|
6
|
+
tags,
|
|
7
|
+
aadt,
|
|
8
|
+
heavy_vehicle_share,
|
|
9
|
+
congestion_profile_id,
|
|
10
|
+
nolanes,
|
|
11
|
+
speed,
|
|
12
|
+
width,
|
|
13
|
+
slope,
|
|
14
|
+
roadclass_id,
|
|
15
|
+
fleet_id,
|
|
16
|
+
ST_Transform(geom, {srid}) as geom
|
|
17
|
+
FROM edb_roadsource as sources
|
|
18
|
+
{source_filters}
|
|
19
|
+
),
|
|
20
|
+
/*
|
|
21
|
+
Get vehicles for each road with fraction and timevar.
|
|
22
|
+
If heavy vehicle share is specified on the road, the vehicle
|
|
23
|
+
fractions in the fleet are re-scaled accordingly.
|
|
24
|
+
Emission factors for different fuels are aggregated
|
|
25
|
+
*/
|
|
26
|
+
fleet_veh as (
|
|
27
|
+
SELECT
|
|
28
|
+
fm.fleet_id,
|
|
29
|
+
rc.id as roadclass_id,
|
|
30
|
+
fm.vehicle_id as vehicle_id,
|
|
31
|
+
veh.isheavy,
|
|
32
|
+
veh.max_speed,
|
|
33
|
+
timevar_id,
|
|
34
|
+
coldstart_timevar_id,
|
|
35
|
+
fm.fraction as fraction,
|
|
36
|
+
fm.coldstart_fraction,
|
|
37
|
+
fleet.default_heavy_vehicle_share,
|
|
38
|
+
substance_id,
|
|
39
|
+
freeflow_ef,
|
|
40
|
+
heavy_ef,
|
|
41
|
+
saturated_ef,
|
|
42
|
+
stopngo_ef,
|
|
43
|
+
coldstart_ef
|
|
44
|
+
FROM
|
|
45
|
+
(
|
|
46
|
+
SELECT
|
|
47
|
+
fleet_id,
|
|
48
|
+
fm.id as fleetmember_id,
|
|
49
|
+
veh_ef.traffic_situation_id as traffic_situation,
|
|
50
|
+
fm.vehicle_id,
|
|
51
|
+
veh_ef.substance_id,
|
|
52
|
+
sum(fmf.fraction * veh_ef.freeflow) as freeflow_ef,
|
|
53
|
+
sum(fmf.fraction * veh_ef.heavy) as heavy_ef,
|
|
54
|
+
sum(fmf.fraction * veh_ef.saturated) as saturated_ef,
|
|
55
|
+
sum(fmf.fraction * veh_ef.stopngo) as stopngo_ef,
|
|
56
|
+
sum(fmf.fraction * veh_ef.coldstart) as coldstart_ef
|
|
57
|
+
FROM edb_fleetmember fm
|
|
58
|
+
JOIN edb_fleetmemberfuel as fmf ON fm.id=fmf.fleet_member_id
|
|
59
|
+
JOIN edb_vehicleef as veh_ef
|
|
60
|
+
ON fm.vehicle_id = veh_ef.vehicle_id
|
|
61
|
+
AND fmf.fuel_id = veh_ef.fuel_id
|
|
62
|
+
JOIN edb_fleet as fleet ON fm.fleet_id = fleet.id
|
|
63
|
+
JOIN edb_vehiclefuelcomb as vfc on fm.vehicle_id = vfc.vehicle_id
|
|
64
|
+
AND fmf.fuel_id = vfc.fuel_id
|
|
65
|
+
LEFT JOIN edb_activitycode as ac1 ON vfc.activitycode1_id=ac1.id
|
|
66
|
+
LEFT JOIN edb_activitycode as ac2 ON vfc.activitycode2_id=ac2.id
|
|
67
|
+
LEFT JOIN edb_activitycode as ac3 ON vfc.activitycode3_id=ac3.id
|
|
68
|
+
WHERE veh_ef.substance_id != {traffic_work_subst_id}
|
|
69
|
+
GROUP BY fm.fleet_id, fm.id, veh_ef.traffic_situation_id,
|
|
70
|
+
fm.vehicle_id, veh_ef.substance_id
|
|
71
|
+
UNION
|
|
72
|
+
SELECT
|
|
73
|
+
fleet_id,
|
|
74
|
+
fm.id as fleetmember_id,
|
|
75
|
+
ts.id as traffic_situation,
|
|
76
|
+
fm.vehicle_id,
|
|
77
|
+
{traffic_work_subst_id} as substance_id,
|
|
78
|
+
1.0 as freeflow_ef,
|
|
79
|
+
1.0 as saturated_ef,
|
|
80
|
+
1.0 as congested_ef,
|
|
81
|
+
1.0 as stopngo_ef,
|
|
82
|
+
0.0 as coldstart_ef
|
|
83
|
+
FROM edb_fleetmember fm
|
|
84
|
+
JOIN edb_fleetmemberfuel as fmf ON fm.id=fmf.fleet_member_id
|
|
85
|
+
CROSS JOIN edb_trafficsituation as ts
|
|
86
|
+
JOIN edb_fleet as fleet ON fm.fleet_id = fleet.id
|
|
87
|
+
JOIN edb_vehiclefuelcomb as vfc on fm.vehicle_id = vfc.vehicle_id
|
|
88
|
+
AND fmf.fuel_id = vfc.fuel_id
|
|
89
|
+
LEFT JOIN edb_activitycode as ac1 ON vfc.activitycode1_id=ac1.id
|
|
90
|
+
LEFT JOIN edb_activitycode as ac2 ON vfc.activitycode2_id=ac2.id
|
|
91
|
+
LEFT JOIN edb_activitycode as ac3 ON vfc.activitycode3_id=ac3.id
|
|
92
|
+
) as ef
|
|
93
|
+
JOIN edb_fleetmember fm ON ef.fleetmember_id = fm.id
|
|
94
|
+
JOIN edb_vehicle veh ON ef.vehicle_id=veh.id
|
|
95
|
+
LEFT JOIN edb_roadclass rc
|
|
96
|
+
ON ef.traffic_situation=rc.traffic_situation_id
|
|
97
|
+
JOIN edb_fleet as fleet ON fm.fleet_id = fleet.id
|
|
98
|
+
WHERE (
|
|
99
|
+
ef.freeflow_ef != 0 OR
|
|
100
|
+
ef.coldstart_ef != 0 OR
|
|
101
|
+
ef.stopngo_ef != 0 OR
|
|
102
|
+
ef.heavy_ef != 0 OR
|
|
103
|
+
ef.saturated_ef != 0
|
|
104
|
+
) {ef_substance_filter}
|
|
105
|
+
),
|
|
106
|
+
/*
|
|
107
|
+
Calculate weight of each LOS (Level Of Service) for all valid combinations
|
|
108
|
+
of congestion profile and timevar. A custom sqlite function in python called
|
|
109
|
+
condition_weight is used (see cetk.edb.signals).
|
|
110
|
+
*/
|
|
111
|
+
congestion as (
|
|
112
|
+
SELECT c.id as congestion_profile_id, t.id as timevar_id,
|
|
113
|
+
condition_weight(c.traffic_condition, t.typeday, t.typeday_sum, 1) as freeflow_share,
|
|
114
|
+
condition_weight(c.traffic_condition, t.typeday, t.typeday_sum, 2) as heavy_share,
|
|
115
|
+
condition_weight(c.traffic_condition, t.typeday, t.typeday_sum, 3) as saturated_share,
|
|
116
|
+
condition_weight(c.traffic_condition, t.typeday, t.typeday_sum, 4) as stopngo_share
|
|
117
|
+
FROM (
|
|
118
|
+
SELECT DISTINCT congestion_profile_id, timevar_id
|
|
119
|
+
FROM edb_roadsource AS src
|
|
120
|
+
JOIN edb_fleetmember AS fm ON src.fleet_id = fm.fleet_id
|
|
121
|
+
)as ct
|
|
122
|
+
JOIN edb_flowtimevar as t ON ct.congestion_profile_id = c.id
|
|
123
|
+
JOIN edb_congestionprofile as c ON ct.timevar_id = t.id
|
|
124
|
+
)
|
|
125
|
+
/*
|
|
126
|
+
Calculate emissions for each combination of road, vehicle and substance
|
|
127
|
+
*/
|
|
128
|
+
SELECT source_id, source_name, geom,
|
|
129
|
+
fleet_id, roadclass_id,
|
|
130
|
+
substance_id, substances.slug as substance,
|
|
131
|
+
vehicle_id, vehicle.name as vehicle, isheavy,
|
|
132
|
+
ST_Length(road_veh_ef.geom) as length,
|
|
133
|
+
ST_AsText(road_veh_ef.geom) as wkt,
|
|
134
|
+
timevar_id,
|
|
135
|
+
coldstart_timevar_id,
|
|
136
|
+
congestion_profile_id,
|
|
137
|
+
aadt,
|
|
138
|
+
nolanes,
|
|
139
|
+
speed,
|
|
140
|
+
width,
|
|
141
|
+
slope,
|
|
142
|
+
heavy_vehicle_share,
|
|
143
|
+
fraction,
|
|
144
|
+
veh_m_per_sec,
|
|
145
|
+
(
|
|
146
|
+
road_veh_ef.veh_m_per_sec * (
|
|
147
|
+
freeflow_ef * COALESCE(freeflow_share, 1.0) +
|
|
148
|
+
heavy_ef * COALESCE(heavy_share, 0.0) +
|
|
149
|
+
saturated_ef * COALESCE(saturated_share, 0.0) +
|
|
150
|
+
stopngo_ef * COALESCE(stopngo_share, 0.0) +
|
|
151
|
+
coldstart_ef * coldstart_fraction
|
|
152
|
+
)
|
|
153
|
+
) as emis,
|
|
154
|
+
freeflow_ef, heavy_ef, saturated_ef, stopngo_ef, coldstart_ef,
|
|
155
|
+
freeflow_share, heavy_share, saturated_share, stopngo_share,
|
|
156
|
+
coldstart_fraction, max_speed
|
|
157
|
+
FROM
|
|
158
|
+
(
|
|
159
|
+
SELECT sources.source_id,
|
|
160
|
+
source_name,
|
|
161
|
+
geom,
|
|
162
|
+
sources.fleet_id,
|
|
163
|
+
sources.roadclass_id,
|
|
164
|
+
aadt,
|
|
165
|
+
heavy_vehicle_share,
|
|
166
|
+
nolanes,
|
|
167
|
+
speed,
|
|
168
|
+
width,
|
|
169
|
+
slope,
|
|
170
|
+
fleet_veh.vehicle_id,
|
|
171
|
+
fleet_veh.timevar_id,
|
|
172
|
+
fleet_veh.coldstart_fraction,
|
|
173
|
+
fleet_veh.coldstart_timevar_id,
|
|
174
|
+
fleet_veh.fraction,
|
|
175
|
+
congestion.congestion_profile_id,
|
|
176
|
+
congestion.freeflow_share,
|
|
177
|
+
congestion.heavy_share,
|
|
178
|
+
congestion.saturated_share,
|
|
179
|
+
congestion.stopngo_share,
|
|
180
|
+
fleet_veh.substance_id,
|
|
181
|
+
fleet_veh.freeflow_ef,
|
|
182
|
+
fleet_veh.heavy_ef,
|
|
183
|
+
fleet_veh.saturated_ef,
|
|
184
|
+
fleet_veh.stopngo_ef,
|
|
185
|
+
fleet_veh.coldstart_ef,
|
|
186
|
+
(
|
|
187
|
+
CASE
|
|
188
|
+
WHEN fleet_veh.isheavy=TRUE THEN
|
|
189
|
+
COALESCE(
|
|
190
|
+
sources.heavy_vehicle_share,
|
|
191
|
+
fleet_veh.default_heavy_vehicle_share
|
|
192
|
+
)
|
|
193
|
+
ELSE
|
|
194
|
+
1 - COALESCE(
|
|
195
|
+
sources.heavy_vehicle_share,
|
|
196
|
+
fleet_veh.default_heavy_vehicle_share
|
|
197
|
+
)
|
|
198
|
+
END
|
|
199
|
+
* fleet_veh.fraction * sources.aadt / (3600.0 * 24) * ST_Length(sources.geom)
|
|
200
|
+
) as veh_m_per_sec
|
|
201
|
+
FROM sources
|
|
202
|
+
JOIN fleet_veh
|
|
203
|
+
ON sources.roadclass_id=fleet_veh.roadclass_id
|
|
204
|
+
AND sources.fleet_id=fleet_veh.fleet_id
|
|
205
|
+
LEFT JOIN congestion
|
|
206
|
+
ON congestion.congestion_profile_id=sources.congestion_profile_id
|
|
207
|
+
AND congestion.timevar_id=fleet_veh.timevar_id
|
|
208
|
+
) as road_veh_ef
|
|
209
|
+
JOIN substances ON road_veh_ef.substance_id = substances.id
|
|
210
|
+
JOIN edb_vehicle as vehicle ON vehicle.id = vehicle_id
|
|
211
|
+
ORDER BY source_id
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Similar to inventory_substances.sql in gadget.
|
|
3
|
+
*/
|
|
4
|
+
with point_subst as (
|
|
5
|
+
select distinct psrc_emis.substance_id
|
|
6
|
+
from edb_pointsourcesubstance as psrc_emis
|
|
7
|
+
join edb_pointsource as psrc on psrc.id = psrc_emis.source_id
|
|
8
|
+
),
|
|
9
|
+
area_subst as (
|
|
10
|
+
select distinct asrc_emis.substance_id
|
|
11
|
+
from edb_areasourcesubstance as asrc_emis
|
|
12
|
+
join edb_areasource as asrc on asrc.id = asrc_emis.source_id
|
|
13
|
+
),
|
|
14
|
+
grid_subst as (
|
|
15
|
+
select distinct gsrc_emis.substance_id
|
|
16
|
+
from edb_gridsourcesubstance as gsrc_emis
|
|
17
|
+
join edb_gridsource as gsrc on gsrc.id = gsrc_emis.source_id
|
|
18
|
+
),
|
|
19
|
+
road_subst as (
|
|
20
|
+
select distinct veh_ef.substance_id
|
|
21
|
+
from edb_vehicleef as veh_ef
|
|
22
|
+
),
|
|
23
|
+
source_ef_subst as (
|
|
24
|
+
select distinct ef.substance_id
|
|
25
|
+
from edb_emissionfactor as ef
|
|
26
|
+
join (
|
|
27
|
+
select distinct activity_id
|
|
28
|
+
from (
|
|
29
|
+
select psrc_act.activity_id
|
|
30
|
+
from edb_pointsource as psrc
|
|
31
|
+
join edb_pointsourceactivity as psrc_act on psrc.id = psrc_act.source_id
|
|
32
|
+
union all
|
|
33
|
+
select asrc_act.activity_id
|
|
34
|
+
from edb_areasource as asrc
|
|
35
|
+
join edb_areasourceactivity as asrc_act on asrc.id = asrc_act.source_id
|
|
36
|
+
union all
|
|
37
|
+
select gsrc_act.activity_id
|
|
38
|
+
from edb_gridsource as gsrc
|
|
39
|
+
join edb_gridsourceactivity as gsrc_act on gsrc.id = gsrc_act.source_id
|
|
40
|
+
) as all_act_ids
|
|
41
|
+
) as activity_ids on activity_ids.activity_id = ef.activity_id
|
|
42
|
+
)
|
|
43
|
+
select distinct slug
|
|
44
|
+
from (
|
|
45
|
+
select * from point_subst
|
|
46
|
+
union all
|
|
47
|
+
select * from area_subst
|
|
48
|
+
union all
|
|
49
|
+
select * from grid_subst
|
|
50
|
+
union all
|
|
51
|
+
select * from road_subst
|
|
52
|
+
union all
|
|
53
|
+
select * from source_ef_subst
|
|
54
|
+
) as all_subst
|
|
55
|
+
join substances on substance_id = substances.id
|
|
56
|
+
order by slug
|
cetk/emissions/views.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Create emission calculation views."""
|
|
2
|
+
|
|
3
|
+
from django.db import connection
|
|
4
|
+
|
|
5
|
+
from cetk.edb.const import DEFAULT_EMISSION_UNIT
|
|
6
|
+
from cetk.edb.models import Settings, Substance
|
|
7
|
+
from cetk.edb.units import emis_conversion_factor_from_si
|
|
8
|
+
from cetk.emissions.calc import get_used_substances
|
|
9
|
+
from cetk.emissions.queries import create_source_emis_query
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def make_emission_sql(sourcetype, substances):
|
|
13
|
+
"""Create views for pointsource emissions."""
|
|
14
|
+
settings = Settings.get_current()
|
|
15
|
+
# create point source emission view
|
|
16
|
+
sql = create_source_emis_query(
|
|
17
|
+
sourcetype=sourcetype,
|
|
18
|
+
srid=settings.srid,
|
|
19
|
+
substances=substances,
|
|
20
|
+
)
|
|
21
|
+
return sql
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def create_emission_view(sourcetype, substances, unit=DEFAULT_EMISSION_UNIT):
|
|
25
|
+
"""create emission view with columns source_id, subst1, subst2...substn."""
|
|
26
|
+
sql = make_emission_sql(sourcetype, substances)
|
|
27
|
+
|
|
28
|
+
if sourcetype == "road":
|
|
29
|
+
substances = substances.copy()
|
|
30
|
+
substances.append(Substance.objects.get(slug="traffic_work"))
|
|
31
|
+
|
|
32
|
+
fac = emis_conversion_factor_from_si(unit)
|
|
33
|
+
source_subst_cols = ",".join(
|
|
34
|
+
f'sum(rec.emis*{fac if s.slug != "traffic_work" else 1.0}) FILTER (WHERE rec.substance_id={s.id}) AS "{s.slug}"' # noqa
|
|
35
|
+
for s in substances
|
|
36
|
+
)
|
|
37
|
+
view_sql = f"""\
|
|
38
|
+
CREATE VIEW {sourcetype}source_emissions AS
|
|
39
|
+
SELECT source_id,
|
|
40
|
+
{source_subst_cols}
|
|
41
|
+
FROM (
|
|
42
|
+
{sql}
|
|
43
|
+
) as rec
|
|
44
|
+
GROUP BY source_id
|
|
45
|
+
"""
|
|
46
|
+
cur = connection.cursor()
|
|
47
|
+
cur.execute(f"DROP VIEW IF EXISTS {sourcetype}source_emissions")
|
|
48
|
+
cur.execute(view_sql)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def create_emission_table(sourcetype, substances=None, unit=DEFAULT_EMISSION_UNIT):
|
|
52
|
+
"""create emission table with columns source_id, subst1, subst2...substn."""
|
|
53
|
+
|
|
54
|
+
sql = make_emission_sql(sourcetype, substances)
|
|
55
|
+
fac = emis_conversion_factor_from_si(unit)
|
|
56
|
+
if sourcetype == "road":
|
|
57
|
+
substances = substances.copy()
|
|
58
|
+
substances.append(Substance.objects.get(slug="traffic_work"))
|
|
59
|
+
|
|
60
|
+
if substances is None:
|
|
61
|
+
substances = get_used_substances()
|
|
62
|
+
source_subst_cols = ",".join(
|
|
63
|
+
f'sum(rec.emis*{fac if s.slug != "traffic_work" else 1.0}) FILTER (WHERE rec.substance_id={s.id}) AS "{s.slug}"' # noqa
|
|
64
|
+
for s in substances
|
|
65
|
+
)
|
|
66
|
+
table_sql = (
|
|
67
|
+
f"CREATE TABLE {sourcetype}source_emissions AS SELECT source_id, "
|
|
68
|
+
+ ", ".join([f'cast("{s.slug}" as real) as "{s.slug}"' for s in substances])
|
|
69
|
+
)
|
|
70
|
+
table_sql += f"""
|
|
71
|
+
FROM (
|
|
72
|
+
SELECT source_id,
|
|
73
|
+
{source_subst_cols}
|
|
74
|
+
FROM (
|
|
75
|
+
{sql}
|
|
76
|
+
) as rec
|
|
77
|
+
GROUP BY source_id
|
|
78
|
+
)
|
|
79
|
+
"""
|
|
80
|
+
cur = connection.cursor()
|
|
81
|
+
cur.execute(f"DROP TABLE IF EXISTS {sourcetype}source_emissions")
|
|
82
|
+
# breakpoint()
|
|
83
|
+
cur.execute(table_sql)
|
|
84
|
+
cur.execute(
|
|
85
|
+
f"""
|
|
86
|
+
CREATE INDEX {sourcetype}source_emis_idx
|
|
87
|
+
ON {sourcetype}source_emissions (source_id)
|
|
88
|
+
"""
|
|
89
|
+
)
|
cetk/logging.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"""Utility functions for logging."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
# For convenience
|
|
8
|
+
from logging import CRITICAL, DEBUG, ERROR, INFO, WARNING, getLogger, root # noqa
|
|
9
|
+
|
|
10
|
+
_logLevels = {
|
|
11
|
+
"0": logging.NOTSET,
|
|
12
|
+
"1": logging.WARNING,
|
|
13
|
+
"2": logging.INFO,
|
|
14
|
+
"3": logging.DEBUG,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class CetkHandler(logging.Handler):
|
|
19
|
+
|
|
20
|
+
"""Log handler for cetk."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def create_terminal_handler(loglevel=logging.INFO, prog=None):
|
|
24
|
+
"""Configure a log handler for the terminal."""
|
|
25
|
+
if prog is None:
|
|
26
|
+
prog = os.path.basename(sys.argv[0])
|
|
27
|
+
streamhandler = logging.StreamHandler()
|
|
28
|
+
streamhandler.setLevel(loglevel)
|
|
29
|
+
format = ": ".join((prog, "%(levelname)s", "%(message)s"))
|
|
30
|
+
streamformatter = logging.Formatter(format)
|
|
31
|
+
streamhandler.setFormatter(streamformatter)
|
|
32
|
+
return streamhandler
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def create_cetk_handler(loglevel=logging.INFO):
|
|
36
|
+
"""Configure a handler for the Prepper log."""
|
|
37
|
+
handler = CetkHandler()
|
|
38
|
+
handler.setLevel(loglevel)
|
|
39
|
+
formatter = logging.Formatter("%(message)s")
|
|
40
|
+
handler.setFormatter(formatter)
|
|
41
|
+
return handler
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def create_file_handler(filename, loglevel=logging.INFO):
|
|
45
|
+
"""Configure a log handler for *filename*."""
|
|
46
|
+
filehandler = logging.FileHandler(filename, mode="w", encoding="utf-8")
|
|
47
|
+
filehandler.setLevel(loglevel)
|
|
48
|
+
fmt = "%(asctime)s %(levelname)s %(name)s.%(funcName)s: %(message)s"
|
|
49
|
+
fileformatter = logging.Formatter(fmt)
|
|
50
|
+
filehandler.setFormatter(fileformatter)
|
|
51
|
+
return filehandler
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def configure(terminal_level=logging.WARNING, file_level=logging.INFO, filename=None):
|
|
55
|
+
"""Configure logging.
|
|
56
|
+
|
|
57
|
+
*terminal_level* sets the minimum log level to send to stderr.
|
|
58
|
+
*file_level* sets the minimum log level to send to the file given
|
|
59
|
+
*by *filename*. If *filename* is None, no log records will be
|
|
60
|
+
*sent to file.
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
baselogger = logging.getLogger("cetk")
|
|
64
|
+
baselogger.setLevel(logging.INFO)
|
|
65
|
+
if terminal_level:
|
|
66
|
+
baselogger.addHandler(create_terminal_handler(terminal_level))
|
|
67
|
+
if filename and file_level:
|
|
68
|
+
baselogger.addHandler(create_file_handler(filename, file_level))
|
cetk/settings.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Django settings for the offline-editor."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
DEBUG = os.environ.get("CETK_DEBUG", False)
|
|
6
|
+
|
|
7
|
+
if "FLATPAK_ID" in os.environ:
|
|
8
|
+
SPATIALITE_LIBRARY_PATH = "/app/lib/mod_spatialite.so"
|
|
9
|
+
elif os.name == "posix":
|
|
10
|
+
SPATIALITE_LIBRARY_PATH = "/usr/lib64/mod_spatialite.so"
|
|
11
|
+
elif os.name == "nt":
|
|
12
|
+
SPATIALITE_LIBRARY_PATH = r"C:\OSGeo4W\bin\mod_spatialite.dll"
|
|
13
|
+
else:
|
|
14
|
+
SPATIALITE_LIBRARY_PATH = "/usr/lib64/mod_spatialite.so"
|
|
15
|
+
|
|
16
|
+
# Application definition
|
|
17
|
+
INSTALLED_APPS = [
|
|
18
|
+
"django.contrib.gis",
|
|
19
|
+
"cetk.edb.apps.EdbConfig",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
|
|
23
|
+
|
|
24
|
+
# Database
|
|
25
|
+
# directory where template database will be stored
|
|
26
|
+
DATABASE_DIR = os.path.join(
|
|
27
|
+
os.environ.get("XDG_CONFIG_HOME", os.path.join(os.path.expanduser("~"), ".config")),
|
|
28
|
+
"eclair",
|
|
29
|
+
)
|
|
30
|
+
os.makedirs(DATABASE_DIR, exist_ok=True)
|
|
31
|
+
|
|
32
|
+
# get database path from environment variable
|
|
33
|
+
DATABASES = {
|
|
34
|
+
"default": {
|
|
35
|
+
"ENGINE": "django.contrib.gis.db.backends.spatialite",
|
|
36
|
+
"NAME": os.environ.get(
|
|
37
|
+
"CETK_DATABASE_PATH", os.path.join(DATABASE_DIR, "eclair.gpkg")
|
|
38
|
+
),
|
|
39
|
+
"TEST": {"NAME": os.path.join(DATABASE_DIR, "test.eclair.gpkg")},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
# Internationalization
|
|
44
|
+
LANGUAGE_CODE = "en-us"
|
|
45
|
+
|
|
46
|
+
TIME_ZONE = "UTC"
|
|
47
|
+
|
|
48
|
+
USE_I18N = True
|
|
49
|
+
|
|
50
|
+
USE_TZ = True
|
|
51
|
+
|
|
52
|
+
# Logging
|
|
53
|
+
level = "DEBUG" if DEBUG else "INFO"
|
|
54
|
+
|
|
55
|
+
if level == "DEBUG":
|
|
56
|
+
cetk_logger = {"handlers": ["console_debug"], "level": level}
|
|
57
|
+
else:
|
|
58
|
+
cetk_logger = {"handlers": ["console"], "level": level}
|
|
59
|
+
|
|
60
|
+
LOGGING = {
|
|
61
|
+
"version": 1,
|
|
62
|
+
"disable_existing_loggers": False,
|
|
63
|
+
"formatters": {
|
|
64
|
+
"cetk": {"format": "%(levelname)s: %(message)s"}, # noqa
|
|
65
|
+
"cetk_debug": {
|
|
66
|
+
"format": "%(asctime)s %(levelname)s: %(name)s %(message)s"
|
|
67
|
+
}, # noqa
|
|
68
|
+
},
|
|
69
|
+
"handlers": {
|
|
70
|
+
"console": {"class": "logging.StreamHandler", "formatter": "cetk"},
|
|
71
|
+
"console_debug": {"class": "logging.StreamHandler", "formatter": "cetk_debug"},
|
|
72
|
+
},
|
|
73
|
+
"loggers": {"cetk": cetk_logger},
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if os.name == "nt":
|
|
77
|
+
|
|
78
|
+
OSGEO4W = r"C:\OSGeo4W"
|
|
79
|
+
assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
|
|
80
|
+
os.environ["OSGEO4W_ROOT"] = OSGEO4W
|
|
81
|
+
os.environ["GDAL_DATA"] = OSGEO4W + r"\share\gdal"
|
|
82
|
+
os.environ["PROJ_LIB"] = OSGEO4W + r"\share\proj"
|
|
83
|
+
# TODO this should look for which gdal version is located here, not hardcoded!
|
|
84
|
+
# os.environ['GDAL_LIBRARY_PATH'] = OSGEO4W + r"\bin\gdal308"
|
|
85
|
+
os.environ["PATH"] = OSGEO4W + r"\bin;" + os.environ["PATH"]
|
cetk/tools/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .cetk_command import Editor # noqa
|