Skip to content

Commit

Permalink
fixes for crs conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
situx committed Oct 4, 2024
1 parent f8a9025 commit 430fefd
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 81 deletions.
4 changes: 2 additions & 2 deletions tasks/processing/convertcrstask.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(self):
for s, p, o in self.graph:
if isinstance(o, Literal):
if str(o.datatype) in SPARQLUtils.supportedLiteralTypes:
newliteral = Literal(LayerUtils.processLiteral(o, o.datatype, "", self.crsdef), datatype=o.datatype)
newliteral = Literal(LayerUtils.processLiteral(o, o.datatype, self.crsdef,None,None,False,o.datatype), datatype=o.datatype)
self.graph.set((s, p, newliteral))
return True

Expand All @@ -43,7 +43,7 @@ def finished(self, result):
if fileName and self.graph is not None:
fo = open(fileName, "w")
fo.write(ConvertCRS().ttlhead)
fo.write(self.graph.serialize(format="turtle").decode())
fo.write(self.graph.serialize(format="turtle"))
for crs in self.crsdefs:
fo.write(self.crsdefs[crs])
fo.close()
Expand Down
6 changes: 2 additions & 4 deletions tasks/processing/extractlayertask.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ def run(self):
g.parse(self.graphname, format="ttl")
for toex in self.toextract:
if "owl" not in str(toex) and "rdf" not in str(toex):
QgsMessageLog.logMessage(str(toex),
MESSAGE_CATEGORY, Qgis.Info)
QgsMessageLog.logMessage(str(toex),MESSAGE_CATEGORY, Qgis.Info)
layergraph = Graph()
for sub in g.subjects(RDF.type, URIRef(toex),True):
QgsMessageLog.logMessage(str(sub),
MESSAGE_CATEGORY, Qgis.Info)
QgsMessageLog.logMessage(str(sub),MESSAGE_CATEGORY, Qgis.Info)
for trip in g.triples((sub,None,None)):
layergraph.add(trip)
res=LayerUtils.subGraphToLayer(layergraph,g,False, self.triplestoreconf, True, False)
Expand Down
142 changes: 74 additions & 68 deletions tasks/query/data/querylayertask.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,50 +125,52 @@ def processResults(self, results, reproject, mandatoryvars, geooptional):
features = []
nongeofeatures=[]
properties={}
first = True
newobject = True
item = ""
relval=False
crsset=set()
QgsMessageLog.logMessage('Processing results....',
MESSAGE_CATEGORY, Qgis.Info)
lastaddeditem=""
for result in results["results"]["bindings"]:
if self.concept is not None and "item" not in result:
result["item"]={}
result["item"]["value"]=self.concept
if "item" in result and "rel" in result and "val" in result and "geo" in result and (
item == "" or result["item"]["value"] != item) and "geo" in mandatoryvars:
relval=True
if item != "":
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
properties = {}
item = result["item"]["value"]
if "item" in result and "rel" in result and "val" in result and "lat" in result and "lon" in result and (
result["item"]={"value":self.concept}
if "item" in result and "rel" in result and "val" in result:
QgsMessageLog.logMessage('rel val' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if "geo" in result and (item == "" or result["item"]["value"] != item) and "geo" in mandatoryvars:
QgsMessageLog.logMessage('rel val + geo' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if item != "":
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
lastaddeditem=result["item"]["value"]
elif "lat" in result and "lon" in result and (
item == "" or result["item"]["value"] != item) and "lat" in mandatoryvars and "lon" in mandatoryvars:
relval=True
if item != "":
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(
"POINT(" + str(float(result[lonval]["value"])) + " " + str(
float(result[latval]["value"])) + ")", "wkt", reproject,{'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},self.triplestoreconf),features,nongeofeatures,crsset)
properties = {}
item = result["item"]["value"]
if "item" in result and "rel" in result and "val" in result and geooptional and (
item == "" or result["item"]["value"] != item):
relval=True
if item != "":
self.addFeatureToCorrectCollection({'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},features,nongeofeatures,crsset)
QgsMessageLog.logMessage('rel val + lat lon' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if item != "":
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(
"POINT(" + str(float(result[lonval]["value"])) + " " + str(
float(result[latval]["value"])) + ")", "wkt", reproject,{'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},self.triplestoreconf),features,nongeofeatures,crsset)
lastaddeditem = result["item"]["value"]
elif geooptional and (item == "" or result["item"]["value"] != item):
QgsMessageLog.logMessage('rel val + no geo' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if item != "":
self.addFeatureToCorrectCollection({'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},features,nongeofeatures,crsset)
lastaddeditem = result["item"]["value"]
properties = {}
item = result["item"]["value"]
if "rel" not in result and "val" not in result:
elif "rel" not in result and "val" not in result:
properties = {}
for var in results["head"]["vars"]:
if var in result:
if var=="item":
item = result["item"]["value"]
if var == "rel" and "val" in result:
if self.shortenURIs:
properties[SPARQLUtils.labelFromURI(result[var]["value"])] = result["val"]["value"]
Expand All @@ -190,43 +192,47 @@ def processResults(self, results, reproject, mandatoryvars, geooptional):
properties[SPARQLUtils.labelFromURI(result[var]["value"])] = SPARQLUtils.labelFromURI(result["val"]["value"])
else:
properties[var] = result[var]["value"]
if not "rel" in result and not "val" in result and "geo" in result:
if "rel" not in result and "val" not in result:
QgsMessageLog.logMessage('Not rel val ' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if "geo" in result:
QgsMessageLog.logMessage('Not rel val + geo' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id': result["item"]["value"], 'type': 'Feature',
'properties': self.dropUnwantedKeys(properties),'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
lastaddeditem = result["item"]["value"]
elif latval in result and lonval in result:
QgsMessageLog.logMessage('Not rel val + lat lon' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(
"POINT(" + str(float(result[lonval]["value"])) + " " + str(float(result[latval]["value"])) + ")",
"wkt", reproject,
{'id': result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
lastaddeditem = result["item"]["value"]
elif "geo" not in result and geooptional:
QgsMessageLog.logMessage('Not rel val + no geo' + str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
self.addFeatureToCorrectCollection({'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},features,nongeofeatures,crsset)
lastaddeditem = result["item"]["value"]

if len(results)>0 and lastaddeditem!=item:
if "geo" in properties:
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id': result["item"]["value"], 'type': 'Feature',
'properties': self.dropUnwantedKeys(properties),'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
elif not "rel" in result and not "val" in result and latval in result and lonval in result:
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(
"POINT(" + str(float(result[lonval]["value"])) + " " + str(float(result[latval]["value"])) + ")",
"wkt", reproject,
{'id': result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties),
'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
elif not "rel" in result and not "val" in result and not "geo" in result and geooptional:
self.addFeatureToCorrectCollection({'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},features,nongeofeatures,crsset)
#if relval and not geooptional and "lat" not in result and "lon" not in result:
# self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
# result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
# { 'id':result["item"]["value"],'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},self.triplestoreconf),features,nongeofeatures,crsset)
#if relval and geooptional:
# #myGeometryInstanceJSON = LayerUtils.processLiteral(result["geo"]["value"], (
# # result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,self.triplestoreconf)
# feature = {'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}}#json.loads(myGeometryInstanceJSON)}
# features.append(feature)
#if len(features)==0:
if "geo" in properties:
self.addFeatureToCorrectCollection(LayerUtils.processLiteral(result["geo"]["value"], (
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id':result["item"]["value"], 'type': 'Feature','properties': self.dropUnwantedKeys(properties),'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
elif "lat" in properties and "lon" in properties:
self.addFeatureToCorrectCollection(LayerUtils.processLiteral("POINT(" + str(float(result[lonval]["value"]))
+ " " + str(float(result[latval]["value"])) + ")",
"wkt", reproject,{'id':result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},self.triplestoreconf),features,nongeofeatures,crsset)
else:
self.addFeatureToCorrectCollection({'id':result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},features,nongeofeatures,crsset)
QgsMessageLog.logMessage('Number of features '+str(len(features)),
result["geo"]["datatype"] if "datatype" in result["geo"] else ""), reproject,
{'id':result["item"]["value"], 'type': 'Feature','properties': self.dropUnwantedKeys(properties),'geometry': {}},
self.triplestoreconf),features,nongeofeatures,crsset)
elif "lat" in properties and "lon" in properties:
self.addFeatureToCorrectCollection(LayerUtils.processLiteral("POINT(" + str(float(result[lonval]["value"]))
+ " " + str(float(result[latval]["value"])) + ")",
"wkt", reproject,{'id':result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},self.triplestoreconf),features,nongeofeatures,crsset)
else:
self.addFeatureToCorrectCollection({'id':result["item"]["value"], 'type': 'Feature', 'properties': self.dropUnwantedKeys(properties), 'geometry': {}},features,nongeofeatures,crsset)
QgsMessageLog.logMessage('Number of features '+str(len(features)),
MESSAGE_CATEGORY, Qgis.Info)
if features == [] and len(results["results"]["bindings"]) == 0:
return [None,None,None]
Expand Down
34 changes: 27 additions & 7 deletions util/layerutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
QgsMessageLog
)
from osgeo import ogr
from qgis.core import QgsFeature, Qgis, QgsWkbTypes, QgsProject, QgsGeometry, QgsCoordinateReferenceSystem, QgsCoordinateTransform
from qgis.core import QgsFeature, Qgis, QgsWkbTypes, QgsProject, QgsGeometry, QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsJsonExporter
import traceback
import json
from .sparqlutils import SPARQLUtils
Expand Down Expand Up @@ -424,10 +424,28 @@ def detectLayerColumnTypes(layer):
columnmap[i]=LayerUtils.detectLayerColumnType(layer,i)
return LayerUtils.detectColumnType(columnmap)

@staticmethod
def convertGeometryToFormat(geom,format,reproject=None):
if "geojson" in format.lower():
return geom.asJson()
if "wkt" in format.lower():
if reproject is not None:
return "<"+str(reproject.toOgcUri())+"> "+geom.asWkt()
return geom.asWkt()
if "wkb" in format.lower():
return geom.asWkb()
return None

@staticmethod
def reprojectGeometry(geom,fromcrs,tocrs="EPSG:4326"):
sourceCrs = QgsCoordinateReferenceSystem(fromcrs)
destCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs(tocrs)
if isinstance(fromcrs,QgsCoordinateReferenceSystem):
sourceCrs=fromcrs
else:
sourceCrs = QgsCoordinateReferenceSystem(fromcrs)
if isinstance(tocrs,QgsCoordinateReferenceSystem):
destCrs=tocrs
else:
destCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs(tocrs)
tr = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance())
#QgsMessageLog.logMessage("FIELDNAMES: " + str(geom.asJson()),
# MESSAGE_CATEGORY, Qgis.Info)
Expand All @@ -437,7 +455,7 @@ def reprojectGeometry(geom,fromcrs,tocrs="EPSG:4326"):
return geom

@staticmethod
def processLiteral(literal, literaltype, reproject, currentlayergeojson=None,triplestoreconf=None, reprojecttask=False):
def processLiteral(literal, literaltype, reproject, currentlayergeojson=None,triplestoreconf=None, reprojecttask=False,formatt=None):
geom = None
if triplestoreconf is not None and "literaltype" in triplestoreconf:
literaltype = triplestoreconf["literaltype"]
Expand Down Expand Up @@ -490,11 +508,13 @@ def processLiteral(literal, literaltype, reproject, currentlayergeojson=None,tri
return currentlayergeojson
if curcrs is not None:
res["crs"]=curcrs
if formatt is not None:
return LayerUtils.convertGeometryToFormat(geom,formatt,reproject)
return res
except Exception as e:
print("Literal: " + str(literal) + " " + str(literaltype))
print(e)
print(traceback.format_exc())
QgsMessageLog.logMessage("Literal: " + str(literal) + " " + str(literaltype), MESSAGE_CATEGORY, Qgis.Info)
QgsMessageLog.logMessage(str(e) , MESSAGE_CATEGORY, Qgis.Info)
QgsMessageLog.logMessage(str(traceback.format_exc()), MESSAGE_CATEGORY, Qgis.Info)
return None


Expand Down

0 comments on commit 430fefd

Please sign in to comment.