From aa2dd3b1073d697d084ef46f2f808970401f09fd Mon Sep 17 00:00:00 2001 From: Timo Date: Tue, 20 Feb 2024 03:39:15 +0100 Subject: [PATCH] enabled layer to graph export in more formats than rdf --- tasks/processing/convertlayertask.py | 49 +++ .../data/exporter/layer/layerexporter.py | 235 +++++++++++++- .../export/data/exporter/rdf/graphexporter.py | 2 +- util/layerutils.py | 307 ------------------ 4 files changed, 279 insertions(+), 314 deletions(-) diff --git a/tasks/processing/convertlayertask.py b/tasks/processing/convertlayertask.py index e69de29bb..a1e2a2b5d 100644 --- a/tasks/processing/convertlayertask.py +++ b/tasks/processing/convertlayertask.py @@ -0,0 +1,49 @@ + +from ...util.export.data.exporter.layer.layerexporter import LayerExporter +from qgis.utils import iface +from qgis.core import Qgis,QgsTask, QgsMessageLog +from qgis.PyQt.QtWidgets import QMessageBox + +MESSAGE_CATEGORY = 'ConvertLayerTask' + +class ConvertLayerTask(QgsTask): + + def __init__(self, description, layer, filename, vocabulary, literaltype, prefixes, dialog, progress): + super().__init__(description, QgsTask.CanCancel) + self.exception = None + self.progress = progress + self.filename = filename + self.prefixes=prefixes + self.vocabulary = vocabulary + self.layer= layer + self.literaltype = literaltype + self.dialog = dialog + + + def run(self): + fileext=self.filename[0][self.filename[0].rfind('.')+1:].upper() + ttlstring = LayerExporter.layerToTTLString(self.layer, + self.prefixes, + self.vocabulary, self.literaltype, + None, None, None, None, None, None) + QgsMessageLog.logMessage('Started task "{}"'.format( + fileext), + "Convert Layer Dialog", Qgis.Info) + with open(self.filename[0], 'w') as output_file: + LayerExporter.exportToFormat(ttlstring, output_file, self.filename[0], fileext,self.prefixes) + return True + + def finished(self, result): + self.progress.close() + if result == True: + iface.messageBar().pushMessage("Exported layer successfully to " + str(self.filename[0]) + "!", "OK", + level=Qgis.Success) + msgBox = QMessageBox() + msgBox.setText("Layer converted to and saved as "+str(self.filename[0])) + msgBox.exec() + else: + msgBox = QMessageBox() + msgBox.setText("An error occurred while converting the layer converted to "+str(self.filename[0])) + msgBox.exec() + + diff --git a/util/export/data/exporter/layer/layerexporter.py b/util/export/data/exporter/layer/layerexporter.py index a463b174d..b9ba9deb4 100644 --- a/util/export/data/exporter/layer/layerexporter.py +++ b/util/export/data/exporter/layer/layerexporter.py @@ -1,24 +1,247 @@ import json +import uuid +import re +import urllib.parse +from ....srs.crsexporttools import ConvertCRS from ..exporterutils import ExporterUtils from .....layerutils import LayerUtils +from .....sparqlutils import SPARQLUtils +from qgis.core import Qgis,QgsTask, QgsMessageLog from rdflib import Graph -class LayerExporter: - +class LayerExporter: @staticmethod - def exportToFormat(layer,format,prefixes): - layerToTTL=LayerUtils.layerToTTLString(layer,prefixes) + def exportToFormat(layerOrTTLString,file,filename,format,prefixes): + if isinstance(layerOrTTLString, str): + layerToTTL=layerOrTTLString + else: + layerToTTL=LayerUtils.layerToTTLString(layerOrTTLString,prefixes) + QgsMessageLog.logMessage(str(layerToTTL),"LayerExporter", Qgis.Info) + QgsMessageLog.logMessage("Format: "+str(format), "LayerExporter", Qgis.Info) + QgsMessageLog.logMessage("File: " + str(file), "LayerExporter", Qgis.Info) g=Graph() - g.parse(layerToTTL) + g.parse(data=layerToTTL) + g.bind("suni","http://www.github.com/sparqlunicorn#") if format in ExporterUtils.exportToFunction: - ExporterUtils.exportToFunction[format](g,None,None,None,format.lower()) + if format not in ExporterUtils.rdfformats: + ExporterUtils.exportToFunction[format](g,file,None,None,format.lower()) + else: + ExporterUtils.exportToFunction[format](g,filename,None,None,format.lower()) + + + ## Converts a QGIS layer to TTL with or without a given column mapping. + # @param self The object pointer. + # @param layer The layer to convert. + @staticmethod + def layerToTTLString(layer, prefixes, vocab="GeoSPARQL", literaltype=["WKT"], urilist=None, classurilist=None, + includelist=None, proptypelist=None, + valuemappings=None, valuequeries=None, exportNameSpace=None, exportIdCol=None, + exportSetClass=None): + fieldnames = [field.name() for field in layer.fields()] + # QgsMessageLog.logMessage("FIELDNAMES: "+str(fieldnames), + # MESSAGE_CATEGORY, Qgis.Info) + # QgsMessageLog.logMessage("FIELDNAMES: "+str(vocab), + # MESSAGE_CATEGORY, Qgis.Info) + ttlstring = set() + first = 0 + if exportNameSpace == None or exportNameSpace == "": + namespace = "http://www.github.com/sparqlunicorn#" + else: + namespace = exportNameSpace + if exportIdCol == "": + idcol = "id" + else: + idcol = exportIdCol + classcol = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" + curid = "" + if exportSetClass == None or exportSetClass == "": + curclassid = namespace + str(uuid.uuid4()) + elif exportSetClass.startswith("http"): + curclassid = exportSetClass + else: + curclassid = urllib.parse.quote(exportSetClass) + layercrs = layer.crs() + ttlstring.add(" .\n") + ttlstring.add(" \"" + str( + layercrs.toWkt()).replace("\"", "\\\"") + "\"^^ .\n") + ttlstring.add(" \"" + str( + layercrs.toProj4()) + "\"^^ .\n") + ccrs = ConvertCRS() + ttlstring = ccrs.convertCRSFromWKTStringSet(layercrs.toWkt(), ttlstring) + init = True + for f in layer.getFeatures(): + geom = f.geometry() + if idcol not in fieldnames: + curid = namespace + str(uuid.uuid4()) + elif not str(f[idcol]).startswith("http"): + curid = namespace + str(f[idcol]) + else: + curid = f[idcol] + if classcol not in fieldnames: + ttlstring.add( + "<" + str(curid) + "> <" + curclassid + "> .\n") + if first == 0: + ttlstring.add("<" + str( + curclassid) + "> .\n") + ttlstring.add("<" + str( + curclassid) + "> .\n") + else: + curclassid = f["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] + ttlstring = LayerUtils.exportGeometryType(curid, geom, vocab, literaltype, init, ttlstring) + if init: + init = False + fieldcounter = -1 + for propp in fieldnames: + fieldcounter += 1 + # if fieldcounter>=len(fieldnames): + # fieldcounter=0 + if includelist != None and fieldcounter < len(includelist) and includelist[fieldcounter] == False: + continue + prop = propp + print(str(fieldcounter)) + print(str(urilist) + "\n") + print(str(classurilist) + "\n") + print(str(includelist) + "\n") + if urilist != None and urilist[fieldcounter] != "": + print(urilist) + if not urilist[fieldcounter].startswith("http"): + print("Does not start with http") + prop = urllib.parse.quote(urilist[fieldcounter]) + else: + prop = urilist[fieldcounter] + print("New Prop from list: " + str(prop)) + if prop == "id": + continue + if not prop.startswith("http"): + prop = namespace + prop + if prop == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" and "http" in str(f[propp]): + ttlstring.add( + "<" + str(f[propp]) + "> <" + str(prop) + "> .\n") + ttlstring.add("<" + str(f[ + propp]) + "> .\n") + ttlstring.add("<" + str(curid) + "> <" + str( + f[propp]) + "> .\n") + # elif urilist!=None and fieldcounter <"+prop+"> <"+str(f[propp])+"> .\n" + # if first<10: + # ttlstring+="<"+prop+"> .\n" + # ttlstring+="<"+prop+"> <"+curclassid+"> .\n" + # if classurilist[fieldcounter]!="": + # ttlstring+="<"+prop+"> <"+classurilist[fieldcounter]+"> .\n" + elif prop == "http://www.w3.org/2000/01/rdf-schema#label" or prop == "http://www.w3.org/2000/01/rdf-schema#comment" or ( + proptypelist != None and proptypelist[fieldcounter] == "AnnotationProperty"): + ttlstring.add("<" + curid + "> <" + prop + "> \"" + str(f[propp]).replace('"', + '\\"') + "\"^^ .\n") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + elif not f[propp] or f[propp] == None or f[propp] == "": + continue + elif proptypelist != None and proptypelist[fieldcounter] == "SubClass": + ttlstring.add( + "<" + curid + "> <" + str(f[propp]) + "> .\n") + ttlstring.add( + "<" + curid + "> <" + curclassid + "> .\n") + if first < 10: + ttlstring.add("<" + str(f[ + propp]) + "> .\n") + elif valuequeries != None and propp in valuequeries: + # ttlstring += "" + results = SPARQLUtils.executeQuery(valuequeries[propp][1], "".join( + prefixes + valuequeries[propp][0].replace("%%" + propp + "%%", "\"" + str(f[propp]) + "\""))) + ttlstring.add( + "<" + curid + "> <" + prop + "> <" + results["results"]["bindings"][0]["item"]["value"] + "> .") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + if classurilist[fieldcounter] != "": + ttlstring.add( + "<" + prop + "> <" + classurilist[ + fieldcounter] + "> .\n") + elif valuemappings != None and propp in valuemappings and f[propp] in valuemappings[propp]: + ttlstring.add("<" + curid + "> <" + prop + "> <" + str(valuemappings[propp][f[propp]]) + "> .\n") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + if classurilist[fieldcounter] != "": + ttlstring.add( + "<" + prop + "> <" + classurilist[ + fieldcounter] + "> .\n") + elif "http" in str(f[propp]) or ( + proptypelist != None and proptypelist[fieldcounter] == "ObjectProperty"): + ttlstring.add("<" + curid + "> <" + prop + "> <" + str(f[propp]) + "> .\n") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + if classurilist != None and fieldcounter < len(classurilist) and classurilist[ + fieldcounter] != "": + ttlstring.add( + "<" + prop + "> <" + classurilist[ + fieldcounter] + "> .\n") + elif re.match(r'^-?\d+$', str(f[propp])): + ttlstring.add("<" + curid + "> <" + prop + "> \"" + str( + f[propp]) + "\"^^ .\n") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + ttlstring.add( + "<" + prop + "> .\n") + elif re.match(r'^-?\d+(?:\.\d+)?$', str(f[propp])): + ttlstring.add("<" + curid + "> <" + prop + "> \"" + str( + f[propp]) + "\"^^ .\n") + if first: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + ttlstring.add( + "<" + prop + "> .\n") + else: + ttlstring.add("<" + curid + "> <" + prop + "> \"" + str(f[propp]).replace('"', + '\\"') + "\"^^ .\n") + if first < 10: + ttlstring.add( + "<" + prop + "> .\n") + ttlstring.add( + "<" + prop + "> <" + curclassid + "> .\n") + ttlstring.add( + "<" + prop + "> .\n") + if first < 10: + first = first + 1 + return ccrs.ttlhead + "".join(ttlstring) + + + @staticmethod + def layerToDot(layer, prefixes, urilist=None, classurilist=None, includelist=None, proptypelist=None, + valuemappings=None, valuequeries=None,exportNameSpace=None,exportIdCol=None,exportSetClass=None): + ttlstring=LayerUtils.layerToTTLString(layer, prefixes, urilist, classurilist, includelist, proptypelist, + valuemappings, valuequeries,exportNameSpace,exportIdCol,exportSetClass) + #g=Graph() + #g.parse(data=ttlstring,format="ttl") + #stream = io.StringIO() + #rdf2dot(g, stream) + return ""#stream.getvalue() + @staticmethod def layerToGraphML(layer): fieldnames = [field.name() for field in layer.fields()] diff --git a/util/export/data/exporter/rdf/graphexporter.py b/util/export/data/exporter/rdf/graphexporter.py index 6fc353bae..d94b623c7 100644 --- a/util/export/data/exporter/rdf/graphexporter.py +++ b/util/export/data/exporter/rdf/graphexporter.py @@ -264,7 +264,7 @@ def convertTTLToGEXF(g,file,subjectstorender,classlist=None,formatt="gexf"): for tup in g.predicate_objects(sub): if isinstance(tup[1],Literal): if str(tup[1]) not in uriToNodeId: - file.write("",">").replace("&","&"))+"\" label=\"" + str(str(tup[1]).replace("<","<").replace(">",">").replace("&","&")) + "\">\n") + file.write("",">").replace("&","&").replace("\"","'"))+"\" label=\"" + str(str(tup[1]).replace("<","<").replace(">",">").replace("&","&").replace("\"","'")) + "\">\n") if str(tup[0]) == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type": file.write("\n") else: diff --git a/util/layerutils.py b/util/layerutils.py index eb9dd5bbe..e8ce3742a 100644 --- a/util/layerutils.py +++ b/util/layerutils.py @@ -4,15 +4,9 @@ ) from osgeo import ogr from qgis.core import QgsFeature, Qgis, QgsWkbTypes, QgsProject, QgsGeometry, QgsCoordinateReferenceSystem, QgsCoordinateTransform -import uuid import traceback -import re import json -import urllib.parse -from .export.srs.crsexporttools import ConvertCRS from .sparqlutils import SPARQLUtils -#from rdflib.tools.rdf2dot import rdf2dot -#from rdflib import Graph MESSAGE_CATEGORY = 'LayerUtils' @@ -260,304 +254,3 @@ def exportGeometryType(curid,geom,vocab,literaltype,init,ttlstring): curid) + "> \"" + str( geom.centroid().vertexAt(0).y()) + "\" .\n") return ttlstring - - - - ## Converts a QGIS layer to TTL with or without a given column mapping. - # @param self The object pointer. - # @param layer The layer to convert. - @staticmethod - def layerToTTLString(layer, prefixes,vocab="GeoSPARQL",literaltype=["WKT"], urilist=None, classurilist=None, includelist=None, proptypelist=None, - valuemappings=None, valuequeries=None,exportNameSpace=None,exportIdCol=None,exportSetClass=None): - fieldnames = [field.name() for field in layer.fields()] - #QgsMessageLog.logMessage("FIELDNAMES: "+str(fieldnames), - # MESSAGE_CATEGORY, Qgis.Info) - #QgsMessageLog.logMessage("FIELDNAMES: "+str(vocab), - # MESSAGE_CATEGORY, Qgis.Info) - ttlstring=set() - first = 0 - if exportNameSpace == None or exportNameSpace == "": - namespace = "http://www.github.com/sparqlunicorn#" - else: - namespace = exportNameSpace - if exportIdCol == "": - idcol = "id" - else: - idcol = exportIdCol - classcol = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" - curid = "" - if exportSetClass == None or exportSetClass == "": - curclassid = namespace + str(uuid.uuid4()) - elif exportSetClass.startswith("http"): - curclassid = exportSetClass - else: - curclassid = urllib.parse.quote(exportSetClass) - layercrs = layer.crs() - ttlstring.add(" .\n") - ttlstring.add(" \"" + str( - layercrs.toWkt()).replace("\"", "\\\"") + "\"^^ .\n") - ttlstring.add(" \"" + str( - layercrs.toProj4()) + "\"^^ .\n") - ccrs=ConvertCRS() - ttlstring=ccrs.convertCRSFromWKTStringSet(layercrs.toWkt(),ttlstring) - init=True - for f in layer.getFeatures(): - geom = f.geometry() - if idcol not in fieldnames: - curid = namespace + str(uuid.uuid4()) - elif not str(f[idcol]).startswith("http"): - curid = namespace + str(f[idcol]) - else: - curid = f[idcol] - if classcol not in fieldnames: - ttlstring.add("<" + str(curid) + "> <" + curclassid + "> .\n") - if first == 0: - ttlstring.add("<" + str(curclassid) + "> .\n") - ttlstring.add("<" + str(curclassid) + "> .\n") - else: - curclassid=f["http://www.w3.org/1999/02/22-rdf-syntax-ns#type"] - ttlstring=LayerUtils.exportGeometryType(curid, geom, vocab, literaltype, init, ttlstring) - if init: - init=False - fieldcounter = -1 - for propp in fieldnames: - fieldcounter += 1 - # if fieldcounter>=len(fieldnames): - # fieldcounter=0 - if includelist != None and fieldcounter < len(includelist) and includelist[fieldcounter] == False: - continue - prop = propp - print(str(fieldcounter)) - print(str(urilist) + "\n") - print(str(classurilist) + "\n") - print(str(includelist) + "\n") - if urilist != None and urilist[fieldcounter] != "": - print(urilist) - if not urilist[fieldcounter].startswith("http"): - print("Does not start with http") - prop = urllib.parse.quote(urilist[fieldcounter]) - else: - prop = urilist[fieldcounter] - print("New Prop from list: " + str(prop)) - if prop == "id": - continue - if not prop.startswith("http"): - prop = namespace + prop - if prop == "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" and "http" in str(f[propp]): - ttlstring.add("<" + str(f[propp]) + "> <" + str(prop) + "> .\n") - ttlstring.add("<" + str(f[propp]) + "> .\n") - ttlstring.add("<" + str(curid) + "> <"+str(f[propp])+"> .\n") - # elif urilist!=None and fieldcounter <"+prop+"> <"+str(f[propp])+"> .\n" - # if first<10: - # ttlstring+="<"+prop+"> .\n" - # ttlstring+="<"+prop+"> <"+curclassid+"> .\n" - # if classurilist[fieldcounter]!="": - # ttlstring+="<"+prop+"> <"+classurilist[fieldcounter]+"> .\n" - elif prop == "http://www.w3.org/2000/01/rdf-schema#label" or prop == "http://www.w3.org/2000/01/rdf-schema#comment" or ( - proptypelist != None and proptypelist[fieldcounter] == "AnnotationProperty"): - ttlstring.add("<" + curid + "> <" + prop + "> \"" + str(f[propp]).replace('"','\\"') + "\"^^ .\n") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - elif not f[propp] or f[propp] == None or f[propp] == "": - continue - elif proptypelist != None and proptypelist[fieldcounter] == "SubClass": - ttlstring.add("<" + curid + "> <" + str(f[propp]) + "> .\n") - ttlstring.add("<" + curid + "> <" + curclassid + "> .\n") - if first < 10: - ttlstring.add("<" + str(f[propp]) + "> .\n") - elif valuequeries != None and propp in valuequeries: - #ttlstring += "" - results=SPARQLUtils.executeQuery(valuequeries[propp][1],"".join(prefixes + valuequeries[propp][0].replace("%%" + propp + "%%","\"" + str(f[propp]) + "\""))) - ttlstring.add("<" + curid + "> <" + prop + "> <" + results["results"]["bindings"][0]["item"]["value"] + "> .") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - if classurilist[fieldcounter] != "": - ttlstring.add( "<" + prop + "> <" + classurilist[ - fieldcounter] + "> .\n") - elif valuemappings != None and propp in valuemappings and f[propp] in valuemappings[propp]: - ttlstring.add( "<" + curid + "> <" + prop + "> <" + str(valuemappings[propp][f[propp]]) + "> .\n") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - if classurilist[fieldcounter] != "": - ttlstring.add("<" + prop + "> <" + classurilist[fieldcounter] + "> .\n") - elif "http" in str(f[propp]) or ( - proptypelist != None and proptypelist[fieldcounter] == "ObjectProperty"): - ttlstring.add("<" + curid + "> <" + prop + "> <" + str(f[propp]) + "> .\n") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - if classurilist != None and fieldcounter < len(classurilist) and classurilist[fieldcounter] != "": - ttlstring.add("<" + prop + "> <" + classurilist[fieldcounter] + "> .\n") - elif re.match(r'^-?\d+$', str(f[propp])): - ttlstring.add("<" + curid + "> <" + prop + "> \"" + str(f[propp]) + "\"^^ .\n") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - ttlstring.add("<" + prop + "> .\n") - elif re.match(r'^-?\d+(?:\.\d+)?$', str(f[propp])): - ttlstring.add( "<" + curid + "> <" + prop + "> \"" + str(f[propp]) + "\"^^ .\n") - if first: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - ttlstring.add("<" + prop + "> .\n") - else: - ttlstring.add("<" + curid + "> <" + prop + "> \"" + str(f[propp]).replace('"','\\"') + "\"^^ .\n") - if first < 10: - ttlstring.add("<" + prop + "> .\n") - ttlstring.add("<" + prop + "> <" + curclassid + "> .\n") - ttlstring.add("<" + prop + "> .\n") - if first < 10: - first = first + 1 - return ccrs.ttlhead+"".join(ttlstring) - - @staticmethod - def layerToDot(layer, prefixes, urilist=None, classurilist=None, includelist=None, proptypelist=None, - valuemappings=None, valuequeries=None,exportNameSpace=None,exportIdCol=None,exportSetClass=None): - ttlstring=LayerUtils.layerToTTLString(layer, prefixes, urilist, classurilist, includelist, proptypelist, - valuemappings, valuequeries,exportNameSpace,exportIdCol,exportSetClass) - #g=Graph() - #g.parse(data=ttlstring,format="ttl") - #stream = io.StringIO() - #rdf2dot(g, stream) - return ""#stream.getvalue() - - - @staticmethod - def layerToGraphML(layer): - fieldnames = [field.name() for field in layer.fields()] - result="\n" - result+="\n" - result+="\n\n" - nodeset=set() - edgeset=set() - fidcounter=0 - edgecounter=0 - literalcounter=1 - nodeset.add( - "geo:SpatialObject\n") - - nodeset.add("geo:Feature\n") - nodeset.add( - "geo:Geometry\n") - edgeset.add("\n\n\n" + str( - "rdfs:subClassOf") + "\n\n\n\n") - edgeset.add("\n\n\n" + str( - "rdfs:subClassOf") + "\n\n\n\n") - for f in layer.getFeatures(): - geom = f.geometry() - nodeset.add("fid_"+str(fidcounter)+"\n") - fieldcounter=0 - for propp in fieldnames: - fieldcounter += 1 - prop = propp - if prop.startswith("http"): - toadd="" - if f[propp].startswith("http"): - toadd += SPARQLUtils.labelFromURI(str(f[propp]).replace("<", "").replace(">","")) + "\n" - else: - toadd+="","")+"]]>\n" - nodeset.add(toadd) - edgeset.add(""+str(propp)+"\n\n\n\n") - edgecounter+=1 - else: - nodeset.add(""+str(f[propp]).replace("<","").replace(">","")[0:10]+"\n") - edgeset.add("\n\n\n"+str(propp)+"\n\n\n\n") - literalcounter+=1 - edgecounter+=1 - nodeset.add("fid_" + str(fidcounter) + "_geom\n") - nodeset.add("" + str( - geom.asWkt())[0:10] + "\n") - edgeset.add("\n\n\n" + str( - "geom:asWkt") + "\n\n\n\n") - edgeset.add("\n\n\n" + str( - "rdf:type") + "\n\n\n\n") - edgeset.add("\n\n\n" + str( - "geom:asWkt") + "\n\n\n\n") - edgecounter+=1 - edgeset.add("\n\n\n" + str( - "rdf:type") + "\n\n\n\n") - literalcounter += 1 - edgecounter+=1 - fidcounter+=1 - result+="".join(nodeset) - result+="".join(edgeset) - result+="\n" - result+="" - return result - - - ## Fetch the currently loaded layers. - @staticmethod - def loadLayerList(target): - layers = QgsProject.instance().layerTreeRoot().children() - if not isinstance(target, list): - target=[target] - for elem in target: - elem.clear() - for layer in layers: - for elem in target: - elem.addItem(layer.name()) - - ## Exports a layer as GeoJSONLD. - # @param self The object pointer. - @staticmethod - def exportLayerAsGeoJSONLD(layer): - context = { - "geojson": "https://purl.org/geojson/vocab#", - "Feature": "geojson:Feature", - "FeatureCollection": "geojson:FeatureCollection", - "GeometryCollection": "geojson:GeometryCollection", - "LineString": "geojson:LineString", - "MultiLineString": "geojson:MultiLineString", - "MultiPoint": "geojson:MultiPoint", - "MultiPolygon": "geojson:MultiPolygon", - "Point": "geojson:Point", - "Polygon": "geojson:Polygon", - "bbox": { - "@container": "@list", - "@id": "geojson:bbox" - }, - "coordinates": { - "@container": "@list", - "@id": "geojson:coordinates" - }, - "features": { - "@container": "@set", - "@id": "geojson:features" - }, - "geometry": "geojson:geometry", - "id": "@id", - "properties": "geojson:properties", - "type": "@type", - "description": "http://purl.org/dc/terms/description", - "title": "http://purl.org/dc/terms/title" - } - fieldnames = [field.name() for field in layer.fields()] - currentgeo = {} - geos = [] - for f in layer.getFeatures(): - geom = f.geometry() - currentgeo = {'id': "", 'geometry': json.loads(geom.asJson()), 'properties': {}} - for prop in fieldnames: - if prop == "id": - currentgeo["id"] = f[prop] - else: - currentgeo["properties"][prop] = f[prop] - geos.append(currentgeo) - featurecollection = {"@context": context, "type": "FeatureCollection", - "@id": "http://example.com/collections/1", "features": geos} - return featurecollection \ No newline at end of file