Skip to content

Commit

Permalink
Merge pull request #21 from KnowWhereGraph/dev-0.3
Browse files Browse the repository at this point in the history
Dev 0.3
  • Loading branch information
rushirajnenuji authored Aug 16, 2022
2 parents 21f9d8c + a68bcb7 commit 2afc689
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 109 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,5 @@ kwg_geoenrichment.zip
*.shp
*.shx
*.gpkg
*.gpkg-shm
*.gpkg-wal
91 changes: 62 additions & 29 deletions kwg_geoenrichment/kwg_geoenrichment.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,25 @@
***************************************************************************/
"""
import json
import ntpath
import logging
import os.path
from configparser import ConfigParser
from datetime import time
from qgis.PyQt.QtCore import QTranslator, QSettings, QCoreApplication, QVariant, QObject, pyqtSignal, QTimer, \
from qgis.PyQt.QtCore import QTranslator, QSettings, QCoreApplication, QVariant, \
QThreadPool, QRunnable, pyqtSlot
from qgis.PyQt.QtGui import QIcon
from qgis.PyQt.QtWidgets import QAction, QInputDialog, QLineEdit, QComboBox, QHeaderView, QMessageBox, QCheckBox, \
QLabel, QPushButton
from qgis.PyQt.QtWidgets import QAction, QInputDialog, QLineEdit, QComboBox, QMessageBox, \
QLabel, QPushButton, QFileDialog
from qgis.core import QgsFeature, QgsProject, QgsGeometry, \
QgsCoordinateTransform, QgsCoordinateTransformContext, QgsMapLayer, \
QgsFeatureRequest, QgsVectorLayer, QgsLayerTreeGroup, QgsRenderContext, \
QgsCoordinateReferenceSystem, QgsMessageLog, Qgis, QgsFields, QgsField, QgsVectorFileWriter, QgsLayerTreeLayer, \
QgsWkbTypes
from time import sleep
from multiprocessing import Process

QgsWkbTypes, QgsDataProvider

from typing import re
import statistics

# Import QDraw settings
from PyQt5.uic.properties import QtCore, QtGui

from .drawtools import DrawPolygon, \
SelectPoint
from .kwg_plugin_dialog import kwg_pluginDialog
Expand All @@ -60,6 +55,7 @@

_SPARQL_ENDPOINT_DICT = {
"KnowWhereGraph": "https://stko-kwg.geog.ucsb.edu/graphdb/repositories/KWG",
"KnowWhereGraph - Stage": "https://staging.knowwheregraph.org/graphdb/repositories/KWG"
}

class kwg_geoenrichment:
Expand Down Expand Up @@ -298,7 +294,7 @@ def run(self):
self.dlg.show()

# disable GDB Button
self.dlg.pushButton_gdb.clicked.connect(lambda: self.displayButtonHelp(isGDB=True))
self.dlg.pushButton_gdb.clicked.connect(lambda: self.handleGeoPackageFileBrowser())

self.dlg.pushButton_refresh.clicked.connect(lambda: self.refreshLayer())

Expand Down Expand Up @@ -495,13 +491,6 @@ def draw(self):

name = "geo_enrichment_polygon"
pjt = QgsProject.instance()
if pjt.layerTreeRoot().findGroup(self.tr('Geometry')) is not None:
group = pjt.layerTreeRoot().findGroup(
self.tr('Geometry'))

for child in group.children():
if isinstance(child, QgsLayerTreeLayer):
QgsProject.instance().removeMapLayer(child.layerId())

# save the buffer
if self.drawShape == 'point':
Expand Down Expand Up @@ -536,14 +525,7 @@ def draw(self):
layer.dataProvider().addFeatures([feature])
layer.commitChanges()

pjt.addMapLayer(layer, False)
if pjt.layerTreeRoot().findGroup(self.tr('Geometry')) is None:
pjt.layerTreeRoot().insertChildNode(
0, QgsLayerTreeGroup(self.tr('Geometry')))
group = pjt.layerTreeRoot().findGroup(
self.tr('Geometry'))
group.insertLayer(0, layer)
self.iface.layerTreeView().refreshLayerSymbology(layer.id())
pjt.addMapLayer(layer)
self.iface.mapCanvas().refresh()

self.dlg.comboBox_layers.currentIndexChanged.disconnect()
Expand Down Expand Up @@ -645,8 +627,8 @@ def refreshLayer(self):
if layer.name() not in currentItems:
self.dlg.comboBox_layers.addItem(layer.name())

def selectNewlyDrawnLayer(self):
index = self.dlg.comboBox_layers.findText("geo_enrichment_polygon")
def selectNewlyDrawnLayer(self, layerName="geo_enrichment_polygon"):
index = self.dlg.comboBox_layers.findText(layerName)
if index >= 0:
self.dlg.comboBox_layers.setCurrentIndex(index)

Expand Down Expand Up @@ -861,6 +843,9 @@ def createGeoPackage(self, GeoQueryResult, objName=[], layerName="geo_results",
pr.addAttributes(layerFields)
vl.updateFields()

if gtype == "LineString":
vl.renderer().symbol().setWidth(1.2)

if len(objList) == 0:
QgsMessageLog.logMessage("No results found!",
level=Qgis.Info)
Expand Down Expand Up @@ -933,7 +918,7 @@ def updateWkt(self, gtype, wkt1, wkt2):

def generateFormattedEntityDict(self, entityDict = {}, mergeRule = 1):
isAlpha = False
intMergeRules = [4, 5, 6, 7, 8]
intMergeRules = [4, 5, 6, 7]
for gtype in entityDict:
for eVal in entityDict[gtype]:
if (any(c.isalpha() for c in entityDict[gtype][eVal]["o"][0])):
Expand Down Expand Up @@ -1058,6 +1043,54 @@ def enableRunButton(self):
""")
self.dlg.show()

