Skip to content

Commit

Permalink
Merge pull request #22 from KnowWhereGraph/dev-0.4
Browse files Browse the repository at this point in the history
Dev 0.4
  • Loading branch information
rushirajnenuji authored Oct 18, 2022
2 parents 2afc689 + 9bf5507 commit 3da938e
Show file tree
Hide file tree
Showing 2 changed files with 374 additions and 187 deletions.
120 changes: 100 additions & 20 deletions kwg_geoenrichment/kwg_geoenrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ def __init__(self, iface):
self.eventPlaceTypeDict = dict()
self.kwg_endpoint_dict = _SPARQL_ENDPOINT_DICT

# content dict s-o
self.selectedValDict = {}

# setting up button flags
self.disableGDB = True
self.disableSelectContent = True
Expand Down Expand Up @@ -286,6 +289,7 @@ def run(self):
self.retrievePolygonLayers()

self.enrichmentObjBuffer = []
self.labelObjBuffer = []
self.lineObjBuffer = []
self.comboBoxBuffer = []
self.deleteObjBuffer = []
Expand Down Expand Up @@ -490,6 +494,7 @@ def draw(self):
if ok and not warning:

name = "geo_enrichment_polygon"
self.selectedLayername = "geo_enrichment_polygon"
pjt = QgsProject.instance()

# save the buffer
Expand Down Expand Up @@ -553,12 +558,12 @@ def retrievePolygonLayers(self):
self.dlg.comboBox_layers.currentIndexChanged.connect(lambda: self.handleLayerSelection())

def handleLayerSelection(self):
selectedLayername = self.dlg.comboBox_layers.currentText()
self.selectedLayername = self.dlg.comboBox_layers.currentText()

# enable the select content button
self.updateSelectContent()

self.setUpCaller(layerName=selectedLayername)
self.setUpCaller(layerName=self.selectedLayername)

def getInputs(self, layerName=None):
params = {}
Expand Down Expand Up @@ -649,6 +654,16 @@ def saveContent(self):
selectedVal.append(self.enrichmentObjBuffer[self.contentCounter - 1].tableWidget.cellWidget(i - 1, 2).currentText())


if selectedVal[-1] == "LITERAL" or selectedVal[-1] == "--- SELECT ---":
if selectedVal[0] not in self.selectedValDict:
self.selectedValDict[selectedVal[0]] = []
self.selectedValDict[selectedVal[0]].append(selectedVal[-2])

else:
if selectedVal[0] not in self.selectedValDict:
self.selectedValDict[selectedVal[0]] = []
self.selectedValDict[selectedVal[0]] = selectedVal[-1]

stringVal = " -> ".join(selectedVal)

x, y = self.getPosition(self.dlg.pushButton_content)
Expand All @@ -672,8 +687,11 @@ def displaySelectedContent(self, selectedVal, x, y, i=0):
label = QLabel(stringVal, self.dlg)
label.move(x + 1, y)
label.setAccessibleName("label_%s" % (i))
label.setObjectName("label_%s" % (i))
label.setStyleSheet("background-color: #9AB4D2;")

self.labelObjBuffer.append(label)

# add text box
lineEdit = QLineEdit(self.dlg)
lineEdit.move(x + 1, y + 25)
Expand All @@ -683,6 +701,7 @@ def displaySelectedContent(self, selectedVal, x, y, i=0):
else:
lineEdit.setText(selectedVal[-1])
lineEdit.setAccessibleName("lineEdit_%s" % (i))
lineEdit.setObjectName("lineEdit_%s" % (i))

self.lineObjBuffer.append(lineEdit)

Expand All @@ -702,6 +721,7 @@ def displaySelectedContent(self, selectedVal, x, y, i=0):
comboBox.move(x + 221, y + 25)
comboBox.resize(320, 25)
comboBox.setAccessibleName("comboBox_%s" % (i))
comboBox.setObjectName("comboBox_%s" % (i))

button = QPushButton(self.dlg)
icon = QIcon(":/plugins/kwg_geoenrichment/resources/delete.png")
Expand All @@ -710,35 +730,69 @@ def displaySelectedContent(self, selectedVal, x, y, i=0):
button.resize(32, 25)
button.released.connect(lambda: self.manageContent(i))
button.setStyleSheet("background-color: transparent; color: #ffffff; border: none;")
comboBox.setObjectName("delete_%s" % (i))
button.setAccessibleName("delete_%s" % (i))
button.setObjectName("delete_%s" % (i))

self.deleteObjBuffer.append(button)
self.comboBoxBuffer.append(comboBox)

self.dlg.pushButton_content.move(x + 1, y + 55)

def manageContent(self, buttonIdx):
QgsMessageLog.logMessage('%s Delete Clicked!' % (buttonIdx))
pass

# handle deletion for PYQT widgets
buttonDelete = self.dlg.findChild(QPushButton, "delete_%s" % (buttonIdx))
comboBoxDelete = self.dlg.findChild(QComboBox, "comboBox_%s" % (buttonIdx))
lineDelete = self.dlg.findChild(QLineEdit, "lineEdit_%s" % (buttonIdx))
labelDelete = self.dlg.findChild(QLabel, "label_%s" % (buttonIdx))
buttonDelete.deleteLater()
comboBoxDelete.deleteLater()
lineDelete.deleteLater()
labelDelete.deleteLater()

