| author | ymh <ymh.work@gmail.com> |
| Fri, 19 Jul 2024 09:38:03 +0200 | |
| changeset 704 | b5835dca2624 |
| parent 359 | 46ad324f6fe4 |
| child 693 | 09e00f38d177 |
| permissions | -rw-r--r-- |
|
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 |
''' |
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
266
diff
changeset
|
7 |
from django.conf import settings |
|
122
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 |
|
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
283
diff
changeset
|
20 |
GeoInclusion.objects.all().delete() |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
283
diff
changeset
|
21 |
Country.objects.all().delete() |
|
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
283
diff
changeset
|
22 |
|
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
23 |
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
|
24 |
total = qs.count() |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
25 |
|
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
266
diff
changeset
|
26 |
#endpoint = SPARQLWrapper("http://dbpedia.org/sparql") |
|
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
266
diff
changeset
|
27 |
endpoint = SPARQLWrapper(settings.DBPEDIA_URI_TEMPLATE % ( 'sparql', '' )) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
28 |
endpoint.setReturnFormat(JSON) |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
29 |
sparqltext = """ |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
30 |
SELECT ?resource WHERE { |
| 283 | 31 |
{ <%s> ?resource <http://dbpedia.org/ontology/Country> . } |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
32 |
UNION |
| 283 | 33 |
{ <%s> <http://dbpedia.org/ontology/country> ?resource . } |
|
122
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 |
""" |
|
281
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
266
diff
changeset
|
36 |
#resourceprefix = "http://dbpedia.org/resource/" |
|
bc0f26b1acc2
Hdalab : commands now work after update. Requests update with a dbpedia url from settings.
cavaliet
parents:
266
diff
changeset
|
37 |
resourceprefix = settings.DBPEDIA_URI_TEMPLATE % ( 'resource', '' ) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
38 |
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
|
39 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
40 |
writer = None |
|
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 |
for i,tag in enumerate(qs): |
| 283 | 43 |
endpoint.setQuery(sparqltext % (tag.dbpedia_uri, tag.dbpedia_uri)) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
44 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
45 |
results = endpoint.query().convert()['results']['bindings'] |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
46 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
47 |
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
|
48 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
49 |
resourceuri = results[0]['resource']['value'] |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
50 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
51 |
if re.match(resourceprefix, resourceuri): |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
52 |
countrytxt = re.findall('([^/]+$)', resourceuri)[0] |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
53 |
|
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
283
diff
changeset
|
54 |
country, _ = Country.objects.get_or_create(dbpedia_uri=resourceuri) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
55 |
GeoInclusion.objects.get_or_create(tag=tag, country=country) |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
56 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
57 |
if resourceuri == identityuri: |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
58 |
countrytxt = '<is a country>' |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
59 |
|
|
359
46ad324f6fe4
Correct qery_dbpedia and improve model.
ymh <ymh.work@gmail.com>
parents:
283
diff
changeset
|
60 |
country, _ = Country.objects.get_or_create(dbpedia_uri=tag.dbpedia_uri) |
|
122
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
61 |
GeoInclusion.objects.get_or_create(tag=tag, country=country) |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
62 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
63 |
else: |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
64 |
countrytxt = '<unknown>' |
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
65 |
|
|
fde8335a037c
Added Geographic Inclusion in the Django version (server only)
veltr
parents:
diff
changeset
|
66 |
writer = show_progress(i+1, total, '%s => %s'%(tag.label, countrytxt), 50, writer) |