-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Google Tiles: Add Lat / Lon display (#338)
* Add lat lon utility * Add display of latitude / longitude * Fix lat lon handling
- Loading branch information
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { MathUtils, Spherical, Vector3 } from 'three'; | ||
import { swapToGeoFrame, swapToThreeFrame, sphericalPhiToLatitude, latitudeToSphericalPhi } from './GeoUtils.js'; | ||
|
||
function getHoursMinutesSeconds( value, pos = 'E', neg = 'W' ) { | ||
|
||
const direction = value < 0 ? neg : pos; | ||
value = Math.abs( value ); | ||
|
||
const hours = ~ ~ value; | ||
|
||
const minDec = ( value - hours ) * 60; | ||
const minutes = ~ ~ minDec; | ||
|
||
const secDec = ( minDec - minutes ) * 60; | ||
const seconds = ~ ~ secDec; | ||
|
||
return `${ hours }° ${ minutes }' ${ seconds }" ${ direction }` | ||
|
||
|
||
} | ||
|
||
// lat = [ -90, 90 ], lon = [ -180, 180 ] | ||
const _vec = new Vector3(); | ||
const _spherical = new Spherical(); | ||
export class GeoCoord { | ||
|
||
constructor( lat = 0, lon = 0 ) { | ||
|
||
this.lat = lat; | ||
this.lon = lon; | ||
|
||
} | ||
|
||
fromSpherical( spherical ) { | ||
|
||
this.lon = spherical.theta; | ||
this.lat = sphericalPhiToLatitude( spherical.phi ); | ||
return this; | ||
|
||
} | ||
|
||
getSpherical( target ) { | ||
|
||
target.theta = this.lon; | ||
target.phi = latitudeToSphericalPhi( this.lat ); | ||
return this; | ||
|
||
} | ||
|
||
fromVector3( vector ) { | ||
|
||
swapToThreeFrame( vector ); | ||
|
||
_spherical.setFromVector3( vector ); | ||
this.fromSpherical( _spherical ); | ||
return this; | ||
|
||
} | ||
|
||
getVector3( target ) { | ||
|
||
this.getSpherical( _spherical ); | ||
target.setFromSpherical( _spherical ); | ||
|
||
swapToGeoFrame( target ); | ||
|
||
return this; | ||
|
||
} | ||
|
||
normalize() { | ||
|
||
this.getVector3( _vec ); | ||
this.fromVector3( _vec ); | ||
return this; | ||
|
||
} | ||
|
||
toLatitudeString() { | ||
|
||
return getHoursMinutesSeconds( MathUtils.RAD2DEG * this.lat, 'N', 'S' ); | ||
|
||
} | ||
|
||
toLongitudeString() { | ||
|
||
return getHoursMinutesSeconds( MathUtils.RAD2DEG * this.lon, 'E', 'W' ); | ||
|
||
} | ||
|
||
toString() { | ||
|
||
return `${ this.toLatitudeString() }, ${ this.toLongitudeString() }`; | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
export function swapToGeoFrame( target ) { | ||
|
||
const { x, y, z } = target; | ||
target.x = z; | ||
target.y = x; | ||
target.z = y; | ||
|
||
} | ||
|
||
export function swapToThreeFrame( target ) { | ||
|
||
const { x, y, z } = target; | ||
target.z = x; | ||
target.x = y; | ||
target.y = z; | ||
|
||
} | ||
|
||
export function sphericalPhiToLatitude( phi ) { | ||
|
||
return - ( phi - Math.PI / 2 ); | ||
|
||
} | ||
|
||
export function latitudeToSphericalPhi( latitude ) { | ||
|
||
return - latitude + Math.PI / 2; | ||
|
||
} |