def handleGeoPackageFileBrowser(self):
filters = "Shape files (*.shp);;Geopackage files (*.gpkg)"
selected_filter = "Geopackage files (*.gpkg)"
isShapeFile = False
isGpkgFile = False
# TODO
file_tup = QFileDialog.getOpenFileName(self.dlg, "KWG File browser", self.path, filters, selected_filter)
self.logger.info(file_tup)
if file_tup is not None:
path = file_tup[0]
if path is not None:
if len(path.split(".")) > 1:
isShapeFile = True if path.split(".")[1] == "shp" else False
isGpkgFile = True if path.split(".")[1] == "gpkg" else False
layerName = self.path_leaf(path)
layer = QgsVectorLayer(path, layerName, "ogr")

if isShapeFile: self.loadShapeFile(layer, layerName)
if isGpkgFile: self.loadGpkgFile(path)

def loadShapeFile(self, layer='', layerName=None):
layer.setOpacity(0.50)
pjt = QgsProject.instance()
pjt.addMapLayer(layer)
self.iface.mapCanvas().refresh()

self.dlg.comboBox_layers.currentIndexChanged.disconnect()
self.refreshLayer()
self.dlg.comboBox_layers.currentIndexChanged.connect(lambda: self.handleLayerSelection())
self.selectNewlyDrawnLayer(layerName=self.path_leaf(layerName))

def loadGpkgFile(self, fileName):
layer = QgsVectorLayer(fileName, "test", "ogr")
subLayers = layer.dataProvider().subLayers()

for subLayer in subLayers:
name = subLayer.split(QgsDataProvider.SUBLAYER_SEPARATOR)[1]
uri = "%s|layername=%s" % (fileName, name,)
# Create layer
sub_vlayer = QgsVectorLayer(uri, name, 'ogr')
sub_vlayer.setOpacity(0.50)
# Add layer to map
QgsProject.instance().addMapLayer(sub_vlayer)

def path_leaf(self, path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)


class Worker(QRunnable, ):

Expand Down
5 changes: 5 additions & 0 deletions kwg_geoenrichment/kwg_plugin_dialog_base.ui
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ When you're ready to create your new Feature Class, provide a name for the new
<string>KnowWhereGraph</string>
</property>
</item>
<item>
<property name="text">
<string>KnowWhereGraph - Stage</string>
</property>
</item>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
Expand Down
26 changes: 3 additions & 23 deletions kwg_geoenrichment/kwg_plugin_enrichment_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,31 +213,11 @@ def execute(self):
except TypeError:
pass
self.tableWidget.cellWidget(0, 0).clear()
s2CellBindingObject = []
entityBindingObject = []

for wkt in self.params["wkt_literal"]:
# retrieve S2 cells
response = self.sparql_query.getS2CellsFromGeometry(sparql_endpoint=self.params["end_point"],
wkt_literal=wkt)

if response == "error: s2c":
self.handleError(errCode="s2c")
return "s2c"
s2CellBindingObject.extend(response)

self.logger.debug(json.dumps(s2CellBindingObject))
for obj in s2CellBindingObject:
self.logger.debug(json.dumps(obj))
try:
# self.logger.debug(obj["s2Cell"]["value"])
self.s2Cells.append(obj["s2Cell"]["value"])
except Exception as e:
self.logger.debug(e)
continue

# retrieve Entity associated with S2 cells
entityBindingObject = self.sparql_query.getEntityValuesFromS2Cells(sparql_endpoint=self.params["end_point"],
s2Cells=self.s2Cells)
entityBindingObject.extend(self.sparql_query.getEntityValuesFromGeometry(sparql_endpoint=self.params["end_point"],
wkt_literal=wkt))

