exploreData.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #from csv import reader
  2. #import json
  3. from pprint import pprint
  4. from SPARQLWrapper import SPARQLWrapper, JSON
  5. #import ssl
  6. #import sys
  7. import time
  8. #import urllib.request, urllib.parse, urllib.error
  9. import rdflib
  10. from rdflib.plugins import sparql
  11. def getPOIsWithEqualNameAndTypeWithinDistance(distance):
  12. fc_sparql = SPARQLWrapper("https://www.foodie-cloud.org/sparql")
  13. q = """
  14. PREFIX unit: <http://www.opengis.net/def/uom/OGC/1.0/>
  15. SELECT DISTINCT ?Resource1 ?Resource2 ?distance
  16. FROM <http://www.sdi4apps.eu/poi/czech>
  17. WHERE {{
  18. ?Resource1 a ?type1 .
  19. ?Resource1 rdfs:label ?label1 .
  20. ?Resource1 ogcgs:asWKT ?geo1 .
  21. ?Resource2 a ?type2 .
  22. ?Resource2 rdfs:label ?label2 .
  23. ?Resource2 ogcgs:asWKT ?geo2 .
  24. FILTER (lcase(str(?label1)) = lcase(str(?label2) )) .
  25. FILTER ( ?Resource1 != ?Resource2 && ?Resource1 < ?Resource2)
  26. FILTER (NOT EXISTS {{?x a ?Resource1}} && NOT EXISTS {{?y a ?Resource2}}) .
  27. BIND (ogcgsf:distance ( ?geo1 , ?geo2 , unit:meter ) as ?distance) .
  28. FILTER ( ?distance < {0})
  29. }}
  30. LIMIT 100
  31. OFFSET 0
  32. """.format(distance)
  33. print(q)
  34. fc_sparql.setQuery(q)
  35. fc_sparql.setReturnFormat(JSON)
  36. results = fc_sparql.query().convert()
  37. #for result in results["results"]["bindings"]:
  38. #print(result)
  39. return results["results"]["bindings"]
  40. #g = rdflib.Graph()
  41. print("= LOOKING FOR DUPLICATE POIS =")
  42. distance = input("Distance to search within: ")
  43. print("Searching for duplicate POIs within {0} m ...".format(distance))
  44. t_1 = time.time()
  45. result = getPOIsWithEqualNameAndTypeWithinDistance(distance)
  46. print("Finished in {}".format(time.time()-t_1))
  47. pprint(result)
  48. #g.parse("ontOSM&qids.owl", format="turtle")
  49. #print("Input RDF graph read with {} statements!\n".format(len(g)))
  50. #with open("ontOSM_beta.owl", 'w', encoding="utf-8") as fh:
  51. # fh.write(g.serialize(format="turtle").decode())
  52. #print("File \"ontOSM_beta.owl\" succesfully created")