Skip to content

Commit

Permalink
load tile layer from separate tile server
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmaLRussell committed Nov 7, 2024
1 parent d72080c commit 2dd1174
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions scripts/add_tippecanoe_layer_names.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// For the tiling proof of concept:
// We want each feature in our geojson to become a layer in the mbtiles database, and for this we need to
// inject the tippecanoe.layer property so that tippecanoe generates these layers. We'll set the value
// of this prop to the feature id
// Currently doing proof of concept for Brazil level 2 only!
// In future we will probably include this processing in our tile server repo

/* eslint-disable import/no-dynamic-require */
const path = require("path");
const fs = require("fs");

const dir = `${__dirname}/../public/dengue/may24/resources/geojson/admin2/`;
const inputFile = path.normalize(`${dir}gadm41_BRA_2_100pc.json`);
const outputFile = path.normalize(`${dir}gadm41_BRA_2_100pc_for_tippecanoe.json`);
console.log(`Transforming geojson at ${inputFile}`);

const input = require(inputFile);
const features = input.features;

Check failure on line 18 in scripts/add_tippecanoe_layer_names.cjs

View workflow job for this annotation

GitHub Actions / test

Use object destructuring
console.log(`found ${features.length} features`);
features.forEach((feature) => {
const id = feature.properties.GID_2;
feature.tippecanoe = {

Check failure on line 22 in scripts/add_tippecanoe_layer_names.cjs

View workflow job for this annotation

GitHub Actions / test

Assignment to property of function parameter 'feature'
layer: id
};
});

const output = JSON.stringify(input);
fs.writeFile(outputFile, output, (err) => {
if (err) {
throw err;
}
console.log(`Saved to ${outputFile}`);
});

Check failure on line 33 in scripts/add_tippecanoe_layer_names.cjs

View workflow job for this annotation

GitHub Actions / test

Insert `⏎`
2 changes: 1 addition & 1 deletion src/components/Choropleth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { useIndicatorColors } from "../composables/useIndicatorColors";
import { useLeaflet } from "../composables/useLeaflet";
import "leaflet/dist/leaflet.css";
import { useTooltips } from "../composables/useTooltips";
import { APP_BASE_ROUTE } from "../router/utils";
import { APP_BASE_ROUTE, APP_BASE_URL } from "../router/utils";

Check failure on line 45 in src/components/Choropleth.vue

View workflow job for this annotation

GitHub Actions / test

'APP_BASE_URL' is defined but never used
import { routerPush } from "../utils";
import { backgroundLayer } from "./utils";
import { useLoadingSpinner } from "../composables/useLoadingSpinner";
Expand Down
40 changes: 37 additions & 3 deletions src/composables/useLeaflet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { useAppStore } from "../stores/appStore";
import { countryAdmin1OutlineStyle, countryOutlineStyle, minZoom } from "../components/utils";
import { useDataSummary } from "./useDataSummary";
import { MapFeature } from "../types/resourceTypes";
import { APP_BASE_URL } from "../router/utils";
import 'leaflet/dist/leaflet.css';

type TooltipOptionAndContent = { content: string; options?: TooltipOptions };

Expand Down Expand Up @@ -62,7 +64,7 @@ export const useLeaflet = (

const { dataSummary } = useDataSummary(bounds);

const { countryBoundingBoxes, admin0GeojsonFeature, admin1Geojson, mapSettings } = storeToRefs(useAppStore());
const { countryBoundingBoxes, admin0GeojsonFeature, admin1Geojson, admin2Geojson, mapSettings } = storeToRefs(useAppStore());

const getRegionBounds = (countryId?: string) => {
if (Object.keys(countryBoundingBoxes.value).length === 0) return null;
Expand Down Expand Up @@ -143,13 +145,44 @@ export const useLeaflet = (
};

// TODO: fix up types again - shouldn't really need to use L
vectorGridLayer.value = vectorGrid.slicer(geoJsonDocument, {
/*vectorGridLayer.value = vectorGrid.slicer(geoJsonDocument, {
//rendererFactory: L.canvas.tile, // or L.svg.tile
interactive: true,
vectorTileLayerStyles: {
sliced: style
}
}).on("click",
})
.on("click",
clickEvent
).on("mouseover", (e: LayerMouseEvent) => {
const content = getTooltip(e);
closeTooltip(); //close any existing tooltip
tooltip.value = L.tooltip({ sticky: true, permanent: false})
.setContent(content)
.setLatLng(e.latlng)
.openOn(leafletMap);
}).on("mouseout", () => {
closeTooltip();
})
.addTo(leafletMap);*/
const testBounds = getRegionBounds("BRA"); // set bounds on region2 tile layer to only get the tile(s) we need
// This is a bit tedious but doesn't seem to be any way around it - need to provide styles as a dictionary where
// keys are layer names i.e. IDs of each feature. They call all have the same value - the function to calculate
// feature style!
const vectorTileLayerStyles = Object.fromEntries(admin2Geojson.value["BRA"].map((feature) => [feature.properties.GID_2, style]));

const vectorTileOptions = {
vectorTileLayerStyles,
// rendererFactory: L.canvas.tile, // or L.svg.tile
interactive: true,
maxNativeZoom: 10,
tms: true, // y values are inverted without this!
bounds: [testBounds.getSouthWest(), testBounds.getNorthEast()]
}
const testTileUrl = `http://localhost:5000/testtiles/{z}/{x}/{y}`;

const pbfLayer= vectorGrid.protobuf(testTileUrl,vectorTileOptions);
pbfLayer.on("click",
clickEvent
).on("mouseover", (e: LayerMouseEvent) => {
const content = getTooltip(e);
Expand All @@ -162,6 +195,7 @@ export const useLeaflet = (
closeTooltip();
}).addTo(leafletMap);


// adding country outline if we have a admin0GeojsonFeature
if (admin0GeojsonFeature.value) {
const latLngs = GeoJSON.coordsToLatLngs(admin0GeojsonFeature.value.geometry.coordinates, 2);
Expand Down
Binary file added test_tiles/test.mbtiles
Binary file not shown.

0 comments on commit 2dd1174

Please sign in to comment.