Skip to content

Commit

Permalink
fix(web): sketch data not processed correctly (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
airslice authored Jul 16, 2024
1 parent 6c6734b commit e87230d
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import TextAreaField from "@reearth/beta/components/fields/TextAreaField";
import TextField from "@reearth/beta/components/fields/TextField";
import ToggleField from "@reearth/beta/components/fields/ToggleField";
import URLField from "@reearth/beta/components/fields/URLField";
import { Feature } from "@reearth/core";
import { SketchFeature } from "@reearth/services/api/layersApi/utils";
import { useT } from "@reearth/services/i18n";

import { FieldProp, ValueProp } from ".";

type Props = {
field: any;

Check warning on line 14 in web/src/beta/features/Editor/Map/InspectorPanel/LayerInspector/FeatureInspector/CustomPropertField.tsx

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type
selectedFeature?: Feature;
selectedFeature?: SketchFeature;
setField?: (v: FieldProp[] | ((prevFields: FieldProp[]) => FieldProp[])) => void;
onSubmit?: (inp: any) => void;

Check warning on line 17 in web/src/beta/features/Editor/Map/InspectorPanel/LayerInspector/FeatureInspector/CustomPropertField.tsx

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type
};
Expand Down Expand Up @@ -50,7 +50,7 @@ export const FieldComponent = ({ field, selectedFeature, setField, onSubmit }: P
);

const getDynamicValue = useCallback(
(selectedFeature: Feature | undefined, fieldTitle: string, fieldValue: any) => {
(selectedFeature: SketchFeature | undefined, fieldTitle: string, fieldValue: any) => {

Check warning on line 53 in web/src/beta/features/Editor/Map/InspectorPanel/LayerInspector/FeatureInspector/CustomPropertField.tsx

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type
return selectedFeature?.properties && fieldTitle in selectedFeature.properties
? selectedFeature.properties[fieldTitle]
: fieldValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { v4 as uuidv4 } from "uuid";
import SidePanelSectionField from "@reearth/beta/components/SidePanelSectionField";
import Text from "@reearth/beta/components/Text";
import { GeoJsonFeatureUpdateProps } from "@reearth/beta/features/Editor/hooks/useSketch";
import { Geometry, Feature } from "@reearth/core";
import { Geometry } from "@reearth/core";
import { SketchFeature } from "@reearth/services/api/layersApi/utils";
import { useT } from "@reearth/services/i18n";
import { styled } from "@reearth/services/theme";

Expand All @@ -22,7 +23,7 @@ type Props = {
isSketchLayer?: boolean;
customProperties?: any;

Check warning on line 24 in web/src/beta/features/Editor/Map/InspectorPanel/LayerInspector/FeatureInspector/index.tsx

View workflow job for this annotation

GitHub Actions / ci-web / ci

Unexpected any. Specify a different type
layerId?: string;
sketchFeature?: Feature;
sketchFeature?: SketchFeature;
onGeoJsonFeatureUpdate?: (inp: GeoJsonFeatureUpdateProps) => void;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import React, { useCallback, useMemo, useState } from "react";
import TabMenu, { TabObject } from "@reearth/beta/components/TabMenu";
import { SelectedLayer } from "@reearth/beta/features/Editor/hooks/useLayers";
import { GeoJsonFeatureUpdateProps } from "@reearth/beta/features/Editor/hooks/useSketch";
import { Feature } from "@reearth/core";
import { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { LayerStyle as LayerStyleType } from "@reearth/services/api/layerStyleApi/utils";
import { useT } from "@reearth/services/i18n"; // If needed
Expand Down Expand Up @@ -61,13 +60,13 @@ const InspectorTabs: React.FC<Props> = ({
if (!selectedLayer?.layer?.sketch) return;

const { sketch } = selectedLayer.layer;
const features = sketch.featureCollection.features;
const features = sketch?.featureCollection?.features;

if (!selectedFeature?.properties?.id) return;

const selectedFeatureId = selectedFeature.properties.id;

return features.find((feature: Feature) => feature.properties.id === selectedFeatureId);
return features?.find(feature => feature.properties.id === selectedFeatureId);
}, [selectedLayer, selectedFeature]);

const tabs: TabObject[] = useMemo(
Expand Down
37 changes: 5 additions & 32 deletions web/src/beta/features/Editor/Visualizer/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "@reearth/beta/features/Visualizer/Crust/Widgets";
import { WidgetAreaPadding } from "@reearth/beta/features/Visualizer/Crust/Widgets/WidgetAlignSystem/types";
import { DEFAULT_LAYER_STYLE, valueTypeFromGQL } from "@reearth/beta/utils/value";
import { LayerAppearanceTypes, Feature } from "@reearth/core";
import { LayerAppearanceTypes } from "@reearth/core";
import type { Layer } from "@reearth/core";
import { NLSLayer } from "@reearth/services/api/layersApi/utils";
import { LayerStyle } from "@reearth/services/api/layerStyleApi/utils";
Expand All @@ -32,7 +32,6 @@ import {
ValueType as GQLValueType,
NlsLayerCommonFragment,
} from "@reearth/services/gql";
import { Geometry } from "@reearth/services/gql/__gen__/graphql";

import convertInfobox from "./convert-infobox";

Expand All @@ -59,23 +58,6 @@ export type Widget = Omit<RawWidget, "layout" | "extended"> & {
extended?: boolean;
};

export const getGeometryCoordinates = (geometry?: Geometry) => {
switch (geometry?.type) {
case "Polygon":
return "polygonCoordinates" in geometry && geometry.polygonCoordinates;
case "MultiPolygon":
return "multiPolygonCoordinates" in geometry && geometry.multiPolygonCoordinates;
case "LineString":
return "lineStringCoordinates" in geometry && geometry.lineStringCoordinates;
case "Point":
return "pointCoordinates" in geometry && geometry.pointCoordinates;
case "GeometryCollection":
return "geometries" in geometry && geometry.geometries;
default:
return geometry;
}
};

export const convertWidgets = (
scene?: Partial<Scene>,
):
Expand Down Expand Up @@ -391,19 +373,10 @@ export function processLayers(
...nlsLayer.config.data,
value: {
type: "FeatureCollection",
features: nlsLayer.sketch.featureCollection.features.map((feature: Feature) => {
const geometryType = feature.geometry?.type;
if (geometryType) return;
const cleanedFeatures = {
...feature,
geometry: {
type: geometryType,
coordinates: getGeometryCoordinates(feature.geometry),
},
};

return cleanedFeatures;
}),
features: nlsLayer.sketch?.featureCollection?.features.map(f => ({
...f,
geometry: f.geometry[0],
})),
},
};

Expand Down
8 changes: 3 additions & 5 deletions web/src/beta/features/Published/convert-new-property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ import { mapValues } from "lodash-es";
import { InfoboxBlock } from "@reearth/beta/features/Visualizer/Crust/Infobox/types";
import { DEFAULT_LAYER_STYLE } from "@reearth/beta/utils/value";
import { Layer, LayerAppearanceTypes } from "@reearth/core";
import { NLSInfobox, NLSLayer } from "@reearth/services/api/layersApi/utils";
import { NLSInfobox, NLSLayer, SketchFeature } from "@reearth/services/api/layersApi/utils";
import { LayerStyle } from "@reearth/services/api/layerStyleApi/utils";

import { Feature } from "./types";

export const processNewProperty = (p: any): any => {
if (typeof p !== "object") return p;
return mapValues(p, g => {
Expand Down Expand Up @@ -69,10 +67,10 @@ export function processLayers(
...nlsLayer.config.data,
value: {
type: "FeatureCollection",
features: nlsLayer.sketch.featureCollection.features.map((feature: Feature) => {
features: nlsLayer.sketch?.featureCollection?.features.map((feature: SketchFeature) => {
return {
...feature,
geometry: feature.geometry?.[0],
geometry: feature.geometry[0],
};
}),
},
Expand Down
1 change: 0 additions & 1 deletion web/src/beta/features/Published/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export default (alias?: string) => {
tiles: [{ id: "___default_tile___" }],
};
}

setData(d);
} catch (e) {
// TODO: display error for users
Expand Down
12 changes: 11 additions & 1 deletion web/src/beta/features/Published/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { DataRange, DataType, Geometry, SceneProperty, TimeInterval } from "@reearth/core";
import { SketchFeature } from "@reearth/services/api/layersApi/utils";
import { LayerStyle } from "@reearth/services/api/layerStyleApi/utils";

export type PublishedData = {
Expand Down Expand Up @@ -62,10 +63,19 @@ export type NLSLayer = {
};
};
isSketch?: boolean;
sketchInfo?: any;
sketchInfo?: SketchInfo;
nlsInfobox?: any;
};

export type SketchInfo = {
propertySchema?: any;
title?: string;
featureCollection: {
type: string;
features: SketchFeature[];
};
};

export type NLSInfobox = {
id: string;
blocks: NLSInfoboxBlock[];
Expand Down
78 changes: 75 additions & 3 deletions web/src/services/api/layersApi/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetSceneQuery } from "../../gql";
import { Geometry, GetSceneQuery, SketchInfo } from "../../gql";

export type NLSInfobox = {
sceneId: string;
Expand All @@ -8,18 +8,83 @@ export type NLSInfobox = {
blocks?: any[];
};

export type SketchGeometry = {
type: string;
coordinates?: number[] | number[][] | number[][][] | number[][][][] | number[][][][];
};

export type SketchFeature = {
id: string;
type: string;
properties: any;
geometry: SketchGeometry[];
};

export type Sketch = {
customPropertySchema?: any;
featureCollection?: {
type: string;
features: SketchFeature[];
};
};

export type NLSLayer = {
id: string;
title: string;
visible: boolean;
layerType: string;
config?: any;
children?: NLSLayer[] | null;
sketch?: any;
sketch?: Sketch;
isSketch?: boolean;
infobox?: NLSInfobox;
};

const getGeometryCoordinates = (geometry: Geometry) => {
switch (geometry["__typename"]) {
case "Point":
return geometry.pointCoordinates;
case "LineString":
return geometry.lineStringCoordinates;
case "Polygon":
return geometry.polygonCoordinates;
case "MultiPolygon":
return geometry.multiPolygonCoordinates;
default:
return;
}
};

const convertGeometry = (geometry: Geometry) => {
return geometry["__typename"] === "GeometryCollection"
? geometry.geometries.map(g => ({
type: g.type,
coordinates: getGeometryCoordinates(g),
}))
: [
{
type: geometry.type,
coordinates: getGeometryCoordinates(geometry),
},
];
};

export const convertSketchFeatureCollection = (
featureCollection: SketchInfo["featureCollection"],
) => {
return featureCollection
? {
type: featureCollection.type,
features: featureCollection.features.map(f => ({
id: f.id,
type: f.type,
properties: f.properties,
geometry: convertGeometry(f.geometry),
})),
}
: undefined;
};

export const getLayers = (rawScene?: GetSceneQuery) => {
const scene = rawScene?.node?.__typename === "Scene" ? rawScene.node : undefined;

Expand All @@ -31,7 +96,14 @@ export const getLayers = (rawScene?: GetSceneQuery) => {
layerType: l.layerType,
config: l.config,
isSketch: l.isSketch,
sketch: l.sketch,
sketch: l.sketch
? {
customPropertySchema: l.sketch.customPropertySchema,
featureCollection: convertSketchFeatureCollection(
l.sketch.featureCollection as SketchInfo["featureCollection"],
),
}
: undefined,
infobox: l.infobox
? {
sceneId: l.infobox.sceneId,
Expand Down

0 comments on commit e87230d

Please sign in to comment.