Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
situx committed Jan 11, 2024
1 parent 69b546f commit a1605bd
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 173 deletions.
45 changes: 0 additions & 45 deletions src/sparqlunicorn_ontdoc/doc/docutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,7 @@

class DocUtils:

@staticmethod
def resolveGeoLiterals(pred,object,graph,geojsonrep,nonns,subject=None):
if subject!=None and isinstance(object, Literal) and (str(pred) in DocConfig.geopairproperties):
pairprop = DocConfig.geopairproperties[str(pred)]["pair"]
latorlong = DocConfig.geopairproperties[str(pred)]["islong"]
othervalue = ""
for obj in graph.objects(subject, URIRef(pairprop)):
othervalue = str(obj)
if latorlong:
geojsonrep = {"type": "Point", "coordinates": [float(str(othervalue)), float(str(object))]}
else:
geojsonrep = {"type": "Point", "coordinates": [float(str(object)), float(str(othervalue))]}
elif isinstance(object, Literal) and (
str(pred) in DocConfig.geoproperties or str(object.datatype) in DocConfig.geoliteraltypes):
geojsonrep = DocUtils.processLiteral(str(object), str(object.datatype), "")
elif isinstance(object, URIRef) and nonns:
for pobj in graph.predicate_objects(object):
if isinstance(pobj[1], Literal) and (
str(pobj[0]) in DocConfig.geoproperties or str(
pobj[1].datatype) in DocConfig.geoliteraltypes):
geojsonrep = DocUtils.processLiteral(str(pobj[1]), str(pobj[1].datatype), "")
return geojsonrep

@staticmethod
def processLiteral(literal, literaltype, reproject,currentlayergeojson=None,triplestoreconf=None):
#print("Process literal: " + str(literal) + " --- " + str(literaltype))
try:
if "wkt" in literaltype.lower():
crsuri=""
curcrs=None
if "http" in literal:
crsuri=literal[0:literal.rfind('>')].replace("<","")
curcrs=crsuri[crsuri.rfind('/')+1:]
literal=literal[literal.rfind('>')+1:].strip()
shapelygeom=shapely.wkt.loads(literal)
thejson=json.loads(json.dumps(shapely.geometry.mapping(shapelygeom),indent=2))
if curcrs!=None:
thejson["crs"]=curcrs
return thejson
if "geojson" in literaltype.lower():
return literal
except Exception as e:
print("Literal: "+str(literal)+" "+str(literaltype))
print(e)
print(traceback.format_exc())
return None

@staticmethod
def getLDFilesFromFolder(folder):
Expand Down
57 changes: 57 additions & 0 deletions src/sparqlunicorn_ontdoc/doc/literalutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from rdflib import URIRef, Literal
import os
import json
import shapely
import traceback

from doc.docutils import DocUtils
from doc.docconfig import DocConfig

class LiteralUtils:

@staticmethod
def resolveGeoLiterals(pred, object, graph, geojsonrep, nonns, subject=None):
if subject != None and isinstance(object, Literal) and (str(pred) in DocConfig.geopairproperties):
pairprop = DocConfig.geopairproperties[str(pred)]["pair"]
latorlong = DocConfig.geopairproperties[str(pred)]["islong"]
othervalue = ""
for obj in graph.objects(subject, URIRef(pairprop)):
othervalue = str(obj)
if latorlong:
geojsonrep = {"type": "Point", "coordinates": [float(str(othervalue)), float(str(object))]}
else:
geojsonrep = {"type": "Point", "coordinates": [float(str(object)), float(str(othervalue))]}
elif isinstance(object, Literal) and (
str(pred) in DocConfig.geoproperties or str(object.datatype) in DocConfig.geoliteraltypes):
geojsonrep = DocUtils.processLiteral(str(object), str(object.datatype), "")
elif isinstance(object, URIRef) and nonns:
for pobj in graph.predicate_objects(object):
if isinstance(pobj[1], Literal) and (
str(pobj[0]) in DocConfig.geoproperties or str(
pobj[1].datatype) in DocConfig.geoliteraltypes):
geojsonrep = DocUtils.processLiteral(str(pobj[1]), str(pobj[1].datatype), "")
return geojsonrep

@staticmethod
def processLiteral(literal, literaltype, reproject, currentlayergeojson=None, triplestoreconf=None):
# print("Process literal: " + str(literal) + " --- " + str(literaltype))
try:
if "wkt" in literaltype.lower():
crsuri = ""
curcrs = None
if "http" in literal:
crsuri = literal[0:literal.rfind('>')].replace("<", "")
curcrs = crsuri[crsuri.rfind('/') + 1:]
literal = literal[literal.rfind('>') + 1:].strip()
shapelygeom = shapely.wkt.loads(literal)
thejson = json.loads(json.dumps(shapely.geometry.mapping(shapelygeom), indent=2))
if curcrs != None:
thejson["crs"] = curcrs
return thejson
if "geojson" in literaltype.lower():
return literal
except Exception as e:
print("Literal: " + str(literal) + " " + str(literaltype))
print(e)
print(traceback.format_exc())
return None
Loading

0 comments on commit a1605bd

Please sign in to comment.