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

Fix thumbnail error in previous analysis #492

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Changes from all 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
43 changes: 40 additions & 3 deletions app/scripts/components/analysis/saved-analysis-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { Link } from 'react-router-dom';
import { FeatureCollection, Polygon } from 'geojson';
import styled, { useTheme } from 'styled-components';
import bbox from '@turf/bbox';
import { glsp, themeVal } from '@devseed-ui/theme-provider';
import { ButtonProps } from '@devseed-ui/button';
import { CollecticonClockBack } from '@devseed-ui/collecticons';
Expand Down Expand Up @@ -150,7 +151,7 @@ function SavedAnalysisThumbnail(props: { aoi: FeatureCollection<Polygon> }) {
const theme = useTheme();

const styledFeatures = {
...aoi,
type: 'FeatureCollection',
features: aoi.features.map(({ geometry }) => ({
type: 'Feature',
properties: {
Expand All @@ -162,9 +163,45 @@ function SavedAnalysisThumbnail(props: { aoi: FeatureCollection<Polygon> }) {
}))
};

const encoded = encodeURIComponent(JSON.stringify(styledFeatures));
let encodedGeoJson = encodeURIComponent(JSON.stringify(styledFeatures));
const encodedGeoJsonChars = encodedGeoJson.length;

const src = `https://api.mapbox.com/styles/v1/covid-nasa/cldac5c2c003k01oebmavw4q3/static/geojson(${encoded})/auto/480x320?access_token=${process.env.MAPBOX_TOKEN}`;
// If more than 8000 chars the request will fail.
// In this case simplify and show a bounding box.
const MAX_MAPBOX_API_CHARS = 8000;
if (encodedGeoJsonChars > MAX_MAPBOX_API_CHARS) {
const [w, s, e, n] = bbox(styledFeatures);
// We want the corners length to be 1/4 of the distance between
// W & E / N & S
const lonSide = (w * -1 + e) * 0.25;
const latSide = (n * -1 + s) * 0.25;

const makeCorner = (p1, p2, p3) => ({
type: 'Feature',
properties: {
'stroke-width': 8,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that value related to the generated thumbnail zoom level? In that case, isn't there a risk of having visually different stroke widths?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screenshot 2023-03-27 at 10 58 56

Nevermind this - looks good on different zoom levels.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For completeness:
the stroke width is dependent on the image size. In this case it's 480x320 (but used in a smaller size) which is why it has such a high value (8)

stroke: theme.color?.primary
},
geometry: {
type: 'LineString',
coordinates: [p1, p2, p3]
}
});

const fc = {
type: 'FeatureCollection',
features: [
makeCorner([w + lonSide, n], [w, n], [w, n + latSide]),
makeCorner([e - lonSide, n], [e, n], [e, n + latSide]),
makeCorner([e - lonSide, s], [e, s], [e, s - latSide]),
makeCorner([w + lonSide, s], [w, s], [w, s - latSide])
]
};

encodedGeoJson = encodeURIComponent(JSON.stringify(fc));
}

const src = `https://api.mapbox.com/styles/v1/covid-nasa/cldac5c2c003k01oebmavw4q3/static/geojson(${encodedGeoJson})/auto/480x320?padding=32&access_token=${process.env.MAPBOX_TOKEN}`;

return <img src={src} alt='Thumbnail showing AOI' />;
}