Skip to content

Commit

Permalink
Admin/Subsection: transform multilinestring from felt api response
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannaPeanut committed Nov 1, 2023
1 parent 1961b49 commit 7333f3e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/subsections/components/utils/multilinestringToLinestring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export const multilinestringToLinestring = (multi: Array<Array<[number, number]>>) => {
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
}
}
11 changes: 6 additions & 5 deletions src/subsections/mutations/updateSubsectionsWithFeltData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions src/subsections/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ export const SubsectionsFormSchema = z.object({

export type TSubsectionSchema = Prettify<z.infer<typeof SubsectionSchema>>

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())
Expand Down

0 comments on commit 7333f3e

Please sign in to comment.