1
|
|
|
# ~*~ coding: utf-8 ~*~ |
2
|
|
|
#- |
3
|
|
|
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge |
4
|
|
|
# Copyright (c) 2016 Dominik George <[email protected]> |
5
|
|
|
# |
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
# in the Software without restriction, including without limitation the rights |
9
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
# furnished to do so, subject to the following conditions: |
12
|
|
|
# |
13
|
|
|
# The above copyright notice and this permission notice shall be included in all |
14
|
|
|
# copies or substantial portions of the Software. |
15
|
|
|
# |
16
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
# SOFTWARE. |
23
|
|
|
# |
24
|
|
|
# Alternatively, you are free to use OSMAlchemy under Simplified BSD, The |
25
|
|
|
# MirOS Licence, GPL-2+, LGPL-2.1+, AGPL-3+ or the same terms as Python |
26
|
|
|
# itself. |
27
|
|
|
|
28
|
1 |
|
""" Module that holds the main OSMAlchemy class. |
29
|
|
|
|
30
|
|
|
The classe encapsulates the model and accompanying logic. |
31
|
|
|
""" |
32
|
|
|
|
33
|
1 |
|
from .model import _generate_model |
34
|
1 |
|
from .online import _generate_overpass_api |
35
|
1 |
|
from .util import _import_osm_file |
36
|
1 |
|
from .triggers import _generate_triggers |
37
|
|
|
|
38
|
1 |
|
class OSMAlchemy(object): |
39
|
|
|
""" Wrapper class for the OSMAlchemy model and logic |
40
|
|
|
|
41
|
|
|
This class holds all the SQLAlchemy classes and logic that make up |
42
|
|
|
OSMAlchemy. It is contained in a separate class because it is a |
43
|
|
|
template that can be modified as needed by users, e.g. by using a |
44
|
|
|
different table prefix or a different declarative base. |
45
|
|
|
""" |
46
|
|
|
|
47
|
1 |
|
def __init__(self, base=None, prefix="osm_", overpass=None, maxage=60*60*24): |
48
|
|
|
""" Initialise the table definitions in the wrapper object |
49
|
|
|
|
50
|
|
|
This function generates the OSM element classes as SQLAlchemy table |
51
|
|
|
declaratives. If called without an argument, it uses a newly created |
52
|
|
|
declarative base. |
53
|
|
|
|
54
|
|
|
The base argument, if provided, can be either a declarative base or |
55
|
|
|
a Flask-SQLAlchemy object. |
56
|
|
|
""" |
57
|
|
|
|
58
|
|
|
# Check what we got as declarative base |
59
|
1 |
|
if base is None: |
60
|
|
|
# Nothing, so create one |
61
|
|
|
self._base = declarative_base() |
|
|
|
|
62
|
1 |
|
elif hasattr(base, "Model"): |
63
|
|
|
# Unwrap Flask-SQLAlchemy object if we got one |
64
|
|
|
self._base = base.Model |
65
|
|
|
else: |
66
|
1 |
|
self._base = base |
67
|
|
|
|
68
|
|
|
# Store prefix |
69
|
1 |
|
self._prefix = prefix |
70
|
|
|
|
71
|
|
|
# Store API endpoint for Overpass |
72
|
1 |
|
if overpass is not None: |
73
|
|
|
self._overpass = _generate_overpass_api(overpass) |
74
|
|
|
else: |
75
|
1 |
|
self._overpass = None |
76
|
|
|
|
77
|
|
|
# Generate model and store as instance members |
78
|
1 |
|
self.Node, self.Way, self.Relation, self.Element = _generate_model(self._base, |
|
|
|
|
79
|
|
|
self._prefix) |
80
|
|
|
|
81
|
|
|
# Add triggers if online functionality is enabled |
82
|
1 |
|
if self._overpass is not None: |
83
|
|
|
_generate_triggers(self, maxage) |
84
|
|
|
|
85
|
1 |
|
def import_osm_file(self, session, path): |
|
|
|
|
86
|
|
|
_import_osm_file(self, session, path) |
87
|
|
|
|