Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust overlaps check for zones and communities in /company route #154

Merged
merged 12 commits into from
Sep 26, 2024
15 changes: 15 additions & 0 deletions src/lib/sqlHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ export const covers = (
): RawBuilder<boolean> => {
return sql<boolean>`ST_Covers(zone.area, ST_SetSRID(ST_MakePoint(${coordinates!.lng}, ${coordinates!.lat}),${SRID}))`;
};

export const intersects = async (compulsory: number, community: number): Promise<boolean> => {
return (
(await db
.selectFrom('zone as compulsory_area')
.where('compulsory_area.id', '=', compulsory)
.innerJoin(
(eb) => eb.selectFrom('zone').where('id', '=', community).selectAll().as('community'),
(join) => join.onTrue()
)
.where(sql<boolean>`ST_Area(ST_Intersection(compulsory_area.area, community.area)) >= 1`)
.selectAll()
.executeTakeFirst()) != undefined
);
};
36 changes: 13 additions & 23 deletions src/routes/(user)/company/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { PageServerLoad, Actions } from './$types.js';
import { fail } from '@sveltejs/kit';
import { db } from '$lib/database';
import { AddressGuess, geoCode } from '$lib/api.js';
import { sql } from 'kysely';
import { covers } from '$lib/sqlHelpers.js';
import type { Coordinates } from '$lib/location.js';
import { covers, intersects } from '$lib/sqlHelpers.js';

export const load: PageServerLoad = async (event) => {
const companyId = event.locals.user?.company;
Expand Down Expand Up @@ -72,32 +72,13 @@ export const actions = {
return fail(400, { error: 'Die Addresse konnte nicht gefunden werden.' });
}

if (
!(await db
.selectFrom('zone')
.where((eb) =>
eb.and([eb('zone.id', '=', community_area), covers(eb, bestAddressGuess!.pos)])
)
.executeTakeFirst())
) {
if (!(await contains(community_area, bestAddressGuess.pos))) {
return fail(400, {
error: 'Die Addresse liegt nicht in der ausgewählten Gemeinde.'
});
}

if (
!(await db
.selectFrom('zone as compulsory_area')
.where('compulsory_area.id', '=', zone)
.innerJoin(
(eb) =>
eb.selectFrom('zone').where('id', '=', community_area).selectAll().as('community'),
(join) => join.onTrue()
)
.where(sql<boolean>`ST_Intersects(compulsory_area.area, community.area)`)
.selectAll()
.executeTakeFirst())
) {
if (!(await intersects(zone, community_area))) {
return fail(400, {
error: 'Die Gemeinde liegt nicht im Pflichtfahrgebiet.'
});
Expand All @@ -119,3 +100,12 @@ export const actions = {
return { success: true };
}
} satisfies Actions;

const contains = async (community: number, coordinates: Coordinates): Promise<boolean> => {
return (
(await db
.selectFrom('zone')
.where((eb) => eb.and([eb('zone.id', '=', community), covers(eb, coordinates!)]))
.executeTakeFirst()) != undefined
);
};
87 changes: 87 additions & 0 deletions src/routes/(user)/company/intersects.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { db } from '$lib/database';
import { sql } from 'kysely';
import { describe, expect, it } from 'vitest';
import { intersects } from '$lib/sqlHelpers';

type ZonePair = {
compulsory: number;
community: number;
};

describe('intersects test', () => {
const useOldMethod = (zones: ZonePair[]) => {
return zones.map(
async (z) =>
(await db
.selectFrom('zone as compulsory_area')
.where('compulsory_area.id', '=', z.compulsory)
.innerJoin(
(eb) => eb.selectFrom('zone').where('id', '=', z.community).selectAll().as('community'),
(join) => join.onTrue()
)
.where(sql<boolean>`ST_Intersects(compulsory_area.area, community.area)`)
.selectAll()
.executeTakeFirst()) != undefined
);
};

const useNewMethod = (zones: ZonePair[]) => {
return zones.map(async (z) => await intersects(z.compulsory, z.community));
};

const noOverlaps = [
{ compulsory: 1, community: 34 },
{ compulsory: 1, community: 36 },
{ compulsory: 1, community: 37 },
{ compulsory: 1, community: 39 },
{ compulsory: 1, community: 40 },
{ compulsory: 1, community: 41 }
];
const oneDimensionalOverlaps = [
{ compulsory: 1, community: 35 },
{ compulsory: 1, community: 38 },
{ compulsory: 1, community: 49 },
{ compulsory: 1, community: 57 },
{ compulsory: 1, community: 59 },
{ compulsory: 1, community: 68 }
];
const twoDimensionalOverlaps = [
{ compulsory: 1, community: 7 },
{ compulsory: 1, community: 8 },
{ compulsory: 1, community: 9 },
{ compulsory: 1, community: 10 },
{ compulsory: 1, community: 11 },
{ compulsory: 1, community: 12 }
];

it('compare old and new Intersects method on zones and companies with no overlap', async () => {
const old = useOldMethod(noOverlaps);
for (let i = 0; i != old.length; ++i) {
expect(await old[i]).toBe(false);
}
const newIntersects = useNewMethod(noOverlaps);
for (let i = 0; i != newIntersects.length; ++i) {
expect(await newIntersects[i]).toBe(false);
}
});
it('compare old and new Intersects method on zones and companies with 1-dim overlap', async () => {
const old = useOldMethod(oneDimensionalOverlaps);
for (let i = 0; i != old.length; ++i) {
expect(await old[i]).toBe(true);
}
const newIntersects = useNewMethod(oneDimensionalOverlaps);
for (let i = 0; i != newIntersects.length; ++i) {
expect(await newIntersects[i]).toBe(false);
}
});
it('compare old and new Intersects method on zones and companies with 2-dim overlap', async () => {
const old = useOldMethod(twoDimensionalOverlaps);
for (let i = 0; i != old.length; ++i) {
expect(await old[i]).toBe(true);
}
const newIntersects = useNewMethod(twoDimensionalOverlaps);
for (let i = 0; i != newIntersects.length; ++i) {
expect(await newIntersects[i]).toBe(true);
}
});
});