Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
flattenEach(geoJson, feature => {
featureOf(feature, 'LineString', 'Graph::fromGeoJson');
// When a LineString if formed by many segments, split them
coordReduce(feature, (prev, cur) => {
if (prev) {
const start = graph.getNode(prev),
end = graph.getNode(cur);
graph.addEdge(start, end);
}
return cur;
});
});
flattenEach(geoJson, function (feature) {
featureOf(feature, 'LineString', 'Graph::fromGeoJson');
// When a LineString if formed by many segments, split them
coordReduce(feature, function (prev, cur) {
if (prev) {
var start = graph.getNode(prev),
end = graph.getNode(cur);
graph.addEdge(start, end);
}
return cur;
});
});
function pointToLineDistance(pt: Coord, line: Feature | LineString, options: {
units?: Units,
method?: "geodesic" | "planar",
} = {}): number {
// Optional parameters
if (!options.method) { options.method = "geodesic"; }
if (!options.units) { options.units = "kilometers"; }
// validation
if (!pt) { throw new Error("pt is required"); }
if (Array.isArray(pt)) { pt = point(pt);
} else if (pt.type === "Point") { pt = feature(pt);
} else { featureOf(pt, "Point", "point"); }
if (!line) { throw new Error("line is required"); }
if (Array.isArray(line)) { line = lineString(line);
} else if (line.type === "LineString") { line = feature(line);
} else { featureOf(line, "LineString", "line"); }
let distance = Infinity;
const p = pt.geometry.coordinates;
segmentEach(line, (segment) => {
const a = segment!.geometry.coordinates[0];
const b = segment!.geometry.coordinates[1];
const d = distanceToSegment(p, a, b, options);
if (d < distance) { distance = d; }
});
return convertLength(distance, "degrees", options.units);
}
method?: "geodesic" | "planar",
} = {}): number {
// Optional parameters
if (!options.method) { options.method = "geodesic"; }
if (!options.units) { options.units = "kilometers"; }
// validation
if (!pt) { throw new Error("pt is required"); }
if (Array.isArray(pt)) { pt = point(pt);
} else if (pt.type === "Point") { pt = feature(pt);
} else { featureOf(pt, "Point", "point"); }
if (!line) { throw new Error("line is required"); }
if (Array.isArray(line)) { line = lineString(line);
} else if (line.type === "LineString") { line = feature(line);
} else { featureOf(line, "LineString", "line"); }
let distance = Infinity;
const p = pt.geometry.coordinates;
segmentEach(line, (segment) => {
const a = segment!.geometry.coordinates[0];
const b = segment!.geometry.coordinates[1];
const d = distanceToSegment(p, a, b, options);
if (d < distance) { distance = d; }
});
return convertLength(distance, "degrees", options.units);
}