import requests import json import os from core.config import GEOSERVER_URL, GEOSERVER_USER, GEOSERVER_PASS, GEOSERVER_WORKSPACE # DATASTORE = "postgis" #per OPD DATASTORE = "server_lokal" # SLD_DIR = "./styles" # BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # SLD_DIR = os.path.join(BASE_DIR, "styles") BASE_DIR = os.path.dirname(os.path.abspath(__file__)) MAIN_DIR = os.path.abspath(os.path.join(BASE_DIR, "..", "..")) SLD_DIR = os.path.join(MAIN_DIR, "style_temp") def publish_layer_to_geoserver(table: str, job_id: str): print(f"[GeoServer] Publish layer + upload SLD: {table}") # ========================== # 1. Publish Feature Type # ========================== # ft_url = f"{GEOSERVER_URL}/rest/workspaces/{GEOSERVER_WORKSPACE}/datastores/{DATASTORE}/featuretypes" ft_url = f"{GEOSERVER_URL}/rest/workspaces/{GEOSERVER_WORKSPACE}/datastores/{DATASTORE}/featuretypes?computeDefault=true" payload = { "featureType": { "name": table, "nativeName": table, "enabled": True } } requests.post( ft_url, auth=(GEOSERVER_USER, GEOSERVER_PASS), headers={"Content-Type": "application/json"}, data=json.dumps(payload) ) print(f"[GeoServer] FeatureType published for: {table}") # ========================================== # 2. Upload SLD file to GeoServer # ========================================== sld_file = f"{SLD_DIR}/{job_id}.sld" style_name = table # style name sama dengan table if not os.path.exists(sld_file): print(f"[WARNING] SLD file tidak ditemukan: {sld_file}") else: print(f"[GeoServer] Upload SLD {sld_file}") #old # style_url = f"{GEOSERVER_URL}/rest/styles" # with open(sld_file, "rb") as sld: # requests.post( # f"{style_url}?name={style_name}&workspace={GEOSERVER_WORKSPACE}", # auth=(GEOSERVER_USER, GEOSERVER_PASS), # headers={"Content-Type": "application/vnd.ogc.sld+xml"}, # data=sld.read() # ) # print(f"[GeoServer] SLD uploaded: {style_name}") #new style_url = ( f"{GEOSERVER_URL}/rest/workspaces/" f"{GEOSERVER_WORKSPACE}/styles" ) with open(sld_file, "r", encoding="utf-8") as f: sld_content = f.read() # 🔥 INI BARIS PENTINGNYA sld_content = sld_content.lstrip("\ufeff \t\r\n") resp = requests.post( f"{style_url}?name={style_name}", auth=(GEOSERVER_USER, GEOSERVER_PASS), headers={"Content-Type": "application/vnd.ogc.sld+xml"}, data=sld_content.encode("utf-8") ) if resp.status_code not in (200, 201): raise Exception( f"Upload SLD gagal ({resp.status_code}): {resp.text}" ) print(f"[GeoServer] SLD uploaded: {style_name}") # ========================================== # 3. Apply SLD to the layer # ========================================== layer_url = f"{GEOSERVER_URL}/rest/layers/{GEOSERVER_WORKSPACE}:{table}" payload = { "layer": { "defaultStyle": { "name": style_name, "workspace": GEOSERVER_WORKSPACE }, "enabled": True } } requests.put( layer_url, auth=(GEOSERVER_USER, GEOSERVER_PASS), headers={"Content-Type": "application/json"}, data=json.dumps(payload) ) print(f"[GeoServer] SLD applied as default style for {table}") # ========================================== # 4. Delete SLD file from local folder # ========================================== os.remove(sld_file) print(f"[CLEANUP] SLD file removed: {sld_file}") # ============================================== # 5. Reload GeoServer (optional but recommended) # ============================================== requests.post( f"{GEOSERVER_URL}/rest/reload", auth=(GEOSERVER_USER, GEOSERVER_PASS) ) # ==================================================== # 7. Generate GeoServer WMS/WFS link untuk GeoNetwork # ==================================================== wms_link = ( f"{GEOSERVER_URL}/{GEOSERVER_WORKSPACE}/wms?" f"service=WMS&request=GetMap&layers={GEOSERVER_WORKSPACE}:{table}" ) wfs_link = ( f"{GEOSERVER_URL}/{GEOSERVER_WORKSPACE}/wfs?" f"service=WFS&request=GetFeature&typeName={GEOSERVER_WORKSPACE}:{table}" ) # print(f"[GeoServer] WMS URL: {wms_link}") # print(f"[GeoServer] WFS URL: {wfs_link}") # print(f"[GeoServer] Reload completed. Layer {table} ready.") openlayer_url = ( f"{GEOSERVER_URL}/{GEOSERVER_WORKSPACE}/wms?" f"service=WMS" f"&version=1.1.0" f"&request=GetMap" f"&layers={GEOSERVER_WORKSPACE}:{table}" f"&styles=" f"&bbox=110.89528623700005%2C-8.780412043999945%2C116.26994997700001%2C-5.042971664999925" f"&width=768" f"&height=384" f"&srs=EPSG:4326" f"&format=application/openlayers" ) return { "table": table, "style": style_name, "wms_url": wms_link, "wfs_url": wfs_link, "layer_url": openlayer_url } # use default style # def publish_layer_to_geoserver(table: str): # print(f"[GeoServer] Publish layer: {table}") # # ========== 1. Publish Feature Type ========== # ft_url = f"{GEOSERVER_URL}/rest/workspaces/{WORKSPACE}/datastores/{DATASTORE}/featuretypes" # payload = { # "featureType": { # "name": table, # "nativeName": table, # "enabled": True # } # } # requests.post( # ft_url, # auth=(GEOSERVER_USER, GEOSERVER_PASS), # headers={"Content-Type": "application/json"}, # data=json.dumps(payload) # ) # # =================================================== # # 2. Tentukan SLD file (prioritas table.sld → fallback default) # # =================================================== # table_sld = SLD_DIR / f"{table}.sld" # default_sld = SLD_DIR / "default_style.sld" # if table_sld.exists(): # chosen_sld = table_sld # delete_after = True # style_name = table # pakai nama style sama dengan layer # print(f"[SLD] Menggunakan SLD khusus: {chosen_sld}") # else: # chosen_sld = default_sld # delete_after = False # style_name = "default_style" # print(f"[SLD] Menggunakan default SLD: {chosen_sld}") # # ========================================== # # 3. Upload SLD # # ========================================== # style_url = f"{GEOSERVER_URL}/rest/styles" # with open(chosen_sld, "rb") as sld: # requests.post( # f"{style_url}?name={style_name}&workspace={WORKSPACE}", # auth=(GEOSERVER_USER, GEOSERVER_PASS), # headers={"Content-Type": "application/vnd.ogc.sld+xml"}, # data=sld.read() # ) # print(f"[GeoServer] SLD uploaded: {style_name}") # # ========================================== # # 4. Apply SLD ke layer # # ========================================== # layer_url = f"{GEOSERVER_URL}/rest/layers/{WORKSPACE}:{table}" # payload = { # "layer": { # "defaultStyle": { # "name": style_name, # "workspace": WORKSPACE # }, # "enabled": True # } # } # requests.put( # layer_url, # auth=(GEOSERVER_USER, GEOSERVER_PASS), # headers={"Content-Type": "application/json"}, # data=json.dumps(payload) # ) # print(f"[GeoServer] Style '{style_name}' applied to layer '{table}'") # # ========================================== # # 5. Delete table.sld jika ada # # ========================================== # if delete_after: # table_sld.unlink() # print(f"[CLEANUP] File SLD '{table}.sld' dihapus") # # ==================================================== # # 6. Reload GeoServer (opsional tapi aman) # # ==================================================== # requests.post( # f"{GEOSERVER_URL}/rest/reload", # auth=(GEOSERVER_USER, GEOSERVER_PASS) # ) # # ==================================================== # # 7. Generate GeoServer WMS/WFS link untuk GeoNetwork # # ==================================================== # wms_link = ( # f"{GEOSERVER_URL}/{WORKSPACE}/wms?" # f"service=WMS&request=GetMap&layers={WORKSPACE}:{table}" # ) # wfs_link = ( # f"{GEOSERVER_URL}/{WORKSPACE}/wfs?" # f"service=WFS&request=GetFeature&typeName={WORKSPACE}:{table}" # ) # print(f"[GeoServer] WMS URL: {wms_link}") # print(f"[GeoServer] WFS URL: {wfs_link}") # return { # "table": table, # "style": style_name, # "wms_url": wms_link, # "wfs_url": wfs_link # }