web/hdalab/management/commands/query_geo_inclusion.py
author veltr
Wed, 22 Feb 2012 18:55:35 +0100
changeset 122 fde8335a037c
permissions -rw-r--r--
Added Geographic Inclusion in the Django version (server only)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
122
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     2
'''
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     3
Created on Feb 22, 2012
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     4
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     5
@author: raphv
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     6
'''
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     7
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     8
from django.core.management.base import NoArgsCommand
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
     9
from django.core.management.color import no_style
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    10
from hdabo.utils import show_progress
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    11
from hdabo.models import Tag
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    12
from hdalab.models import Country, GeoInclusion
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    13
from SPARQLWrapper import SPARQLWrapper, JSON
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    14
import re
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    15
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    16
class Command(NoArgsCommand):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    17
    def handle_noargs(self, **options):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    18
        self.style = no_style()
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    19
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    20
        qs = Tag.objects.filter(category__label="Localisation").exclude(dbpedia_uri = None)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    21
        total = qs.count()
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    22
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    23
        endpoint = SPARQLWrapper("http://dbpedia.org/sparql")
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    24
        endpoint.setReturnFormat(JSON)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    25
        sparqltext = """
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    26
            SELECT ?resource WHERE {
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    27
             { <%s> ?resource <http://dbpedia.org/ontology/Country> . }
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    28
            UNION
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    29
             { <%s> <http://dbpedia.org/ontology/country> ?resource . }
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    30
            }
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    31
        """
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    32
        resourceprefix = "http://dbpedia.org/resource/"
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    33
        identityuri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    34
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    35
        writer = None
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    36
        
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    37
        for i,tag in enumerate(qs):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    38
            endpoint.setQuery(sparqltext % (tag.dbpedia_uri, tag.dbpedia_uri))
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    39
    
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    40
            results = endpoint.query().convert()['results']['bindings']
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    41
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    42
            if len(results) == 1: # We don't want places located in multiple countries
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    43
                
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    44
                resourceuri = results[0]['resource']['value']
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    45
                
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    46
                if re.match(resourceprefix, resourceuri):
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    47
                    countrytxt = re.findall('([^/]+$)', resourceuri)[0]
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    48
                    
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    49
                    country, created = Country.objects.get_or_create(dbpedia_uri=resourceuri)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    50
                    GeoInclusion.objects.get_or_create(tag=tag, country=country)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    51
                    
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    52
                if resourceuri == identityuri:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    53
                    countrytxt = '<is a country>'
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    54
                    
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    55
                    country, created = Country.objects.get_or_create(dbpedia_uri=tag.dbpedia_uri)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    56
                    GeoInclusion.objects.get_or_create(tag=tag, country=country)
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    57
                
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    58
            else:
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    59
                countrytxt = '<unknown>'
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    60
            
fde8335a037c Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff changeset
    61
            writer = show_progress(i+1, total, '%s => %s'%(tag.label, countrytxt), 50, writer)