Добавил обработку координат для жижи
This commit is contained in:
parent
227e5bf836
commit
bd3b83ea4c
1 changed files with 76 additions and 1 deletions
|
@ -4,17 +4,92 @@ from io import BytesIO, StringIO
|
|||
import simplekml
|
||||
import gpxpy
|
||||
import re
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
router = APIRouter(prefix="/coords", tags=["Координаты для жижи"])
|
||||
|
||||
|
||||
@router.post(
|
||||
"/coords.txt",
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
async def create_txt(file: UploadFile):
|
||||
try:
|
||||
file = await file.read()
|
||||
file = StringIO(file.decode("utf-8"))
|
||||
txt = generate_txt(file)
|
||||
|
||||
|
||||
txt.seek(0)
|
||||
|
||||
return StreamingResponse(
|
||||
content=txt,
|
||||
media_type="text/plain",
|
||||
headers={"Content-Disposition": "attachment; filename=dildo.txt", "Content-Type": "application/octet-stream"}
|
||||
)
|
||||
|
||||
except:
|
||||
raise HTTPException(status_code=409, detail="Ты передал какую-то хуйню")
|
||||
|
||||
def generate_txt(file: StringIO):
|
||||
tree = ET.parse(file)
|
||||
root = tree.getroot()
|
||||
|
||||
# Пространства имён — нужно указать, иначе findall ничего не найдёт
|
||||
ns = {
|
||||
'kml': 'http://www.opengis.net/kml/2.2',
|
||||
'gx': 'http://www.google.com/kml/ext/2.2'
|
||||
}
|
||||
|
||||
placemarks = root.findall('.//kml:Placemark', ns)
|
||||
|
||||
points = []
|
||||
|
||||
for placemark in placemarks:
|
||||
# Получаем имя точки
|
||||
name_elem = placemark.find('kml:name', ns)
|
||||
name = name_elem.text.strip() if name_elem is not None else "(без названия)"
|
||||
|
||||
# Получаем координаты
|
||||
coord_elem = placemark.find('.//kml:Point/kml:coordinates', ns)
|
||||
if coord_elem is not None and coord_elem.text:
|
||||
lon, lat, *_ = map(float, coord_elem.text.strip().split(','))
|
||||
points.append((name, lat, lon))
|
||||
|
||||
buffer = StringIO()
|
||||
for name, lat, lon in points:
|
||||
point = (lat, lon)
|
||||
buffer.write(f"{convert_point_decimal_to_dms(point)}:{name}\n")
|
||||
|
||||
return buffer
|
||||
|
||||
|
||||
def decimal_to_dms_str(deg, is_lat=True):
|
||||
direction = ''
|
||||
if is_lat:
|
||||
direction = 'N' if deg >= 0 else 'S'
|
||||
else:
|
||||
direction = 'E' if deg >= 0 else 'W'
|
||||
|
||||
deg = abs(deg)
|
||||
d = int(deg)
|
||||
m_float = (deg - d) * 60
|
||||
m = int(m_float)
|
||||
s = round((m_float - m) * 60)
|
||||
|
||||
return f"{d}°{m:02d}'{s:02d}\"{direction}"
|
||||
|
||||
|
||||
def convert_point_decimal_to_dms(point):
|
||||
lat, lon = point
|
||||
return f"{decimal_to_dms_str(lat, True)} {decimal_to_dms_str(lon, False)}"
|
||||
|
||||
|
||||
@router.post(
|
||||
"/map.kmz",
|
||||
status_code=status.HTTP_200_OK,
|
||||
)
|
||||
async def create_chat(track_name: str, file: UploadFile):
|
||||
async def create_kmz(track_name: str, file: UploadFile):
|
||||
try:
|
||||
file = await file.read()
|
||||
file = StringIO(file.decode("utf-8"))
|
||||
|
|
Loading…
Add table
Reference in a new issue