-
-
Notifications
You must be signed in to change notification settings - Fork 575
feat(export): Exclude hidden fields from vector data #1705
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aa290cf
feat(export): Exclude hidden fields from vector data (closes #1676)
RohithPariki 04e12ec
style: auto-format (ruff + oxfmt) [pre-commit.ci]
pre-commit-ci[bot] 978ed54
fix(core,backend): address CodeRabbit review feedback on field exclusion
RohithPariki eb48425
style: auto-format (ruff + oxfmt) [pre-commit.ci]
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import type { FeatureCollection } from "geojson"; | ||
| import type { GeoLibreProject, FieldVisibility } from "./types"; | ||
|
|
||
| /** | ||
| * Returns a new FeatureCollection with properties marked as "excluded" removed. | ||
| */ | ||
| export function excludeHiddenFieldsFromGeojson( | ||
| geojson: FeatureCollection, | ||
| fieldVisibility?: Record<string, FieldVisibility>, | ||
| ): FeatureCollection { | ||
| const excludedKeys = new Set( | ||
| Object.entries(fieldVisibility || {}) | ||
| .filter(([_, visibility]) => visibility === "excluded") | ||
| .map(([key]) => key), | ||
| ); | ||
|
|
||
| if (excludedKeys.size === 0) { | ||
| return geojson; | ||
| } | ||
|
|
||
| // Deep clone to avoid mutating the live store state | ||
| const stripped: FeatureCollection = { | ||
| ...geojson, | ||
| features: geojson.features.map((feature) => { | ||
| const properties = { ...feature.properties }; | ||
| for (const key of excludedKeys) { | ||
| delete properties[key]; | ||
| } | ||
| return { ...feature, properties }; | ||
| }), | ||
| }; | ||
|
|
||
| return stripped; | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new GeoLibreProject where all layers have their excluded fields | ||
| * physically removed from their inline GeoJSON. | ||
| */ | ||
| export function excludeHiddenFieldsFromProject(project: GeoLibreProject): GeoLibreProject { | ||
| let changed = false; | ||
| const layers = project.layers.map((layer) => { | ||
| if (!layer.fieldVisibility) return layer; | ||
|
|
||
| let updatedLayer = layer; | ||
|
|
||
| if (layer.geojson) { | ||
| const strippedGeojson = excludeHiddenFieldsFromGeojson(layer.geojson, layer.fieldVisibility); | ||
| if (strippedGeojson !== layer.geojson) { | ||
| changed = true; | ||
| updatedLayer = { ...updatedLayer, geojson: strippedGeojson }; | ||
| } | ||
| } | ||
|
|
||
| if (layer.metadata?.embeddedGeoJSON) { | ||
| const strippedEmbedded = excludeHiddenFieldsFromGeojson( | ||
| layer.metadata.embeddedGeoJSON as FeatureCollection, | ||
| layer.fieldVisibility, | ||
| ); | ||
| if (strippedEmbedded !== layer.metadata.embeddedGeoJSON) { | ||
| changed = true; | ||
| updatedLayer = { | ||
| ...updatedLayer, | ||
| metadata: { | ||
| ...updatedLayer.metadata, | ||
| embeddedGeoJSON: strippedEmbedded, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return updatedLayer; | ||
| }); | ||
|
|
||
| return changed ? { ...project, layers } : project; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { test, describe } from "node:test"; | ||
| import assert from "node:assert"; | ||
| import type { GeoLibreProject } from "@geolibre/core"; | ||
| import { excludeHiddenFieldsFromProject } from "../packages/core/src/visibility"; | ||
|
|
||
| describe("visibility", () => { | ||
| test("excludeHiddenFieldsFromProject strips excluded fields from geojson and embeddedGeoJSON", () => { | ||
| const project: GeoLibreProject = { | ||
| id: "proj-1", | ||
| name: "Test", | ||
| version: 1, | ||
| viewState: { | ||
| longitude: 0, | ||
| latitude: 0, | ||
| zoom: 0, | ||
| pitch: 0, | ||
| bearing: 0, | ||
| }, | ||
| layers: [ | ||
| { | ||
| id: "layer-1", | ||
| name: "Layer", | ||
| type: "geojson", | ||
| visible: true, | ||
| metadata: { | ||
| embeddedGeoJSON: { | ||
| type: "FeatureCollection", | ||
| features: [ | ||
| { | ||
| type: "Feature", | ||
| geometry: { type: "Point", coordinates: [0, 0] }, | ||
| properties: { keep: 1, drop: 2 }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| fieldVisibility: { drop: "excluded" }, | ||
| geojson: { | ||
| type: "FeatureCollection", | ||
| features: [ | ||
| { | ||
| type: "Feature", | ||
| geometry: { type: "Point", coordinates: [0, 0] }, | ||
| properties: { keep: 1, drop: 2 }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| const stripped = excludeHiddenFieldsFromProject(project); | ||
|
|
||
| // Check main geojson | ||
| const feature1 = stripped.layers[0].geojson!.features[0]; | ||
| assert.strictEqual(feature1.properties?.keep, 1); | ||
| assert.strictEqual(feature1.properties?.drop, undefined); | ||
|
|
||
| // Check embedded geojson | ||
| const embeddedFeature = (stripped.layers[0].metadata.embeddedGeoJSON as any).features[0]; | ||
| assert.strictEqual(embeddedFeature.properties?.keep, 1); | ||
| assert.strictEqual(embeddedFeature.properties?.drop, undefined); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor:
contentToSaveis now unconditionally reassigned in every branch of theif/elsebelow (both theredactedPaths.length > 0"keep" branch and theelsebranch callserializeProject(projectToEgress), and "strip" callsserializeProject(redacted.project)), so this initiallet contentToSave = content;is dead — it's never read before being overwritten. Slightly misleading since it looks like a real fallback. Could simplify to declarecontentToSaveinside the branches, or drop the outercontentvariable if it's now unused elsewhere.