Skip to content

Commit

Permalink
Clean up pyriv
Browse files Browse the repository at this point in the history
  • Loading branch information
avmey committed Oct 16, 2024
1 parent 632fc10 commit daf7c72
Show file tree
Hide file tree
Showing 8 changed files with 5 additions and 61 deletions.
1 change: 0 additions & 1 deletion data/bin/landShrunk.01.geojson

This file was deleted.

File renamed without changes.
1 change: 0 additions & 1 deletion data/bin/network.05.json

This file was deleted.

1 change: 0 additions & 1 deletion data/bin/nodes.01nogrid.json

This file was deleted.

36 changes: 2 additions & 34 deletions data/bin/pyriv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ const dataDir = "./data/bin";
const fullPath = (s: string) => path.join(dataDir, s);
const watersPath = fullPath("clippingLayer.01.geojson");
const landPath = fullPath("land.01.geojson");
const landShrunkOut = fullPath("landShrunk.01.geojson");
const jsonOut = fullPath("network.01.nogridJson.json");
const nodesOut = fullPath("nodes.01nogrid.json");
const landShrunkOut = fullPath("landShrunk.01.json");
const jsonOut = fullPath("network.01.json");

// Read and process land data
function loadAndShrinkLandData(landFile: string): any {
Expand All @@ -34,24 +33,6 @@ export function extractVerticesFromPolygon(
});
}

// Add grid nodes within polygons
function addGridNodes(
polygon: any,
featureIndex: number,
vertices: Map<string, number[]>
): void {
const bbox = turf.bbox(turf.polygon(polygon));
const grid = turf.pointGrid(bbox, 3, { units: "miles" });

grid.features.forEach((point: any, gridIndex: number) => {
const [x, y] = point.geometry.coordinates;
if (turf.booleanPointInPolygon(point, turf.polygon(polygon))) {
const id = `gridnode_${featureIndex}_${gridIndex}`;
vertices.set(id, [x, y]);
}
});
}

// Create graph with vertices
function createGraph(watersData: any): graphlib.Graph {
const G = new graphlib.Graph();
Expand All @@ -64,26 +45,13 @@ function createGraph(watersData: any): graphlib.Graph {
featureIndex,
vertices
);
//addGridNodes(feature.geometry.coordinates, featureIndex, vertices);
} else if (feature.geometry.type === "MultiPolygon") {
feature.geometry.coordinates.forEach((polygon: any) => {
extractVerticesFromPolygon(polygon, featureIndex, vertices);
//addGridNodes(polygon, featureIndex, vertices);
});
}
});

fs.writeFileSync(
nodesOut,
JSON.stringify(
turf.featureCollection(
Array.from(vertices.values()).map((coord, id) =>
turf.point(coord, { id })
)
)
)
);

vertices.forEach((coord, id) => {
G.setNode(id, coord);
});
Expand Down
19 changes: 1 addition & 18 deletions src/functions/spacing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
import { InvocationResponse } from "@aws-sdk/client-lambda";
import { spacingWorker } from "./spacingWorker.js";
import graphlib from "graphlib";
import graphData from "../../data/bin/network.01.nogridJson.json";
import graphData from "../../data/bin/network.01.json";
import landData from "../../data/bin/landShrunk.01.json";
import {
bboxPolygon,
Expand Down Expand Up @@ -52,7 +52,6 @@ export async function spacing(
const metricGroup = project.getMetricGroup("spacing");

// First, start the spacing workers to calculate metrics
console.log("Starting spacing workers");
const metricPromises: Promise<
{ id: string; replicateIds: string[] } | InvocationResponse
>[] = [];
Expand All @@ -74,15 +73,9 @@ export async function spacing(

// Then add the sketches to the graph
const sketchArray = toSketchArray(sketch);
let addSketchStart = Date.now();
const finalGraph = await addSketchesToGraph(sketch, request);
let addSketchEnd = Date.now();
console.log(
`Adding sketches to graph took ${addSketchEnd - addSketchStart} ms`,
);

// Await the replicate metrics
let start = Date.now();
const replicates: Record<string, string[]> = {};
(await Promise.all(metricPromises)).forEach((result) => {
const finalResult =
Expand All @@ -94,11 +87,8 @@ export async function spacing(
});
replicates[finalResult.id] = finalResult.replicateIds;
});
let end = Date.now();
console.log(`Waited an additional ${end - start} ms for metrics`);

// Generate MST
let mstStart = Date.now();
const result = await Promise.all(
metricGroup.classes.map(async (curClass) => {
const classReplicateIds = replicates[curClass.datasourceId!];
Expand Down Expand Up @@ -185,9 +175,6 @@ export async function spacing(
}),
);

let mstEnd = Date.now();
console.log(`Generating all paths: ${mstEnd - mstStart} ms`);

return {
sketch: sketchArray.map((sketch) => simplify(sketch, { tolerance: 0.005 })),
result,
Expand Down Expand Up @@ -352,8 +339,6 @@ function findShortestPath(
let minTotalDistance = Infinity;

// Iterate over all nodes in nodes0
let start = Date.now();

nodes0.forEach((node0) => {
// Run Dijkstra's algorithm for node0
const pathResults = graphlib.alg.dijkstra(graph, node0, (edge) =>
Expand Down Expand Up @@ -400,8 +385,6 @@ function findShortestPath(
}
});
});
let end = Date.now();
console.log(`Checking distances took ${end - start} ms`);

if (minTotalDistance === Infinity) {
throw new Error(
Expand Down
2 changes: 1 addition & 1 deletion src/functions/spacingGraphWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
polygon,
} from "@turf/turf";
import { FeatureCollection } from "@seasketch/geoprocessing/client-core";
import graphData from "../../data/bin/network.01.nogridJson.json";
import graphData from "../../data/bin/network.01.json";
import { readGraphFromFile } from "./spacing.js";
const SEARCH_RADIUS_MILES = 75; // Set search radius to 75 miles

Expand Down
6 changes: 1 addition & 5 deletions src/functions/spacingWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,7 @@ export async function spacingWorker(
.filter((skm) => skm.sketchId === m.sketchId)
.reduce((acc, val) => acc + val.value, 0),
);
console.log(
extraParams.datasourceId,
value,
replicateTest[extraParams.datasourceId].replicateVal,
);

return value > replicateTest[extraParams.datasourceId].replicateVal;
});

Expand Down

0 comments on commit daf7c72

Please sign in to comment.