Make the relations all work.

This commit is contained in:
Schuyler Erle 2011-09-01 09:19:46 +02:00
parent dcccbb3f66
commit 0a4bae4b43
4 changed files with 72 additions and 78 deletions

View File

@ -108,21 +108,23 @@ class Feature(models.Model):
def similar_features(self, max_distance=15000, scale_factor=2000, limit=20):
sql = """
SELECT *, %f * similarity / (distance + 1.0) AS score FROM (
SELECT f.*, r.*,
similarity(preferred_name, '%s') AS similarity,
st_distance_sphere(geometry, 'SRID=4326;%s') AS distance
SELECT *, %s * similarity / (distance + 1.0) AS score FROM (
SELECT f.*, r.feature1_id, r.feature2_id, r.relationship_type,
similarity(preferred_name, %s) AS similarity,
st_distance_sphere(geometry, %s) AS distance
FROM places_feature f LEFT JOIN places_relationship r
ON (f.id=r.feature_from OR f.id=r.feature_to)
WHERE geometry && st_buffer('%s', %f)
AND preferred_name %%%% '%s'
AND f.id <> %d
LIMIT %d
ON (f.id=r.feature1_id OR f.id=r.feature2_id)
WHERE geometry && st_buffer(%s, %s)
AND preferred_name %% %s
AND f.id <> %s
AND (r.feature1_id IS NULL OR r.feature1_id = %s OR r.feature2_id = %s)
LIMIT %s
) AS whatever
ORDER BY similarity / (distance + 1.0) DESC"""
qset = self.objects.raw(sql, (scale_factor, name, self.geometry.wkt,
self.geometry.wkt, max_distance*0.00001, name,
self.id, limit))
qset = type(self).objects.raw(sql, [scale_factor, self.preferred_name,
"SRID=4326;" + self.geometry.centroid.wkt,
self.geometry.centroid.wkt, max_distance*0.00001,
self.preferred_name, self.id, self.id, self.id, limit])
return qset
LANGUAGE_CHOICES = (

View File

@ -2,7 +2,8 @@
from django.shortcuts import render_to_response, get_object_or_404
from ox.django.shortcuts import render_to_json_response, get_object_or_404_json
from django.template import RequestContext
from models import Feature, FeatureType, AuthorityRecord
from models import Feature, FeatureType, AuthorityRecord, Relationship
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator, InvalidPage, EmptyPage
def search(request):
@ -56,31 +57,6 @@ def search_json(request):
}
return render_to_json_response(d)
'''
This function will be removed. No need for this page.
'''
'''
def search_related(request):
id = request.GET.get("id", "0")
feature = get_object_or_404(Feature, pk=id)
similar_features = feature.similar_features()
d = []
for s in similar_features:
d.append({
'id': s.id,
'feature_type': FeatureType.objects.get(pk=s.feature_type_id),
'preferred_name': s.preferred_name,
'similarity': s.similarity,
'distance': s.distance,
'relationship': ''
})
return render_to_response("search_related.html", {
'feature': feature,
'similar_features': d
})
'''
def search_related_json(request, id):
# id = request.GET.get("id", "0")
id = str(id)
@ -88,15 +64,15 @@ def search_related_json(request, id):
similar_features = feature.similar_features()
d = []
for f in similar_features:
for s in similar_features:
time_frame = relates_to = related_by = ""
if s.time_frame is not None:
time_frame = s.time_frame.description
else:
time_frame = ''
if s.relationship_type is not None and s.feature_to == s.id:
relationship = s.relationship_type
else:
relationship = ''
if s.relationship_type is not None:
if s.feature2_id == s.id:
relates_to = s.relationship_type
elif s.feature1_id == s.id:
related_by = s.relationship_type
d.append({
'id': s.id,
'feature_type': s.feature_type.name, #FeatureType.objects.get(pk=s.feature_type_id).name,
@ -105,7 +81,8 @@ def search_related_json(request, id):
'distance': s.distance,
'time_frame': time_frame,
'is_primary': s.is_primary,
'relationship': relationship
'relates_to': relates_to,
'related_by': related_by
})
return render_to_json_response(d)
@ -139,14 +116,23 @@ def add_relation(request):
if not request.user.is_staff:
return render_to_json_response({'error': 'insufficient permissions error. try logging in again? are you staff / admin?'})
feature1 = get_object_or_404_json(Feature, feature1)
feature2 = get_object_or_404_json(Feature, feature2)
Relationship(feature_from=feature1, feature_to=feature2, relationship_type=relation).save()
feature1 = get_object_or_404_json(Feature, pk=feature1)
feature2 = get_object_or_404_json(Feature, pk=feature2)
rel_obj = None
try:
rel_obj = Relationship.objects.get(feature1=feature1, feature2=feature2, relationship_type=relation)
except ObjectDoesNotExist:
rel_obj = Relationship(feature1=feature1, feature2=feature2, relationship_type=relation)
verb = "created"
if relation == "":
if rel_obj.id:
verb = "deleted"
rel_obj.delete()
else:
return render_to_json_response({'error': 'relation is already deleted'})
else:
rel_obj.save()
if relation == "subsumes":
feature2.is_primary = False
feature2.save()
return render_to_json_response({'success': 'relation made successfully.'})
return render_to_json_response({'success': 'relation %s successfully.' % verb})

View File

@ -24,7 +24,7 @@ function getRow(d) {
var distance = Math.round(parseFloat(d.distance));
$('<td />').text(d.feature_type).appendTo($tr);
$('<td />').text(similarity).appendTo($tr);
$('<td />').text(d.distance).appendTo($tr);
$('<td />').text(distance).appendTo($tr);
$('<td />').text(d.time_frame).appendTo($tr);
var $primary_td = $('<td />').appendTo($tr);
var $checkbox = $('<input />').attr("type", "checkbox").attr("disabled", "disabled").appendTo($primary_td);
@ -34,7 +34,13 @@ function getRow(d) {
$.noop();
}
var $relations_td = $('<td />').appendTo($tr);
var $relations_td = $('<td />').prependTo($tr);
if (d.related_by != '') {
var verb = d.related_by.replace(/e?s$/i, "");
var related_by = "is " + verb + "ed by";
var $relationselect = $('<a />').attr("href", "/admin/places/feature/" + d.id)
.text(related_by).appendTo($relations_td);
} else {
var $relationselect = $('<select />').appendTo($relations_td);
var $opt1 = $('<option />').val('').text('---').appendTo($relationselect);
@ -43,13 +49,14 @@ function getRow(d) {
$('<option />').val(r).text(r).appendTo($relationselect);
}
if (d.relationship != '') {
if (d.relates_to != '') {
$relationselect.children().each(function() {
if ($(this).val() == d.relationship) {
if ($(this).val() == d.relates_to) {
$(this).attr("selected", "selected");
}
});
}
}
$relationselect.change(function() {
var feature1 = FEATURE_ID;

View File

@ -20,6 +20,9 @@
<table id="similarTable" class="customTable">
<thead>
<tr>
<th>
Relation
</th>
<th>
Name
</th>
@ -30,7 +33,7 @@
Similarity
</th>
<th>
Distance
Distance (m)
</th>
<th>
Time Frame
@ -39,10 +42,6 @@
Is Primary?
</th>
<th>
Relation
</th>
</tr>
</thead>
<tbody>