diff --git a/src/subsections/components/utils/multilinestringToLinestring.ts b/src/subsections/components/utils/multilinestringToLinestring.ts new file mode 100644 index 000000000..955477ab5 --- /dev/null +++ b/src/subsections/components/utils/multilinestringToLinestring.ts @@ -0,0 +1,21 @@ +export const multilinestringToLinestring = (multi: Array>) => { + let linestring = multi[0] + if (multi.length < 2) { + return linestring + } + for (let i = 1; i < multi.length; i++) { + { + if ( + // @ts-expect-error + multi[i][0][0] === multi[i - 1][multi[i - 1].length - 1][0] && + // @ts-expect-error + multi[i][0][1] === multi[i - 1][multi[i - 1].length - 1][1] + ) { + linestring?.pop() + // @ts-expect-error + linestring = linestring?.concat(multi[i]) + } + } + return linestring + } +} diff --git a/src/subsections/mutations/updateSubsectionsWithFeltData.ts b/src/subsections/mutations/updateSubsectionsWithFeltData.ts index b42836dac..f1e44a52f 100644 --- a/src/subsections/mutations/updateSubsectionsWithFeltData.ts +++ b/src/subsections/mutations/updateSubsectionsWithFeltData.ts @@ -3,6 +3,7 @@ import db from "db" import { z } from "zod" import { SubsectionWithPosition } from "../queries/getSubsection" import { FeltApiResponseSchema, SubsectionSchema } from "../schema" +import { multilinestringToLinestring } from "../components/utils/multilinestringToLinestring" const UpdateSubsectionsWithFeltDataSchema = z.object({ subsections: z.array( @@ -47,15 +48,15 @@ export default resolver.pipe( (s) => s?.properties["ts_pa_id"] === tsSubsection.slug, ) // if ts-subsection-slug matches one of the felt-subsection-id, update db subsection, fields: start, end, geometry - if (matchingFeltSubsection) { + if (matchingFeltSubsection && matchingFeltSubsection.geometry.type === "MultiLineString") { const updatedSubsection = await db.subsection.update({ where: { id: tsSubsection.id }, data: { - // todo multilinestring problem - // felt data is a multilinestring, but we need a inestring - so we take the first linestring - // this only works when we use the line tool in felt, if we use the Route tool, we only save the first part of the geometry here + // Felt data is a multilinestring, but we need a linestring + // in Felt we get multiple lines, if we use the Route tool + // so we take the first linestring or we concat the multiple lines (in case last point of line a matches first point of line b) geometry: matchingFeltSubsection.geometry - ? matchingFeltSubsection?.geometry["coordinates"][0] + ? multilinestringToLinestring(matchingFeltSubsection?.geometry["coordinates"]) : tsSubsection.geometry, start: matchingFeltSubsection.properties diff --git a/src/subsections/schema.ts b/src/subsections/schema.ts index ebe0e0ba9..d9f6c6344 100644 --- a/src/subsections/schema.ts +++ b/src/subsections/schema.ts @@ -29,11 +29,17 @@ export const SubsectionsFormSchema = z.object({ export type TSubsectionSchema = Prettify> -const CoordinatesSchema = z.array(z.array(z.array(z.number()).min(2))) - const GeometrySchema = z.object({ - type: z.literal("MultiLineString"), - coordinates: CoordinatesSchema, + type: z.union([ + z.literal("Point"), + z.literal("MultiPoint"), + z.literal("LineString"), + z.literal("MultiLineString"), + z.literal("Polygon"), + z.literal("MultiPolygon"), + z.literal("GeometryCollection"), + ]), + coordinates: z.array(z.any()), }) const PropertiesSchema = z.record(z.any())