Skip to content

Commit ca5a01c

Browse files
feat: add source imagery type detection and update prepare_data function
1 parent 8d52fb0 commit ca5a01c

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

backend/core/models.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
from django.contrib.gis.db import models as geomodels
24
from django.contrib.postgres.fields import ArrayField
35
from django.core.exceptions import ValidationError
@@ -112,6 +114,36 @@ class Training(models.Model):
112114
freeze_layers = models.BooleanField(default=False)
113115
centroid = geomodels.PointField(srid=4326, null=True, blank=True)
114116

117+
def get_source_imagery_type(self):
118+
"""
119+
Returns: 'TILEJSON', 'XYZ', 'TMS', or 'UNKNOWN'
120+
"""
121+
## source ref here : https://github.com/hotosm/fAIr/blob/develop/frontend/src/utils/regex-utils.ts
122+
if not self.source_imagery:
123+
return "UNKNOWN"
124+
xyz_pattern = re.compile(
125+
r"^https?:\/\/[^\/]+(?:\/[^\/]+)*\/\{z\}\/\{x\}\/\{y\}(?:@[0-9a-z]+)?(?:\.(jpg|png|jpeg|webp))?(?:\?.*)?$",
126+
re.IGNORECASE,
127+
)
128+
tms_pattern = re.compile(
129+
r"^https?:\/\/[^\/]+(?:\/[^\/]+)*\/\{z\}\/\{x\}\/\{-y\}(?:@[0-9a-z]+)?(?:\.(jpg|png|jpeg|webp))?(?:\?.*)?$",
130+
re.IGNORECASE,
131+
)
132+
tilejson_pattern = re.compile(
133+
r"^https?:\/\/[^\/]+(?:\/[^\/?#]+)*\/[^\/?#]+\.json(?:\?.*)?$",
134+
re.IGNORECASE,
135+
)
136+
137+
url = self.source_imagery
138+
if tilejson_pattern.match(url):
139+
return "TILEJSON"
140+
elif xyz_pattern.match(url):
141+
return "XYZ"
142+
elif tms_pattern.match(url):
143+
return "TMS"
144+
else:
145+
return "UNKNOWN"
146+
115147

116148
class Feedback(models.Model):
117149
ACTION_CHOICES = (

backend/core/tasks.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ def upload_to_s3(path, parent=None):
316316
).upload(path)
317317

318318

319-
def prepare_data(inst, dataset_id, feedback, zoom_level, imagery, input_path):
319+
def prepare_data(
320+
inst, dataset_id, feedback, zoom_level, imagery, input_path, imagery_type
321+
):
320322
from geomltoolkits.downloader import tms as TMSDownloader
321323

322324
safe_rmtree(input_path)
@@ -349,6 +351,7 @@ def prepare_data(inst, dataset_id, feedback, zoom_level, imagery, input_path):
349351
georeference=False,
350352
crs="3857",
351353
extension="png",
354+
is_tilejson=("TILEJSON" == imagery_type),
352355
)
353356
)
354357
input_path = pathlib.Path(input_path)
@@ -457,7 +460,13 @@ def train_model(
457460
)
458461

459462
input_path, aoi_ser, labels = prepare_data(
460-
inst, dataset_id, feedback, zoom_level, source_imagery, input_path
463+
inst,
464+
dataset_id,
465+
feedback,
466+
zoom_level,
467+
source_imagery,
468+
input_path,
469+
inst.get_source_imagery_type,
461470
)
462471
output_path = os.path.join(
463472
settings.TRAINING_WORKSPACE,

0 commit comments

Comments
 (0)