-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap-server.py
More file actions
100 lines (81 loc) · 2.59 KB
/
Copy pathmap-server.py
File metadata and controls
100 lines (81 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from fastapi import FastAPI
def get_map_data(layer: str,
schema_name='public',
table_prefix='route'):
"""
Find the shortest route between two entities (desks or rooms) using their IDs.
Args:
schema_name (str): Database schema name
table_prefix (str): Prefix for the tables
Returns:
list: List of coordinates representing the path
"""
import psycopg2
from psycopg2.extras import RealDictCursor
from shapely import wkt
import geojson
conn_params = {
"dbname": "floorplan",
"user": "postgres",
"password": "postgres",
"host": "localhost",
"port": "5432"
}
try:
conn = psycopg2.connect(**conn_params)
cur = conn.cursor(cursor_factory=RealDictCursor)
if layer == 'floors':
cur.execute(f"""
SELECT name as name, ST_AsText(geom) as geom
FROM {schema_name}.{table_prefix}_{layer}
""")
elif layer == 'desks':
cur.execute(f"""
SELECT asset_id as name, ST_AsText(geom) as geom
FROM {schema_name}.{table_prefix}_assets
WHERE asset_type = 'desk';
""")
elif layer == 'rooms':
cur.execute(f"""
SELECT asset_id as name, ST_AsText(geom) as geom
FROM {schema_name}.{table_prefix}_assets
WHERE asset_type = 'room';
""")
elif layer == 'obstacles':
cur.execute(f"""
SELECT id as name, ST_AsText(geom) as geom
FROM {schema_name}.{table_prefix}_{layer};
""")
records = cur.fetchall()
features = []
for record in records:
shape = wkt.loads(record["geom"])
geo_shape = geojson.Feature(
geometry=shape.__geo_interface__,
properties={
"name": record["name"]
}
)
features.append(geo_shape)
return geojson.FeatureCollection(features)
except Exception as e:
print(f"Error finding route: {str(e)}")
return None
finally:
cur.close()
conn.close()
app = FastAPI()
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/api/geojson/{layer}")
def get_floors(layer: str):
return get_map_data(layer)
if __name__ == "__main__":
import uvicorn
uvicorn.run("map-server:app", host="0.0.0.0", port=8000, reload=True)