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

Replace WKT with GeoJSON as geometry format #80

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open

Replace WKT with GeoJSON as geometry format #80

wants to merge 19 commits into from

Conversation

SvenVw
Copy link
Owner

@SvenVw SvenVw commented Feb 17, 2025

closes #78

Summary by CodeRabbit

  • Refactor
    • Geometric data handling has been updated to use GeoJSON, aligning with modern web mapping standards.
  • Chore
    • Eliminated redundant dependencies, including the removal of the wkx library, and removed unused imports to streamline the codebase.
  • Tests
    • Updated test cases to validate the new GeoJSON structure for geometric data.

These changes improve compatibility and maintainability while ensuring that geometric data is handled more clearly and directly.

@SvenVw SvenVw added this to the Alpha release milestone Feb 17, 2025
@SvenVw SvenVw self-assigned this Feb 17, 2025
Copy link

changeset-bot bot commented Feb 17, 2025

🦋 Changeset detected

Latest commit: 157f210

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@svenvw/fdm-core Minor
@svenvw/fdm-app Major
@svenvw/fdm-data Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

coderabbitai bot commented Feb 17, 2025

Warning

Rate limit exceeded

@SvenVw has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 8 minutes and 40 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between d0117b3 and 157f210.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (2)
  • fdm-core/package.json (2 hunks)
  • fdm-core/src/db/schema-custom-types.ts (2 hunks)

Walkthrough

The changes update how geometric data is handled throughout the codebase. The representation of the b_geometry property is switched from WKT to GeoJSON. This update includes the removal of the wkx dependency from various files and packages, modifications in loader functions, UI components, and tests to accommodate direct use of GeoJSON objects, and corresponding updates to database schema definitions and type declarations. Error handling in field transactions has also been refined.

Changes

File(s) Change Summary
.changeset/calm-swans-turn.md, fdm-core/src/cultivation.test.ts, fdm-core/src/field.test.ts, fdm-core/src/fertilizer.test.ts, fdm-core/src/soil.test.ts, fdm-app/app/routes/farm.$b_id_farm.atlas.fields.tsx, fdm-app/app/routes/farm.$b_id_farm.field.$b_id.atlas.tsx Changed b_geometry representation from WKT to GeoJSON by removing wkx parsing and using direct assignment of structured GeoJSON objects.
.changeset/curly-mayflies-invite.md, .changeset/unlucky-eyes-doubt.md, fdm-app/app/routes/farm.$b_id_farm.atlas.elevation.tsx, fdm-app/app/routes/farm.$b_id_farm.atlas.soil.tsx, fdm-app/app/routes/farm.create.$b_id_farm.atlas.tsx, fdm-app/app/routes/farm.create.$b_id_farm.fields.tsx, fdm-app/package.json, fdm-core/package.json Removed the wkx dependency and related import statements to eliminate its usage for geometry processing.
fdm-core/src/db/schema-custom-types.ts, fdm-core/src/db/schema.ts, fdm-core/src/field.d.ts Updated database schema definitions and custom types by introducing new functions, an enum (GeometryType), and flexible geometry handling to support GeoJSON formats.
fdm-core/src/field.ts Modified addField and updateField functions to update parameter descriptions and directly use GeoJSON for geometry, with refined error handling in transaction blocks.
fdm-app/app/routes/farm.create.$b_id_farm.cultivations.tsx Cleaned up the code by removing unnecessary commented sections related to UI components without changing core functionality.
.changeset/calm-swans-turn.md Added dependency "@svenvw/fdm-core": minor to align with the updated geometry handling.

Sequence Diagram(s)

sequenceDiagram
    participant U as User Request
    participant R as Route Loader
    participant D as Database
    participant G as Geometry Handler
    participant UI as UI Component

    U->>R: Request field data
    R->>D: Query field with GeoJSON geometry
    D-->>R: Return field data (GeoJSON)
    alt Parsing/Conversion
      R->>G: Validate/transform geometry data (no wkx conversion)
      G-->>R: Return valid GeoJSON geometry
    else Direct Assignment
      Note over R: Geometry assigned directly as GeoJSON
    end
    R->>UI: Render field with GeoJSON geometry
