112 lines
3.7 KiB
Python
112 lines
3.7 KiB
Python
import json
|
|
from app.apiController import apiController
|
|
|
|
|
|
def swap_latlon(coords):
|
|
# return [[lon, lat] for lat, lon in coords]
|
|
# print(coords)
|
|
# return [
|
|
# [ [lon, lat] for lat, lon in ring ]
|
|
# for ring in coords
|
|
# ]
|
|
return coords
|
|
return [
|
|
[ # Polygon
|
|
[ # Ring
|
|
[lon, lat] for lat, lon in ring
|
|
] for ring in polygon
|
|
] for polygon in coords
|
|
]
|
|
|
|
|
|
#MAP DATA
|
|
#=========================================================
|
|
def fetch_geojson(conn, level, parent_code=None):
|
|
# cur = conn.cursor()
|
|
# cur.execute(f"SELECT id, kode, nama, lat, lng, path, iso FROM {table_name} WHERE id = 1 LIMIT 1")
|
|
cur = conn.cursor()
|
|
if level == 'provinsi':
|
|
old_sql = """
|
|
SELECT id, kode, nama, lat, lng,
|
|
CONCAT(
|
|
REPEAT('[', 4 - (LENGTH(path) - LENGTH(REPLACE(path, '[', '')))),
|
|
path,
|
|
REPEAT(']', 4 - (LENGTH(path) - LENGTH(REPLACE(path, ']', ''))))
|
|
) AS path
|
|
FROM wil_provinsi
|
|
"""
|
|
cur.execute(old_sql)
|
|
elif level == 'kabupaten':
|
|
cur.execute(""" SELECT id, kode, nama,lat,lng,
|
|
CONCAT(
|
|
REPEAT('[', 4 - (LENGTH(path) - LENGTH(REPLACE(path, '[', '')))),
|
|
path,
|
|
REPEAT(']', 4 - (LENGTH(path) - LENGTH(REPLACE(path, ']', ''))))
|
|
) AS path
|
|
FROM wil_kabupatenkota WHERE provinsi_id = %s""", (parent_code,))
|
|
elif level == 'kecamatan':
|
|
cur.execute("""
|
|
SELECT id, kode, nama,lat,lng,
|
|
CONCAT(
|
|
REPEAT('[', 4 - (LENGTH(path) - LENGTH(REPLACE(path, '[', '')))),
|
|
path,
|
|
REPEAT(']', 4 - (LENGTH(path) - LENGTH(REPLACE(path, ']', ''))))
|
|
) AS path
|
|
FROM wil_kecamatan WHERE kabupatenkota_id = %s""", (parent_code,))
|
|
elif level == 'desa':
|
|
cur.execute("""
|
|
SELECT id, kode, nama, null as lat, null as lng,
|
|
CONCAT(
|
|
REPEAT('[', 4 - (LENGTH(path) - LENGTH(REPLACE(path, '[', '')))),
|
|
path,
|
|
REPEAT(']', 4 - (LENGTH(path) - LENGTH(REPLACE(path, ']', ''))))
|
|
) AS path
|
|
FROM wil_desa WHERE kecamatan_id = %s""", (parent_code,))
|
|
else:
|
|
return {"type": "FeatureCollection", "features": []}
|
|
features = []
|
|
|
|
for row in cur.fetchall():
|
|
path = row[5]
|
|
# path = swap_latlon(path)
|
|
# if not path:
|
|
# continue
|
|
# geometry = {
|
|
# "type": "Polygon",
|
|
# "coordinates": json.loads(path)
|
|
# }
|
|
|
|
#Multi poligons
|
|
try:
|
|
parsed_path = json.loads(path)
|
|
corrected_path = swap_latlon(parsed_path)
|
|
except Exception as e:
|
|
print(f"Error parsing path for row {row[0]}: {e}")
|
|
continue
|
|
|
|
if not corrected_path:
|
|
continue
|
|
|
|
geometry = {
|
|
"type": "MultiPolygon", # karena bentuk datamu seperti itu
|
|
"coordinates": corrected_path
|
|
}
|
|
|
|
feature = {
|
|
"type": "Feature",
|
|
"geometry": geometry,
|
|
"properties": {
|
|
"id": row[0],
|
|
"kode": row[1],
|
|
"nama": row[2],
|
|
"lat": row[3],
|
|
"lng": row[4]
|
|
}
|
|
}
|
|
features.append(feature)
|
|
|
|
return {
|
|
"type": "FeatureCollection",
|
|
"features": features
|
|
}
|