Skip to content

Commit

Permalink
refactor: update TinyCim type and schema to include code field (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-lebeau authored Feb 24, 2025
1 parent 61cd3b2 commit 2d617ba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 34 deletions.
7 changes: 4 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
provider = "postgresql"
url = env("DATABASE_URL")
directUrl = env("DIRECT_URL")
}

model Account {
Expand Down Expand Up @@ -66,6 +66,7 @@ model Cim {
altitude Int
longitude Float
latitude Float
code String
url String @unique
img String? @unique
essencial Boolean
Expand Down
32 changes: 22 additions & 10 deletions prisma/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ const fs = require("fs");
const { PrismaClient } = require("@prisma/client");
const { z } = require("zod");
const { withBar } = require("./utils");
const {
encode,
CODE_PRECISION_EXTRA,
} = require("../src/lib/open-location-code");

const TABLES = [
"account",
Expand All @@ -27,16 +31,23 @@ const seedSchema = z.object({
})
),
cim: z.array(
z.object({
name: z.string(),
altitude: z.number(),
latitude: z.number(),
longitude: z.number(),
essencial: z.boolean(),
img: z.string().nullable(),
url: z.string(),
comarca: z.array(z.string()),
})
z
.object({
name: z.string(),
altitude: z.number(),
latitude: z.number(),
longitude: z.number(),
essencial: z.boolean(),
img: z.string().nullable(),
url: z.string(),
comarca: z.array(z.string()),
})
.transform((cim) => {
return {
...cim,
code: encode(cim.latitude, cim.longitude, CODE_PRECISION_EXTRA),
};
})
),
user: z.array(
z.object({
Expand Down Expand Up @@ -76,6 +87,7 @@ async function main() {
altitude: cim.altitude,
latitude: cim.latitude,
longitude: cim.longitude,
code: cim.code,
essencial: cim.essencial,
img: cim.img,
url: cim.url,
Expand Down
19 changes: 10 additions & 9 deletions src/lib/db/cims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ import { prisma } from "@/lib/prisma";
import { z } from "zod";
import { toIsoDate } from "../to-iso-date-zod-preprocessor";
import { comarcaSchema } from "./comarcas";
import { CODE_PRECISION_NORMAL, encode } from "../open-location-code";

export type TinyCim = [Cim["id"], Cim["longitude"], Cim["latitude"], string];
export type TinyCim = [
Cim["id"],
Cim["longitude"],
Cim["latitude"],
Cim["code"],
];

const tinyCimSchema = z
.object({
id: z.string(),
longitude: z.number(),
latitude: z.number(),
code: z.string(),
})
.transform(
(cim) =>
[
cim.id,
cim.longitude,
cim.latitude,
encode(cim.latitude, cim.longitude, CODE_PRECISION_NORMAL),
] as TinyCim
(cim) => [cim.id, cim.longitude, cim.latitude, cim.code] as TinyCim
);

export async function getTinyCims() {
Expand All @@ -29,6 +28,7 @@ export async function getTinyCims() {
id: true,
longitude: true,
latitude: true,
code: true,
},
})
);
Expand Down Expand Up @@ -71,6 +71,7 @@ export const cimSchema = z.object({
altitude: z.number(),
latitude: z.number(),
longitude: z.number(),
code: z.string(),
url: z.string(),
img: z.string().nullable(),
essencial: z.boolean(),
Expand Down
22 changes: 10 additions & 12 deletions src/lib/geojson.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Cim, TinyCim } from "@/lib/db/cims";
import { datadogRum } from "@datadog/browser-rum";
import { decode as decodePolyline, toGeoJSON } from "@mapbox/polyline";
import { decode as decodePolyline } from "@mapbox/polyline";
import OpenLocationCode from "./open-location-code";
import { CODE_PRECISION_NORMAL } from "./open-location-code";
import pointToLineDistance from "@turf/point-to-line-distance";
import type { BBox } from "geojson";
import { type Feature } from "geojson";
Expand Down Expand Up @@ -51,12 +50,7 @@ export function getBBox(
}

export function cimsToTinyCims(cims: Cim[]): TinyCim[] {
return cims.map((cim) => [
cim.id,
cim.longitude,
cim.latitude,
OpenLocationCode.encode(cim.latitude, cim.longitude, CODE_PRECISION_NORMAL),
]);
return cims.map((cim) => [cim.id, cim.longitude, cim.latitude, cim.code]);
}

function shortestCommonPrefix(a: PlusCode, b: PlusCode): string {
Expand Down Expand Up @@ -101,13 +95,17 @@ function getPlusCodeRegex(a: PlusCode, b: PlusCode) {

export function getCimForPolyline(cims: TinyCim[], polyline: string) {
performance.mark("getCimForPolylineStart");
const geoJsonLine = toGeoJSON(polyline);
const coordinates = decodePolyline(polyline);
const geoJsonLine = {
type: "LineString" as const,
coordinates,
};

if (geoJsonLine.coordinates.length < 2) {
if (coordinates.length < 2) {
return [];
}

const [x1, y1, x2, y2] = getBBox(decodePolyline(polyline));
const [x1, y1, x2, y2] = getBBox(coordinates);
const startPlusCode = OpenLocationCode.encode(y1, x1, SEARCH_PRECISION);
const endPlusCode = OpenLocationCode.encode(y2, x2, SEARCH_PRECISION);

Expand All @@ -123,7 +121,7 @@ export function getCimForPolyline(cims: TinyCim[], polyline: string) {
const matches = [];

for (const cim of cimsInRegion) {
const distance = pointToLineDistance([cim[1], cim[2]], geoJsonLine, {
const distance = pointToLineDistance([cim[2], cim[1]], geoJsonLine, {
units: "meters",
});

Expand Down

1 comment on commit 2d617ba

@vercel
Copy link

@vercel vercel bot commented on 2d617ba Feb 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

100-cims – ./

100-cims-git-main-100cims.vercel.app
100-cims-100cims.vercel.app
100-cims.vercel.app

Please sign in to comment.