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

Widgets work on either mask or viewport #661

Merged
Show file tree
Hide file tree
Changes from 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Not released

- Changed the way widget are calculated when a mask is set: use just the mask, no more intersection between mask and viewport [#661](https://github.com/CartoDB/carto-react/pull/661)
- LegendCategories component migrated from makeStyles to styled-components + cleanup [#634](https://github.com/CartoDB/carto-react/pull/634)
- LegendProportion component migrated from makeStyles to styled-components + cleanup [#635](https://github.com/CartoDB/carto-react/pull/635)
- LegendRamp component migrated from makeStyles to styled-components + cleanup [#636](https://github.com/CartoDB/carto-react/pull/636)
Expand Down
8 changes: 4 additions & 4 deletions packages/react-api/src/hooks/useCartoLayerProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getDataFilterExtensionProps } from './dataFilterExtensionUtil';
import { getMaskExtensionProps } from './maskExtensionUtil';
import FeaturesDroppedLoader from './FeaturesDroppedLoader';
import { CLIENT_ID } from '../api/common';
import { getGeometryToIntersect } from '@carto/react-core';

const LOADERS = [FeaturesDroppedLoader];

Expand All @@ -19,19 +20,18 @@ export default function useCartoLayerProps({
}) {
const viewport = useSelector((state) => state.carto.viewport);
const spatialFilter = useSelector((state) => selectSpatialFilter(state, source?.id));
const geometryToIntersect = getGeometryToIntersect(viewport, spatialFilter);

const [onDataLoadForGeojson] = useGeojsonFeatures({
source,
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
debounceTimeout: viewporFeaturesDebounceTimeout
});

const [onDataLoadForTile, onViewportLoad, fetch] = useTileFeatures({
source,
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
debounceTimeout: viewporFeaturesDebounceTimeout
});
Expand Down
17 changes: 7 additions & 10 deletions packages/react-api/src/hooks/useGeojsonFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import useFeaturesCommons from './useFeaturesCommons';

export default function useGeojsonFeatures({
source,
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
debounceTimeout = 250
}) {
Expand All @@ -23,11 +22,11 @@ export default function useGeojsonFeatures({
const sourceId = source?.id;

const computeFeatures = useCallback(
({ viewport, spatialFilter, uniqueIdProperty }) => {
({ geometryToIntersect, uniqueIdProperty }) => {
executeTask(sourceId, Methods.GEOJSON_FEATURES, {
viewport,
geometry: spatialFilter,
uniqueIdProperty
geometryToIntersect,
uniqueIdProperty,
tileFormat: undefined
})
.then(() => {
setSourceFeaturesReady(true);
Expand All @@ -48,14 +47,12 @@ export default function useGeojsonFeatures({
clearDebounce();
setSourceFeaturesReady(false);
debounceIdRef.current = debouncedComputeFeatures({
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty
});
}
}, [
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
sourceId,
isGeoJsonLoaded,
Expand Down
14 changes: 5 additions & 9 deletions packages/react-api/src/hooks/useTileFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { useDispatch } from 'react-redux';

export default function useTileFeatures({
source,
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
debounceTimeout = 250
}) {
Expand All @@ -32,16 +31,15 @@ export default function useTileFeatures({
const sourceId = source?.id;

const computeFeatures = useCallback(
({ viewport, spatialFilter, uniqueIdProperty }) => {
({ geometryToIntersect, uniqueIdProperty }) => {
if (!tileFormat) {
return null;
}

setSourceFeaturesReady(false);

executeTask(sourceId, Methods.TILE_FEATURES, {
viewport,
geometry: spatialFilter,
geometryToIntersect,
uniqueIdProperty,
tileFormat,
geoColumName,
Expand Down Expand Up @@ -101,14 +99,12 @@ export default function useTileFeatures({
clearDebounce();
setSourceFeaturesReady(false);
debounceIdRef.current = debouncedComputeFeatures({
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty
});
}
}, [
viewport,
spatialFilter,
geometryToIntersect,
uniqueIdProperty,
debouncedComputeFeatures,
sourceId,
Expand Down
33 changes: 23 additions & 10 deletions packages/react-core/__tests__/filters/geojsonFeatures.test.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import bboxPolygon from '@turf/bbox-polygon';
import { geojsonFeatures } from '../../src/filters/geojsonFeatures';

describe('viewport features with geojson data', () => {
/** @type { import('../../src').Viewport } */
const viewport = [-10, -10, 10, 10]; // west - south - east - north
const viewportGeometry = bboxPolygon(viewport).geometry;

describe('return no data', () => {
test('should return an empty array if no geojson features present', () => {
/** @type { import('geojson').FeatureCollection } */
const geojson = {
type: 'FeatureCollection',
features: []
};

const properties = geojsonFeatures({
geojson,
viewport
geometryToIntersect: viewportGeometry
});

expect(properties).toEqual([]);
Expand All @@ -21,6 +25,7 @@ describe('viewport features with geojson data', () => {

describe('correctly returns data', () => {
test('should handle linestrings correctly fwsf', () => {
/** @type { import('geojson').FeatureCollection } */
const linestrings = {
type: 'FeatureCollection',
features: [...Array(3)].map((_, i) => ({
Expand All @@ -39,7 +44,7 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: linestrings,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

Expand All @@ -52,6 +57,7 @@ describe('viewport features with geojson data', () => {
});

test('should handle multilinestrings correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const multilinestrings = {
type: 'FeatureCollection',
features: [...Array(3)].map((_, i) => ({
Expand All @@ -70,7 +76,7 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: multilinestrings,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

Expand All @@ -83,6 +89,7 @@ describe('viewport features with geojson data', () => {
});

test('should handle polygons correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const polygons = {
type: 'FeatureCollection',
features: [...Array(3)].map((_, i) => ({
Expand All @@ -101,7 +108,7 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: polygons,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

Expand All @@ -114,6 +121,7 @@ describe('viewport features with geojson data', () => {
});

test('should handle multilipolygons correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const multipolygons = {
type: 'FeatureCollection',
features: [...Array(3)].map((_, i) => ({
Expand All @@ -135,7 +143,7 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: multipolygons,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

Expand All @@ -150,6 +158,7 @@ describe('viewport features with geojson data', () => {

describe('with repeated features', () => {
test('should handle points correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const points = {
type: 'FeatureCollection',
features: [...Array(4)].map(() => ({
Expand All @@ -167,14 +176,15 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: points,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

expect(properties).toEqual([{ cartodb_id: 1, other_prop: 1 }]);
});

test('should handle linestrings correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const linestrings = {
type: 'FeatureCollection',
features: [...Array(4)].map(() => ({
Expand All @@ -193,14 +203,15 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: linestrings,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

expect(properties).toEqual([{ cartodb_id: 1, other_prop: 1 }]);
});

test('should handle multilinestrings correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const multilinestrings = {
type: 'FeatureCollection',
features: [...Array(4)].map(() => ({
Expand All @@ -219,14 +230,15 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: multilinestrings,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

expect(properties).toEqual([{ cartodb_id: 1, other_prop: 1 }]);
});

test('should handle polygons correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const polygons = {
type: 'FeatureCollection',
features: [...Array(4)].map(() => ({
Expand All @@ -245,14 +257,15 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: polygons,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

expect(properties).toEqual([{ cartodb_id: 1, other_prop: 1 }]);
});

test('should handle multipolygons correctly', () => {
/** @type { import('geojson').FeatureCollection } */
const multipolygons = {
type: 'FeatureCollection',
features: [...Array(4)].map(() => ({
Expand All @@ -274,7 +287,7 @@ describe('viewport features with geojson data', () => {

const properties = geojsonFeatures({
geojson: multipolygons,
viewport,
geometryToIntersect: viewportGeometry,
uniqueIdProperty: 'cartodb_id'
});

Expand Down
Loading