# update selected val dict
label_val = self.labelObjBuffer[buttonIdx].text().split(" -> ")[0]
line_val = self.lineObjBuffer[buttonIdx].text()
self.selectedValDict[label_val].remove(line_val)

# remove the objects from the buffer
del self.enrichmentObjBuffer[buttonIdx]
del self.labelObjBuffer[buttonIdx]
del self.lineObjBuffer[buttonIdx]
del self.comboBoxBuffer[buttonIdx]
del self.deleteObjBuffer[buttonIdx]

# decrement content counter
self.contentCounter -= 1
if self.contentCounter == 0:
self.disableRun = True

return


def handleRun(self):

if self.disableRun:
self.displayButtonHelp()
return

results = []
objName = []
mergeRuleNo = []
for i in range(self.contentCounter):
results.append(self.enrichmentObjBuffer[i].getResults())
subtype_count = 0
for subtype in self.selectedValDict:
results = []
objName = []
mergeRuleNo = []

for i in range(len(self.selectedValDict[subtype])):

objName.append(self.lineObjBuffer[i].text())
layerName = self.dlg.lineEdit_layerName.text()
mergeRule = self.comboBoxBuffer[i].currentText()
mergeRuleNo.append(int(mergeRule.split(" - ")[0]))
results.append(self.enrichmentObjBuffer[subtype_count + i].getResults())

self.createGeoPackage(results, objName, layerName, mergeRule=mergeRuleNo, out_path=self.path)
objName.append(self.lineObjBuffer[subtype_count + i].text())
layerName = self.dlg.lineEdit_layerName.text()
mergeRule = self.comboBoxBuffer[subtype_count + i].currentText()
mergeRuleNo.append(int(mergeRule.split(" - ")[0]))

subtype_count = subtype_count + len(self.selectedValDict[subtype])
self.createGeoPackage(results, subtype, objName, layerName, mergeRule=mergeRuleNo, out_path=self.path)
self.dlg.close()
self.enrichmentObjBuffer = list()

Expand Down Expand Up @@ -783,8 +837,8 @@ def transformSourceCRStoDestinationCRS(self, geom, src=3857, dest=4326):
geom_reproj = geom_converter.transform(geom)
return geom_reproj

def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results", mergeRule = [],
out_path=None):
def createGeoPackage(self, GeoQueryResult, subtype='', objName=[], layerName="geo_results", mergeRule = [],
out_path=None, input_Layer = None):
'''
GeoQueryResult: a sparql query result json obj serialized as a list of dict()
SPARQL query like this:
Expand All @@ -800,6 +854,9 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
# a set of unique WKT for each found places
out_path += ".gpkg"

if input_Layer == None:
input_Layer = self.selectedLayername

entityDict = {}

for idx in range(len(objName)):
Expand All @@ -818,7 +875,6 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
entityDict[gtype][eValue]["wkt"] = self.updateWkt(gtype, entityDict[gtype][eValue]["wkt"], tempDict[gtype][eValue]["wkt"])

for gtype in entityDict:

layerFields = QgsFields()
layerFields.append(QgsField('entity', QVariant.String))
layerFields.append(QgsField('entityLabel', QVariant.String))
Expand All @@ -838,7 +894,7 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
record.append(entityDict[gtype][entity]["wkt"])
objList.append(record)

vl = QgsVectorLayer(gtype + "?crs=epsg:4326", "%s_%s"%(layerName, gtype), "memory")
vl = QgsVectorLayer(gtype + "?crs=epsg:4326", "%s_%s_%s"%(layerName, subtype, gtype), "memory")
pr = vl.dataProvider()
pr.addAttributes(layerFields)
vl.updateFields()
Expand All @@ -863,6 +919,30 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
geom = QgsGeometry.fromWkt(wkt)

feat.setGeometry(geom)

# check if we need to discard this polygon
matchFound = False
for layer in QgsProject.instance().mapLayers().values():
if layer.name() == input_Layer:
ft = layer.getFeatures()
for f in ft:
geom_poly = f.geometry()

if gtype == "Point":
if geom_poly.contains(geom):
matchFound = True

if gtype == "LineString":
if geom_poly.intersects(geom):
matchFound = True

if gtype == "Polygon":
if geom_poly.intersects(geom):
matchFound = True

if not matchFound:
continue

feat.setAttributes(item[0:-1])

vl.addFeature(feat)
Expand All @@ -871,7 +951,7 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
vl.updateExtents()

options = QgsVectorFileWriter.SaveVectorOptions()
options.layerName = "%s_%s"%(layerName, gtype)
options.layerName = "%s_%s_%s"%(layerName, subtype, gtype)
context = QgsProject.instance().transformContext()
error = QgsVectorFileWriter.writeAsVectorFormatV2(vl, out_path, context, options)
QgsProject.instance().addMapLayer(vl)
Expand Down
Loading

0 comments on commit 3da938e

Please sign in to comment.