Loading

Possibly related PRs

Suggested reviewers

  • gerardhros

Poem

I'm a little rabbit, coding with delight,
Hopping past WKT into a GeoJSON light.
I left behind wkx with a skip and a leap,
Now clean data flows, no more parsing to keep.
With schema and tests shining so bright,
I nibble on bytes by day and byte by byte! 🐇
Happy code trails, under moonlit night!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Feb 17, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.26%. Comparing base (0d36f03) to head (157f210).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #80      +/-   ##
==========================================
- Coverage   94.19%   93.26%   -0.93%     
==========================================
  Files          27       27              
  Lines        3563     3830     +267     
  Branches      259      274      +15     
==========================================
+ Hits         3356     3572     +216     
- Misses        207      258      +51     
Flag Coverage Δ
fdm-core 93.26% <ø> (-0.93%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@SvenVw SvenVw marked this pull request as ready for review February 17, 2025 14:07
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (3)
fdm-core/src/db/schema-custom-types.ts (1)

83-131: Remove unused performance timestamp and redundant try/catch.
The variable startMs is never used, and the bare try { ... } catch(e) { throw e } rethrows without alteration. Simplify as follows:

 export const parseHexToGeometry = (hex: string): GeoJSON.Geometry => {
-    const startMs = performance.now()
     try {
         ...
     } catch (e) {
-        throw e
+        throw new Error("Failed to parse hex geometry", { cause: e })
     }
 }
fdm-core/src/field.ts (1)

199-295: Consider including geometry in error context.

While the error handling structure has been improved by encapsulating the transaction logic, commenting out b_geometry in the error context might make debugging geometry-related issues more difficult.

Apply this diff to include geometry in error context:

-            // b_geometry,
+            b_geometry,
fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx (1)

599-608: Simplify updateField call by removing unnecessary undefined parameters.

Based on the retrieved learnings, only fdm and b_id are required parameters for updateField. The remaining parameters are optional and don't need to be passed as undefined.

-        await updateField(
-            fdm,
-            b_id,
-            formValues.b_name,
-            undefined,
-            undefined,
-            undefined,
-            undefined,
-            undefined,
-        )
+        await updateField(fdm, b_id, formValues.b_name)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0d36f03 and 5169d48.

⛔ Files ignored due to path filters (8)
  • fdm-core/src/db/migrations/0001_loud_fallen_one.sql is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/0002_clammy_kylun.sql is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/0003_thick_stature.sql is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/meta/0001_snapshot.json is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/meta/0002_snapshot.json is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/meta/0003_snapshot.json is excluded by !fdm-core/src/db/migrations/**
  • fdm-core/src/db/migrations/meta/_journal.json is excluded by !fdm-core/src/db/migrations/**
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (21)
  • .changeset/calm-swans-turn.md (1 hunks)
  • .changeset/curly-mayflies-invite.md (1 hunks)
  • .changeset/unlucky-eyes-doubt.md (1 hunks)
  • fdm-app/app/routes/farm.$b_id_farm.atlas.elevation.tsx (0 hunks)
  • fdm-app/app/routes/farm.$b_id_farm.atlas.fields.tsx (1 hunks)
  • fdm-app/app/routes/farm.$b_id_farm.atlas.soil.tsx (0 hunks)
  • fdm-app/app/routes/farm.$b_id_farm.field.$b_id.atlas.tsx (1 hunks)
  • fdm-app/app/routes/farm.create.$b_id_farm.atlas.tsx (2 hunks)
  • fdm-app/app/routes/farm.create.$b_id_farm.cultivations.tsx (0 hunks)
  • fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx (1 hunks)
  • fdm-app/app/routes/farm.create.$b_id_farm.fields.tsx (0 hunks)
  • fdm-app/package.json (0 hunks)
  • fdm-core/package.json (2 hunks)
  • fdm-core/src/cultivation.test.ts (2 hunks)
  • fdm-core/src/db/schema-custom-types.ts (2 hunks)
  • fdm-core/src/db/schema.ts (3 hunks)
  • fdm-core/src/fertilizer.test.ts (1 hunks)
  • fdm-core/src/field.d.ts (1 hunks)
  • fdm-core/src/field.test.ts (17 hunks)
  • fdm-core/src/field.ts (7 hunks)
  • fdm-core/src/soil.test.ts (1 hunks)
💤 Files with no reviewable changes (5)
  • fdm-app/app/routes/farm.$b_id_farm.atlas.soil.tsx
  • fdm-app/package.json
  • fdm-app/app/routes/farm.create.$b_id_farm.cultivations.tsx
  • fdm-app/app/routes/farm.$b_id_farm.atlas.elevation.tsx
  • fdm-app/app/routes/farm.create.$b_id_farm.fields.tsx
✅ Files skipped from review due to trivial changes (1)
  • .changeset/unlucky-eyes-doubt.md
🧰 Additional context used
🧠 Learnings (1)
fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx (5)
Learnt from: SvenVw
PR: SvenVw/fdm#67
File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx:601-610
Timestamp: 2025-01-31T15:05:14.310Z
Learning: The `updateField` function in fdm-core has optional parameters after `fdm` and `b_id`. The TypeScript definitions might show 8 required parameters due to a potential version mismatch.
Learnt from: SvenVw
PR: SvenVw/fdm#49
File: fdm-app/app/routes/farm.create.$b_id_farm.atlas.tsx:208-208
Timestamp: 2025-01-23T15:17:23.027Z
Learning: The `addField` function in fdm-core should verify field creation within the same transaction by checking the existence of the field and all its required relations (field data, acquiring info, geometry) before resolving its promise.
Learnt from: SvenVw
PR: SvenVw/fdm#67
File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx:601-610
Timestamp: 2025-01-31T15:05:14.310Z
Learning: When using `updateField` from fdm-core, all 8 parameters must be provided in order: fdm, b_id, b_name, b_geometry, b_area, b_id_source, b_id_farm, and b_id_farm_source.
Learnt from: SvenVw
PR: SvenVw/fdm#67
File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx:601-610
Timestamp: 2025-01-31T15:34:20.850Z
Learning: The `updateField` function in fdm-core has optional parameters that don't need to be passed as undefined. Only `fdm` and `b_id` are required.
Learnt from: SvenVw
PR: SvenVw/fdm#67
File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx:601-610
Timestamp: 2025-01-31T15:05:14.310Z
Learning: The `updateField` function in fdm-core requires 8 parameters: fdm, b_id (required), and 6 optional parameters (b_name, b_id_source, b_geometry, b_acquiring_date, b_acquiring_method, b_discarding_date).
🔇 Additional comments (22)
fdm-core/package.json (2)

30-32: Enhanced "files" Array Formatting Improves Readability

The reformatting of the "files" array (now clearly listing "dist") increases clarity and maintainability. This clean structure will help future developers quickly understand which build artifacts are being included in the package.


64-70: Successful Removal of the wkx Dependency and Dependency Cleanup

The removal of the "wkx": "^0.5.0" dependency aligns perfectly with the shift to using GeoJSON for geometry. The remaining dependency list (including "postgres": "^3.4.5") correctly reflects the new architectural direction. Please ensure no residual references to wkx exist elsewhere in the codebase.

fdm-core/src/db/schema-custom-types.ts (4)

2-2: Import statement looks good.
No issues found with the new import for customType and CustomTypeValues.


73-81: GeometryType enum is well-defined.
No logical issues found. Each geometric type is enumerated clearly.


159-180: Polygon reading logic looks consistent.
The readPolygon implementation respectably uses the passed-in offset at all steps. No immediate issues found.


26-71:

❓ Verification inconclusive

Verify parameter binding for ST_GeomFromGeoJSON.
The function geometry() correctly leverages ST_GeomFromGeoJSON(JSON.stringify(value)). However, confirm that Drizzle ORM handles parameter binding here to avoid injection risks if untrusted data is ever passed in.

Generate a script to search for all references to geometry() so we can confirm usage is always trusted:


🏁 Script executed:

#!/bin/bash
# Description: Locate all references to the geometry custom type for usage review.
fd --type f --extension ts --exec rg 'geometry\(' {}

Length of output: 56


Action Required: Verify Parameter Binding for ST_GeomFromGeoJSON

The current implementation in fdm-core/src/db/schema-custom-types.ts uses ST_GeomFromGeoJSON(JSON.stringify(value)) within the toDriver method, relying on Drizzle ORM’s SQL tagged template to handle parameter binding. However, our automated search for references to geometry( in TypeScript files produced no output, so we couldn’t definitively confirm that all invocations are made with trusted data.

Please manually verify that:

  • All calls to geometry() in the codebase inject only trusted data.
  • The Drizzle ORM’s handling of SQL template literals correctly applies parameter binding for the ST_GeomFromGeoJSON invocation.

Once you’ve reviewed these points, update or confirm the usage accordingly.

fdm-core/src/field.d.ts (1)

4-13: Consistent schema-based typing.
Referencing the type definitions directly from the schema helps maintain alignment with database fields. No issues found.

fdm-app/app/routes/farm.$b_id_farm.atlas.fields.tsx (1)

44-44: Handle potential null or invalid GeoJSON geometry.
Currently, field.b_geometry is used directly as the feature’s geometry. If the database stores null or invalid geometry, it might crash the map. Consider a fallback or validation.

fdm-app/app/routes/farm.$b_id_farm.field.$b_id.atlas.tsx (1)

51-51: LGTM! Direct GeoJSON assignment.

The change aligns with the PR objective of replacing WKT with GeoJSON format. The geometry is now directly assigned without any conversion, which simplifies the code and improves maintainability.

fdm-core/src/soil.test.ts (1)

44-55: LGTM! Well-structured GeoJSON test data.

The change improves test readability by using a proper GeoJSON object with explicit type and coordinates, making the geometry structure clearer than the previous WKT string format.

fdm-core/src/field.ts (1)

15-15: LGTM! Updated parameter documentation.

The parameter descriptions have been correctly updated to reflect the change from WKT to GeoJSON format.

Also applies to: 182-182

fdm-app/app/routes/farm.create.$b_id_farm.atlas.tsx (1)

51-51: LGTM! Simplified geometry handling.

The changes improve code maintainability by:

  • Removing unused imports
  • Directly using GeoJSON geometry without conversion

Also applies to: 237-237

fdm-core/src/db/schema.ts (2)

73-75: LGTM! Explicit geometry type definition improves type safety.

The change from geometryPolygon() to geometry("b_geometry", { type: "Polygon" }) enhances type safety and aligns with the transition to GeoJSON format. The comment about PGLite support provides valuable context for future maintenance.


512-514: LGTM! Consistent geometry type definition for sampling points.

The change from geometryMultipoint() to geometry("b_sampling_geometry", { type: "MultiPoint" }) maintains consistency with the new geometry type definition approach.

fdm-core/src/field.test.ts (2)

8-8: LGTM! Type alias improves code maintainability.

The Polygon type alias derived from the schema ensures type consistency between the schema and tests.


38-49: LGTM! Test assertions properly validate GeoJSON structure.

The change to use toStrictEqual for geometry comparison is correct as it performs deep equality checks on the GeoJSON object structure.

Also applies to: 69-69

fdm-core/src/cultivation.test.ts (2)

57-68: LGTM! Consistent GeoJSON format in cultivation tests.

The field geometry is correctly defined using GeoJSON format, maintaining consistency with other test files.


385-396: LGTM! Consistent geometry definition in cultivation plan tests.

The field geometry in the cultivation plan tests maintains the same GeoJSON structure and coordinates.

fdm-core/src/fertilizer.test.ts (1)

407-418: LGTM! Consistent GeoJSON format in fertilizer tests.

The field geometry is correctly defined using GeoJSON format, maintaining consistency with other test files.

fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx (1)

163-163:

❓ Verification inconclusive

Verify GeoJSON format validation.

With the removal of WKT parsing, ensure that field.b_geometry is always provided in valid GeoJSON format to prevent runtime errors.


🏁 Script executed:

#!/bin/bash
# Description: Check if b_geometry is consistently in GeoJSON format across the codebase

# Search for any remaining WKT format geometry assignments
rg -A 5 'b_geometry.*=.*POINT|b_geometry.*=.*LINESTRING|b_geometry.*=.*POLYGON'

# Search for GeoJSON type validations
rg -A 5 'b_geometry.*type.*Feature|b_geometry.*type.*Point|b_geometry.*type.*LineString|b_geometry.*type.*Polygon'

Length of output: 198


Verify GeoJSON Geometry Format in Loader Function

The direct assignment of field.b_geometry to feature.geometry (line 163) now bypasses previous WKT parsing. Please manually verify that field.b_geometry is always a well-formed GeoJSON object (including properties like type and proper coordinates) to avoid runtime issues. The automated search did not return any GeoJSON validation logic or remaining WKT parsing, so extra caution is advised.

  • File: fdm-app/app/routes/farm.create.$b_id_farm.fields.$b_id.tsx (line 163)
  • Confirm that the loader or upstream processing enforces/validates that b_geometry meets the GeoJSON specification.
.changeset/curly-mayflies-invite.md (1)

1-6: LGTM! Appropriate version bump for dependency removal.

The patch version bump is appropriate for removing the wkx dependency, as this is a non-breaking internal implementation change.

.changeset/calm-swans-turn.md (1)

1-6: LGTM! Appropriate version bump for format change.

The minor version bump is appropriate for changing the b_geometry format from WKT to GeoJSON, as this is a new feature that maintains backward compatibility.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: Sven Verweij <[email protected]>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
fdm-core/src/db/schema-custom-types.ts (4)

26-29: Update documentation to reflect write support.

The comment states "only supports reads" but the implementation includes write support via toDriver. Please update the documentation to accurately reflect the capabilities.


65-67: Enhance error message with value details.

The error message could be more helpful by including the actual value that failed to parse.

-                throw new Error("Failed to parse geometry", {
+                throw new Error(`Failed to parse geometry: ${value}`, {
                     cause: e,
                 })

84-84: Remove unused performance measurement.

The startMs variable is initialized but never used. Either use it to log/monitor parsing performance or remove it.

-    const startMs = performance.now()
     try {
         // ... parsing logic ...
-    } catch (e) {
-        throw e
-    }
+    } catch (e) {
+        throw new Error(`Failed to parse hex geometry: ${hex}`, { cause: e })
+    }

Also applies to: 128-130


219-235: Improve clarity of MultiPoint and MultiLineString parsing.

The code reuses readLineString for MultiPoint and readPolygon for MultiLineString. While this works due to similar binary structures, it makes the code harder to understand and maintain.

Consider creating dedicated readers for clarity:

+function readMultiPoint(
+    dataView: DataView,
+    littleEndian: boolean,
+    offset: number,
+): GeoJSON.Position[] {
+    if (offset + 4 > dataView.byteLength) {
+        throw new Error('Buffer too small to read MultiPoint')
+    }
+    const numPoints = dataView.getUint32(offset, littleEndian)
+    offset += 4
+    const points: GeoJSON.Position[] = []
+    for (let i = 0; i < numPoints; i++) {
+        points.push(readPoint(dataView, littleEndian, offset + (i * 16)))
+    }
+    return points
+}

 case GeometryType.MultiPoint:
     return {
         type: "MultiPoint",
-        coordinates: readLineString(
+        coordinates: readMultiPoint(
             dataView,
             littleEndian,
             offset,
         ) as GeoJSON.MultiPoint["coordinates"],
     }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5169d48 and a76dc69.

📒 Files selected for processing (1)
  • fdm-core/src/db/schema-custom-types.ts (2 hunks)
🔇 Additional comments (2)
fdm-core/src/db/schema-custom-types.ts (2)

73-81: Implement or remove incomplete MultiPolygon and GeometryCollection handling.

The enum includes MultiPolygon and GeometryCollection, but the parseGeometry function lacks support for these types.


241-249: LGTM!

The type definitions are well-structured and correctly map all geometry types to their GeoJSON equivalents.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
fdm-core/src/db/schema-custom-types.ts (4)

27-72: Add input validation for GeoJSON data.

The toDriver method should validate the GeoJSON object before stringifying to ensure data integrity.

 toDriver(value) {
+    // Validate GeoJSON structure
+    if (!value || typeof value !== 'object' || !value.type || !value.coordinates) {
+        throw new Error('Invalid GeoJSON object')
+    }
     return sql`ST_GeomFromGeoJSON(${JSON.stringify(value)})`
 },

74-131: Add JSDoc comments for better documentation.

The geometry parsing functions would benefit from detailed documentation explaining the binary format structure and SRID handling.

+/**
+ * Parses a PostGIS EWKB (Extended Well-Known Binary) hex string into a GeoJSON geometry.
+ * The binary format follows the PostGIS EWKB specification:
+ * - Byte order (1 byte)
+ * - Geometry type (4 bytes, including optional SRID flag)
+ * - SRID (4 bytes, optional)
+ * - Geometry data (variable length)
+ * @param hex - The EWKB hex string to parse
+ * @returns A GeoJSON geometry object
+ * @throws Error if parsing fails
+ */
 export const parseHexToGeometry = (hex: string): GeoJSON.Geometry => {

133-241: Optimize array allocations in geometry reading functions.

Consider pre-allocating arrays with known sizes to reduce memory allocations and improve performance.

 function readMultiPoint(
     dataView: DataView,
     littleEndian: boolean,
     offset: number,
 ): GeoJSON.Position[] {
     if (offset + 4 > dataView.byteLength) {
         throw new Error("Buffer too small to read MultiPoint")
     }
     const numPoints = dataView.getUint32(offset, littleEndian)
     offset += 4
-    const points: GeoJSON.Position[] = []
+    const points: GeoJSON.Position[] = new Array(numPoints)
     for (let i = 0; i < numPoints; i++) {
-        points.push(readPoint(dataView, littleEndian, offset + i * 16))
+        points[i] = readPoint(dataView, littleEndian, offset + i * 16)
     }
     return points
 }

243-319: Improve error messages in geometry parsing.

The error messages could be more descriptive to help with debugging.

-            throw new Error("GeometryCollection is not supported yet")
+            throw new Error("GeometryCollection type is not implemented. Please use individual geometry types instead.")
-            throw new Error("Unsupported geometry type")
+            throw new Error(`Unknown geometry type: ${type}. Supported types are: ${Object.keys(GeometryType).join(', ')}`)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a76dc69 and d0117b3.

📒 Files selected for processing (2)
  • fdm-core/codecov.yml (1 hunks)
  • fdm-core/src/db/schema-custom-types.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • fdm-core/codecov.yml
🔇 Additional comments (1)
fdm-core/src/db/schema-custom-types.ts (1)

1-320: Great implementation of the GeoJSON geometry handling!

The code successfully addresses all previous review comments and provides a robust, type-safe implementation for handling PostGIS geometries. The implementation includes proper error handling, bounds checking, and support for multiple geometry types.

@SvenVw SvenVw requested a review from gerardhros February 17, 2025 15:02
Copy link
Collaborator

@gerardhros gerardhros left a comment

Choose a reason for hiding this comment

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

seems fine to me. strange that the wkt package maintenance is not up to date.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

For geometries replace WKT with GeoJSON
2 participants