Skip to content

Commit

Permalink
feat: overwrite coastlines and coastline table function
Browse files Browse the repository at this point in the history
  • Loading branch information
YingtingChen committed Oct 17, 2024
1 parent 0eca5fc commit 43a5697
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
62 changes: 32 additions & 30 deletions buildings/gui/reference_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"shelter_points",
"bivouac_points",
"protected_areas_polygons",
"coastlines and islands",
"coastlines_and_islands",
"suburb_locality",
]
DATASET_STATSNZ = ["territorial_authority"]
Expand All @@ -52,7 +52,6 @@ def __init__(self, dockwidget, parent=None):
super(UpdateReferenceData, self).__init__(parent)
self.setupUi(self)
self.dockwidget = dockwidget
self.api_key = ""
self.db = db
self.db.connect()
self.error_dialog = None
Expand Down Expand Up @@ -171,9 +170,9 @@ def update_clicked(self, commit_status=True):
# protected areas
if self.chbx_protected_areas.isChecked():
self.topo_layer_processing("protected_areas_polygons")
# coastlines and islands (placeholder)
# coastlines and islands (overwrite the existing table)
if self.chbx_coastline_and_islands.isChecked():
self.message += "The coastlines and islands table must be updated manually"
self.topo_layer_processing("coastlines_and_islands")
# suburb localities
if self.chbx_suburbs.isChecked():
self.admin_bdy_layer_processing("suburb_locality")
Expand Down Expand Up @@ -256,45 +255,48 @@ def request_error(self):
self.error_dialog.show()
QApplication.restoreOverrideCursor()

def topo_layer_processing(self, layer):
"""Processes to run for all topo layers"""
if not self.check_api_key(layer):
def topo_layer_processing(self, dataset):
"""Processes to run for topo layers"""
api_key = self.check_api_key(dataset)
if api_key is None:
return
status = topo50.update_topo50(self.api_key, layer, self.db)
self.update_message(status, layer)
if dataset == "coastlines_and_islands":
status = topo50.update_coastlines_and_islands(api_key, dataset, self.db)
else:
status = topo50.update_topo50(api_key, dataset, self.db)
self.update_message(status, dataset)
if status != "error":
self.updates.append(layer)
self.updates.append(dataset)

def admin_bdy_layer_processing(self, layer):
"""Processes to run for all admin bdy layers"""
if not self.check_api_key(layer):
def admin_bdy_layer_processing(self, dataset):
"""Processes to run for admin bdy layers"""
api_key = self.check_api_key(dataset)
if api_key is None:
return
status = admin_bdys.update_admin_bdys(self.api_key, layer, self.db)
self.update_message(status, layer)
status = admin_bdys.update_admin_bdys(api_key, dataset, self.db)
self.update_message(status, dataset)
if status != "error":
self.updates.append(layer)
if layer == "territorial_authority":
self.updates.append(dataset)
if dataset == "territorial_authority":
self.db.execute_no_commit(reference_select.refresh_ta_grid_view)
self.update_message("updated", "territorial_authority_grid")
self.updates.append("territorial_authority_grid")

def check_api_key(self, layer):
# check for API key
if layer in DATASET_LINZ:
self.api_key = API_KEY_LINZ
return API_KEY_LINZ
elif layer in DATASET_STATSNZ:
self.api_key = API_KEY_STATSNZ
if self.api_key == "":
self.error_dialog = ErrorDialog()
self.error_dialog.fill_report(
"\n------------- NO API KEY -------------"
"\n\nPlease enter a koordinates api key to"
" update the reference data."
)
self.error_dialog.show()
QApplication.restoreOverrideCursor()
return False
return True
return API_KEY_STATSNZ
self.error_dialog = ErrorDialog()
self.error_dialog.fill_report(
"\n------------- NO API KEY -------------"
"\n\nPlease enter a koordinates api key to"
" update the reference data."
)
self.error_dialog.show()
QApplication.restoreOverrideCursor()
return None

def update_message(self, status, name):
"""add to message for display at end of processing"""
Expand Down
21 changes: 21 additions & 0 deletions buildings/reference_data/topo50.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"shelter_points": 50245,
"bivouac_points": 50239,
"protected_areas_polygons": 53564,
"coastlines_and_islands": 51153,
}

LDS_LAYER_HAS_NAME = [
Expand Down Expand Up @@ -165,6 +166,26 @@ def update_topo50(kx_api_key, dataset, dbconn):
return "updated"


def update_coastlines_and_islands(kx_api_key, dataset, dbconn):
if dataset != "coastlines_and_islands":
return "error"
layer = QgsVectorLayer(
"https://data.linz.govt.nz/services;key={1}/wfs?service=WFS&version=2.0.0&request=GetFeature&typeNames=layer-{0}&outputFormat=json".format(
LDS_LAYER_IDS[dataset], kx_api_key
)
)
if not layer.isValid():
return "error"
# clear the table and insert all data via WFS
dbconn.execute_no_commit("DELETE FROM buildings_reference.coastlines_and_islands;")
for feature in layer.getFeatures():
sql = "INSERT INTO buildings_reference.coastlines_and_islands(external_coastline_and_island_id, shape) VALUES (%s, ST_SetSRID(ST_GeometryFromText(%s), 2193))"
dbconn.execute_no_commit(
sql, (feature["TARGET_FID"], feature.geometry().asWkt())
)
return "updated"


def correct_name_format(name):
if not name:
name = ""
Expand Down

0 comments on commit 43a5697

Please sign in to comment.