Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const polygonEvents = ['set_at', 'insert_at', 'remove_at'];
const circleEvents = ['center_changed', 'radius_changed'];
import turfCircle from '@turf/circle';
import { polygon as turfPolygon } from '@turf/helpers';
import { featureToWKT } from './map-utilities';
import { cond, has, hasIn, pipe, path } from 'ramda';
export const setShapeEventListener = (shape, action) => {
if (shape.radius) {
circleEvents.forEach(event => {
shape.addListener(event, action);
});
}
if (shape.getPath) {
polygonEvents.forEach(event => {
shape.getPath().addListener(event, action);
});
}
return shape;
};
const closeRing = points => [...points, points[0]];
const pointToArray = point => [point.lng(), point.lat()];
// google polygons are not closed so we use closeRing to make it a proper geoJson feature
const mapsPolygonToFeature = polygon => turfPolygon([closeRing(polygon
.getPath()
.getArray()
.map(pointToArray)
)]);
const mapsCircleToFeature = circle => turfCircle(pointToArray(circle.getCenter()), circle.radius, null, 'meters');
const getFeatureFromGoogleMapsShape = cond([[has('radius'), mapsCircleToFeature], [hasIn('getPath'), mapsPolygonToFeature]]);
export const convertFromGoogleMapsShape = pipe(getFeatureFromGoogleMapsShape, path(['geometry']));
export const getFiltersFromGoogleMapsShape = pipe(getFeatureFromGoogleMapsShape, featureToWKT);
const metersToSquareMiles = meters => (meters / 2589988.11).toPrecision(3);
const getCircleArea = ({ radius }) => metersToSquareMiles(Math.PI * radius * radius);
const getPolygonArea = polygon => metersToSquareMiles(google.maps.geometry.spherical.computeArea(polygon.getPath())); // eslint-disable-line no-undef
export const getShapeArea = cond([[has('radius'), getCircleArea], [hasIn('getPath'), getPolygonArea]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.