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
|
|
|
""" Module that holds the main OSMAlchemy class. |
29
|
|
|
|
30
|
|
|
The classe encapsulates the model and accompanying logic. |
31
|
|
|
""" |
32
|
|
|
|
33
|
|
|
from .model import _generate_model |
34
|
|
|
from .util import _import_osm_file |
35
|
|
|
|
36
|
|
|
class OSMAlchemy(object): |
37
|
|
|
""" Wrapper class for the OSMAlchemy model and logic |
38
|
|
|
|
39
|
|
|
This class holds all the SQLAlchemy classes and logic that make up |
40
|
|
|
OSMAlchemy. It is contained in a separate class because it is a |
41
|
|
|
template that can be modified as needed by users, e.g. by using a |
42
|
|
|
different table prefix or a different declarative base. |
43
|
|
|
""" |
44
|
|
|
|
45
|
|
|
def __init__(self, base=None, prefix="osm_"): |
46
|
|
|
""" Initialise the table definitions in the wrapper object |
47
|
|
|
|
48
|
|
|
This function generates the OSM element classes as SQLAlchemy table |
49
|
|
|
declaratives. If called without an argument, it uses a newly created |
50
|
|
|
declarative base. |
51
|
|
|
|
52
|
|
|
The base argument, if provided, can be either a declarative base or |
53
|
|
|
a Flask-SQLAlchemy object. |
54
|
|
|
""" |
55
|
|
|
|
56
|
|
|
# Check what we got as declarative base |
57
|
|
|
if base is None: |
58
|
|
|
# Nothing, so create one |
59
|
|
|
self._base = declarative_base() |
60
|
|
|
elif hasattr(base, "Model"): |
61
|
|
|
# Unwrap Flask-SQLAlchemy object if we got one |
62
|
|
|
self._base = base.Model |
63
|
|
|
else: |
64
|
|
|
self._base = base |
65
|
|
|
|
66
|
|
|
# Store prefix |
67
|
|
|
self._prefix = prefix |
68
|
|
|
|
69
|
|
|
# Generate model and store as instance members |
70
|
|
|
self.Node, self.Way, self.Relation, self.Element = _generate_model(self._base, |
71
|
|
|
self._prefix) |
72
|
|
|
|
73
|
|
|
def import_osm_file(self, session, path): |
74
|
|
|
_import_osm_file(self, session, path) |
75
|
|
|
|