for obj in entityBindingObject:
try:
Expand Down
80 changes: 23 additions & 57 deletions kwg_geoenrichment/kwg_sparqlquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,64 +24,16 @@ def __init__(self):
handler.setFormatter(formatter) # Pass handler as a parameter, not assign
self.logger.addHandler(handler)

def getS2CellsFromGeometry(self,
def getEntityValuesFromGeometry(self,
sparql_endpoint="http://stko-kwg.geog.ucsb.edu/graphdb/repositories/KWG-V3",
wkt_literal="Polygon((-119.73822343857 34.4749685817967, -119.571162553598 34.4767458252538, -119.670688187198 34.3754429481962, -119.73822343857 34.4749685817967))"):
"""
Retrieves S2 cells based on the selected wkt literal
Retrieves Entities based on the selected wkt literal
Arguments:
sparql_endpoint: the sparql end point for the graph database
wkt_literal: the wkt literal for the user selected polygon
Returns:
The raw JSON response containing S2 cells retrieved from the specified graph end point
"""

SPARQLUtil = kwg_sparqlutil()
queryPrefix = SPARQLUtil.make_sparql_prefix()

queryString = """
select ?s2Cell where {
?adminRegion2 a kwg-ont:AdministrativeRegion_3.
?adminRegion2 geo:hasGeometry ?arGeo.
?arGeo geo:asWKT ?arWKT.
FILTER(geof:sfIntersects("%s"^^geo:wktLiteral, ?arWKT) || geof:sfWithin("%s"^^geo:wktLiteral, ?arWKT)).
?adminRegion2 kwg-ont:sfContains ?s2Cell.
?s2Cell a kwg-ont:KWGCellLevel13.
?s2Cell geo:hasGeometry ?s2Geo.
?s2Geo geo:asWKT ?s2WKT.
FILTER(geof:sfIntersects("%s"^^geo:wktLiteral, ?s2WKT) || geof:sfWithin("%s"^^geo:wktLiteral, ?s2WKT)).
}
""" % (wkt_literal, wkt_literal, wkt_literal, wkt_literal)

query = queryPrefix + queryString

try:

s2CellsQueryResult = SPARQLUtil.sparql_requests(query=query,
sparql_endpoint=sparql_endpoint,
request_method="get")

if s2CellsQueryResult is None:
return "error: s2c"

s2CellsQueryResultBindings = s2CellsQueryResult["results"]["bindings"]

except:
return "error: s2c"

return s2CellsQueryResultBindings

def getEntityValuesFromS2Cells(self,
sparql_endpoint="http://stko-kwg.geog.ucsb.edu/graphdb/repositories/KWG-V3",
s2Cells=[]):
"""
Retrieves Entity values that are associated with the passed S2 cells
Arguments:
sparql_endpoint: the sparql end point for the graph database
s2Cells: list object of the S2 cells that belong to the user selected polygon
Returns:
The raw JSON response containing Entity values
The raw JSON response containing entities associated with S2Cells within the given geometry
"""

SPARQLUtil = kwg_sparqlutil()
Expand All @@ -90,17 +42,31 @@ def getEntityValuesFromS2Cells(self,
EntityQueryResultBindings = []

try:
s2CellsPrefixed = ""
for s2Cell in s2Cells:
s2CellsPrefixed += " " + self.sparqlUTIL.make_prefixed_iri(s2Cell)

queryString = """
select distinct ?entity where {
values ?userWKT {"%s"^^geo:wktLiteral}.
?adminRegion2 a kwg-ont:AdministrativeRegion_2.
?adminRegion2 geo:hasGeometry ?arGeo2.
?arGeo2 geo:asWKT ?arWKT2.
FILTER(geof:sfIntersects(?userWKT, ?arWKT2) || geof:sfWithin(?userWKT, ?arWKT2)).
?adminRegion3 kwg-ont:sfWithin ?adminRegion2.
?adminRegion3 a kwg-ont:AdministrativeRegion_3.
?adminRegion3 geo:hasGeometry ?arGeo3.
?arGeo3 geo:asWKT ?arWKT3.
FILTER(geof:sfIntersects(?userWKT, ?arWKT3) || geof:sfWithin(?userWKT, ?arWKT3)).
?adminRegion3 kwg-ont:sfContains ?s2Cell.
?s2Cell a kwg-ont:KWGCellLevel13.
?s2Cell geo:hasGeometry ?s2Geo.
?s2Geo geo:asWKT ?s2WKT.
FILTER(geof:sfIntersects(?userWKT, ?s2WKT) || geof:sfWithin(?userWKT, ?s2WKT)).
{?entity ?p ?s2Cell.} union {?s2Cell ?p ?entity.}
?entity a geo:Feature.
values ?s2Cell {%s}
}
""" % (s2CellsPrefixed)
""" % (wkt_literal)

query = queryPrefix + queryString

Expand Down

0 comments on commit 2afc689

Please sign in to comment.