From 94540c1ad0e9050bae2d1606f73bc8efb8f83b05 Mon Sep 17 00:00:00 2001 From: suyubin <84109842+websybin@users.noreply.github.com> Date: Fri, 8 Mar 2024 10:21:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20geojson=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E6=A0=A1=E9=AA=8C=E9=94=99=E8=AF=AF=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20(#47)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: 修复geojson数据校验错误的问题 * fix: 过滤掉右手报错原则 --------- Co-authored-by: syb01094648 --- package.json | 1 + .../app-header/btn/import-btn/file-upload.tsx | 1 - src/pages/components/editor.tsx | 15 +++++++++-- src/recoil/feature.ts | 27 ++++++++++++------- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index cffb5f5..6f11492 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "@antv/larkmap": "^1.4.4", "@antv/sam": "^0.1.0", "@emotion/css": "^11.11.2", + "@mapbox/geojsonhint": "^3.3.0", "@mapbox/togeojson": "^0.16.0", "@turf/turf": "^6.5.0", "ahooks": "^3.7.0", diff --git a/src/components/app-header/btn/import-btn/file-upload.tsx b/src/components/app-header/btn/import-btn/file-upload.tsx index e1c63e5..ec8de2e 100644 --- a/src/components/app-header/btn/import-btn/file-upload.tsx +++ b/src/components/app-header/btn/import-btn/file-upload.tsx @@ -34,7 +34,6 @@ const FileUpload = forwardRef(function FileUpload({}, ref) { const { file, onSuccess, onError } = uploadRequestOption; parserFileToSource(file, t) .then((dataSource) => { - console.log(dataSource); if (!dataSource?.columns) { if (dataSource.data?.features?.length) { const newData = uploadData; diff --git a/src/pages/components/editor.tsx b/src/pages/components/editor.tsx index d198fa8..5d66184 100644 --- a/src/pages/components/editor.tsx +++ b/src/pages/components/editor.tsx @@ -1,3 +1,5 @@ +//@ts-ignore +import { hint } from '@mapbox/geojsonhint'; import { useAsyncEffect, useUpdateEffect } from 'ahooks'; import { ConfigProvider, theme as antdTheme, message } from 'antd'; import classNames from 'classnames'; @@ -55,8 +57,17 @@ export const Editor: React.FC = (props) => { )) as string | null; if (newEditorText && scene && !props.features) { try { - const newFeatures = JSON.parse(newEditorText).features; - bboxAutoFit(newFeatures); + const errors = hint(JSON.parse(newEditorText)).filter( + (item: { message: string }) => + item.message !== + 'Polygons and MultiPolygons should follow the right-hand rule', + ); + if (errors.length > 0) { + message.error(t('import_btn.file_upload.qingJianChaShuJu')); + } else { + const newFeatures = JSON.parse(newEditorText).features; + bboxAutoFit(newFeatures); + } } catch (error) { message.error(t('import_btn.file_upload.qingJianChaShuJu')); } diff --git a/src/recoil/feature.ts b/src/recoil/feature.ts index dd0d7c0..19db9d7 100644 --- a/src/recoil/feature.ts +++ b/src/recoil/feature.ts @@ -1,3 +1,5 @@ +//@ts-ignore +import { hint } from '@mapbox/geojsonhint'; import type { Feature } from '@turf/turf'; import { bbox, featureCollection, getType } from '@turf/turf'; import { message } from 'antd'; @@ -71,15 +73,22 @@ export default function useFeature() { let newFeatures: Feature[] = []; if (editorText || value) { try { - newFeatures = transformFeatures(value ?? editorText, t); - if (value) { - setEditorText(value); + const errors = hint(JSON.parse(value ?? editorText)).filter( + (item: { message: string }) => + item.message !== + 'Polygons and MultiPolygons should follow the right-hand rule', + ); + if (errors.length > 0) { + message.warning(t('recoil.feature.shuJuJiaZaiYou')); + } else { + newFeatures = transformFeatures(value ?? editorText, t); + if (value) { + setEditorText(value); + } + setSavedText(value ?? editorText); + setFeatures(newFeatures as IFeatures); } - setSavedText(value ?? editorText); - setFeatures(newFeatures as IFeatures); - } catch (e) { - message.warning(t('recoil.feature.shuJuJiaZaiYou')); - } + } catch (error) {} } else { setEditorText(emptyFeatures); setSavedText(emptyFeatures); @@ -195,4 +204,4 @@ export default function useFeature() { transformCoord, revertCoord, }; -} \ No newline at end of file +}