Skip to content
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

Return the spatial index ID as a property to be available for widgets #440

Merged
merged 2 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 22 additions & 2 deletions packages/react-api/src/hooks/useTileFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function useTileFeatures({

const [tileFormat, setTileFormat] = useState('');
const [spatialIndex, setSpatialIndex] = useState();
const [geoColumName, setGeoColumName] = useState();

const sourceId = source?.id;

Expand All @@ -44,6 +45,7 @@ export default function useTileFeatures({
geometry: spatialFilter,
uniqueIdProperty,
tileFormat,
geoColumName,
spatialIndex
})
.then(() => {
Expand All @@ -52,7 +54,14 @@ export default function useTileFeatures({
.catch(throwError)
.finally(clearDebounce);
},
[tileFormat, setSourceFeaturesReady, sourceId, spatialIndex, clearDebounce]
[
tileFormat,
setSourceFeaturesReady,
sourceId,
geoColumName,
spatialIndex,
clearDebounce
]
);

const loadTiles = useCallback(
Expand Down Expand Up @@ -133,8 +142,14 @@ export default function useTileFeatures({
);

const onDataLoad = useCallback(({ tiles: [tile], scheme }) => {
const tilesFormat = new URL(tile).searchParams.get('formatTiles');
const url = new URL(tile);
const tilesFormat = url.searchParams.get('formatTiles');
setTileFormat(tilesFormat || TILE_FORMATS.MVT);
const geoColum = url.searchParams.get('geo_column');

if (geoColum) {
setGeoColumName(getColumnNameFromGeoColumn(geoColum));
}
setSpatialIndex(Object.values(SpatialIndex).includes(scheme) ? scheme : undefined);
}, []);

Expand All @@ -153,3 +168,8 @@ const customFetch = async (url, { layer, loaders, loadOptions, signal }) => {
const result = await parse(response, loaders, loadOptions);
return result ? { ...result, isDroppingFeatures } : null;
};

const getColumnNameFromGeoColumn = (geoColumn) => {
const parts = geoColumn.split(':');
return parts.length === 1 ? parts[0] : parts.length === 2 ? parts[1] : null;
};
8 changes: 7 additions & 1 deletion packages/react-core/src/filters/tileFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function tileFeatures({
geometry,
uniqueIdProperty,
tileFormat,
geoColumName,
spatialIndex
}) {
const geometryToIntersect = getGeometryToIntersect(viewport, geometry);
Expand All @@ -22,7 +23,12 @@ export function tileFeatures({
}

if (spatialIndex) {
return tileFeaturesSpatialIndex({ tiles, geometryToIntersect, spatialIndex });
return tileFeaturesSpatialIndex({
tiles,
geometryToIntersect,
geoColumName,
spatialIndex
});
}
return tileFeaturesGeometries({
tiles,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ import { TileFeaturesResponse } from "../types";
export default function tileFeaturesSpatialIndex(arg: {
tiles: any;
geometryToIntersect: Feature<Polygon | MultiPolygon>;
geoColumName: string;
spatialIndex: SpatialIndex;
}): TileFeaturesResponse;
4 changes: 3 additions & 1 deletion packages/react-core/src/filters/tileFeaturesSpatialIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { SpatialIndex } from '../operations/constants/SpatialIndexTypes';
export default function tileFeaturesSpatialIndex({
tiles,
geometryToIntersect,
geoColumName,
spatialIndex
}) {
const map = new Map();
const resolution = getResolution(tiles, spatialIndex);
const spatialIndexIDName = geoColumName ? geoColumName : spatialIndex;

if (!resolution) {
return [];
Expand All @@ -39,7 +41,7 @@ export default function tileFeaturesSpatialIndex({

tile.data.forEach((d) => {
if (cellsDictionary[d.id]) {
map.set(d.id, d.properties);
map.set(d.id, { ...d.properties, [spatialIndexIDName]: d.id });
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export enum SpatialIndex {
H3 = 'h3',
S2 = 's2',
QUADKEY = 'quadkey',
QUADINT = 'quadint'
QUADINT = 'quadint',
QUADBIN = 'quadbin'
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ export const SpatialIndex = Object.freeze({
H3: 'h3',
S2: 's2',
QUADKEY: 'quadkey',
QUADINT: 'quadint'
QUADINT: 'quadint',
QUADBIN: 'quadbin'
});
5 changes: 3 additions & 2 deletions packages/react-core/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ export type ScatterPlotFeature = [number, number][];
export type Viewport = [number, number, number, number];

export type TileFeatures = {
tiles: any; // TODO: add proper deck.gl type
tiles?: any; // TODO: add proper deck.gl type
viewport: Viewport;
geometry?: Geometry;
uniqueIdProperty?: string;
tileFormat: TILE_FORMATS;
spatialIndex?: SpatialIndex
geoColumName?: string;
spatialIndex?: SpatialIndex;
};

export type TileFeaturesResponse = Record<string, unknown>[] | [];
2 changes: 2 additions & 0 deletions packages/react-workers/src/workers/features.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function getTileFeatures({
geometry,
uniqueIdProperty,
tileFormat,
geoColumName,
spatialIndex
}) {
currentFeatures = tileFeatures({
Expand All @@ -67,6 +68,7 @@ function getTileFeatures({
geometry,
uniqueIdProperty,
tileFormat,
geoColumName,
spatialIndex
});
postMessage({ result: true });
Expand Down