Skip to content

Commit

Permalink
try reading shapefile not working
Browse files Browse the repository at this point in the history
  • Loading branch information
derGraph committed Dec 17, 2024
1 parent 6bc7397 commit fc26d8e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions workers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 3 additions & 0 deletions workers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
39 changes: 39 additions & 0 deletions workers/simplifyGps.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) },
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fc26d8e

Please sign in to comment.