Skip to content

Commit

Permalink
DBC22-893: added transformations and check for road conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ray-oxd committed Apr 15, 2024
1 parent 9116a06 commit e4c16fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/backend/apps/event/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ class Event(BaseModel):
end = models.DateTimeField(null=True)

def save(self, *args, **kwargs):
# Put buffer on LineStrings on creation only
if self._state.adding and self.location and self.location.geom_type == 'LineString':
self.polygon = self.location.buffer_with_style(0.0182, end_cap_style=2) # ~2km buffer in degrees
# Put buffer on road condition LineStrings on creation only
if self._state.adding and self.location and self.location.geom_type == 'LineString' and self.pk.startswith('DBCRCON'):
self.location.transform(3857) # Transform to 3857 before adding buffer
self.polygon = self.location.buffer_with_style(2000, end_cap_style=2) # 2km buffer

# Transform back to 4326 before updating
self.location.transform(4326)
self.polygon.transform(4326)

super().save(*args, **kwargs)
14 changes: 13 additions & 1 deletion src/backend/apps/event/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ def build_data_diff(current_obj, new_obj_data):
# {'coordinates': [-122.601346, 49.143921], 'type': 'Point'}
if new_field_data['type'] == 'Point':
data_diff[field] = Point(new_field_data['coordinates'])
data_diff['polygon'] = None

else:
ls = LineString(new_field_data['coordinates'])
data_diff[field] = ls
data_diff['polygon'] = ls.buffer_with_style(distance=0.0182, end_cap_style=2) # ~2km buffer in degrees

# Add buffer to road condition linestrings
if new_obj_data['id'].startswith('DBCRCON'):
ls.transform(3857) # Transform to 3857 before adding buffer
data_diff['polygon'] = ls.buffer_with_style(2000, end_cap_style=2) # Add 2km buffer

# transform back to 4326 before updating
ls.transform(4326)
data_diff['polygon'].transform(4326)

else:
data_diff['polygon'] = None

else:
data_diff[field] = new_field_data
Expand Down

0 comments on commit e4c16fe

Please sign in to comment.