diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1db92c4..0377be6 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -78,6 +78,7 @@ model Trip { skipper User? @relation("skipper", fields: [skipperName], references: [username]) skipperName String? crew User[] @relation("crew") + location Location[] visibility Int @default(1) // 0: private // 1: logged in // 2: public @@ -107,6 +108,11 @@ model Datapoint { // 2: needed } +model Location { + name String @id + trips Trip[] +} + model Media { id String @id @default(cuid()) visibility Int @default(1) // 0: private diff --git a/workers/index.ts b/workers/index.ts index 0aeeeed..2e97d55 100644 --- a/workers/index.ts +++ b/workers/index.ts @@ -2,6 +2,7 @@ import { calculateUsers } from "./calculateUser"; import { simplify } from "./simplifyGps"; while(true){ + console.log("HI"); await simplify(); await calculateUsers(); await new Promise(f => setTimeout(f, 1000)); diff --git a/workers/package.json b/workers/package.json index 685046d..7c2ad98 100644 --- a/workers/package.json +++ b/workers/package.json @@ -10,10 +10,13 @@ "author": "", "license": "ISC", "dependencies": { + "@turf/turf": "^7.1.0", + "shapefile": "^0.6.6", "workers": "file:" }, "devDependencies": { "@prisma/client": "^5.19.1", + "@types/shapefile": "^0.6.4", "tsc-alias": "^1.8.10", "typescript": "^5.5.4" } diff --git a/workers/simplifyGps.ts b/workers/simplifyGps.ts index 997f7c9..1aa2dc9 100644 --- a/workers/simplifyGps.ts +++ b/workers/simplifyGps.ts @@ -1,6 +1,12 @@ import { prisma } from '../src/lib/server/prisma'; import type { Decimal } from '@prisma/client/runtime/library'; import { getDistance, getDistanceFromLine } from 'geolib'; +import shapefile from "shapefile"; +import * as turf from '@turf/turf'; + +console.log("BEFORE READ") +const reader = await shapefile.open("../store/World_Seas_IHO_v3.shp"); +console.log("after read") export async function simplifyGps(trip: string, amount: number) { let totalAmount = 0; @@ -29,6 +35,7 @@ export async function simplifyGps(trip: string, amount: number) { for (let i = 1; i < inputData.length - 1; i++) { + await getRegion(inputData[i]); let crosstrackError = getDistanceFromLine( { lat: Number(inputData[i].lat), lng: Number(inputData[i].long) }, { lat: Number(lastPoint.lat), lng: Number(lastPoint.long) }, @@ -143,6 +150,7 @@ export async function calculateDistance(trip: string){ export async function simplify(){ let trips = await prisma.trip.findMany({}); for (var trip in trips){ + console.log(trip); await simplifyGps(trips[trip].id, 100000); if(trips[trip].recalculate){ console.log("calculating "+trips[trip].id); @@ -186,6 +194,37 @@ export async function simplify(){ return; } +export async function getRegion(datapoint:Datapoint) { + try { + // Step 2: Create a point using the GPS coordinates + const point = turf.point([Number(datapoint.lat), Number(datapoint.long)]); + + // Step 3: Iterate over the features (regions) in the shapefile + for (const feature of features) { + // Check if the feature is a Polygon or MultiPolygon + if (feature.geometry.type === 'Polygon') { + console.log(feature); + // Handle Polygon + const polygon = turf.polygon(feature.geometry.coordinates); + if (turf.booleanPointInPolygon(point, polygon)) { + return feature.properties?.name || 'Unknown Region'; + } + } else if (feature.geometry.type === 'MultiPolygon') { + // Handle MultiPolygon (multiple polygons) + for (const coords of feature.geometry.coordinates) { + const polygon = turf.polygon(coords); + if (turf.booleanPointInPolygon(point, polygon)) { + return feature.properties?.name || 'Unknown Region'; + } + } + } + } + return null; + } catch (error) { + console.error("Error processing shapefile or point:", error); + return null; + } +} interface Datapoint { id: string;