Skip to content

Commit

Permalink
Filtering using postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
underbluewaters committed Oct 17, 2024
1 parent 0ee4cee commit adac17f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/h3-filter-ingest/build-cell-pmtiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const MIN_RESOLUTION = 6;
layer.features.add(feature);
progressBar.increment();
});
ds.close();
progressBar.stop();
cells.clear();
parents.forEach((parent) => cells.add(parent));
Expand Down
57 changes: 37 additions & 20 deletions packages/h3-filter-ingest/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Pool } from "pg";
import { createServer } from "http";
import { createHash } from "crypto";
import { stops, zoomToH3Resolution } from "./stops";

const attributeData = require("../output/attributes.json");

Expand Down Expand Up @@ -50,6 +51,8 @@ const server = createServer(async (req, res) => {
// add cors headers to allow all origins
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Type", "text/plain");
// set cache headers
res.setHeader("Cache-Control", "public, max-age=60");
res.end(data);
} catch (error) {
console.error("Error querying database", error);
Expand All @@ -73,6 +76,39 @@ const server = createServer(async (req, res) => {
res.end("Error querying database");
}
}
} else if (req.method === "GET" && path === "/count") {
// get filters from query string
let filters: { [column: string]: Filter } | null = null;
if (parsedUrl.searchParams.has("filter")) {
const filter = JSON.parse(
decodeURIComponent(parsedUrl.searchParams.get("filter")!)
);
if (typeof filter === "object") {
filters = filter;
}
}
// get count of cells
const f = buildWhereClauses(filters || {}, 1);
const data = await pool.query({
name:
"count" +
createHash("md5").update(JSON.stringify(filters)).digest("hex"),
text: `
select
count(distinct(id)) as count
from
cells
where
${f.values.length > 0 ? f.where : "true"}
`,
values: f.values,
});
// add cors headers to allow all origins
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Type", "application/json");
// set cache headers
res.setHeader("Cache-Control", "public, max-age=3600");
res.end(JSON.stringify({ count: parseInt(data.rows[0].count || 0) }));
} else {
res.statusCode = 404;
res.setHeader("Access-Control-Allow-Origin", "*");
Expand Down Expand Up @@ -165,27 +201,8 @@ type Stop = {
zoomLevel: number;
};

const stops: Stop[] = [
{ h3Resolution: 11, zoomLevel: 14 },
{ h3Resolution: 10, zoomLevel: 13 },
{ h3Resolution: 9, zoomLevel: 12 },
{ h3Resolution: 9, zoomLevel: 11 },
{ h3Resolution: 8, zoomLevel: 10 },
// { h3Resolution: 7, zoomLevel: 8 },
{ h3Resolution: 7, zoomLevel: 8 },
{ h3Resolution: 6, zoomLevel: 6 },
// { h3Resolution: 5, zoomLevel: 5 },
].sort((a, b) => a.zoomLevel - b.zoomLevel);

function getResolutionForZoom(zoom: number) {
const idx = stops.findIndex((stop) => stop.zoomLevel > zoom);
if (idx === -1) {
return stops[stops.length - 1].h3Resolution;
} else if (idx === 0) {
return stops[0].h3Resolution;
} else {
return stops[idx - 1].h3Resolution;
}
return zoomToH3Resolution(zoom, stops);
}

type NumberFilter = {
Expand Down
13 changes: 12 additions & 1 deletion packages/h3-filter-ingest/src/stops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ export const stops: Stop[] = [
{ h3Resolution: 11, zoomLevel: 14 },
{ h3Resolution: 10, zoomLevel: 13 },
{ h3Resolution: 9, zoomLevel: 12 },
{ h3Resolution: 9, zoomLevel: 11 },
// { h3Resolution: 9, zoomLevel: 11 },
{ h3Resolution: 8, zoomLevel: 10 },
// { h3Resolution: 7, zoomLevel: 8 },
{ h3Resolution: 7, zoomLevel: 8 },
{ h3Resolution: 6, zoomLevel: 6 },
// { h3Resolution: 5, zoomLevel: 5 },
].sort((a, b) => b.zoomLevel - a.zoomLevel);

export function zoomToH3Resolution(zoom: number, stops: Stop[]) {
let h3Resolution = stops[0].h3Resolution;
for (let i = 0; i < stops.length; i++) {
if (zoom > stops[i].zoomLevel) {
break;
}
h3Resolution = stops[i].h3Resolution;
}
return h3Resolution;
}

0 comments on commit adac17f

Please sign in to comment.