Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const getBufferFeature = (placeGeometry, bufferOptions) => {
try {
return buffer(placeGeometry, bufferOptions.distance, {
units: bufferOptions.units,
});
} catch (e) {
// eslint-disable-next-line no-console
console.error(e);
Mixpanel.track("Error", {
message: "unable to perform getBufferFeature",
error: e,
});
return null;
}
};
export function bufferAndUnion(features, meters) {
let geometry = null;
for(let i = 0, ii = features.length; i < ii; i++) {
// buffer the geometry.
const g = turf_buffer(features[i], meters, {units: 'meters'});
// if the output geometry is still null, then set the
// first member to the new geometry
if(geometry === null) {
geometry = g;
// otherwise buffer it.
} else {
geometry = turf_union(geometry, g);
}
}
return geometry.geometry;
}
polygon: selPolygon,
lineColor: POLYGON_LINE_COLOR,
fillColor: POLYGON_FILL_COLOR
});
} else if (this.usePolygon && this.landPoints.length) {
data.push({
polygon: this.landPoints,
lineColor: POLYGON_LINE_COLOR,
fillColor: POLYGON_FILL_COLOR
});
// Hack: use a polygon to hide the outside, because pickObjects()
// does not support polygons
if (this.landPoints.length >= 3) {
const landPointsPoly = polygon([[...this.landPoints, this.landPoints[0]]]);
const bigBuffer = turfBuffer(point(this.landPoints[0]), EXPANSION_KM);
let bigPolygon;
try {
// turfDifference throws an exception if the polygon
// intersects with itself
bigPolygon = turfDifference(bigBuffer, landPointsPoly);
dataPick.push({
polygon: bigPolygon.geometry.coordinates,
fillColor: [0, 0, 0, 1]
});
this.validPolygon = true;
} catch (e) {
// invalid selection polygon
this.validPolygon = false;
}
}
}
featureEach(output, current => {
let
area = buffer(current, sr),
ptsWithin = pointsWithinPolygon(output, area);
// the initial value of -1 is on purpose to disregard the point itself.
current.properties.kernelDensity = featureReduce(ptsWithin, prev => prev + 1, -1);
});
return output;
moveTo(location, zoom) {
if (!location) return;
if (location.bbox) {
// We have a bbox to fit to
const distance = turfDistance(
[location.bbox[0], location.bbox[1]],
[location.bbox[2], location.bbox[3]]
);
const buffered = turfBuffer(
turfBboxPolygon(location.bbox),
distance / 2,
"kilometers"
);
const bbox = turfBbox(buffered);
try {
this.map.fitBounds(bbox, { linear: true });
} catch (e) {
this.map.fitBounds(location.bbox, { linear: true });
}
} else {
// We just have a point
this.map.easeTo({
center: location.geometry.coordinates,
zoom: zoom || 16
});
this.trip.path.push(goal);
return;
}
let goal_layer = this.agentmap.units.getLayer(goal_place.id) || this.agentmap.streets.getLayer(goal_place.id);
//If the goal isn't unanchored, see if it's a street or a unit and schedule the agent appropriately.
if (goal_layer) {
let goal_coords = L.A.pointToCoordinateArray(goal_lat_lng);
//Buffering so that points on the perimeter, like the door, are captured.
//Also expands street lines into thin polygons (booleanPointInPolygon requires polys).
//Might be more efficient to generate the door so that it's slightly inside the area.
let goal_polygon = buffer(goal_layer.toGeoJSON(), .001);
if (booleanPointInPolygon(goal_coords, goal_polygon)) {
if (start_place.type === "unit" && goal_place.type === "unit" && start_place.id === goal_place.id) {
this.setTravelInUnit(goal_lat_lng, goal_place, speed);
return;
}
//Move to the street if it's starting at a unit and its goal is elsewhere.
else if (start_place.type === "unit") {
let start_unit_door = this.agentmap.getUnitDoor(start_place.id);
start_unit_door.new_place = start_place,
start_unit_door.speed = speed;
this.trip.path.push(start_unit_door);
let start_unit_street_id = this.agentmap.units.getLayer(start_place.id).street_id,
start_unit_street_point = this.agentmap.getStreetNearDoor(start_place.id);
start_unit_street_point.new_place = { type: "street", id: start_unit_street_id },
if(searchType === TileType.GEOMETRY)
tilePaths.addType(TileType.GEOMETRY);
else if(searchType === TileType.INTERSECTION)
tilePaths.addType(TileType.INTERSECTION);
else
throw "invalid search type must be GEOMETRY or INTERSECTION"
if(additionalTypes) {
for(var type of additionalTypes) {
tilePaths.addType(type);
}
}
await this.indexTilesByPathGroup(tilePaths);
var bufferedPoint:turfHelpers.Feature = buffer(point, searchRadius, {'units':'meters'});
var data:turfHelpers.FeatureCollection
if(searchType === TileType.GEOMETRY)
data = this.geometryIndex.search(bufferedPoint);
else if(searchType === TileType.INTERSECTION)
data = this.intersectionIndex.search(bufferedPoint);
return data;
}
_makeStartPointHighlight(center: [number, number]): number[] {
const buffer = turfBuffer(point(center), POLYGON_THRESHOLD / 4.0);
return turfBboxPolygon(turfBbox(buffer)).geometry.coordinates;
}
export function buffer(feature, meters) {
return turf_buffer(feature, meters, {units: 'meters'}).geometry;
}
static addBuffer(geometry, radius = 0, projection = 'EPSG:3857') {
if (radius === 0) {
return geometry;
}
const geoJsonFormat = new OlFormatGeoJSON({
dataProjection: 'EPSG:4326',
featureProjection: projection
});
const geoJson = geometry instanceof OlFeature
? geoJsonFormat.writeFeatureObject(geometry)
: geoJsonFormat.writeGeometryObject(geometry);
const buffered = buffer(geoJson, radius, {
units: 'meters'
});
if (geometry instanceof OlFeature) {
return geoJsonFormat.readFeature(buffered);
} else {
return geoJsonFormat.readGeometry(buffered.geometry);
}
}