web/hdalab/management/commands/geojson_transform.py
changeset 135 dd6578e36a57
parent 123 94fc5f5b5cfd
equal deleted inserted replaced
134:75f8f05f9a60 135:dd6578e36a57
     4 '''
     4 '''
     5 from django.core.management.base import BaseCommand, CommandError
     5 from django.core.management.base import BaseCommand, CommandError
     6 from django.utils.http import urlquote
     6 from django.utils.http import urlquote
     7 import django.utils.simplejson as json
     7 import django.utils.simplejson as json
     8 from SPARQLWrapper import SPARQLWrapper, JSON
     8 from SPARQLWrapper import SPARQLWrapper, JSON
       
     9 
       
    10 #import pydevd #@UnresolvedImport
       
    11 #pydevd.settrace(suspend=False)
       
    12 
     9 
    13 
    10 class Command(BaseCommand):
    14 class Command(BaseCommand):
    11     '''
    15     '''
    12     Command to export tags
    16     Command to export tags
    13     '''
    17     '''
    57             start += grp
    61             start += grp
    58         
    62         
    59         base_uri_list = [base_uris[uri] for uri in base_uris]
    63         base_uri_list = [base_uris[uri] for uri in base_uris]
    60         
    64         
    61         french_labels = {}
    65         french_labels = {}
       
    66         labels = {}
    62         
    67         
    63         start = 0
    68         start = 0
    64         while start < len(base_uri_list):
    69         while start < len(base_uri_list):
    65             
    70             
    66             sparql = """
    71             sparql = """
    67                 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    72                 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    68                 SELECT ?country ?label WHERE {
    73                 SELECT ?country ?label WHERE {
    69                   ?country rdfs:label ?label .
    74                   ?country rdfs:label ?label .
    70                   FILTER (?country in (<%s>)) .
    75                   FILTER (?country in (<%s>))
    71                   FILTER langMatches( lang(?label), "fr" )
       
    72                 }
    76                 }
    73             """ % '>, <'.join(base_uri_list[start:start + grp])
    77             """ % '>, <'.join(base_uri_list[start:start + grp])
    74             
    78             
    75             endpoint.setQuery(sparql)
    79             endpoint.setQuery(sparql)
    76             results = endpoint.query().convert()
    80             results = endpoint.query().convert()
    77             
    81             
       
    82             print repr(results)
       
    83             
    78             for r in results["results"]["bindings"]:
    84             for r in results["results"]["bindings"]:
    79                 country = r["country"]["value"]
    85                 country = r["country"]["value"]
    80                 label = r["label"]["value"]
    86                 label = r["label"]["value"]
    81                 french_labels[country] = label
    87                 if country not in labels:
       
    88                     labels[country] = {}
       
    89                 labels[country][r["label"]["xml:lang"]] = label 
       
    90                 if r["label"]["xml:lang"] == 'fr':
       
    91                     french_labels[country] = label
    82                 print "%s label: %s"%(country, label)
    92                 print "%s label: %s"%(country, label)
    83             
    93             
    84             start += grp
    94             start += grp
    85         
    95         
    86         for feature in geojson['features']:
    96         for feature in geojson['features']:
    87             base_uri = base_uris[uris[feature['properties']['name']]]
    97             base_uri = base_uris[uris[feature['properties']['name']]]
    88             feature['properties']['dbpedia_uri'] = base_uri
    98             feature['properties']['dbpedia_uri'] = base_uri
       
    99                         
       
   100             if labels.has_key(base_uri):
       
   101                 feature['properties']['labels'] = labels[base_uri]
    89             if french_labels.has_key(base_uri):
   102             if french_labels.has_key(base_uri):
    90                 feature['properties']['label_fr'] = french_labels[base_uri]
   103                 feature['properties']['label_fr'] = french_labels[base_uri]
    91             else:
   104             else:
    92                 print "No label for %s"%base_uri
   105                 print "No label for %s"%base_uri
    93         
   106