Skip to content

Commit

Permalink
Merge pull request #724 from Freika/feature/tracks
Browse files Browse the repository at this point in the history
Paths for trips
  • Loading branch information
Freika authored Jan 29, 2025
2 parents e06b2f0 + 8a309a2 commit 375c50d
Show file tree
Hide file tree
Showing 30 changed files with 333 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- image: cimg/ruby:3.3.4
environment:
RAILS_ENV: test
- image: cimg/postgres:13.3
- image: cimg/postgres:13.3-postgis
environment:
POSTGRES_USER: postgres
POSTGRES_DB: test_database
Expand Down
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

# 0.23.6 - 2025-01-23
# 0.23.6 - 2025-01-29

### Added

- Enabled Postgis extension for PostgreSQL.
- Trips are now store their paths in the database independently of the points.
- Trips are now being rendered on the map using their precalculated paths instead of list of coordinates.

### Changed

- Requesting photos on the Map page now uses the start and end dates from the URL params. #589

# 0.23.5 - 2025-01-22

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#### **Did you write a patch that fixes a bug?**

* Open a new GitHub pull request with the patch.
* Open a new GitHub pull request with the patch against the `dev` branch.

* Ensure the PR description clearly describes the problem and solution. Include the relevant issue number if applicable.

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ gem 'activerecord-postgis-adapter', github: 'StoneGod/activerecord-postgis-adapt
gem 'puma'
gem 'pundit'
gem 'rails', '~> 8.0'
gem 'rgeo'
gem 'rswag-api'
gem 'rswag-ui'
gem 'shrine', '~> 3.6'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ DEPENDENCIES
pundit
rails (~> 8.0)
redis
rgeo
rspec-rails
rswag-api
rswag-specs
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Donate using crypto: [0x6bAd13667692632f1bF926cA9B421bEe7EaEB8D4](https://ethers
- Explore statistics like the number of countries and cities visited, total distance traveled, and more!

📄 **Changelog**: Find the latest updates [here](CHANGELOG.md).

👩‍💻 **Contribute**: See [CONTRIBUTING.md](CONTRIBUTING.md) for how to contribute to Dawarich.
---

## ⚠️ Disclaimer
Expand Down
3 changes: 2 additions & 1 deletion app/assets/stylesheets/actiontext.css
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
background-color: white !important;
}

.trix-content {
.trix-content-editor {
min-height: 10rem;
width: 100%;
}
5 changes: 0 additions & 5 deletions app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ def index
end

def show
@coordinates = @trip.points.pluck(
:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id,
:country
).map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] }

@photo_previews = Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do
@trip.photo_previews
end
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/controllers/datetime_controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// This controller is being used on:
// - trips/new
// - trips/edit

import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
Expand Down
4 changes: 2 additions & 2 deletions app/javascript/controllers/maps_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ export default class extends Controller {
}

const urlParams = new URLSearchParams(window.location.search);
const startDate = urlParams.get('start_at')?.split('T')[0] || new Date().toISOString().split('T')[0];
const endDate = urlParams.get('end_at')?.split('T')[0] || new Date().toISOString().split('T')[0];
const startDate = urlParams.get('start_at') || new Date().toISOString();
const endDate = urlParams.get('end_at')|| new Date().toISOString();
await fetchAndDisplayPhotos({
map: this.map,
photoMarkers: this.photoMarkers,
Expand Down
76 changes: 63 additions & 13 deletions app/javascript/controllers/trip_map_controller.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
// This controller is being used on:
// - trips/index

import { Controller } from "@hotwired/stimulus"
import L from "leaflet"

export default class extends Controller {
static values = {
tripId: Number,
coordinates: Array,
path: String,
apiKey: String,
userSettings: Object,
timezone: String,
distanceUnit: String
}

connect() {
console.log("TripMap controller connected")

setTimeout(() => {
this.initializeMap()
}, 100)
Expand All @@ -23,7 +28,7 @@ export default class extends Controller {
zoomControl: false,
dragging: false,
scrollWheelZoom: false,
attributionControl: true // Disable default attribution control
attributionControl: true
})

// Add the tile layer
Expand All @@ -33,24 +38,69 @@ export default class extends Controller {
}).addTo(this.map)

// If we have coordinates, show the route
if (this.hasCoordinatesValue && this.coordinatesValue.length > 0) {
if (this.hasPathValue && this.pathValue) {
this.showRoute()
} else {
console.log("No path value available")
}
}

showRoute() {
const points = this.coordinatesValue.map(coord => [coord[0], coord[1]])
const points = this.parseLineString(this.pathValue)

const polyline = L.polyline(points, {
color: 'blue',
opacity: 0.8,
weight: 3,
zIndexOffset: 400
}).addTo(this.map)
// Only create polyline if we have points
if (points.length > 0) {
const polyline = L.polyline(points, {
color: 'blue',
opacity: 0.8,
weight: 3,
zIndexOffset: 400
})

this.map.fitBounds(polyline.getBounds(), {
padding: [20, 20]
})
// Add the polyline to the map
polyline.addTo(this.map)

// Fit the map bounds
this.map.fitBounds(polyline.getBounds(), {
padding: [20, 20]
})
} else {
console.error("No valid points to create polyline")
}
}

parseLineString(linestring) {
try {
// Remove 'LINESTRING (' from start and ')' from end
const coordsString = linestring
.replace(/LINESTRING\s*\(/, '') // Remove LINESTRING and opening parenthesis
.replace(/\)$/, '') // Remove closing parenthesis
.trim() // Remove any leading/trailing whitespace

// Split into coordinate pairs and parse
const points = coordsString.split(',').map(pair => {
// Clean up any extra whitespace and remove any special characters
const cleanPair = pair.trim().replace(/[()"\s]+/g, ' ')
const [lng, lat] = cleanPair.split(' ').filter(Boolean).map(Number)

// Validate the coordinates
if (isNaN(lat) || isNaN(lng) || !lat || !lng) {
console.error("Invalid coordinates:", cleanPair)
return null
}

return [lat, lng] // Leaflet uses [lat, lng] order
}).filter(point => point !== null) // Remove any invalid points

// Validate we have points before returning
if (points.length === 0) {
return []
}

return points
} catch (error) {
return []
}
}

disconnect() {
Expand Down
Loading

0 comments on commit 375c50d

Please sign in to comment.