sadpandajoe commented on code in PR #39760: URL: https://github.com/apache/superset/pull/39760#discussion_r4401903838
########## superset-frontend/src/explore/components/controls/LayerConfigsControl/geoStylerUtil.ts: ########## @@ -0,0 +1,187 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { FeatureCollection, GeoJsonGeometryTypes } from 'geojson'; +import { VectorData } from 'geostyler-data'; +import { GeoStylerContextInterface, GeoStylerLocale, locale } from 'geostyler'; + +import { Style, Symbolizer } from 'geostyler-style'; +import { ColTypeMapping } from './types'; +import { SupersetTheme } from '@apache-superset/core/theme'; + +/** + * Map Superset column type to GeoStyler column type. + * @param colType The Superset columntype + * @returns The GeoStyler column type. + */ +export const colTypeToGeoStylerType = (colType: string) => { + // TODO add missing types + switch (colType) { + case 'LONGINTEGER': + case 'INTEGER': + return 'integer'; + case 'STRING': + return 'string'; + case 'DATETIME': + case 'DATE': + case 'FLOAT': + case 'DECIMAL': + return 'number'; + default: + return colType; + } +}; + +/** + * Map Superset column types to GeoStyler data. + * @param colTypes The superset column types. + * @returns A geotyler-data object. + */ +export const colTypesToGeoStylerData = ( + colTypes: ColTypeMapping, + dataFeatureCollection: FeatureCollection = { + type: 'FeatureCollection', + features: [], + }, +) => { + const data: VectorData = { + schema: { + type: 'object', + properties: Object.keys(colTypes).reduce( + (prev, cur) => ({ + ...prev, + [cur]: { + type: colTypeToGeoStylerType(colTypes[cur]), + }, + }), + {}, + ), + }, + exampleFeatures: dataFeatureCollection, + }; + return data; +}; + +/** + * Create a GeoStylerContext. + * + * Sets the locales, GsData, and enables and disables certain features + * depending on given data and their geometries. + * + * @param gsLocale The locales of the active language. + * @param data The GeoStyler Data. + * @param geomTypes List of distinct geometry types. + * @returns The GeoStylerContext. + */ +export const createGeoStylerContext = ( + gsLocale: GeoStylerLocale, + data: VectorData | undefined, + composition: GeoStylerContextInterface['composition'], +) => { + const context: GeoStylerContextInterface = { + locale: gsLocale, + composition: composition ?? {}, + }; + + if (data) { + context.data = data; + } + + if (!context.composition!.Rules) { + context.composition!.Rules = {}; + } + // If disableClassification is not explicitly set to true, + // determine its value based on the presence of example features. + if (context.composition?.Rules.disableClassification !== true) { Review Comment: `context.composition` is the memoized `MAP_GEOSTYLER_COMPOSITION` object itself, and this block mutates it. On first render `data` is undefined, so `Rules.disableClassification` is written as `true`; when the WFS features arrive, the same object is passed back in and the `!== true` check reads that generated value as an explicit operator setting, so classification stays disabled even though example features now exist. Could the composition be cloned before deriving `disableClassification`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
