Skip to content

Commit

Permalink
new endpoint to insert layer defs
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillipsOwen committed Jan 24, 2025
1 parent 97cd42b commit 79202c0
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ scipy==1.14.1
xarray==2024.10.0
netcdf4==1.7.2
pydap==3.5.1
beautifulsoup4==4.12.3
67 changes: 67 additions & 0 deletions src/common/pg_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@
"""
from datetime import datetime, timedelta
from enum import Enum, EnumType
import json
import dateutil.parser
import pytz
import pandas as pd
import numpy as np
import requests

from bs4 import BeautifulSoup
from urllib.parse import urlparse

from src.common.pg_utils_multi import PGUtilsMultiConnect
from src.common.logger import LoggingUtil
Expand Down Expand Up @@ -54,6 +59,68 @@ def __del__(self):
# clean up connections and cursors
PGUtilsMultiConnect.__del__(self)

def get_wms_xml_data(self, wms_xml_url: str):
"""
Gets/parses a WMS "get capabilities" data from the URL passes and puts it into the DB
:return:
"""
ret_val: int = 0

# get the XML data
response = requests.get(wms_xml_url)

# if it went ok and there is data to parse
if response.status_code == 200 and len(response.content) > 0:
# load the XML data
data = BeautifulSoup(response.text, "xml")

# get all the supported layer types
crs = data.find_all('CRS')

# if the UI supports the layer type
if len([x.text for x in crs if x.text == 'EPSG:3857']) == 1:
# parse/extract the capabilities
source: str = [x.text for x in data.find_all('Title')][0]

# if no source data found
if len(source) == 0:
# use the FQDN
source: str = urlparse(wms_xml_url).netloc

url: str = data.find('OnlineResource').get('xlink:href')
params: dict = {"format": "image/png", "transparent": True, "srs": "EPSG:3857"}

# get all the layers
layers = data.find_all('Layer')

for layer in layers:
if layer.get('queryable') == '1':
# get the title of the layer
name: str = layer.Title.text

# get the layer name
layer_name = layer.Name.text

# insert the data into the DB
sql = (f"SELECT public.insert_external_layers(_name:='{name}', _source:='{source}', _url:='{url}', _layer:='{layer_name}', "
f"_params:='{json.dumps(params)}')")

# insert the layer details
ret_val = self.exec_sql('apsviz', sql)

# check for an insertion error
if ret_val == -1:
break
else:
ret_val = -3
else:
# return a failure code
ret_val = -2

# return to the caller
return ret_val

def get_map_workbench_data(self, **kwargs):
"""
Gets the catalog workbench data
Expand Down
51 changes: 51 additions & 0 deletions src/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,57 @@
NOPP_InstanceNames: EnumType = db_info.get_instance_names('NOPP_InstanceNames', 'nopp')


@APP.get('/get_wms_data', dependencies=[Depends(JWTBearer(security))], status_code=200, response_model=None)
async def get_wms_data(wms_xml_url: str) -> json:
"""
Parses the XML data from a call to get WMS capabilities and puts it in the DB.
<br/>&nbsp;&nbsp;&nbsp;wms_xml_url: The URL for "get capabilities" data.
"""
# init the returned data and HTML status code
ret_val: dict = {}
status_code: int = 200

try:
if len(wms_xml_url) > 0:
# try to make the call for records
ret_val = db_info.get_wms_xml_data(wms_xml_url)

if ret_val == 0:
# set a warning message
ret_val = {'Success': 'The capabilities were successfully installed.'}
# check the return for DB errors
elif ret_val == -1:
ret_val = {'Error': 'Error inserting the data into the database.'}

# set the status to a not found
status_code = 404
# check the return for parsing errors
elif ret_val == -2:
ret_val = {'Error': 'Invalid URL or no capabilities were found.'}
# set the status to a not found
status_code = 404

elif ret_val == -3:
ret_val = {'Error': 'Error parsing the capabilities data.'}
# set the status to a not found
status_code = 404
else:
ret_val = {'Error': 'The URL must be declared.'}

except Exception:
# return a failure message
ret_val = {'Exception': 'Error detected parsing the data.'}

# log the exception
logger.exception(ret_val)

# set the status to a server error
status_code = 500

# return to the caller
return JSONResponse(content=ret_val, status_code=status_code, media_type="application/json")

@APP.get('/get_ui_data', dependencies=[Depends(JWTBearer(security))], status_code=200, response_model=None)
async def get_ui_data(grid_type: Union[str, None] = Query(default=None), event_type: Union[str, None] = Query(default=None),
instance_name: Union[str, None] = Query(default=None), met_class: Union[str, None] = Query(default=None),
Expand Down

0 comments on commit 79202c0

Please sign in to comment.