pyvalhalla-git 3.5.1.post132__cp313-cp313-manylinux_2_28_x86_64.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.
- _valhalla.cpython-313-x86_64-linux-gnu.so +0 -0
- pyvalhalla_git-3.5.1.post132.dist-info/METADATA +137 -0
- pyvalhalla_git-3.5.1.post132.dist-info/RECORD +130 -0
- pyvalhalla_git-3.5.1.post132.dist-info/WHEEL +5 -0
- pyvalhalla_git-3.5.1.post132.dist-info/entry_points.txt +7 -0
- pyvalhalla_git-3.5.1.post132.dist-info/licenses/AUTHORS +5 -0
- pyvalhalla_git-3.5.1.post132.dist-info/licenses/COPYING +22 -0
- pyvalhalla_git-3.5.1.post132.dist-info/licenses/LICENSE.md +1 -0
- pyvalhalla_git-3.5.1.post132.dist-info/top_level.txt +2 -0
- pyvalhalla_git.libs/libaec-91f0ca0f.so.0.0.8 +0 -0
- pyvalhalla_git.libs/libarmadillo-b35ffa1e.so.12.6.6 +0 -0
- pyvalhalla_git.libs/libarpack-042921fd.so.2.1.0 +0 -0
- pyvalhalla_git.libs/libblas-fe34f726.so.3.8.0 +0 -0
- pyvalhalla_git.libs/libbrotlicommon-6ce2a53c.so.1.0.6 +0 -0
- pyvalhalla_git.libs/libbrotlidec-811d1be3.so.1.0.6 +0 -0
- pyvalhalla_git.libs/libbsd-526a917f.so.0.12.2 +0 -0
- pyvalhalla_git.libs/libbz2-a1e77c99.so.1.0.6 +0 -0
- pyvalhalla_git.libs/libcfitsio-b4c37dff.so.7.3.47 +0 -0
- pyvalhalla_git.libs/libcom_err-b4375b23.so.2.1 +0 -0
- pyvalhalla_git.libs/libcrypt-52aca757.so.1.1.0 +0 -0
- pyvalhalla_git.libs/libcrypto-bdaed0ea.so.1.1.1k +0 -0
- pyvalhalla_git.libs/libcurl-c96f124b.so.4.5.0 +0 -0
- pyvalhalla_git.libs/libczmq-d9886762.so.4.2.1 +0 -0
- pyvalhalla_git.libs/libdap-7e467616.so.25.0.1 +0 -0
- pyvalhalla_git.libs/libdapclient-5ca79ace.so.6.1.7 +0 -0
- pyvalhalla_git.libs/libdapserver-e7ab5c47.so.7.6.7 +0 -0
- pyvalhalla_git.libs/libdf-2848fafa.so.0.0.0 +0 -0
- pyvalhalla_git.libs/libexpat-d2b8ed35.so.1.6.7 +0 -0
- pyvalhalla_git.libs/libfontconfig-dcb2ce6c.so.1.12.0 +0 -0
- pyvalhalla_git.libs/libfreetype-2f3b32b6.so.6.16.1 +0 -0
- pyvalhalla_git.libs/libfreexl-c4cb0770.so.1.1.0 +0 -0
- pyvalhalla_git.libs/libgdal-bed6323b.so.26.0.4 +0 -0
- pyvalhalla_git.libs/libgeos-40815548.so.3.13.1 +0 -0
- pyvalhalla_git.libs/libgeos_c-386b0945.so.1.19.2 +0 -0
- pyvalhalla_git.libs/libgeotiff-cfdb9fd6.so.5.0.0 +0 -0
- pyvalhalla_git.libs/libgfortran-8f1e9814.so.5.0.0 +0 -0
- pyvalhalla_git.libs/libgif-5ed6ffef.so.7.0.0 +0 -0
- pyvalhalla_git.libs/libgomp-870cb1d0.so.1.0.0 +0 -0
- pyvalhalla_git.libs/libgssapi_krb5-5b6840e5.so.2.2 +0 -0
- pyvalhalla_git.libs/libgta-6ed1f83a.so.1.0.0 +0 -0
- pyvalhalla_git.libs/libhdf5-b5e70f84.so.103.1.0 +0 -0
- pyvalhalla_git.libs/libhdf5_hl-0b60eabd.so.100.1.2 +0 -0
- pyvalhalla_git.libs/libidn2-2f4a5893.so.0.3.6 +0 -0
- pyvalhalla_git.libs/libjasper-61dc4fbf.so.4.0.0 +0 -0
- pyvalhalla_git.libs/libjbig-2504a0c3.so.2.1 +0 -0
- pyvalhalla_git.libs/libjpeg-e87d2658.so.62.2.0 +0 -0
- pyvalhalla_git.libs/libjson-c-53e1d64f.so.4.0.0 +0 -0
- pyvalhalla_git.libs/libk5crypto-404ad962.so.3.1 +0 -0
- pyvalhalla_git.libs/libkeyutils-2777d33d.so.1.6 +0 -0
- pyvalhalla_git.libs/libkmlbase-57a08976.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkmlconvenience-ff3e16e9.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkmldom-137398f0.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkmlengine-aab5dd2a.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkmlregionator-d109603c.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkmlxsd-2dcaca9b.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libkrb5-eda01fcc.so.3.3 +0 -0
- pyvalhalla_git.libs/libkrb5support-123248d0.so.0.1 +0 -0
- pyvalhalla_git.libs/liblapack-31d7d384.so.3.8.0 +0 -0
- pyvalhalla_git.libs/liblber-2-d20824ef.4.so.2.10.9 +0 -0
- pyvalhalla_git.libs/liblcms2-2a3d7c52.so.2.0.8 +0 -0
- pyvalhalla_git.libs/libldap-2-cea2a960.4.so.2.10.9 +0 -0
- pyvalhalla_git.libs/libldap_r-2-99c793ee.4.so.2.10.9 +0 -0
- pyvalhalla_git.libs/libltdl-c869cdfb.so.7 +0 -0
- pyvalhalla_git.libs/libluajit-5-e85ce75d.1.so.2.1.0 +0 -0
- pyvalhalla_git.libs/liblz4-a5753129.so.1.8.3 +0 -0
- pyvalhalla_git.libs/liblzma-51a76f52.so.5.2.4 +0 -0
- pyvalhalla_git.libs/libmariadb-b978418d.so.3 +0 -0
- pyvalhalla_git.libs/libmd-3e096870.so.0.1.0 +0 -0
- pyvalhalla_git.libs/libmfhdf-38b34047.so.0.0.0 +0 -0
- pyvalhalla_git.libs/libminizip-89ecf5b6.so.2.8.9 +0 -0
- pyvalhalla_git.libs/libnetcdf-3915b03a.so.15.0.1 +0 -0
- pyvalhalla_git.libs/libnghttp2-39367a22.so.14.17.0 +0 -0
- pyvalhalla_git.libs/libnspr4-50db7ede.so +0 -0
- pyvalhalla_git.libs/libnss3-97de30de.so +0 -0
- pyvalhalla_git.libs/libnssutil3-6f5c6901.so +0 -0
- pyvalhalla_git.libs/libodbc-cdeefa4a.so.2.0.0 +0 -0
- pyvalhalla_git.libs/libodbcinst-433c34e3.so.2.0.0 +0 -0
- pyvalhalla_git.libs/libogdi-48a138d1.so.4.1 +0 -0
- pyvalhalla_git.libs/libopenblaso-r0-d77a1985.3.15.so +0 -0
- pyvalhalla_git.libs/libopenblasp-r0-59ffcd50.3.15.so +0 -0
- pyvalhalla_git.libs/libopenjp2-88dcb7ba.so.2.4.0 +0 -0
- pyvalhalla_git.libs/libpcre-0dd207b5.so.1.2.10 +0 -0
- pyvalhalla_git.libs/libpcre2-8-516f4c9d.so.0.7.1 +0 -0
- pyvalhalla_git.libs/libpgm-5-d8330baa.2.so.0.0.122 +0 -0
- pyvalhalla_git.libs/libplc4-5da7697c.so +0 -0
- pyvalhalla_git.libs/libplds4-c96adacf.so +0 -0
- pyvalhalla_git.libs/libpng16-748299c7.so.16.34.0 +0 -0
- pyvalhalla_git.libs/libpoppler-2ef877a6.so.104.0.0 +0 -0
- pyvalhalla_git.libs/libpq-a39742ca.so.5.13 +0 -0
- pyvalhalla_git.libs/libprime_server-d1a42131.so.0.6.7 +0 -0
- pyvalhalla_git.libs/libproj-650ed77a.so.15.3.2 +0 -0
- pyvalhalla_git.libs/libpsl-99becdd3.so.5.3.1 +0 -0
- pyvalhalla_git.libs/libquadmath-828275a7.so.0.0.0 +0 -0
- pyvalhalla_git.libs/libsasl2-7de4d792.so.3.0.0 +0 -0
- pyvalhalla_git.libs/libsatlas-6c1f674f.so.3.10 +0 -0
- pyvalhalla_git.libs/libselinux-d0805dcb.so.1 +0 -0
- pyvalhalla_git.libs/libsmime3-374c4dc4.so +0 -0
- pyvalhalla_git.libs/libsodium-2d1e6a3a.so.23.3.0 +0 -0
- pyvalhalla_git.libs/libspatialite-779b5fe7.so.7.1.1 +0 -0
- pyvalhalla_git.libs/libsqlite3-7a6fdc49.so.3.49.2 +0 -0
- pyvalhalla_git.libs/libssh-c11d285b.so.4.8.7 +0 -0
- pyvalhalla_git.libs/libssl-60250281.so.1.1.1k +0 -0
- pyvalhalla_git.libs/libsuperlu-17b63ae5.so.5.1 +0 -0
- pyvalhalla_git.libs/libsz-0deeef7d.so.2.0.1 +0 -0
- pyvalhalla_git.libs/libtiff-84c53c12.so.5.3.0 +0 -0
- pyvalhalla_git.libs/libtirpc-cff449a4.so.3.0.0 +0 -0
- pyvalhalla_git.libs/libunistring-05abdd40.so.2.1.0 +0 -0
- pyvalhalla_git.libs/libunwind-7e4e9a3c.so.8.0.1 +0 -0
- pyvalhalla_git.libs/liburiparser-8aafb6b8.so.1.0.31 +0 -0
- pyvalhalla_git.libs/libuuid-95b83d40.so.1.3.0 +0 -0
- pyvalhalla_git.libs/libwebp-78a6964e.so.7.0.2 +0 -0
- pyvalhalla_git.libs/libxerces-c-3-f7c60809.2.so +0 -0
- pyvalhalla_git.libs/libxml2-54d19b2f.so.2.9.7 +0 -0
- pyvalhalla_git.libs/libz-282eaf2a.so.1.2.11 +0 -0
- pyvalhalla_git.libs/libzmq-a60bbc04.so.5.2.4 +0 -0
- valhalla/__init__.py +11 -0
- valhalla/__main__.py +72 -0
- valhalla/__version__.py +21 -0
- valhalla/_scripts.py +46 -0
- valhalla/_valhalla.cc +81 -0
- valhalla/actor.py +118 -0
- valhalla/bin/valhalla_add_landmarks +0 -0
- valhalla/bin/valhalla_add_predicted_traffic +0 -0
- valhalla/bin/valhalla_build_admins +0 -0
- valhalla/bin/valhalla_build_tiles +0 -0
- valhalla/bin/valhalla_convert_transit +0 -0
- valhalla/bin/valhalla_ingest_transit +0 -0
- valhalla/config.py +45 -0
- valhalla/utils.py +62 -0
- valhalla/valhalla_build_config.py +760 -0
valhalla/__init__.py
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
|
3
|
+
try:
|
4
|
+
from ._valhalla import *
|
5
|
+
except ModuleNotFoundError:
|
6
|
+
from _valhalla import *
|
7
|
+
from .actor import Actor
|
8
|
+
from .config import get_config, get_help
|
9
|
+
from .__version__ import __version__
|
10
|
+
|
11
|
+
PYVALHALLA_DIR = Path(__file__).parent.resolve()
|
valhalla/__main__.py
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
import subprocess
|
2
|
+
import sys
|
3
|
+
from typing import Optional
|
4
|
+
|
5
|
+
from ._scripts import PYVALHALLA_BIN_DIR, run
|
6
|
+
|
7
|
+
try:
|
8
|
+
from .__version__ import version as __version__
|
9
|
+
except ImportError:
|
10
|
+
__version__ = "undefined"
|
11
|
+
|
12
|
+
PRINT_BIN_PATH = "print_bin_path"
|
13
|
+
|
14
|
+
|
15
|
+
def print_help():
|
16
|
+
# left-align executable names
|
17
|
+
exe_names = [p.name for p in PYVALHALLA_BIN_DIR.iterdir()]
|
18
|
+
fixed_width = len(max(exe_names, key=lambda x: len(x)))
|
19
|
+
|
20
|
+
# TODO: we also want the git commit to show here, probably written to __version__.py by setup.py's BDistWheelCommand
|
21
|
+
print(
|
22
|
+
F"""
|
23
|
+
valhalla Python package {__version__}
|
24
|
+
|
25
|
+
pyvalhalla package provides a CLI to run Valhalla C++ executables. Arguments are simply passed on as they are.
|
26
|
+
One notable exception is --quiet/-q, which will forward the C++ executable's stdout to /dev/null.
|
27
|
+
'python -m valhalla --help' will print this help.
|
28
|
+
|
29
|
+
Example usage: 'python -m valhalla --quiet valhalla_build_tiles -c valhalla.json -s initialize -e build test.pbf'
|
30
|
+
|
31
|
+
Available commands (in {PYVALHALLA_BIN_DIR}):
|
32
|
+
\t{PRINT_BIN_PATH:<{fixed_width}} - Print the absolute path of directory containing the C++ executables
|
33
|
+
"""
|
34
|
+
)
|
35
|
+
|
36
|
+
|
37
|
+
def main():
|
38
|
+
if len(sys.argv) < 2:
|
39
|
+
print("[FATAL] command needs arguments, see 'python -m valhalla --help': \n")
|
40
|
+
print_help()
|
41
|
+
sys.exit(1)
|
42
|
+
|
43
|
+
prog_or_opt = sys.argv[1]
|
44
|
+
|
45
|
+
if prog_or_opt in ("--help", "-h"):
|
46
|
+
print_help()
|
47
|
+
sys.exit(0)
|
48
|
+
elif prog_or_opt == PRINT_BIN_PATH:
|
49
|
+
# useful when another script wants to run the executables directly for some reason
|
50
|
+
print(PYVALHALLA_BIN_DIR)
|
51
|
+
else:
|
52
|
+
exc: Optional[Exception] = None
|
53
|
+
try:
|
54
|
+
run(from_main=True)
|
55
|
+
except (FileNotFoundError, subprocess.CalledProcessError) as e:
|
56
|
+
exc = e
|
57
|
+
finally:
|
58
|
+
if not exc:
|
59
|
+
sys.exit(0)
|
60
|
+
|
61
|
+
if isinstance(exc, subprocess.CalledProcessError):
|
62
|
+
print(f"Failed calling command '{exc.cmd}'\n")
|
63
|
+
|
64
|
+
print_help()
|
65
|
+
print("Sub-command failed with:\n")
|
66
|
+
print(exc, file=sys.stderr)
|
67
|
+
|
68
|
+
sys.exit(1)
|
69
|
+
|
70
|
+
|
71
|
+
if __name__ == "__main__":
|
72
|
+
main()
|
valhalla/__version__.py
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# file generated by setuptools-scm
|
2
|
+
# don't change, don't track in version control
|
3
|
+
|
4
|
+
__all__ = ["__version__", "__version_tuple__", "version", "version_tuple"]
|
5
|
+
|
6
|
+
TYPE_CHECKING = False
|
7
|
+
if TYPE_CHECKING:
|
8
|
+
from typing import Tuple
|
9
|
+
from typing import Union
|
10
|
+
|
11
|
+
VERSION_TUPLE = Tuple[Union[int, str], ...]
|
12
|
+
else:
|
13
|
+
VERSION_TUPLE = object
|
14
|
+
|
15
|
+
version: str
|
16
|
+
__version__: str
|
17
|
+
__version_tuple__: VERSION_TUPLE
|
18
|
+
version_tuple: VERSION_TUPLE
|
19
|
+
|
20
|
+
__version__ = version = '3.5.1.post132'
|
21
|
+
__version_tuple__ = version_tuple = (3, 5, 1, 'post132')
|
valhalla/_scripts.py
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from shutil import which
|
3
|
+
import subprocess
|
4
|
+
import sys
|
5
|
+
|
6
|
+
from . import PYVALHALLA_DIR
|
7
|
+
|
8
|
+
PYVALHALLA_BIN_DIR = PYVALHALLA_DIR.joinpath("bin").resolve()
|
9
|
+
|
10
|
+
|
11
|
+
def run(from_main=False) -> None:
|
12
|
+
"""
|
13
|
+
Parses the command line arguments and runs the Valhalla executables with the
|
14
|
+
provided arguments. Note, by default we assume this is not being called by
|
15
|
+
__main__.py via e.g. 'python -m valhalla ...', but directly with e.g.d
|
16
|
+
'valhalla_build_tiles -h'. sys.argv relates to different executables for
|
17
|
+
both scenarios.
|
18
|
+
|
19
|
+
:param from_main: We parse sys.argv differently if it's called from __main__.py
|
20
|
+
"""
|
21
|
+
prog = sys.argv[1] if from_main else Path(sys.argv[0]).name
|
22
|
+
prog_args = sys.argv[2:] if from_main else sys.argv[1:]
|
23
|
+
|
24
|
+
if not which(prog):
|
25
|
+
raise FileNotFoundError(f"Can't find executable at {prog}")
|
26
|
+
prog_path = PYVALHALLA_BIN_DIR.joinpath(prog).resolve()
|
27
|
+
|
28
|
+
print(f"[INFO] Running {prog_path} with args: {prog_args}...")
|
29
|
+
|
30
|
+
# --quiet/-q is our option, don't pass it to the executables
|
31
|
+
is_quiet = False
|
32
|
+
for arg in prog_args:
|
33
|
+
if arg not in ("--quiet", "-q"):
|
34
|
+
continue
|
35
|
+
prog_args.pop(prog_args.index(arg))
|
36
|
+
is_quiet = True
|
37
|
+
|
38
|
+
# recommended way to call exe
|
39
|
+
proc = subprocess.run(
|
40
|
+
[str(prog_path)] + prog_args,
|
41
|
+
stderr=sys.stderr,
|
42
|
+
stdout=subprocess.DEVNULL if is_quiet else sys.stdout,
|
43
|
+
)
|
44
|
+
|
45
|
+
# raises CalledProcessError if not successful
|
46
|
+
proc.check_returncode()
|
valhalla/_valhalla.cc
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
#include "baldr/rapidjson_utils.h"
|
2
|
+
#include "midgard/logging.h"
|
3
|
+
#include "midgard/util.h"
|
4
|
+
#include "tyr/actor.h"
|
5
|
+
|
6
|
+
#include <boost/make_shared.hpp>
|
7
|
+
#include <boost/noncopyable.hpp>
|
8
|
+
#include <boost/property_tree/ptree.hpp>
|
9
|
+
#include <pybind11/pybind11.h>
|
10
|
+
|
11
|
+
#include <string>
|
12
|
+
|
13
|
+
namespace vt = valhalla::tyr;
|
14
|
+
namespace {
|
15
|
+
|
16
|
+
// configuring multiple times is wasteful/ineffectual but not harmful
|
17
|
+
// TODO: make this threadsafe just in case its abused
|
18
|
+
const boost::property_tree::ptree configure(const std::string& config) {
|
19
|
+
boost::property_tree::ptree pt;
|
20
|
+
try {
|
21
|
+
// parse the config and configure logging
|
22
|
+
rapidjson::read_json(config, pt);
|
23
|
+
|
24
|
+
auto logging_subtree = pt.get_child_optional("mjolnir.logging");
|
25
|
+
if (logging_subtree) {
|
26
|
+
auto logging_config = valhalla::midgard::ToMap<const boost::property_tree::ptree&,
|
27
|
+
std::unordered_map<std::string, std::string>>(
|
28
|
+
logging_subtree.get());
|
29
|
+
valhalla::midgard::logging::Configure(logging_config);
|
30
|
+
}
|
31
|
+
} catch (...) { throw std::runtime_error("Failed to load config from: " + config); }
|
32
|
+
|
33
|
+
return pt;
|
34
|
+
}
|
35
|
+
} // namespace
|
36
|
+
|
37
|
+
namespace py = pybind11;
|
38
|
+
|
39
|
+
PYBIND11_MODULE(_valhalla, m) {
|
40
|
+
py::class_<vt::actor_t>(m, "_Actor", "Valhalla Actor class")
|
41
|
+
.def(py::init<>([](std::string config) { return vt::actor_t(configure(config), true); }))
|
42
|
+
.def(
|
43
|
+
"route", [](vt::actor_t& self, std::string& req) { return self.route(req); },
|
44
|
+
"Calculates a route.")
|
45
|
+
.def(
|
46
|
+
"locate", [](vt::actor_t& self, std::string& req) { return self.locate(req); },
|
47
|
+
"Provides information about nodes and edges.")
|
48
|
+
.def(
|
49
|
+
"optimized_route",
|
50
|
+
[](vt::actor_t& self, std::string& req) { return self.optimized_route(req); },
|
51
|
+
"Optimizes the order of a set of waypoints by time.")
|
52
|
+
.def(
|
53
|
+
"matrix", [](vt::actor_t& self, std::string& req) { return self.matrix(req); },
|
54
|
+
"Computes the time and distance between a set of locations and returns them as a matrix table.")
|
55
|
+
.def(
|
56
|
+
"isochrone", [](vt::actor_t& self, std::string& req) { return self.isochrone(req); },
|
57
|
+
"Calculates isochrones and isodistances.")
|
58
|
+
.def(
|
59
|
+
"trace_route", [](vt::actor_t& self, std::string& req) { return self.trace_route(req); },
|
60
|
+
"Map-matching for a set of input locations, e.g. from a GPS.")
|
61
|
+
.def(
|
62
|
+
"trace_attributes",
|
63
|
+
[](vt::actor_t& self, std::string& req) { return self.trace_attributes(req); },
|
64
|
+
"Returns detailed attribution along each portion of a route calculated from a set of input locations, e.g. from a GPS trace.")
|
65
|
+
.def(
|
66
|
+
"height", [](vt::actor_t& self, std::string& req) { return self.height(req); },
|
67
|
+
"Provides elevation data for a set of input geometries.")
|
68
|
+
.def(
|
69
|
+
"transit_available",
|
70
|
+
[](vt::actor_t& self, std::string& req) { return self.transit_available(req); },
|
71
|
+
"Lookup if transit stops are available in a defined radius around a set of input locations.")
|
72
|
+
.def(
|
73
|
+
"expansion", [](vt::actor_t& self, std::string& req) { return self.expansion(req); },
|
74
|
+
"Returns all road segments which were touched by the routing algorithm during the graph traversal.")
|
75
|
+
.def(
|
76
|
+
"centroid", [](vt::actor_t& self, std::string& req) { return self.centroid(req); },
|
77
|
+
"Returns routes from all the input locations to the minimum cost meeting point of those paths.")
|
78
|
+
.def(
|
79
|
+
"status", [](vt::actor_t& self, std::string& req) { return self.status(req); },
|
80
|
+
"Returns nothing or optionally details about Valhalla's configuration.");
|
81
|
+
}
|
valhalla/actor.py
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
import json
|
2
|
+
import tempfile
|
3
|
+
from pathlib import Path
|
4
|
+
from typing import Union
|
5
|
+
|
6
|
+
try:
|
7
|
+
from ._valhalla import _Actor
|
8
|
+
except ModuleNotFoundError:
|
9
|
+
from _valhalla import _Actor
|
10
|
+
|
11
|
+
|
12
|
+
# TODO: wasteful for dict input/output; more reasonable would be to extend
|
13
|
+
# the Actor's action C++ interfaces with a JSON arg
|
14
|
+
def dict_or_str(func):
|
15
|
+
def wrapped(*args):
|
16
|
+
# /status doesn't take any parameters
|
17
|
+
if not len(args) > 1:
|
18
|
+
return func(*args)
|
19
|
+
|
20
|
+
if isinstance(args[1], dict):
|
21
|
+
return json.loads(func(args[0], json.dumps(args[1])))
|
22
|
+
elif not isinstance(args[1], str):
|
23
|
+
raise ValueError("Request must be either of type str or dict")
|
24
|
+
return func(*args)
|
25
|
+
|
26
|
+
return wrapped
|
27
|
+
|
28
|
+
|
29
|
+
class Actor(_Actor):
|
30
|
+
def __init__(self, config: Union[Path, str, dict]):
|
31
|
+
"""
|
32
|
+
Valhalla's Actor class is used to call its actions, like route, isochrone, matrix etc.
|
33
|
+
|
34
|
+
Configuration passed in either by an existing configuration JSON file path or in `dict` form,
|
35
|
+
e.g. by calling valhalla.config.get_config(). In the latter case a temp file will be
|
36
|
+
created.
|
37
|
+
|
38
|
+
For details on parameters for each function consult Valhalla's documentation:
|
39
|
+
https://github.com/valhalla/valhalla/blob/master/docs/api
|
40
|
+
"""
|
41
|
+
# make sure there's a valhalla.json file
|
42
|
+
if isinstance(config, dict):
|
43
|
+
with tempfile.NamedTemporaryFile(
|
44
|
+
"w", suffix=".json", prefix="valhalla_config_", delete=False
|
45
|
+
) as f:
|
46
|
+
json.dump(config, f)
|
47
|
+
self._config_path = f.name
|
48
|
+
elif isinstance(config, str):
|
49
|
+
if not Path(config).is_file():
|
50
|
+
raise FileNotFoundError(f"Valhalla JSON config file doesn't exist: {config}")
|
51
|
+
self._config_path = config
|
52
|
+
elif isinstance(config, Path):
|
53
|
+
self._config_path = str(config.resolve())
|
54
|
+
else:
|
55
|
+
raise AttributeError(f"Valhalla JSON config can't be of type {type(config)}")
|
56
|
+
|
57
|
+
# test if there's an extract or tile_dir
|
58
|
+
with open(self._config_path) as f:
|
59
|
+
config = json.load(f)
|
60
|
+
tile_extract_fp = config.get("mjolnir", {}).get("tile_extract")
|
61
|
+
tile_dir = config.get("mjolnir", {}).get("tile_dir")
|
62
|
+
|
63
|
+
# raise if neither exists
|
64
|
+
if not tile_extract_fp and not tile_dir:
|
65
|
+
raise AttributeError(
|
66
|
+
"Valhalla config JSON is not valid: mjolnir.tile_extract and mjolnir.tile_dir are missing."
|
67
|
+
)
|
68
|
+
if not Path(config["mjolnir"]["tile_extract"]).is_file():
|
69
|
+
if not Path(config["mjolnir"]["tile_dir"]).is_dir():
|
70
|
+
raise FileNotFoundError(
|
71
|
+
f"Neither mjolnir.tile_extract ({Path(tile_extract_fp).resolve()}) nor mjolnir.tile_dir ({Path(tile_dir).resolve()}) exists. Can't load graph."
|
72
|
+
)
|
73
|
+
|
74
|
+
super(Actor, self).__init__(self._config_path)
|
75
|
+
|
76
|
+
@dict_or_str
|
77
|
+
def route(self, req: Union[str, dict]):
|
78
|
+
return super().route(req)
|
79
|
+
|
80
|
+
@dict_or_str
|
81
|
+
def locate(self, req: Union[str, dict]):
|
82
|
+
return super().locate(req)
|
83
|
+
|
84
|
+
@dict_or_str
|
85
|
+
def isochrone(self, req: Union[str, dict]):
|
86
|
+
return super().isochrone(req)
|
87
|
+
|
88
|
+
@dict_or_str
|
89
|
+
def matrix(self, req: Union[str, dict]):
|
90
|
+
return super().matrix(req)
|
91
|
+
|
92
|
+
@dict_or_str
|
93
|
+
def trace_route(self, req: Union[str, dict]):
|
94
|
+
return super().trace_route(req)
|
95
|
+
|
96
|
+
@dict_or_str
|
97
|
+
def trace_attributes(self, req: Union[str, dict]):
|
98
|
+
return super().trace_attributes(req)
|
99
|
+
|
100
|
+
@dict_or_str
|
101
|
+
def height(self, req: Union[str, dict]):
|
102
|
+
return super().height(req)
|
103
|
+
|
104
|
+
@dict_or_str
|
105
|
+
def transit_available(self, req: Union[str, dict]):
|
106
|
+
return super().transit_available(req)
|
107
|
+
|
108
|
+
@dict_or_str
|
109
|
+
def expansion(self, req: Union[str, dict]):
|
110
|
+
return super().expansion(req)
|
111
|
+
|
112
|
+
@dict_or_str
|
113
|
+
def centroid(self, req: Union[str, dict]):
|
114
|
+
return super().centroid(req)
|
115
|
+
|
116
|
+
@dict_or_str
|
117
|
+
def status(self, req: Union[str, dict] = ""):
|
118
|
+
return super().status(req)
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
valhalla/config.py
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
from pathlib import Path
|
2
|
+
from typing import Union, Dict
|
3
|
+
|
4
|
+
from .valhalla_build_config import config as default_config, Optional, help_text
|
5
|
+
|
6
|
+
|
7
|
+
def _sanitize_config(dict_: dict = None) -> dict:
|
8
|
+
"""remove the "Optional" values from the config."""
|
9
|
+
int_dict_ = dict_.copy()
|
10
|
+
for k, v in int_dict_.items():
|
11
|
+
if isinstance(v, Optional):
|
12
|
+
del dict_[k]
|
13
|
+
elif isinstance(v, dict):
|
14
|
+
_sanitize_config(v)
|
15
|
+
|
16
|
+
return dict_
|
17
|
+
|
18
|
+
|
19
|
+
def get_help() -> Dict[str, Union[Dict[str, str], str]]:
|
20
|
+
"""
|
21
|
+
Returns the help dictionary with the same keys as the config JSON.
|
22
|
+
"""
|
23
|
+
return help_text
|
24
|
+
|
25
|
+
|
26
|
+
def get_config(
|
27
|
+
tile_extract: Union[str, Path] = "valhalla_tiles.tar",
|
28
|
+
tile_dir: Union[str, Path] = "valhalla_tiles",
|
29
|
+
verbose: bool = False,
|
30
|
+
) -> dict:
|
31
|
+
"""
|
32
|
+
Returns a default Valhalla configuration.
|
33
|
+
|
34
|
+
:param tile_extract: The file path (with .tar extension) of the tile extract (mjolnir.tile_extract), if present. Preferred over tile_dir.
|
35
|
+
:param tile_dir: The directory path where the graph tiles are stored (mjolnir.tile_dir), if present.
|
36
|
+
:param verbose: Whether you want to see Valhalla's logs on stdout (mjolnir.logging). Default False.
|
37
|
+
"""
|
38
|
+
|
39
|
+
config = _sanitize_config(default_config.copy())
|
40
|
+
|
41
|
+
config["mjolnir"]["tile_dir"] = str(Path(tile_dir).resolve())
|
42
|
+
config["mjolnir"]["tile_extract"] = str(Path(tile_extract).resolve())
|
43
|
+
config["mjolnir"]["logging"]["type"] = "std_out" if verbose else ""
|
44
|
+
|
45
|
+
return config
|
valhalla/utils.py
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
from typing import List, Tuple
|
2
|
+
|
3
|
+
|
4
|
+
def decode_polyline(
|
5
|
+
polyline: str, precision: int = 6, order: str = "lnglat"
|
6
|
+
) -> List[Tuple[float, float]]:
|
7
|
+
"""Decodes an encoded ``polyline`` string with ``precision`` to a list of coordinate tuples.
|
8
|
+
The coordinate ``order`` of the output can be ``lnglat`` or ``latlng``."""
|
9
|
+
|
10
|
+
return _decode(polyline, precision=precision, order=order, is3d=False)
|
11
|
+
|
12
|
+
|
13
|
+
def _trans(value, index):
|
14
|
+
"""
|
15
|
+
Copyright (c) 2014 Bruno M. Custódio
|
16
|
+
Copyright (c) 2016 Frederick Jansen
|
17
|
+
https://github.com/hicsail/polyline/commit/ddd12e85c53d394404952754e39c91f63a808656
|
18
|
+
"""
|
19
|
+
byte, result, shift = None, 0, 0
|
20
|
+
|
21
|
+
while byte is None or byte >= 0x20:
|
22
|
+
byte = ord(value[index]) - 63
|
23
|
+
index += 1
|
24
|
+
result |= (byte & 0x1F) << shift
|
25
|
+
shift += 5
|
26
|
+
comp = result & 1
|
27
|
+
|
28
|
+
return ~(result >> 1) if comp else (result >> 1), index
|
29
|
+
|
30
|
+
|
31
|
+
def _decode(expression, precision=5, order="lnglat", is3d=False):
|
32
|
+
"""
|
33
|
+
Copyright (c) 2014 Bruno M. Custódio
|
34
|
+
Copyright (c) 2016 Frederick Jansen
|
35
|
+
https://github.com/hicsail/polyline/commit/ddd12e85c53d394404952754e39c91f63a808656
|
36
|
+
|
37
|
+
Modified to be able to work with 3D polylines and a specified coordinate order.
|
38
|
+
"""
|
39
|
+
coordinates, index, lat, lng, z, length, factor = (
|
40
|
+
[],
|
41
|
+
0,
|
42
|
+
0,
|
43
|
+
0,
|
44
|
+
0,
|
45
|
+
len(expression),
|
46
|
+
float(10**precision),
|
47
|
+
)
|
48
|
+
|
49
|
+
while index < length:
|
50
|
+
lat_change, index = _trans(expression, index)
|
51
|
+
lng_change, index = _trans(expression, index)
|
52
|
+
lat += lat_change
|
53
|
+
lng += lng_change
|
54
|
+
coord = (lat / factor, lng / factor) if order == "latlng" else (lng / factor, lat / factor)
|
55
|
+
if not is3d:
|
56
|
+
coordinates.append(coord)
|
57
|
+
else:
|
58
|
+
z_change, index = _trans(expression, index)
|
59
|
+
z += z_change
|
60
|
+
coordinates.append((*coord, z / 100))
|
61
|
+
|
62
|
+
return coordinates
|