Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function pointInShape(
pt: [number, number] | { lat: number; lng: number },
shape: Geometry | FeatureCollection
): boolean {
const point: [number, number] = Array.isArray(pt) ? pt : [pt.lng, pt.lat]
console.log('pointinshape')
console.log(point)
console.log(shape)
console.log('')
if (shape.type === 'Point') {
if (pointInPolygon(point, circleToPolygon(shape.coordinates, RADIUS, NUMBER_OF_EDGES))) {
return true
}
return false
}
if (shape.type === 'MultiPolygon') {
return pointInMultiPolygon(point, shape)
}
if (shape.type === 'Polygon') {
return pointInPolygon(point, shape)
}
if (shape.type === 'FeatureCollection') {
return pointInFeatureCollection(point, shape)
}
return pointInGeometry(point, shape)
}
function makePointInShape(shape: Geometry): { lat: number; lng: number } {
if (!shape) {
throw new Error('no shape')
}
const shapeToCreate = shape.type === 'Point' ? circleToPolygon(shape.coordinates, RADIUS, NUMBER_OF_EDGES) : shape
const bbox = calcBBox(shapeToCreate)
let tries = 0
while (tries < 1000) {
const pt: [number, number] = [rangeRandom(bbox.lngMin, bbox.lngMax), rangeRandom(bbox.latMin, bbox.latMax)]
console.log('make point in shape: ***')
console.log('is pointInShape?', pointInShape(pt, shape))
console.log(pt, shape)
console.log('*******')
if (pointInShape(pt, shape)) {
return {
lng: pt[0],
lat: pt[1]
}
}
tries += 1
audience.geolocations.forEach(geolocation => {
if (geolocation.geojson) {
geojson.features.push({
...geolocation.geojson,
properties: {
_id: geolocation._id
}
});
} else if (geolocation.center) {
const geometry = circleToPolygon(
[geolocation.center.center[1], geolocation.center.center[0]],
geolocation.center.radius * 1000,
32
);
geojson.features.push({
type: "Feature",
properties: {
_id: geolocation._id,
radius: geolocation.center.radius
},
geometry
});
}
});
return geojson;
function pointInGeometry(pt: [number, number], shape: Geometry): boolean {
if (shape.type === 'MultiPolygon') {
return pointInMultiPolygon(pt, shape)
}
if (shape.type === 'Polygon') {
return pointInPolygon(pt, shape)
}
if (shape.type === 'Point') {
if (pointInPolygon(pt, circleToPolygon(shape.coordinates, RADIUS, NUMBER_OF_EDGES))) {
return true
}
return false
}
throw new Error(`cannot check point in shape for type ${shape.type}`)
}