github.com/dgraph-io/dgraph@v1.2.8/types/earth.go (about) 1 /* 2 * Copyright 2016-2018 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 "fmt" 21 22 "github.com/golang/geo/s1" 23 ) 24 25 // Helper functions for earth distances 26 27 // EarthRadiusMeters is the radius of the earth in meters (in a spherical earth model). 28 const EarthRadiusMeters = 1000 * 6371 29 30 // Length denotes a length on Earth 31 type Length float64 32 33 // EarthDistance converts an angle to distance on earth in meters. 34 func EarthDistance(angle s1.Angle) Length { 35 return Length(angle.Radians() * EarthRadiusMeters) 36 } 37 38 // EarthAngle converts a to distance on earth in meters to an angle 39 func EarthAngle(dist float64) s1.Angle { 40 return s1.Angle(dist / EarthRadiusMeters) 41 } 42 43 // Area denotes an area on Earth 44 type Area float64 45 46 // EarthArea converts an area on the unit sphere to an area on earth in sq. meters. 47 func EarthArea(a float64) Area { 48 return Area(a * EarthRadiusMeters * EarthRadiusMeters) 49 } 50 51 // String converts the length to human readable units 52 func (l Length) String() string { 53 switch { 54 case l > 1000: 55 return fmt.Sprintf("%.3f km", l/1000) 56 case l < 1: 57 return fmt.Sprintf("%.3f cm", l*100) 58 } 59 return fmt.Sprintf("%.3f m", l) 60 } 61 62 const km2 = 1000 * 1000 63 const cm2 = 100 * 100 64 65 // String converts the area to human readable units 66 func (a Area) String() string { 67 switch { 68 case a > km2: 69 return fmt.Sprintf("%.3f km^2", a/km2) 70 case a < 1: 71 return fmt.Sprintf("%.3f cm^2", a*cm2) 72 } 73 return fmt.Sprintf("%.3f m^2", a) 74 }