diff --git a/src/data/localGeojsonCore.js b/src/data/localGeojsonCore.js index 18d3a9439..ab1bdec44 100644 --- a/src/data/localGeojsonCore.js +++ b/src/data/localGeojsonCore.js @@ -344,7 +344,7 @@ const given = (value) => value !== undefined && value !== null; * properties (`stroke`, `stroke-opacity`, `stroke-width`), over the * layer's colour and width. */ -function strokeOf(properties, rgba) { +export function strokeOf(properties, rgba) { let color = null; let widthPx = FEATURE_LINE_WIDTH_PX; if (properties) { @@ -362,7 +362,7 @@ function strokeOf(properties, rgba) { * A polygon's own fill as the GeoJSON loader read it (`fill` keeps the * layer's fill alpha; `fill-opacity` sets the alpha). */ -function fillOf(properties, fillColor) { +export function fillOf(properties, fillColor) { let fill = null; if (properties) { if (given(properties.fill)) { @@ -409,7 +409,7 @@ function* markerSymbols(features) { * Ground `coordinates` as `{lon, lat}` degrees along the rhumb lines * between them, as the GeoJSON loader drew its draped edges. */ -function rhumbPositions(coordinates) { +export function rhumbPositions(coordinates) { return densifyRhumb( coordinates.map((c) => ({ lon: c[0] * RADIANS, lat: c[1] * RADIANS })), RHUMB_GRANULARITY_M, diff --git a/src/library/layers.js b/src/library/layers.js index 591cff520..a36a49520 100644 --- a/src/library/layers.js +++ b/src/library/layers.js @@ -7,8 +7,11 @@ */ import { createLocalGeoJsonLayer, + fillOf, localDatasetError, localGeojsonParts, + rhumbPositions, + strokeOf, } from '../data/localGeojsonCore.js'; import { canvasOf, @@ -777,33 +780,39 @@ export function createLibraryViewLayer( const dots = []; for (const part of localGeojsonParts(features)) { const meta = { layerId: id, id: part.id }; - const own = colorOf(part.feature.properties); + const style = part.feature.properties; + const own = colorOf(style); + // As the GeoJSON loader drew them: edges along rhumb lines, and a + // feature's simplestyle fill and stroke over the entry's colour (a + // colorBy fill over both). if (part.kind === 'polygon') fills.push({ id: part.id, geometry: { kind: 'polygon', - rings: part.coordinates.map((ring) => - ring.map(([lon, lat]) => ({ lon, lat })), - ), + rings: part.coordinates.map(rhumbPositions), }, - fill: withAlpha(own, colorBy ? 0.45 : 0.35), + fill: colorBy + ? withAlpha(own, 0.45) + : fillOf(style, withAlpha(color, 0.35)), height: 'clampGround', clampTo: 'both', meta, }); - else if (part.kind === 'line') + else if (part.kind === 'line') { + const stroke = strokeOf(style, color); lines.push({ id: part.id, - positions: part.coordinates.map(([lon, lat]) => + positions: rhumbPositions(part.coordinates).map(({ lon, lat }) => fromDegrees(lon, lat, 0), ), - widthPx: 2, + widthPx: stroke.widthPx, mode: 'draped', clampTo: 'both', - style: { kind: 'solid', color }, + style: { kind: 'solid', color: stroke.color }, meta, }); + } // Points draw as small dots, not pins: a view can hold thousands. else if (part.kind === 'point') dots.push({ diff --git a/src/library/viewLayer.test.mjs b/src/library/viewLayer.test.mjs index 8bf12ff79..3c4886521 100644 --- a/src/library/viewLayer.test.mjs +++ b/src/library/viewLayer.test.mjs @@ -267,3 +267,60 @@ test('values sharing a label are one legend group, and uncoloured values count a ); assert.equal(legend.at(-1).color, '#a0aec0'); }); + +test('features draw as the GeoJSON loader drew them: rhumb edges and their own simplestyle', async (t) => { + // A long east-west edge: a rhumb line holds its latitude, a geodesic bows + // toward the pole, so the edge is drawn as points along its rhumb line. + const wide = { + type: 'Feature', + geometry: { + type: 'Polygon', + coordinates: [ + [ + [-80, 38.89], + [-74, 38.89], + [-74, 38.9], + [-80, 38.89], + ], + ], + }, + properties: { fill: '#ff0000', 'fill-opacity': 0.5 }, + }; + const road = { + type: 'Feature', + geometry: { + type: 'LineString', + coordinates: [ + [-80, 38.89], + [-74, 38.89], + ], + }, + properties: { stroke: '#00ff00', 'stroke-width': 5 }, + }; + // An entry with no colorBy (a colorBy fill is drawn over simplestyle). + const plainEntry = { + ...entry, + display: { ...entry.display, colorBy: undefined }, + }; + const layer = createLibraryViewLayer(plainEntry, { + source: { loadDataset: async () => ({ features: [wide, road, polygon] }) }, + }); + const { viewer, drawn, draw } = fakeViewer(t, { height: 2000 }); + await layer.init(viewer); + await layer.enable(viewer); + const [own, plain] = drawn()[0].items.values(); + const ring = own.geometry.rings[0]; + assert.ok(ring.length > 40, `densified (${ring.length} points)`); + for (const { lat } of ring.slice(0, ring.length / 2)) + assert.ok(Math.abs(lat - 38.89) < 1e-9, 'the long edge holds its latitude'); + assert.deepEqual(own.fill, [1, 0, 0, 0.5]); + // Without simplestyle, the entry's colour at 0.35. + assert.equal(plain.fill[3], 0.35); + const [line] = [...draw.sets.values()] + .find((set) => !set.disposed && set.spec.id.endsWith(':lines')) + .items.values(); + assert.equal(line.widthPx, 5); + assert.deepEqual(line.style.color.slice(0, 3), [0, 1, 0]); + assert.ok(line.positions.length > 40); + await layer.destroy(viewer); +});