Skip to content

Commit

Permalink
Zoom levels depend on focal length, now in the model,
Browse files Browse the repository at this point in the history
but not sure ig it's a good idea
  • Loading branch information
frasanz committed Oct 27, 2024
1 parent 9f2f1c1 commit c3a48b3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 4 deletions.
3 changes: 2 additions & 1 deletion frontend/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ $(document).ready(function () {
data: JSON.stringify(data),

success: function (response) {
console.log(response);
georeferencingModal.hide();
let noCacheUrl = '/media/georeferenced/' + image_name + geoattempt_hash + '/{z}/{x}/{y}.png' + '?nocache=' + new Date().getTime();
if (lyr)
Expand Down Expand Up @@ -194,7 +195,7 @@ $(document).ready(function () {
var currentZoom = map.getZoom();
map.setZoom(11);
setTimeout(function () {
map.setZoom(12);
map.setZoom(response.maxZoom);
}, 300); // Adjust the delay if necessary, 300ms works in most cases
$('#Submit').removeClass('disabled');

Expand Down
18 changes: 18 additions & 0 deletions georeferencing/migrations/0036_geoattempt_maxzoom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.16 on 2024-10-27 07:43

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('georeferencing', '0035_batch_type_alter_batch_originalimages'),
]

operations = [
migrations.AddField(
model_name='geoattempt',
name='maxZoom',
field=models.IntegerField(default=2),
),
]
17 changes: 17 additions & 0 deletions georeferencing/migrations/0037_remove_geoattempt_maxzoom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.16 on 2024-10-27 09:33

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('georeferencing', '0036_geoattempt_maxzoom'),
]

operations = [
migrations.RemoveField(
model_name='geoattempt',
name='maxZoom',
),
]
23 changes: 23 additions & 0 deletions georeferencing/migrations/0038_image_maxzoom_image_minzoom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.2.16 on 2024-10-27 09:57

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('georeferencing', '0037_remove_geoattempt_maxzoom'),
]

operations = [
migrations.AddField(
model_name='image',
name='maxZoom',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='image',
name='minZoom',
field=models.IntegerField(blank=True, null=True),
),
]
27 changes: 26 additions & 1 deletion georeferencing/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,41 @@ class Image(models.Model):
smallImageURL = models.URLField( blank = True, null = True)
batch = models.ForeignKey('Batch', on_delete=models.CASCADE, blank=True, null=True)
replicas = models.IntegerField(default=5)
maxZoom = models.IntegerField(blank=True, null=True)
minZoom = models.IntegerField(blank=True, null=True)

def get_zoom_levels(self):
zoom_levels = [
(30, (4,6)),
(60, (5,7)),
(120, (6,8)),
(240, (7,9)),
(480, (8,10)),
]

for focal, (min_zoom, max_zoom) in zoom_levels:
if self.focalLength <= focal:
return min_zoom, max_zoom
return 7,12

def save(self, *args, **kwargs):
print(self.focalLength)
if self.focalLength is not None:
self.minZoom, self.maxZoom = self.get_zoom_levels()
super().save(*args, **kwargs)

def __str__(self):
return str(self.name)




class Batch(models.Model):
BATCH_CHOICES = (
('SEARCH', 'Search'),
('LIST', 'List'),
)

name = models.CharField(max_length=100)
createdDateTime = models.DateTimeField(auto_now_add=True)
type = models.CharField(
Expand Down
2 changes: 2 additions & 0 deletions georeferencing/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ def get_photo_taken(self,obj):
def get_focal_length(self, obj):
return obj.image.focalLength



class MiniGeoAttemptSerializer(serializers.ModelSerializer):
"""
Serializer for the GeoAttempt model, providing a minimal set of fields.
Expand Down
16 changes: 14 additions & 2 deletions georeferencing/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def patch(self, request, pk=None):
print('Starting the testing process')
# We increment the number of tries
geoattemp.numberTries += 1
geoattemp.maxZoom = 5
geoattemp.save()

# Ensure the georeferenced directory exists
Expand Down Expand Up @@ -163,7 +164,18 @@ def patch(self, request, pk=None):
return Response({"error": f"Failed to remove previous tiles: {e.stderr}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

# Fourth command: (-r bilinear is slower, but....)
command = 'gdal2tiles.py -z 7-12 -r near -s EPSG:4326'
# zoom will depend on the focal length
if geoattemp.image.focalLength < 30:
zoom = "4-6"
elif geoattemp.image.focalLength < 50:
zoom = "5-8"
elif geoattemp.image.focalLength < 100:
zoom = "6-10"
elif geoattemp.image.focalLength < 200:
zoom = "7-11"
else:
zoom = "7-12"
command = 'gdal2tiles.py -z ' + zoom + ' -r near -s EPSG:4326'
command += ' media/georeferenced/' + geoattemp.image.name + geoattemp.hash + '.tif'
command += ' media/georeferenced/' + geoattemp.image.name + geoattemp.hash
print(command)
Expand All @@ -172,7 +184,7 @@ def patch(self, request, pk=None):
subprocess.run(command, shell=True, capture_output=True, text=True, check=True)
except subprocess.CalledProcessError as e:
return Response({"error": f"gdal2tiles failed: {e.stderr}"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)

return Response(serializer.data, status=status.HTTP_200_OK)
# That means that the user is happy with the final result
elif request.data['status'] == 'DONE':
# let's start the georeferencing process
Expand Down

0 comments on commit c3a48b3

Please sign in to comment.