Skip to content

Enable empty path for geojson layer #810

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions packages/base/src/formbuilder/objectform/source/geojsonsource.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { IDict } from '@jupytergis/schema';
import * as geojson from '@jupytergis/schema/src/schema/geojson.json';
import { showErrorMessage } from '@jupyterlab/apputils';
import { ISubmitEvent } from '@rjsf/core';
import { Ajv, ValidateFunction } from 'ajv';

import { loadFile } from '@/src/tools';
Expand Down Expand Up @@ -27,8 +29,11 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
if (data?.path !== '') {
this.removeFormEntry('data', data, schema, uiSchema);
}

super.processSchema(data, schema, uiSchema);
if (this.props.formContext === 'create') {
((schema.properties.path.description =
'The local path to a GeoJSON file. (If no path/url is provided, an empty GeoJSON is created.)'),
super.processSchema(data, schema, uiSchema));
}
}

/**
Expand All @@ -40,7 +45,7 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
const extraErrors: IDict = this.state.extraErrors;

let error = '';
let valid = false;
let valid = true;
if (path) {
try {
const geoJSONData = await loadFile({
Expand All @@ -55,8 +60,6 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
} catch (e) {
error = `"${path}" is not a valid GeoJSON file: ${e}`;
}
} else {
error = 'Path is required';
}

if (!valid) {
Expand All @@ -79,4 +82,17 @@ export class GeoJSONSourcePropertiesForm extends PathBasedSourcePropertiesForm {
this.props.formErrorSignal.emit(!valid);
}
}
protected onFormSubmit(e: ISubmitEvent<any>) {
if (this.state.extraErrors?.path?.__errors?.length >= 1) {
showErrorMessage('Invalid file', this.state.extraErrors.path.__errors[0]);
return;
}
if (!e.formData.path) {
e.formData.data = {
type: 'FeatureCollection',
features: [],
};
super.onFormSubmit(e);
}
}
}
3 changes: 3 additions & 0 deletions packages/base/src/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ export const loadFile = async (fileInfo: {
model: IJupyterGISModel;
}) => {
const { filepath, type, model } = fileInfo;
if (!filepath) {
return;
}

if (filepath.startsWith('http://') || filepath.startsWith('https://')) {
switch (type) {
Expand Down
Loading