Skip to content

Commit fccd7e7

Browse files
committed
feature: optram example and more
1 parent 5d0c55e commit fccd7e7

17 files changed

Lines changed: 1535 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
venv
44
.vscode
55
.env.development.secrets
6+
dist

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Python client library for deepinspection. Designed to simplify and exemplify interaction with the External API.
44

5+
## Install
6+
7+
Use your preferred package manager:
8+
9+
```bash
10+
poetry add deepinspection
11+
```
12+
13+
or
14+
15+
```bash
16+
pip install deepinspection
17+
```
18+
519
## Usage
620

721
```python
@@ -18,7 +32,9 @@ client = deepinspection.track.client(
1832
exports = client.exports.fastenings.list()
1933

2034
# get export data
21-
for line in client.exports.fastenings.get_data(exports[0]["id"]):
35+
export = exports[0]
36+
37+
for fastening in client.exports.fastenings.get_data(export["id"]):
2238
pass
2339
```
2440

@@ -47,17 +63,3 @@ export_data
4763
{"id": "56fb582a-0280-43fa-81f3-2a444e7e4273", "position_geographical": {"track_section": "111",
4864
...
4965
```
50-
51-
## Installation
52-
53-
Use your preferred package manager:
54-
55-
```bash
56-
poetry add deepinspection
57-
```
58-
59-
or
60-
61-
```bash
62-
pip install deepinspection
63-
```

deepinspection/track/client.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ def get(self, export_id) -> dict:
2424

2525
def get_data(self, export_id) -> Generator[dict, None, None]:
2626
url = f"{self.client.base_url()}/exports/fastenings/{export_id}/data"
27-
response = requests.get(url, headers=self.client.auth_headers(), stream=True)
27+
response = requests.get(
28+
url,
29+
headers={
30+
**self.client.auth_headers(),
31+
"Accept": "application/x-ndjson",
32+
},
33+
stream=True,
34+
)
2835
response.raise_for_status()
2936

3037
for line in response.iter_lines():

examples/optram/.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.9.13

examples/optram/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Optram example
2+
3+
This is an example of how to use the `deepinspection` package and converting fastening data to an Optram compatible format.
4+
5+
## Install
6+
7+
```bash
8+
poetry install
9+
```
10+
11+
## Usage
12+
13+
```python
14+
import deepinspection
15+
import deepinspection_optram
16+
17+
18+
client = deepinspection.track.client(
19+
customer_id="customer-id",
20+
client_id="XYZ",
21+
client_secret="XYZ",
22+
)
23+
24+
# list exports
25+
exports = client.exports.fastenings.list()
26+
27+
# get export data
28+
export = exports[0]
29+
30+
for fastening in client.exports.fastenings.get_data(export["id"]):
31+
optram_fastening = deepinspection_optram.convert(line, export)
32+
```
33+
34+
export_data
35+
36+
```json
37+
{
38+
"id": "71ef13c2-cc4e-4fc2-a2c8-58d2ab6d29b3",
39+
"mätning": "20231010_205621_2011T",
40+
"optrambandel": "400A",
41+
"unespår": "U3",
42+
"start_kilometer": 100,
43+
"slut_kilometer": 100,
44+
"start_meter": 210,
45+
"slut_meter": 210,
46+
"northing": 6580942.200002984,
47+
"easting": 673788.4999998582,
48+
"sida": "Höger",
49+
"in_utsida": "Insida",
50+
"feltyp": "Saknad",
51+
"mätvagn": "2011T",
52+
"mätdatum": "2023-10-10T21:56:21Z",
53+
"leverantör": "InfraNord",
54+
"länk_deepinspection": "https://xyz.track.deepinspection.io/inspection/20230920_215621_2011T/80536",
55+
"kartlänk": "https://portal.gis.trafikverket.local/portal/apps/webappviewer/index.html?id=1b6b638eba0d4e689a3c45ba1d082839&marker=673788.4999998582%3b6580942.200002984%3b3006%3b20230920_215621_2011T%3b%3bBefästning&level=15"
56+
}
57+
{
58+
"id": "71ef13c2-cc4e-4fc2-a2c8-58d2ab6d29b3",
59+
"mätning": "20231010_205621_2011T",
60+
...
61+
}
62+
...
63+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .convert import convert
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from . import mappings
2+
from .map_url import map_url
3+
from .sweref99_coordinates import sweref99_coordinates
4+
5+
6+
def convert(item: dict, export: dict):
7+
sweref99 = sweref99_coordinates(
8+
latitude=item["position_geographical"]["coordinates"][0],
9+
longitude=item["position_geographical"]["coordinates"][1],
10+
)
11+
12+
return {
13+
"id": item["id"],
14+
"mätning": item["measurement"],
15+
"optrambandel": item["position_geographical"]["track_section"],
16+
"unespår": item["position_geographical"]["une_track"],
17+
"start_kilometer": item["position_geographical"]["kilometer"],
18+
"slut_kilometer": item["position_geographical"]["kilometer"],
19+
"start_meter": item["position_geographical"]["meter"],
20+
"slut_meter": item["position_geographical"]["meter"],
21+
"northing": sweref99["northing"],
22+
"easting": sweref99["easting"],
23+
"sida": mappings.RAIL_SIDE[item.pop("rail_side")],
24+
"in_utsida": mappings.RAIL_SIDE_LOCATION[item.pop("rail_side_location")],
25+
"feltyp": mappings.INTEGRITY[item.pop("fastening_integrity")],
26+
"mätvagn": "2011T",
27+
"mätdatum": export["measured"],
28+
"leverantör": "InfraNord",
29+
"länk_deepinspection": item["inspection_url"],
30+
"kartlänk": map_url(item, sweref99, description=mappings.TYPE["fastening"]),
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def map_url(fastening, sweref99, description):
2+
measurement = fastening["measurement"]
3+
northing = sweref99["northing"]
4+
easting = sweref99["easting"]
5+
6+
if northing is None or easting is None:
7+
return None
8+
9+
# TODO: unclear what this should be
10+
unknown_id = "1b6b638eba0d4e689a3c45ba1d082839"
11+
12+
return (
13+
"https://portal.gis.trafikverket.local/portal/apps/webappviewer/index.html?"
14+
f"id={unknown_id}&marker={easting}"
15+
f"%3b{northing}%3b3006%3b"
16+
f"{measurement}%3b%3b"
17+
f"{description}&level=15"
18+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
TYPE = {
2+
"rail_weld": "Svets",
3+
"rail_insulated_joint": "Isolerskarv",
4+
"rail_deformation": "Deformation",
5+
"rail_missing_material": "Materialbortfall",
6+
"fastening": "Befästning",
7+
}
8+
9+
INTEGRITY = {
10+
"missing": "Saknad",
11+
"damaged": "Defekt",
12+
"healthy": "Ok",
13+
}
14+
15+
RAIL_SIDE = {
16+
"right": "Höger",
17+
"left": "Vänster",
18+
"both": "Höger och vänster",
19+
}
20+
21+
RAIL_SIDE_LOCATION = {
22+
"inside": "Insida",
23+
"outside": "Utsida",
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from sweref99 import projections
2+
3+
tm = projections.make_transverse_mercator("SWEREF_99_TM")
4+
5+
6+
def sweref99_coordinates(latitude, longitude):
7+
if latitude is None or longitude is None:
8+
return dict(
9+
northing=None,
10+
easting=None,
11+
)
12+
else:
13+
northing, easting = tm.geodetic_to_grid(latitude, longitude)
14+
return dict(
15+
northing=northing,
16+
easting=easting,
17+
)

0 commit comments

Comments
 (0)