-
-
Save qrobin/a728521b4aa64b636779e2c7c99aa20f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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